@@ -22,6 +22,7 @@ import { jsonArrayFrom } from "kysely/helpers/postgres";
2222import { GetBlueprintArgs } from "../graphql/schemas/args/blueprintArgs.js" ;
2323import { sql } from "kysely" ;
2424import { GetSignatureRequestArgs } from "../graphql/schemas/args/signatureRequestArgs.js" ;
25+ import { GetCollectionsArgs } from "../graphql/schemas/args/collectionArgs.js" ;
2526
2627@singleton ( )
2728export class SupabaseDataService extends BaseSupabaseService < KyselyDataDatabase > {
@@ -432,6 +433,79 @@ export class SupabaseDataService extends BaseSupabaseService<KyselyDataDatabase>
432433 . execute ( ) ;
433434 }
434435
436+ async getCollections ( args : GetCollectionsArgs ) {
437+ const query = this . db
438+ . selectFrom ( "collections" )
439+ . distinctOn ( "collections.id" )
440+ . leftJoin ( "hypercerts" , "hypercerts.collection_id" , "collections.id" )
441+ . leftJoin (
442+ "collection_admins" ,
443+ "collection_admins.collection_id" ,
444+ "collections.id" ,
445+ )
446+ . leftJoin ( "users" , "users.id" , "collection_admins.user_id" )
447+ . leftJoin (
448+ "collection_blueprints" ,
449+ "collection_blueprints.collection_id" ,
450+ "collections.id" ,
451+ )
452+ . leftJoin (
453+ "blueprints" ,
454+ "blueprints.id" ,
455+ "collection_blueprints.blueprint_id" ,
456+ )
457+ . select ( ( eb ) => [
458+ "collections.id" ,
459+ "collections.name" ,
460+ "collections.description" ,
461+ "collections.chain_ids" ,
462+ "collections.hidden" ,
463+ "collections.created_at" ,
464+ jsonArrayFrom (
465+ eb
466+ . selectFrom ( "hypercerts" )
467+ . select ( [ "hypercert_id" , "collection_id" ] )
468+ . whereRef ( "collection_id" , "=" , "collections.id" ) ,
469+ ) . as ( "hypercerts" ) ,
470+ jsonArrayFrom (
471+ eb
472+ . selectFrom ( "users" )
473+ . innerJoin (
474+ "collection_admins" ,
475+ "collection_admins.user_id" ,
476+ "users.id" ,
477+ )
478+ . select ( [
479+ "users.address" ,
480+ "users.chain_id" ,
481+ "users.display_name" ,
482+ "users.avatar" ,
483+ ] )
484+ . whereRef ( "collection_admins.collection_id" , "=" , "collections.id" ) ,
485+ ) . as ( "admins" ) ,
486+ jsonArrayFrom (
487+ eb
488+ . selectFrom ( "blueprints" )
489+ . innerJoin (
490+ "collection_blueprints" ,
491+ "collection_blueprints.blueprint_id" ,
492+ "blueprints.id" ,
493+ )
494+ . selectAll ( "blueprints" )
495+ . whereRef (
496+ "collection_blueprints.collection_id" ,
497+ "=" ,
498+ "collections.id" ,
499+ ) ,
500+ ) . as ( "blueprints" ) ,
501+ ] ) ;
502+
503+ return {
504+ data : await query . execute ( ) ,
505+ count : this . handleGetCount ( "collections" , args ) ,
506+ } ;
507+ }
508+
435509 async getCollectionById ( collectionId : string ) {
436510 return this . db
437511 . selectFrom ( "collections" )
@@ -719,6 +793,8 @@ export class SupabaseDataService extends BaseSupabaseService<KyselyDataDatabase>
719793 return this . db . selectFrom ( "users" ) . selectAll ( ) ;
720794 case "signature_requests" :
721795 return this . db . selectFrom ( "signature_requests" ) . selectAll ( ) ;
796+ case "collections" :
797+ return this . db . selectFrom ( "collections" ) . selectAll ( ) ;
722798 default :
723799 throw new Error ( `Table ${ tableName . toString ( ) } not found` ) ;
724800 }
@@ -759,6 +835,10 @@ export class SupabaseDataService extends BaseSupabaseService<KyselyDataDatabase>
759835 return this . db . selectFrom ( "users" ) . select ( ( expressionBuilder ) => {
760836 return expressionBuilder . fn . countAll ( ) . as ( "count" ) ;
761837 } ) ;
838+ case "collections" :
839+ return this . db . selectFrom ( "collections" ) . select ( ( expressionBuilder ) => {
840+ return expressionBuilder . fn . countAll ( ) . as ( "count" ) ;
841+ } ) ;
762842 default :
763843 throw new Error ( `Table ${ tableName . toString ( ) } not found` ) ;
764844 }
0 commit comments