/// import { IQueryParams, DeepPartial, DbSetPickDefaultActionRequired, EntitySelector } from "./common-types"; import { ITrackedData, IDataContext } from "./context-types"; import { DbSetKeyType, ISplitDbSetOptions, PropertyMap } from "./dbset-builder-types"; import { IDbRecord, OmittedEntity, IDbRecordBase, EntityIdKeys } from "./entity-types"; export interface IDbSetEnumerable> extends IDbSetBase { /** * Return all items in the underlying data store for the document type * @returns {Promise} */ all(): Promise; /** * Filter items in the underlying data store and return the results * @param selector Callback to filter entities matching the criteria * @returns {Promise} */ filter(selector: (entity: TEntity, index?: number, array?: TEntity[]) => boolean): Promise; /** * Find first item matching the selector in the underlying data store and return the result * @param selector Callback to find entity matching the criteria * @returns {Promise} */ find(selector: (entity: TEntity, index?: number, array?: TEntity[]) => boolean): Promise; /** * Find first item in the underlying data store and return the result * @returns {Promise} */ first(): Promise; } export interface ISplitDbSet, TExtraExclusions extends string = never> extends IDbSet { lazy(): ISplitDbSet & { include(...properties: string[]): ISplitDbSet; }; endTransaction(): Promise; startTransaction(transactionId: string): Promise; } export interface IDbSet, TExtraExclusions extends string = never> extends IDbSetEnumerable { get types(): { modify: OmittedEntity; result: TEntity; documentType: TEntity["DocumentType"]; map: { [DocumentType in TEntity["DocumentType"]]: TEntity; }; }; query(request: DeepPartial>): Promise>; /** * Add a tag to the transaction (one or more entites from add/remove/upsert) and make available for onAfterSaveChanges or onBeforeSaveChanges. * Data is not persisted to the database and is cleared on saveChanges or when context is disposed in memory * @param value Any value */ tag(value: unknown): this; /** * Direct pouchDB to use an index with your request. Index will only be used with the single request, all subsequent requests will use the default index if any * @param name Name of the index * @returns {Promise} */ useIndex(name: string): IDbSetEnumerable; /** * Mark an entity as dirty, will be saved even if there are no changes detected * @param entities Entities to mark as dirty * @returns {Promise} */ markDirty(...entities: TEntity[]): Promise; /** * Find entity by an id or ids * @param ids ids of the documents to retrieve * @returns {Promise} */ get(...ids: string[]): Promise; /** * Add one or more entities from the underlying data context, saveChanges must be called to persist these items to the store * @param entities Entity or entities to add to the data context * @returns {Promise} */ add(...entities: OmittedEntity[]): Promise; /** * Add or update one or more entities from the underlying data context, saveChanges must be called to persist these items to the store * @param entities Entity or entities to add to the data context * @returns {Promise} */ upsert(...entities: (OmittedEntity | Omit)[]): Promise; /** * Create one or more entities and do not add it to the underlying data context. This is useful for creating entities and passing them to other functions. * Call {@link add} to add the entity to a context for persistance * @param entities Entity or entities to create * @returns {TEntity[]} */ instance(...entities: OmittedEntity[]): TEntity[]; /** * Remove one or more entities from the underlying data context, saveChanges must be called to persist these items to the store * @param entities Entity or entities to remove from the data context * @returns {Promise} */ remove(...entities: TEntity[]): Promise; /** * Remove one or more entities by id from the underlying data context, saveChanges must be called to persist these items to the store * @param ids Entity id or ids to remove from the data context * @returns {Promise} */ remove(...ids: string[]): Promise; /** * Check for equality between two entities * @param first First entity to compare * @param second Second entity to compare * @returns {boolean} */ isMatch(first: TEntity, second: any): boolean; /** * Unlinks an entity or entities from the context so they can be modified and changes will not be persisted to the underlying data store * @param entities Entity or entities to unlink from the data context * @returns {void} */ unlink(...entities: TEntity[]): void; /** * Link an existing entitiy or entities to the underlying Data Context, saveChanges must be called to persist these items to the store * @param entites Entity or entities to link from the data context * @returns {Promise} */ link(...entites: TEntity[]): Promise; /** * Matches items with the same document type. Useful for retrieving all docs and calling match() to find the ones that belong in the db set * @param entities Entity or entities to match on document type. * @returns {TEntity[]} */ match(...entities: IDbRecordBase[]): TEntity[]; /** * Get DbSet info * @returns {IDbSetInfo} */ info(): IDbSetInfo; } export interface IDbSetBase { /** * Remove all entities from the underlying data context, saveChanges must be called to persist these changes to the store */ empty(): Promise; } export interface IDbSetApi { getTrackedData: () => ITrackedData; getAllData: (payload?: IQueryParams) => Promise; find: (selector: PouchDB.Find.FindRequest<{}>) => Promise; query>(selector: PouchDB.Find.FindRequest): Promise>; get: (...ids: string[]) => Promise; getStrict: (...ids: string[]) => Promise; send: (data: IDbRecordBase[]) => void; detach: (data: IDbRecordBase[]) => IDbRecordBase[]; makeTrackable(entity: T, defaults: DeepPartial>, readonly: boolean, maps: PropertyMap[]): T; makePristine(...entities: IDbRecordBase[]): void; map(entity: T, maps: PropertyMap[], defaults?: DeepPartial>): T; tag(id: string, value: unknown): void; readonly DIRTY_ENTITY_MARKER: string; readonly PRISTINE_ENTITY_KEY: string; } export interface IDbSetInfo> { DocumentType: TDocumentType; IdKeys: EntityIdKeys; Defaults: DbSetPickDefaultActionRequired; KeyType: DbSetKeyType; Map: PropertyMap[]; Readonly: boolean; SplitDbSetOptions: ISplitDbSetOptions; } export interface IDbSetProps> { documentType: TDocumentType; context: IDataContext; defaults: DbSetPickDefaultActionRequired; idKeys: EntityIdKeys; readonly: boolean; keyType: DbSetKeyType; map: PropertyMap[]; index: string | undefined; splitDbSetOptions: ISplitDbSetOptions; filterSelector: EntitySelector | null; } export type DbSetEventCallback> = (entity: TEntity) => void; export type DbSetIdOnlyEventCallback = (entity: string) => void; export type DbSetEventCallbackAsync> = (entities: TEntity[]) => Promise; export type DbSetIdOnlyEventCallbackAsync = (entities: string[]) => Promise; export type IncludeType = "all" | string[]; export type EntityAndTag = { entity: T; tag?: unknown; };