| 14 | import { deepObjectAssign } from './utils/deep-merge.js'; |
| 15 | |
| 16 | export class EntityDataStore implements EntityStore { |
| 17 | constructor(public storagePrefix: string[] = []) {} |
| 18 | |
| 19 | getEntity( |
| 20 | storage: KVStoreOrTransaction, |
| 21 | collection: string, |
| 22 | id: string |
| 23 | ): Promise<DBEntity | undefined> { |
| 24 | const prefixedStorage = storage.scope(this.storagePrefix); |
| 25 | return prefixedStorage.get([collection, id]); |
| 26 | } |
| 27 | |
| 28 | async getCollectionStats( |
| 29 | storage: KVStoreOrTransaction, |
| 30 | knownCollections: CollectionName[] | undefined |
| 31 | ): Promise<Map<string, number>> { |
| 32 | const prefixedStorage = storage.scope(this.storagePrefix); |
| 33 | const stats = new Map<string, number>(); |
| 34 | if (knownCollections) { |
| 35 | for (const collection of knownCollections) { |
| 36 | const count = await prefixedStorage.count({ prefix: [collection] }); |
| 37 | stats.set(collection, count); |
| 38 | } |
| 39 | } else { |
| 40 | for await (const [[collection]] of prefixedStorage.scan({ |
| 41 | prefix: [], |
| 42 | })) { |
| 43 | stats.set( |
| 44 | collection as string, |
| 45 | (stats.get(collection as string) || 0) + 1 |
| 46 | ); |
| 47 | } |
| 48 | } |
| 49 | return stats; |
| 50 | } |
| 51 | |
| 52 | async applyChanges( |
| 53 | tx: KVStoreTransaction, |
| 54 | changes: DBChanges, |
| 55 | options: ApplyChangesOptions |
| 56 | ): Promise<DBChanges> { |
| 57 | const prefixedTx = tx.scope(this.storagePrefix); |
| 58 | const appliedChanges: DBChanges = {}; |
| 59 | const deltas: Delta[] = []; |
| 60 | const getInsertChangeset = async ( |
| 61 | collection: string, |
| 62 | id: string, |
| 63 | change: Change |
| 64 | ): Promise<Delta> => { |
| 65 | const current = await this.getEntity(tx, collection, id); |
| 66 | const isUpsert = !!current; |
| 67 | if (options.entityChangeValidator) { |
| 68 | options.entityChangeValidator(collection, change, { |
| 69 | ignoreRequiredProperties: isUpsert, |
| 70 | }); |
| 71 | } |
| 72 | return { |
| 73 | collection, |
nothing calls this directly
no outgoing calls
no test coverage detected