import { Collection as Result , PromiseExtended , Table } from 'dexie' ; import { Query } from '../middleware/mongo' ; import { JSONSchema } from '../middleware/schemas' ; import { Document , DocumentInstanceClassFactory , Instance } from './document' ; /**
 * Index defines an index.
 */ export interface Index { /**
     * Path to the field to index in dot syntax, e.g., "name.last" or "age".
     */ path : string ; /**
     * Unique indicates that only one instance should exist per field value.
     */ unique ? : boolean ; } /**
 * CollectionConfig describes a new Collection.
 */ export interface CollectionConfig { /**
     * The name for the collection
     */ name : string ; /**
     * The JSON Schema definition for instance validation
     */ schema ? : JSONSchema ; /**
     * A set of fields to use for indexing
     */ indexes ? : Index [ ] ; /**
     * A validator function for writes
     */ writeValidator ? : ( author : string , event : any , instance : any ) => boolean ; /**
     * A filter function for reads
     */ readFilter ? : ( reader : string , instance : any ) => any ; } /**
 * Collection is a group of instances sharing a schema.
 * Collections are like db tables. They can only exist in a db.
 */ export class Collection < T = unknown > { constructor ( private table ) { this . table . mapToClass ( DocumentInstanceClassFactory ( table ) ) ; } /**
     * A name for the collection.
     */ get name ( ) : string { return this . table . name ; } /**
     * Delete all instances in the collection.
     */ async clear ( ) { const { result } = await this . table . drop ( ) ; return result . ok > 0 ; } param cb param timeout note note readTransaction < T extends ( ... args : any ) => any > ( cb ) { return this . table . db . transaction ( 'readonly' , [ this . table ] , cb ) as ReturnType < T > ; } param cb param timeout note writeTransaction < T extends ( ... args : any ) => any > ( cb ) { return this . table . db . transaction ( 'readwrite' , [ this . table ] , cb ) as ReturnType < T > ; } param id findById ( id ) { return ( this . table . get ( id ) as unknown ) as PromiseExtended < ( Document < T > & Instance ) | undefined > ; } note note param instances async insert ( ... instances ) { return this . table . bulkAdd ( instances , { allKeys : true } ) ; } param data create ( data ? ) { const cls = this . table . schema . mappedClass ; return new ( cls as any ) ( data ) ; } note param ids async delete ( ... ids ) { await this . table . bulkDelete ( ids ) ; return ; } note param instances save ( ... instances ) { return this . table . bulkPut ( instances , { allKeys : true } ) ; } param id async has ( ... ids ) { const instances = await this . table . bulkGet ( ids ) ; return Boolean ( instances . length ) && instances [ 0 ] !== undefined ; } param query param options find ( query ? ) { return this . table . find ( query ) as Result < Document < T > & Instance , string > ; } param query param options note findOne ( query ? ) { return this . table . findOne ( query ) as Promise < ( Document < T > & Instance ) | undefined > ; } param query param options note count ( query ? ) { return this . table . count ( query ) ; } }