import { AuthorizationKey, CollectionFilter, hashObject, } from '@verdant-web/common'; import { Context } from '../context/context.js'; import { DocumentManager } from '../entities/DocumentManager.js'; import { EntityStore } from '../entities/EntityStore.js'; import { ObjectEntity } from '../index.js'; import { UPDATE } from './BaseQuery.js'; import { FindAllQuery } from './FindAllQuery.js'; import { FindInfiniteQuery } from './FindInfiniteQuery.js'; import { FindOneQuery } from './FindOneQuery.js'; import { FindPageQuery } from './FindPageQuery.js'; import { GetQuery } from './GetQuery.js'; import { QueryCache } from './QueryCache.js'; export class CollectionQueries< T extends ObjectEntity, Init, Filter extends CollectionFilter, > { private cache; private collection; private hydrate: (oid: string) => Promise; private context; private documentManager; put: ( init: Init, options?: { undoable?: boolean; access?: AuthorizationKey }, ) => Promise; delete: (id: string, options?: { undoable?: boolean }) => Promise; deleteAll: (ids: string[], options?: { undoable?: boolean }) => Promise; clone: ( entity: ObjectEntity, options?: { undoable?: boolean; access?: AuthorizationKey; primaryKey?: string; }, ) => Promise; constructor({ collection, cache, entities, context, documentManager, }: { collection: string; cache: QueryCache; entities: EntityStore; context: Context; documentManager: DocumentManager; }) { this.cache = cache; this.collection = collection; this.hydrate = entities.hydrate as any; this.context = context; this.documentManager = documentManager; this.put = this.documentManager.create.bind( this.documentManager, this.collection, ); this.delete = this.documentManager.delete.bind( this.documentManager, this.collection, ); this.deleteAll = this.documentManager.deleteAllFromCollection.bind( this.documentManager, this.collection, ); this.clone = this.documentManager.clone.bind( this.documentManager, this.collection, ); } private serializeIndex = (index?: CollectionFilter) => { if (!index) return ''; return hashObject(index); }; get = (id: string) => { const key = `get:${this.collection}:${id}`; return this.cache.getOrSet( key, () => new GetQuery({ id, collection: this.collection, hydrate: this.hydrate, context: this.context, key, }), ); }; findOne = ({ index, key: providedKey, }: { index?: Filter; key?: string } = {}) => { const key = providedKey || `findOne:${this.collection}:${this.serializeIndex(index)}`; return this.cache.getOrSet( key, () => new FindOneQuery({ index, collection: this.collection, hydrate: this.hydrate, context: this.context, key, }), (existing) => { existing[UPDATE](index); }, ); }; findAll = ({ index, key: providedKey, limit, }: { index?: Filter; key?: string; limit?: number } = {}) => { const key = providedKey || `findAll:${this.collection}:${limit}:${this.serializeIndex(index)}`; return this.cache.getOrSet( key, () => new FindAllQuery({ index, collection: this.collection, hydrate: this.hydrate, context: this.context, key, limit, }), (existing) => { existing[UPDATE](index); }, ); }; findPage = ({ index, pageSize, page, key: providedKey, }: { index?: Filter; pageSize: number; page: number; key?: string; }) => { const key = providedKey || `findPage:${this.collection}:${this.serializeIndex(index)}:${pageSize}`; return this.cache.getOrSet( key, () => new FindPageQuery({ index, collection: this.collection, hydrate: this.hydrate, context: this.context, key, pageSize, page, }), (existing) => { existing[UPDATE](index); }, ); }; findAllInfinite = ({ index, pageSize, key: providedKey, }: { index?: Filter; pageSize: number; key?: string; }) => { const key = providedKey || `findAllInfinite:${this.collection}:${this.serializeIndex( index, )}:${pageSize}`; return this.cache.getOrSet( key, () => new FindInfiniteQuery({ index, collection: this.collection, hydrate: this.hydrate, context: this.context, key, pageSize, }), (existing) => { existing[UPDATE](index); }, ); }; }