import { QueryBuilder , QueryDocument , FindQuery , FindOneQuery , FindOneAndRemoveQuery , FindOneAndUpdateQuery , CountQuery } from "../../src/query/queryBuilder" ; import { MockInternalSession } from "../mockInternalSession" ; import { Constructor } from "../../src/index" ; import { ResultCallback , IteratorCallback , Callback } from "../../src/core/callback" ; import { QueryKind } from "../../src/query/queryKind" ; import { CollationOptions } from "../../src/mapping/collationOptions" ; export class MockQueryBuilder < T > implements QueryBuilder < T > { private _session : MockInternalSession ; private _entityCtr : Constructor < any > ; constructor ( session , entityCtr ) { this . _session = session ; this . _entityCtr = entityCtr ; } findAll ( callback ? ) ; findAll ( criteria , callback ? ) ; findAll ( criteriaOrCallback ? , callback ? ) { return this . _createFindQuery ( QueryKind . FindAll , criteriaOrCallback , callback ) ; } findOne ( callback ? ) ; findOne ( criteria , callback ? ) ; findOne ( criteriaOrCallback ? , callback ? ) { return this . _createFindQuery ( QueryKind . FindOne , criteriaOrCallback , callback ) ; } findOneById ( id , callback ? ) { var query = this . _createQuery ( QueryKind . FindOneById ) ; if ( id == null ) { query . error = new Error ( "Missing or invalid identifier." ) ; } query . id = id ; return query . handleCallback ( callback ) ; } findOneAndRemove ( callback ? ) ; findOneAndRemove ( criteria , callback ? ) ; findOneAndRemove ( criteriaOrCallback ? , callback ? ) { return this . _createRemoveQuery ( QueryKind . FindOneAndRemove , criteriaOrCallback , callback ) ; } findOneAndUpdate ( updateDocument , callback ? ) ; findOneAndUpdate ( criteria , updateDocument , callback ? ) ; findOneAndUpdate ( criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { return this . _createUpdateQuery ( QueryKind . FindOneAndUpdate , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ) ; } findOneAndUpsert ( updateDocument , callback ? ) ; findOneAndUpsert ( criteria , updateDocument , callback ? ) ; findOneAndUpsert ( criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { return this . _createUpdateQuery ( QueryKind . FindOneAndUpsert , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ) ; } removeAll ( callback ? ) ; removeAll ( criteria , callback ? ) ; removeAll ( criteriaOrCallback ? , callback ? ) { this . _createRemoveQuery ( QueryKind . RemoveAll , criteriaOrCallback , callback ) ; } removeOne ( callback ? ) ; removeOne ( criteria , callback ? ) ; removeOne ( criteriaOrCallback ? , callback ? ) { this . _createRemoveQuery ( QueryKind . RemoveOne , criteriaOrCallback , callback ) ; } updateAll ( updateDocument , callback ? ) ; updateAll ( criteria , updateDocument , callback ? ) ; updateAll ( criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { this . _createUpdateQuery ( QueryKind . UpdateAll , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ) ; } updateOne ( updateDocument , callback ? ) ; updateOne ( criteria , updateDocument , callback ? ) ; updateOne ( criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { this . _createUpdateQuery ( QueryKind . UpdateOne , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ) ; } upsert ( updateDocument , callback ? ) ; upsert ( criteria , updateDocument , callback ? ) ; upsert ( criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { this . _createUpdateQuery ( QueryKind . Upsert , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ) ; } distinct ( key , callback ) ; distinct ( key , criteria , callback ) ; distinct ( key , criteriaOrCallback , callback ? ) { var query = this . _createQuery ( QueryKind . Distinct ) ; query . key = key ; if ( typeof criteriaOrCallback === "function" ) { query . criteria = { } ; callback = criteriaOrCallback ; } else { query . criteria = criteriaOrCallback || { } ; } query . handleCallback ( callback ) ; } count ( callback ? ) ; count ( criteria , callback ? ) ; count ( criteriaOrCallback ? , callback ? ) { var query = this . _createQuery ( QueryKind . Count ) ; if ( typeof criteriaOrCallback == "function" ) { callback = criteriaOrCallback ; query . criteria = { } ; } else { query . criteria = criteriaOrCallback || { } ; } return query . handleCallback ( callback ) ; } private _createUpdateQuery ( kind , criteriaOrUpdateDocument , updateDocumentOrCallback , callback ? ) { var query = this . _createQuery ( kind ) ; if ( typeof updateDocumentOrCallback == "function" || updateDocumentOrCallback === undefined ) { callback = updateDocumentOrCallback ; query . updateDocument = criteriaOrUpdateDocument ; query . criteria = { } ; } else { query . updateDocument = updateDocumentOrCallback ; query . criteria = criteriaOrUpdateDocument ; } return query . handleCallback ( callback ) ; } private _createRemoveQuery ( kind , criteriaOrCallback ? , callback ? ) { var query = this . _createQuery ( kind ) ; if ( typeof criteriaOrCallback == "function" ) { callback = criteriaOrCallback ; query . criteria = { } ; } else { query . criteria = criteriaOrCallback || { } ; } return query . handleCallback ( callback ) ; } private _createFindQuery ( kind , criteriaOrCallback ? , callback ? ) { var query = this . _createQuery ( kind ) ; if ( typeof criteriaOrCallback == "function" ) { callback = criteriaOrCallback ; query . criteria = { } ; } else { query . criteria = criteriaOrCallback || { } ; } return query . handleCallback ( callback ) ; } private _createQuery ( kind ) { return new MockQueryObject ( this . _session , this . _entityCtr , kind ) ; } } export class MockQueryObject implements FindQuery < Object > , FindOneQuery < Object > , FindOneAndRemoveQuery < Object > , FindOneAndUpdateQuery < Object > , CountQuery { key : string ; id : any ; criteria : QueryDocument ; updateDocument : QueryDocument ; isLazy : boolean ; wantsUpdated : boolean ; fetchPaths : string [ ] ; sortValue : [ string , number ] [ ] ; limitCount : number ; collationOptions : CollationOptions ; skipCount : number ; iterator : IteratorCallback < Object > ; batchSizeValue : number ; error : Error ; entityCtr : Constructor < any > ; private _session : MockInternalSession ; private _executed : boolean ; constructor ( session , entityCtr , public kind ) { this . _session = session ; this . entityCtr = entityCtr ; } get readOnly ( ) : boolean { switch ( this . kind ) { case QueryKind . FindAll : case QueryKind . FindEach : case QueryKind . FindEachSeries : case QueryKind . FindOne : case QueryKind . FindOneById : case QueryKind . Distinct : case QueryKind . Count : return true ; default : return false ; } } fetch ( path , callback ? ) { if ( ! this . fetchPaths ) { this . fetchPaths = [ ] ; } if ( typeof path === "string" ) { this . fetchPaths . push ( path ) ; } else { this . fetchPaths = this . fetchPaths . concat ( path ) ; } return this . handleCallback ( callback ) ; } sort ( field , directionOrCallback , callback ? ) { if ( ! this . sortValue ) { this . sortValue = [ ] ; } if ( typeof field === "string" ) { if ( typeof directionOrCallback === "number" ) { this . sortValue . push ( [ field , directionOrCallback ] ) ; } else { throw new Error ( "Expected second parameter to be the sort direction when first parameter is a string." ) ; } } else { if ( ! Array . isArray ( field ) ) { throw new Error ( "Expected first parameter to be a string or array" ) ; } this . sortValue = this . sortValue . concat ( field ) ; } if ( typeof directionOrCallback === "number" ) { return this . handleCallback ( callback ) ; } else { return this . handleCallback ( directionOrCallback ) ; } } returnUpdated ( callback ? ) { this . wantsUpdated = true ; return this . handleCallback ( callback ) ; } lazy ( callback ? ) { this . isLazy = true ; return this . handleCallback ( callback ) ; } limit ( value , callback ? ) { this . limitCount = value ; return this . handleCallback ( callback ) ; } collation ( value , callback ? ) { this . collationOptions = value ; return this . handleCallback ( callback ) ; } skip ( value , callback ? ) { this . skipCount = value ; return this . handleCallback ( callback ) ; } batchSize ( value , callback ? ) { this . batchSizeValue = value ; return this . handleCallback ( callback ) ; } each ( iterator , callback ) { if ( ! iterator ) { throw new Error ( "Missing required argument 'iterator'." ) ; } if ( ! callback ) { throw new Error ( "Missing required argument 'callback'." ) ; } this . kind = QueryKind . FindEach ; this . iterator = iterator ; this . handleCallback ( callback ) ; } eachSeries ( iterator , callback ) { if ( ! iterator ) { throw new Error ( "Missing required argument 'iterator'." ) ; } if ( ! callback ) { throw new Error ( "Missing required argument 'callback'." ) ; } this . kind = QueryKind . FindEachSeries ; this . iterator = iterator ; this . handleCallback ( callback ) ; } handleCallback ( callback ) { if ( callback ) { if ( this . _executed ) { callback ( new Error ( "Query already executed. A callback can only be passed to one function in the chain." ) ) ; } else { this . _executed = true ; if ( this . error ) { process . nextTick ( ( ) => callback ( this . error ) ) ; } else { this . _session . executeQuery ( < any > this , callback ) ; } } } return this ; } execute ( callback ) { this . _session . executeQuery ( < any > this , callback ) ; } asPromise ( ) { return new Promise ( ( resolve , reject ) => { this . handleCallback ( ( err , result ) => { if ( err ) { reject ( err ) ; } else { resolve ( result ) ; } } ) ; } ) ; } asObservable ( ) { throw new Error ( "Not implemented" ) ; } }