import { CoatyObject , DbConnectionInfo , Uuid } from ".." ; import { DbContext } from "./db-context" ; import { DbLocalContext } from "./db-local-context" ; import { DbJoinCondition , DbObjectFilter , IDbLocalStore , IDbNoSqlOperations , IDbSqlOperations , IDbTransaction , IQueryIterator , SqlQueryBuilder , SqlQueryOptions , SqlQueryResultSet , } from "./db-operations" ; import { AggregateProperties } from "./db-operations" ; import { AggregateOp } from "./db-operators" ; export type DbAdapterExtension = ( ... args ) => Promise < any > ; export interface IDbAdapterExtension { param name param params callExtension ( name , ... params ) : Promise < any > ; } export interface IDbAdapter extends IDbNoSqlOperations , IDbSqlOperations , IDbTransaction , IDbLocalStore { } export type IDbAdapterConstructor = new ( connectionInfo ) => IDbAdapter & IDbAdapterExtension ; /**
 * The base class for deriving custom database adapters and managing extensions.
 */ export abstract class DbAdapterBase implements IDbAdapter , IDbAdapterExtension { private _extensions : Set < string > ; constructor ( protected connectionInfo ) { this . _extensions = new Set ( ) ; } addCollection ( name ) { return this . rejectNotSupported ( "addCollection" ) ; } removeCollection ( name ) { return this . rejectNotSupported ( "removeCollection" ) ; } clearCollection ( name ) { return this . rejectNotSupported ( "clearCollection" ) ; } insertObjects ( collectionName , objects , shouldReplaceExisting ? ) { return this . rejectNotSupported ( "insertObjects" ) ; } updateObjects ( collectionName , objects ) { return this . rejectNotSupported ( "updateObjects" ) ; } updateObjectProperty ( collectionName , objectIds , property , value , shouldCreateMissing ? ) { return this . rejectNotSupported ( "updateObjectProperty" ) ; } deleteObjectsById ( collectionName , objectIds ) { return this . rejectNotSupported ( "deleteObjects" ) ; } deleteObjects ( collectionName , filter ? ) { return this . rejectNotSupported ( "deleteObjectsBy" ) ; } findObjectById < T extends CoatyObject > ( collectionName , id , isExternalId = false ) { return this . rejectNotSupported ( "findObject" ) ; } findObjects < T extends CoatyObject > ( collectionName , filter ? , joinConditions ? ) { return this . rejectNotSupported ( "findObjects" ) ; } countObjects ( collectionName , filter ? ) { return this . rejectNotSupported ( "countObjects" ) ; } aggregateObjects ( collectionName , aggregateProps , aggregateOp , filter ? ) { return this . rejectNotSupported ( "aggregateObjects" ) ; } query ( sql ) { return this . rejectNotSupported ( "query" ) ; } iquery ( sql , queryOptions ? ) { return this . rejectNotSupported ( "iquery" ) ; } transaction < T > ( action ) { return this . rejectNotSupported ( "transaction" ) ; } addStore ( name ) { return this . rejectNotSupported ( "addStore" ) ; } removeStore ( name ) { return this . rejectNotSupported ( "removeStore" ) ; } getValue ( key , store ? ) { return this . rejectNotSupported ( "getValue" ) ; } getValues ( store ? ) { return this . rejectNotSupported ( "getValues" ) ; } setValue ( key , value , store ? ) { return this . rejectNotSupported ( "setValue" ) ; } deleteValue ( key , store ? ) { return this . rejectNotSupported ( "deleteValue" ) ; } clearValues ( store ? ) { return this . rejectNotSupported ( "clearValues" ) ; } close ( ) { return this . rejectNotSupported ( "close" ) ; } param name param params callExtension ( name , ... params ) { const ext = this . _extensions . has ( name ) ? this [ name ] as DbAdapterExtension : undefined ; if ( ext ) { return Promise . resolve ( ) . then ( ( ) => { return ext . apply ( this , params ) ; } ) ; } return Promise . reject ( new Error ( `Undefined extension '${ name }'` ) ) ; } param name protected registerExtension ( extensionName ) { this . _extensions . add ( extensionName ) ; } protected rejectNotSupported ( operationName ) { return Promise . reject ( new Error ( `Operation '${ operationName }' not supported by adapter` ) ) ; } }