import { O } from 'ts-toolbelt' ; import { merge , mergeAndConcat } from 'merge-anything' ; import { MagnetarFetchAction , MagnetarStreamAction , MagnetarInsertAction , MagnetarDeleteAction , FetchPromises , } from './types/actions' ; import { actionNameTypeMap } from './types/actionsInternal' ; import { handleActionPerStore , HandleActionSharedParams , } from './moduleActions/handleActionPerStore' ; import { handleStreamPerStore } from './moduleActions/handleStreamPerStore' ; import { ModuleConfig , GlobalConfig } from './types/config' ; import { DocFn , CollectionFn , WriteLock } from './Magnetar' ; import { executeSetupModulePerStore , getDataProxyHandler } from './helpers/moduleHelpers' ; import { WhereClause , WhereFilterOp , OrderByClause } from './types/clauses' ; import { OPaths } from './types/helpers' ; export type CollectionInstance < DocDataType extends Record < string , any > = Record < string , any > > = { /**
     * The cached data that was written or read so far
     */ data : Map < string , DocDataType > ; see DocFn example doc : DocFn < DocDataType > ; example id : string ; example example path : string ; /**
     * Returns the open stream promise of this collection, dependant on which `where`/`limit`/`orderBy` filters was used.
     *
     * Returns `null` when there is no open stream.
     *
     * This promise will resolve when `collection().closeStream()` is called, or when the stream closed because of an error.
     */ streaming : ( ) => Promise < void > | null ; /**
     * Close the stream of this collection, dependant on which `where`/`limit`/`orderBy` filters was used.
     *
     * Does nothing if there is no open stream.
     */ closeStream : ( ) => void ; /**
     * Close all streams of this collection, no matter which `where`/`limit`/`orderBy` filters were used.
     *
     * Does nothing if there are no open streams.
     */ closeAllStreams : ( ) => void ; see MagnetarFetchAction fetch : MagnetarFetchAction < DocDataType , 'collection' > ; see MagnetarStreamAction stream : MagnetarStreamAction ; see MagnetarInsertAction insert : MagnetarInsertAction < DocDataType > ; see MagnetarDeleteAction delete : MagnetarDeleteAction ; /**
     * Chainable filter. Returns  CollectionInstance with filter applied.
      orderBy : ( fieldPath : OPaths < DocDataType , 4 > , direction ? : 'asc' | 'desc' ) => CollectionInstance < DocDataType > ; /**
     * Chainable filter. Returns  CollectionInstance with filter applied.
      limit : ( limitCount : number ) => CollectionInstance < DocDataType > ; /**
     * Chainable filter. Returns  CollectionInstance with filter applied.
      where : ( fieldPath : OPaths < DocDataType , 4 > , operator : WhereFilterOp , value : any ) => CollectionInstance < DocDataType > ; } ; export function createCollectionWithContext < DocDataType extends Record < string , any > > ( [ collectionPath , docId ] , moduleConfig , globalConfig , docFn , collectionFn , streamAndFetchPromises ) { const { writeLockMap , fetchPromises , cacheStream , streaming , closeStream , closeAllStreams } = streamAndFetchPromises ; const id = collectionPath . split ( '/' ) . slice ( - 1 ) [ 0 ] ; const path = collectionPath ; const doc = ( docId , _moduleConfig = { } ) => { return docFn ( `${ path }/${ docId }` , merge ( moduleConfig , _moduleConfig ) ) ; } ; const sharedParams = { collectionPath , _docId : docId , moduleConfig , globalConfig , fetchPromises , writeLockMap , docFn , collectionFn , } ; const insert = handleActionPerStore ( sharedParams , 'insert' , actionNameTypeMap . insert ) as MagnetarInsertAction < DocDataType > ; const _delete = handleActionPerStore ( sharedParams , 'delete' , actionNameTypeMap . delete ) as MagnetarDeleteAction ; const fetch = handleActionPerStore ( sharedParams , 'fetch' , actionNameTypeMap . fetch ) as MagnetarFetchAction < DocDataType , 'collection' > ; const stream = handleStreamPerStore ( [ collectionPath , docId ] , moduleConfig , globalConfig , actionNameTypeMap . stream , streaming , cacheStream , writeLockMap ) ; const actions = { stream , fetch , insert , delete : _delete } ; executeSetupModulePerStore ( globalConfig . stores , [ collectionPath , docId ] , moduleConfig ) ; function where ( fieldPath , operator , value ) { const whereClause = [ fieldPath , operator , value ] ; const moduleConfigWithClause = mergeAndConcat ( moduleConfig , { where : [ whereClause ] } ) ; return collectionFn ( path , moduleConfigWithClause ) ; } function orderBy ( fieldPath , direction = 'asc' ) { const orderByClause = [ fieldPath , direction ] ; const moduleConfigWithClause = mergeAndConcat ( moduleConfig , { orderBy : [ orderByClause ] } ) ; return collectionFn ( path , moduleConfigWithClause ) ; } function limit ( limitCount ) { return collectionFn ( path , { ... moduleConfig , limit : limitCount } ) ; } const queryFns = { where , orderBy , limit } ; const moduleInstance = { doc , id , path , streaming , closeStream , closeAllStreams , ... actions , ... queryFns , } ; /**
     * The data returned by the store specified as 'localStoreName'
     */ const dataProxyHandler = getDataProxyHandler < 'collection' , DocDataType > ( [ collectionPath , docId ] , moduleConfig , globalConfig ) ; return new Proxy ( moduleInstance , dataProxyHandler ) ; }