fix(classes.collection): Fix closure issue in managed decorator so Class.collection/instance.collection resolve correctly
This commit is contained in:
@@ -188,6 +188,42 @@ export function managed<TManager extends IManager>(managerArg?: TManager | TDela
|
||||
}
|
||||
};
|
||||
|
||||
// Closure fix: When class methods reference the class name (e.g., `User.collection`),
|
||||
// they get the original constructor via closure, not the decorated class.
|
||||
// Define collection/manager getters on the original constructor.
|
||||
const getCollectionStatic = function(this: any) {
|
||||
let dbArg: SmartdataDb;
|
||||
if (!managerArg) {
|
||||
dbArg = this.prototype.defaultManager.db;
|
||||
} else if (managerArg['db']) {
|
||||
dbArg = (managerArg as TManager).db;
|
||||
} else {
|
||||
dbArg = (managerArg as TDelayed<TManager>)().db;
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
};
|
||||
const getCollectionInstance = function(this: any) {
|
||||
let dbArg: SmartdataDb;
|
||||
if (!managerArg) {
|
||||
dbArg = this.defaultManager.db;
|
||||
} else if (managerArg['db']) {
|
||||
dbArg = (managerArg as TManager).db;
|
||||
} else {
|
||||
dbArg = (managerArg as TDelayed<TManager>)().db;
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
};
|
||||
Object.defineProperty(constructor, 'collection', {
|
||||
get: getCollectionStatic,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(constructor.prototype, 'collection', {
|
||||
get: getCollectionInstance,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
// Initialize prototype properties from context.metadata (TC39 decorator metadata)
|
||||
// This ensures prototype properties are available before any instance is created
|
||||
const originalConstructor = value as any;
|
||||
|
||||
Reference in New Issue
Block a user