@@ -43,10 +43,10 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
4343 /**
4444 * ensure table (schema) exists
4545 */
46- init ( options : { autoMigrateIndexChanges : boolean } = { autoMigrateIndexChanges : true } ) : void {
46+ async init ( options : { autoMigrateIndexChanges : boolean } = { autoMigrateIndexChanges : true } ) {
4747 const tableExists = sqljsHelpers . isTable ( this . db , this . tableName ) ;
4848 if ( ! tableExists ) {
49- this . db . txn ( `${ this . tableName } create table` , txnId => {
49+ await this . db . txnAsync ( `${ this . tableName } create table` , async txnId => {
5050 const createTableSql = `create table if not exists ${ this . tableName } (${ dbRow . id } primary key not null, ${ dbRow . json } ${ this . _indexColumnNamesSql } );` ;
5151 this . db . run ( txnId , createTableSql ) ;
5252 } ) ;
@@ -63,20 +63,20 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
6363 return ;
6464 }
6565 console . log ( `adding missing columns to ${ this . tableName } ` , missingColumns ) ;
66- this . db . txn ( `${ this . tableName } add missing columns` , txnId => {
66+ await this . db . txnAsync ( `${ this . tableName } add missing columns` , async txnId => {
6767 missingColumns . forEach ( columnName => this . db . run ( txnId , `alter table ${ this . tableName } add column ${ columnName } ;` ) ) ;
68- this . rebuildIndexes ( txnId ) ;
68+ await this . rebuildIndexes ( txnId ) ;
6969 } ) ;
7070 }
7171 }
7272
73- get ( id : unknown ) : T {
73+ async get ( id : unknown ) {
7474 const result = this . _tryGet ( id ) ;
7575 if ( result === null || result === undefined ) throw new Error ( `${ TypedDocumentStore . name } <${ this . tableName } >.get(${ id } ) was undefined` ) ;
7676 return < T > result ;
7777 }
7878
79- tryGet ( id : unknown ) : T | undefined {
79+ async tryGet ( id : unknown ) {
8080 return this . _tryGet ( id ) ;
8181 }
8282
@@ -86,7 +86,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
8686 return < T > JSON . parse ( rows [ 0 ] . json ) ;
8787 }
8888
89- getMany ( ids : unknown [ ] ) : T [ ] {
89+ async getMany ( ids : unknown [ ] ) {
9090 const results = this . _tryGetMany ( ids ) ;
9191 const missingIds = ids . filter ( ( _ , i ) => results [ i ] === null || results [ i ] === undefined ) ;
9292 if ( missingIds . length > 0 ) throw new Error ( `${ TypedDocumentStore . name } <${ this . tableName } >.getMany(...) was undefined for ids ${ missingIds . join ( ', ' ) } ` ) ;
@@ -97,7 +97,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
9797 * returns ids => in order set of results, if no result for given id, array item will be null
9898 * prefer use of 'getMany' if all items are expected to exist
9999 */
100- tryGetMany ( ids : unknown [ ] ) {
100+ async tryGetMany ( ids : unknown [ ] ) {
101101 return this . _tryGetMany ( ids ) ;
102102 }
103103
@@ -118,12 +118,12 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
118118 }
119119 }
120120
121- exists ( id : unknown ) : boolean {
121+ async exists ( id : unknown ) {
122122 const result = ( this . db . exec ( `select 1 from ${ this . tableName } where ${ dbRow . id } = ?;` , [ < SqlValue > id ] ) ) ;
123123 return ( result . values ?. length ?? 0 ) > 0 ;
124124 }
125125
126- getAll ( ) : T [ ] {
126+ async getAll ( ) {
127127 const results = sqljsHelpers . query < DbRow > ( this . db , `select ${ dbRow . json } from ${ this . tableName } ;` ) ;
128128 return results . map ( x => < T > JSON . parse ( x . json ) ) ;
129129 }
@@ -133,7 +133,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
133133 * @example .query(x => `where ${x.name} like ? and ${x.active} = ?`, [nameSearchValue, isActive]);
134134 * @example .query(x => `where ${x.name} like ?1 and ${x.active} = ?2`, [nameSearchValue, isActive]);
135135 */
136- query ( whereSql : ( ( x : Record < keyof TIndex , string > ) => string ) , params : unknown [ ] ) : T [ ] {
136+ async query ( whereSql : ( ( x : Record < keyof TIndex , string > ) => string ) , params : unknown [ ] ) {
137137 const querySql = `select ${ dbRow . id } , ${ dbRow . json } from ${ this . tableName } ${ whereSql ( this . _buildQueryObject ( ) ) } ;` ;
138138 const results = sqljsHelpers . query < DbRow > ( this . db , querySql , params ) ;
139139 return results . map ( x => < T > JSON . parse ( x . json ) ) ;
@@ -142,56 +142,56 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
142142 /**
143143 * Return just index values, helpful for doing fast queries on indexed fields without needing to fetch and deserialize the entire object
144144 */
145- queryIndexes ( whereSql ?: ( ( x : Record < keyof TIndex | 'id' , string > ) => string ) , params ?: unknown [ ] ) : ( { [ k in keyof ( TIndex ) ] : ReturnType < TIndex [ k ] > } & Pick < T , 'id' > ) [ ] {
145+ async queryIndexes ( whereSql ?: ( ( x : Record < keyof TIndex | 'id' , string > ) => string ) , params ?: unknown [ ] ) : Promise < ( { [ k in keyof ( TIndex ) ] : ReturnType < TIndex [ k ] > } & Pick < T , 'id' > ) [ ] > {
146146 const querySql = `select ${ dbRow . id } ${ this . _indexColumnNamesSql } from ${ this . tableName } ${ whereSql !== undefined ? whereSql ( this . _buildQueryObject ( ) ) : '' } ;` ;
147147 const results = sqljsHelpers . query < ( { [ k in keyof ( TIndex ) ] : ReturnType < TIndex [ k ] > } & Pick < T , 'id' > ) > ( this . db , querySql , params ) ;
148148 return results ;
149149 }
150150
151- count ( ) : number {
151+ async count ( ) {
152152 const result = this . db . exec ( `select count(1) from ${ this . tableName } ;` ) ;
153153 return result [ 0 ] . values [ 0 ] [ 0 ] as number ;
154154 }
155155
156156 /**
157157 * insert or update a single document
158158 */
159- set ( txnId : string , value : T ) : void { this . db . run ( txnId , this . setSql , this . _buildParams ( value ) ) ; }
159+ async set ( txnId : string , value : T ) { this . db . run ( txnId , this . setSql , this . _buildParams ( value ) ) ; }
160160
161161 /**
162162 * insert or update many documents, prefer use of insertMany if data is expected to not exist
163163 */
164- setMany ( txnId : string , values : T [ ] ) : void {
164+ async setMany ( txnId : string , values : T [ ] ) {
165165 if ( values . length === 0 ) return ;
166166 values . forEach ( value => this . db . run ( txnId , this . setSql , this . _buildParams ( value ) ) ) ;
167167 }
168168
169- insertMany ( txnId : string , values : T [ ] ) : void {
169+ async insertMany ( txnId : string , values : T [ ] ) {
170170 if ( values . length === 0 ) return ;
171171 values . forEach ( value => this . db . run ( txnId , this . insertSql , this . _buildParams ( value ) ) ) ;
172172 }
173173
174174 /**
175175 * fetch, modify, and update a document
176176 */
177- update ( txnId : string , id : Pick < T , 'id' > [ 'id' ] , updateAction : ( existing : Omit < T , 'id' > ) => void ) : void {
178- const value = this . get ( id ) ;
177+ async update ( txnId : string , id : Pick < T , 'id' > [ 'id' ] , updateAction : ( existing : Omit < T , 'id' > ) => void ) {
178+ const value = await this . get ( id ) ;
179179 updateAction ( value ) ;
180- this . set ( txnId , value ) ;
180+ await this . set ( txnId , value ) ;
181181 }
182182
183183 /**
184184 * like update, but falls back to initializer value if document doesn't already exist
185185 */
186- upsert ( txnId : string , initializer : T , updateAction : ( existing : Omit < T , 'id' > ) => void ) : void {
187- const value = this . tryGet ( initializer . id ) ?? initializer ;
186+ async upsert ( txnId : string , initializer : T , updateAction : ( existing : Omit < T , 'id' > ) => void ) {
187+ const value = await this . tryGet ( initializer . id ) ?? initializer ;
188188 updateAction ( value ) ;
189- this . set ( txnId , value ) ;
189+ await this . set ( txnId , value ) ;
190190 }
191191
192- remove ( txnId : string , id : Pick < T , 'id' > [ 'id' ] ) : void { this . db . run ( txnId , `delete from ${ this . tableName } where ${ dbRow . id } = ?;` , sqljsHelpers . sanitizeParams ( [ id ] ) ) ; }
193- removeMany ( txnId : string , ids : Pick < T , 'id' > [ 'id' ] [ ] ) : void { if ( ids . length === 0 ) return ; this . db . run ( txnId , `delete from ${ this . tableName } where ${ dbRow . id } in (${ '?,' . repeat ( ids . length ) . slice ( 0 , - 1 ) } );` , sqljsHelpers . sanitizeParams ( ids ) ) ; }
194- removeAll ( txnId : string ) : void { this . db . run ( txnId , `delete from ${ this . tableName } ;` ) ; }
192+ async remove ( txnId : string , id : Pick < T , 'id' > [ 'id' ] ) { this . db . run ( txnId , `delete from ${ this . tableName } where ${ dbRow . id } = ?;` , sqljsHelpers . sanitizeParams ( [ id ] ) ) ; }
193+ async removeMany ( txnId : string , ids : Pick < T , 'id' > [ 'id' ] [ ] ) { if ( ids . length === 0 ) return ; this . db . run ( txnId , `delete from ${ this . tableName } where ${ dbRow . id } in (${ '?,' . repeat ( ids . length ) . slice ( 0 , - 1 ) } );` , sqljsHelpers . sanitizeParams ( ids ) ) ; }
194+ async removeAll ( txnId : string ) { this . db . run ( txnId , `delete from ${ this . tableName } ;` ) ; }
195195
196196 private _buildQueryObject ( ) : Record < keyof TIndex , string > {
197197 const queryObject = < any > { } ;
@@ -209,7 +209,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
209209
210210 private _indexValues ( obj : T ) { return _ . map ( this . indexedFields , accessor => accessor ( obj ) ) ; }
211211
212- private rebuildIndexes ( txnId : string ) { this . setMany ( txnId , this . getAll ( ) ) ; }
212+ private async rebuildIndexes ( txnId : string ) { await this . setMany ( txnId , await this . getAll ( ) ) ; }
213213}
214214
215215export interface ITypedDocumentStore < T extends IdInterface , TIndex extends TIndexType < T > > extends TypedDocumentStore < T , TIndex > { } ;
0 commit comments