@@ -42,27 +42,27 @@ export class AccountRepo extends ServiceMap.Service<AccountRepo, AccountRepo.Ser
4242 Effect . gen ( function * ( ) {
4343 const decode = Schema . decodeUnknownSync ( Account )
4444
45- const query = < A > ( f : ( db : DbClient ) => A ) =>
46- Effect . try ( {
45+ const query = < A > ( f : ( db : DbClient ) => Promise < A > ) =>
46+ Effect . tryPromise ( {
4747 try : ( ) => Database . use ( f ) ,
4848 catch : ( cause ) => new AccountRepoError ( { message : "Database operation failed" , cause } ) ,
4949 } )
5050
51- const tx = < A > ( f : ( db : DbClient ) => A ) =>
52- Effect . try ( {
51+ const tx = < A > ( f : ( db : DbClient ) => Promise < A > ) =>
52+ Effect . tryPromise ( {
5353 try : ( ) => Database . transaction ( f ) ,
5454 catch : ( cause ) => new AccountRepoError ( { message : "Database operation failed" , cause } ) ,
5555 } )
5656
57- const current = ( db : DbClient ) => {
58- const state = db . select ( ) . from ( AccountStateTable ) . where ( eq ( AccountStateTable . id , ACCOUNT_STATE_ID ) ) . get ( )
57+ const current = async ( db : DbClient ) => {
58+ const state = await db . select ( ) . from ( AccountStateTable ) . where ( eq ( AccountStateTable . id , ACCOUNT_STATE_ID ) ) . get ( )
5959 if ( ! state ?. active_account_id ) return
60- const account = db . select ( ) . from ( AccountTable ) . where ( eq ( AccountTable . id , state . active_account_id ) ) . get ( )
60+ const account = await db . select ( ) . from ( AccountTable ) . where ( eq ( AccountTable . id , state . active_account_id ) ) . get ( )
6161 if ( ! account ) return
6262 return { ...account , active_org_id : state . active_org_id ?? null }
6363 }
6464
65- const state = ( db : DbClient , accountID : AccountID , orgID : Option . Option < OrgID > ) => {
65+ const state = async ( db : DbClient , accountID : AccountID , orgID : Option . Option < OrgID > ) => {
6666 const id = Option . getOrNull ( orgID )
6767 return db
6868 . insert ( AccountStateTable )
@@ -75,41 +75,39 @@ export class AccountRepo extends ServiceMap.Service<AccountRepo, AccountRepo.Ser
7575 }
7676
7777 const active = Effect . fn ( "AccountRepo.active" ) ( ( ) =>
78- query ( ( db ) => current ( db ) ) . pipe ( Effect . map ( ( row ) => ( row ? Option . some ( decode ( row ) ) : Option . none ( ) ) ) ) ,
78+ query ( async ( db ) => current ( db ) ) . pipe ( Effect . map ( ( row ) => ( row ? Option . some ( decode ( row ) ) : Option . none ( ) ) ) ) ,
7979 )
8080
8181 const list = Effect . fn ( "AccountRepo.list" ) ( ( ) =>
82- query ( ( db ) =>
83- db
84- . select ( )
85- . from ( AccountTable )
86- . all ( )
87- . map ( ( row : AccountRow ) => decode ( { ...row , active_org_id : null } ) ) ,
88- ) ,
82+ query ( async ( db ) => {
83+ const rows = await db . select ( ) . from ( AccountTable ) . all ( )
84+ return rows . map ( ( row : AccountRow ) => decode ( { ...row , active_org_id : null } ) )
85+ } ) ,
8986 )
9087
9188 const remove = Effect . fn ( "AccountRepo.remove" ) ( ( accountID : AccountID ) =>
92- tx ( ( db ) => {
93- db . update ( AccountStateTable )
89+ tx ( async ( db ) => {
90+ await db
91+ . update ( AccountStateTable )
9492 . set ( { active_account_id : null , active_org_id : null } )
9593 . where ( eq ( AccountStateTable . active_account_id , accountID ) )
9694 . run ( )
97- db . delete ( AccountTable ) . where ( eq ( AccountTable . id , accountID ) ) . run ( )
95+ await db . delete ( AccountTable ) . where ( eq ( AccountTable . id , accountID ) ) . run ( )
9896 } ) . pipe ( Effect . asVoid ) ,
9997 )
10098
10199 const use = Effect . fn ( "AccountRepo.use" ) ( ( accountID : AccountID , orgID : Option . Option < OrgID > ) =>
102- query ( ( db ) => state ( db , accountID , orgID ) ) . pipe ( Effect . asVoid ) ,
100+ query ( async ( db ) => state ( db , accountID , orgID ) ) . pipe ( Effect . asVoid ) ,
103101 )
104102
105103 const getRow = Effect . fn ( "AccountRepo.getRow" ) ( ( accountID : AccountID ) =>
106- query ( ( db ) => db . select ( ) . from ( AccountTable ) . where ( eq ( AccountTable . id , accountID ) ) . get ( ) ) . pipe (
104+ query ( async ( db ) => db . select ( ) . from ( AccountTable ) . where ( eq ( AccountTable . id , accountID ) ) . get ( ) ) . pipe (
107105 Effect . map ( Option . fromNullishOr ) ,
108106 ) ,
109107 )
110108
111109 const persistToken = Effect . fn ( "AccountRepo.persistToken" ) ( ( input ) =>
112- query ( ( db ) =>
110+ query ( async ( db ) =>
113111 db
114112 . update ( AccountTable )
115113 . set ( {
@@ -123,8 +121,9 @@ export class AccountRepo extends ServiceMap.Service<AccountRepo, AccountRepo.Ser
123121 )
124122
125123 const persistAccount = Effect . fn ( "AccountRepo.persistAccount" ) ( ( input ) =>
126- tx ( ( db ) => {
127- db . insert ( AccountTable )
124+ tx ( async ( db ) => {
125+ await db
126+ . insert ( AccountTable )
128127 . values ( {
129128 id : input . id ,
130129 email : input . email ,
@@ -142,7 +141,7 @@ export class AccountRepo extends ServiceMap.Service<AccountRepo, AccountRepo.Ser
142141 } ,
143142 } )
144143 . run ( )
145- void state ( db , input . id , input . orgID )
144+ await state ( db , input . id , input . orgID )
146145 } ) . pipe ( Effect . asVoid ) ,
147146 )
148147
0 commit comments