import { AggregateInterface , IndexBuilder , QueryTypes , TransactionModes } from "./model.interface.js" ; import { TableSchema } from "../migration/migration.interface.js" ; import { QueryBuilder } from "./query-builder.js" ; import { IDBResultEvent } from "../connection/idb-event.interface.js" ; import { OrmRelationBuilder } from "./orm-relation-builder.js" ; import { nestedAttributeValue } from "../utils.js" ; import { TransactionHandling } from "./transaction-handling.js" ; import { BaseModel } from "./base-model.js" ; export abstract class Aggregate extends BaseModel implements AggregateInterface { public constructor ( public db , public table ) { super ( ) ; } /**
     * Returns the count of matching records
     */ public count ( ) { const tables = [ this . table . name ] ; const transaction = this . getTransaction ( tables , TransactionModes . ReadOnly ) ; const objectStore = transaction . objectStore ( this . table . name ) ; let request ; if ( this . builder . length === 0 && this . indexBuilder === null ) { request = objectStore . count ( ) ; } else if ( this . builder . length === 0 && this . indexBuilder !== null ) { request = objectStore . count ( this . keyRange ( < IndexBuilder > this . indexBuilder ) ) ; } else { request = this . request ( objectStore ) ; } return new Promise < number > ( ( resolve , reject ) => { let count = 0 ; request . addEventListener < 'success' > ( "success" , async ( event ) => { if ( this . builder . length === 0 ) { return resolve ( ( event as IDBResultEvent ) . target . result ) ; } const cursor = ( < IDBResultEvent > event ) . target . result as IDBCursorWithValue | undefined ; if ( cursor ) { if ( ! this . allowedToProcess ( cursor . value ) ) { return cursor . continue ( ) ; } count ++ ; return cursor . continue ( ) ; } resolve ( count ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } param attribute public average ( attribute ) { const tables = [ 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 < number > ( ( resolve , reject ) => { let count = 0 ; let total = 0 ; 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 ( ) ; } const value = nestedAttributeValue ( attribute , cursor . value ) ; if ( ! isNaN ( parseFloat ( value ) ) ) { total += parseFloat ( cursor . value [ attribute ] ) ; } count ++ ; return cursor . continue ( ) ; } resolve ( total / count ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } param func param defaultCarry public reduce < T extends any , U extends any > ( func , defaultCarry ) ; public reduce < T > ( func , defaultCarry ) ; public reduce ( func , defaultCarry ) { const tables = [ 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 ( ( resolve , reject ) => { let result = defaultCarry ; 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 = func ( cursor . value , result ) ; return cursor . continue ( ) ; } resolve ( result ) ; } ) ; request . addEventListener < 'error' > ( "error" , ( error ) => reject ( error ) ) ; } ) ; } }