fix(collectionfactory): isolate collection caching per database and add easy store replace semantics
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartdata',
|
||||
version: '7.1.6',
|
||||
version: '7.1.7',
|
||||
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
||||
}
|
||||
|
||||
@@ -2,13 +2,51 @@ import * as plugins from './plugins.js';
|
||||
import { SmartdataCollection } from './classes.collection.js';
|
||||
import { SmartdataDb } from './classes.db.js';
|
||||
|
||||
/**
|
||||
* Per-SmartdataDb collection cache.
|
||||
*
|
||||
* Historically this class keyed its cache by class name alone, which meant
|
||||
* the first SmartdataDb to request a collection of a given class name
|
||||
* "won" — every subsequent call from a different SmartdataDb instance
|
||||
* received the cached collection bound to the first db. That silently
|
||||
* broke multi-tenant SaaS apps (one db per tenant), tests instantiating
|
||||
* multiple SmartdataDbs in sequence, and any in-process db cluster switch.
|
||||
*
|
||||
* The cache is now keyed by `(SmartdataDb instance, className)` using a
|
||||
* WeakMap of db → Map<className, SmartdataCollection>. Entries are GC'd
|
||||
* with their parent db automatically.
|
||||
*/
|
||||
export class CollectionFactory {
|
||||
public collections: { [key: string]: SmartdataCollection<any> } = {};
|
||||
private perDbCollections: WeakMap<SmartdataDb, Map<string, SmartdataCollection<any>>> = new WeakMap();
|
||||
|
||||
public getCollection = (nameArg: string, dbArg: SmartdataDb): SmartdataCollection<any> => {
|
||||
if (!this.collections[nameArg] && dbArg instanceof SmartdataDb) {
|
||||
this.collections[nameArg] = new SmartdataCollection(nameArg, dbArg);
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
// Preserve the historical behavior of returning undefined-ish for
|
||||
// non-db args. All in-repo callers already guard on instanceof
|
||||
// before using the result (see classes.collection.ts).
|
||||
return undefined as unknown as SmartdataCollection<any>;
|
||||
}
|
||||
return this.collections[nameArg];
|
||||
let dbMap = this.perDbCollections.get(dbArg);
|
||||
if (!dbMap) {
|
||||
dbMap = new Map();
|
||||
this.perDbCollections.set(dbArg, dbMap);
|
||||
}
|
||||
let coll = dbMap.get(nameArg);
|
||||
if (!coll) {
|
||||
coll = new SmartdataCollection(nameArg, dbArg);
|
||||
dbMap.set(nameArg, coll);
|
||||
}
|
||||
return coll;
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated Internal back-compat shim. The previous field was a public
|
||||
* Record<className, SmartdataCollection> but was not part of the
|
||||
* documented public API. WeakMap is not iterable, so this getter returns
|
||||
* an empty object — anyone actually relying on the old shape would get
|
||||
* clean nothing rather than wrong-db data. Will be removed in 8.0.
|
||||
*/
|
||||
public get collections(): { [key: string]: SmartdataCollection<any> } {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,9 @@ export class EasyStore<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* writes all keyValue pairs in the object argument
|
||||
* merges all keyValue pairs from the object argument into the store.
|
||||
* Existing keys that are not present in `keyValueObject` are preserved.
|
||||
* To overwrite the entire store and drop missing keys, use `replace()`.
|
||||
*/
|
||||
public async writeAll(keyValueObject: Partial<T>) {
|
||||
const easyStore = await this.getEasyStore();
|
||||
@@ -101,6 +103,17 @@ export class EasyStore<T> {
|
||||
await easyStore.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* atomically replaces the entire store with the given object.
|
||||
* Unlike `writeAll` (which merges), `replace` clears any keys not
|
||||
* present in `keyValueObject`. Useful when you need to drop a key.
|
||||
*/
|
||||
public async replace(keyValueObject: Partial<T>) {
|
||||
const easyStore = await this.getEasyStore();
|
||||
easyStore.data = { ...keyValueObject };
|
||||
await easyStore.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* wipes a key value store from disk
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user