import { FuseDB } from "./FuseDB" ; import { Model } from "./Model" ; import { IInsertManyResponse } from "./adapters/Adapter" ; export class ActiveQuery < T > { private _sort : any ; private _limit : number ; private _skip : number ; private withConditions = new Map < string , typeof Model | ActiveQuery < T > > ( ) ; constructor ( private query , public db ) { } public sort ( key , direction ? ) { if ( typeof key === "string" ) { this . _sort = { } ; this . _sort [ key ] = direction === "desc" ? - 1 : 1 ; } return this ; } public find ( query ) { this . query = this . query || { } ; query = query || { } ; for ( let item in query ) { this . query [ item ] = query [ item ] ; } return this ; } public limit ( num ) { this . _limit = num ; return this ; } public with ( field , query ) { this . withConditions . set ( field , query ) ; return this ; } public getWithConditions ( ) { return this . withConditions ; } public skip ( num ) { this . _skip = num ; return this ; } public async deleteMany ( ) { return this . db . deleteMany ( this ) ; } public async updateMany ( data ) { return this . db . updateMany ( this , data ) ; } public async first ( ) { this . limit ( 1 ) ; const results = await this . db . query ( this ) ; return results [ 0 ] as T ; } public async count ( ) { const count = await this . db . count ( this ) ; return count ; } public async remove ( ) { const count = await this . db . remove ( ) ; return count ; } public async all ( ) { const results = await this . db . query ( this ) ; return results ; } public getSort ( ) { return this . _sort ; } public getSkip ( ) { return this . _skip ; } public getLimit ( ) { return this . _limit ; } public getQuery ( ) { return this . query || { } ; } }