fix(classes): Add Deno compatibility, prototype-safe decorators and safe collection accessor; bump a few deps
This commit is contained in:
@@ -27,30 +27,51 @@ const collectionFactory = new CollectionFactory();
|
||||
*/
|
||||
export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
|
||||
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
|
||||
// Capture original constructor's prototype in closure for Deno compatibility
|
||||
const originalPrototype = constructor.prototype;
|
||||
const originalConstructor = constructor as any;
|
||||
|
||||
const getCollection = () => {
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
dbArg = dbArg();
|
||||
}
|
||||
const coll = collectionFactory.getCollection(constructor.name, dbArg);
|
||||
// Attach document constructor for searchableFields lookup
|
||||
if (!(coll as any).docCtor) {
|
||||
(coll as any).docCtor = decoratedClass;
|
||||
}
|
||||
return coll;
|
||||
};
|
||||
|
||||
const decoratedClass = class extends constructor {
|
||||
public static className = constructor.name;
|
||||
public static get collection() {
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
dbArg = dbArg();
|
||||
}
|
||||
const coll = collectionFactory.getCollection(constructor.name, dbArg);
|
||||
// Attach document constructor for searchableFields lookup
|
||||
if (!(coll as any).docCtor) {
|
||||
(coll as any).docCtor = decoratedClass;
|
||||
}
|
||||
return coll;
|
||||
return getCollection();
|
||||
}
|
||||
public get collection() {
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
dbArg = dbArg();
|
||||
}
|
||||
const coll = collectionFactory.getCollection(constructor.name, dbArg);
|
||||
if (!(coll as any).docCtor) {
|
||||
(coll as any).docCtor = decoratedClass;
|
||||
}
|
||||
return coll;
|
||||
return getCollection();
|
||||
}
|
||||
};
|
||||
|
||||
// Ensure instance getter works in Deno by defining it on the prototype
|
||||
Object.defineProperty(decoratedClass.prototype, 'collection', {
|
||||
get: getCollection,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
// Deno compatibility note: Property decorators set properties on the prototype.
|
||||
// Since we removed instance property declarations from SmartDataDbDoc,
|
||||
// the decorator-set prototype properties are now accessible without shadowing.
|
||||
// No manual forwarding needed - natural prototype inheritance works!
|
||||
|
||||
// Point to original constructor's _svDbOptions
|
||||
Object.defineProperty(decoratedClass, '_svDbOptions', {
|
||||
get() { return originalConstructor._svDbOptions; },
|
||||
set(value) { originalConstructor._svDbOptions = value; },
|
||||
configurable: true
|
||||
});
|
||||
|
||||
return decoratedClass;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user