import { Aggregate } from "./aggregate.js" ; import { BaseSearchActionsInterface , TransactionModes } from "./model.interface.js" ; import { IDBResultEvent } from "../connection/idb-event.interface.js" ; export abstract class SearchActions extends Aggregate implements BaseSearchActionsInterface { protected abstract loadRelations ( results ) ; protected abstract loadCustomRelations ( results ) ; /**
     * Returns list of all matching records.
     */ public all < T > ( ) ; public all ( ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . getTransaction ( tables , TransactionModes . ReadOnly ) ; const objectStore = transaction . objectStore ( this . table . name ) ; const request = this . request ( objectStore ) ; return new Promise < any [ ] > ( ( resolve , reject ) => { let results = [ ] ; request . addEventListener < 'success' > ( "success" , async ( event ) => { const cursor = ( < IDBResultEvent > event ) . target . result as IDBCursorWithValue | undefined ; if ( cursor ) { if ( ! this . allowedToProcess ( cursor . value ) ) { return cursor . continue ( ) ; } results . push ( cursor . value ) ; return cursor . continue ( ) ; } if ( results . length === 0 || this . relations . length === 0 ) { resolve ( results ) ; } const relations = this . loadRelations ( results ) ; await Promise . all ( relations ) ; resolve ( results ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } /**
     * Returns fist matching record
     */ public first < T > ( ) ; public first ( ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . getTransaction ( tables , TransactionModes . ReadOnly ) ; const objectStore = transaction . objectStore ( this . table . name ) ; const request = this . request ( objectStore ) ; return new Promise < any [ ] > ( ( resolve , reject ) => { let result ; request . addEventListener < 'success' > ( "success" , async ( event ) => { const cursor = ( < IDBResultEvent > event ) . target . result as IDBCursorWithValue | undefined ; if ( cursor ) { if ( ! this . allowedToProcess ( cursor . value ) ) { return cursor . continue ( ) ; } result = cursor . value ; } if ( ! result || this . relations . length === 0 ) { resolve ( result || null ) ; } const relations = this . loadRelations ( [ result ] ) ; await Promise . all ( relations ) ; resolve ( result ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } param id public find < T > ( id ) ; public find ( id ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . getTransaction ( tables , TransactionModes . ReadOnly ) ; const objectStore = transaction . objectStore ( this . table . name ) ; const request = objectStore . get ( id ) ; return new Promise < any [ ] > ( ( resolve , reject ) => { request . addEventListener < 'success' > ( "success" , async ( event ) => { const result = ( event as IDBResultEvent ) . target . result ; if ( ! result || this . relations . length === 0 ) { resolve ( result || null ) ; } const relations = this . loadRelations ( [ result ] ) ; await Promise . all ( relations ) ; resolve ( result || null ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } param indexName param id public findIndex < T > ( indexName , id ) ; public findIndex ( indexName , id ) { this . resetBuilder ( ) ; const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . getTransaction ( tables , TransactionModes . ReadOnly ) ; const objectStore = transaction . objectStore ( this . table . name ) ; const request = objectStore . index ( indexName ) . get ( id ) ; return new Promise < any [ ] > ( ( resolve , reject ) => { request . addEventListener < 'success' > ( "success" , async ( event ) => { const result = ( event as IDBResultEvent ) . target . result ; if ( ! result || this . relations . length === 0 ) { resolve ( result || null ) ; } const relations = this . loadRelations ( [ result ] ) ; await Promise . all ( relations ) ; resolve ( result ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } param indexName param id public findAllIndex < T > ( indexName , id ) ; public findAllIndex ( indexName , id ) { this . resetBuilder ( ) ; this . whereIndex ( indexName , id ) ; return this . all ( ) ; } }