|
|
|
|
@@ -64,6 +64,20 @@ export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
|
|
|
|
|
configurable: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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 getter on the original constructor.
|
|
|
|
|
Object.defineProperty(constructor, 'collection', {
|
|
|
|
|
get: getCollection,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
configurable: true
|
|
|
|
|
});
|
|
|
|
|
Object.defineProperty(constructor.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.
|
|
|
|
|
@@ -79,32 +93,61 @@ export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
|
|
|
|
|
// Initialize prototype properties from context.metadata (TC39 decorator metadata)
|
|
|
|
|
// This ensures prototype properties are available before any instance is created
|
|
|
|
|
const metadata = context.metadata as any;
|
|
|
|
|
logger.log('debug', `Collection decorator for ${constructor.name}: metadata.saveableProperties = ${metadata?.saveableProperties?.length ?? 'undefined'}`);
|
|
|
|
|
if (metadata) {
|
|
|
|
|
const proto = decoratedClass.prototype;
|
|
|
|
|
const origProto = constructor.prototype;
|
|
|
|
|
|
|
|
|
|
// Initialize globalSaveableProperties
|
|
|
|
|
if (metadata.globalSaveableProperties && !proto.globalSaveableProperties) {
|
|
|
|
|
proto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
// Initialize globalSaveableProperties on BOTH prototypes
|
|
|
|
|
if (metadata.globalSaveableProperties) {
|
|
|
|
|
if (!proto.globalSaveableProperties) {
|
|
|
|
|
proto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.globalSaveableProperties) {
|
|
|
|
|
origProto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize saveableProperties
|
|
|
|
|
if (metadata.saveableProperties && !proto.saveableProperties) {
|
|
|
|
|
proto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
// Initialize saveableProperties on BOTH prototypes (closure fix)
|
|
|
|
|
if (metadata.saveableProperties) {
|
|
|
|
|
if (!proto.saveableProperties) {
|
|
|
|
|
proto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
}
|
|
|
|
|
// Also set on original constructor's prototype for closure references
|
|
|
|
|
if (!origProto.saveableProperties) {
|
|
|
|
|
origProto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
logger.log('debug', `Collection decorator: set saveableProperties on original prototype (${origProto.saveableProperties.length} props)`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize uniqueIndexes
|
|
|
|
|
if (metadata.uniqueIndexes && !proto.uniqueIndexes) {
|
|
|
|
|
proto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
// Initialize uniqueIndexes on BOTH prototypes
|
|
|
|
|
if (metadata.uniqueIndexes) {
|
|
|
|
|
if (!proto.uniqueIndexes) {
|
|
|
|
|
proto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.uniqueIndexes) {
|
|
|
|
|
origProto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize regularIndexes
|
|
|
|
|
if (metadata.regularIndexes && !proto.regularIndexes) {
|
|
|
|
|
proto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
// Initialize regularIndexes on BOTH prototypes
|
|
|
|
|
if (metadata.regularIndexes) {
|
|
|
|
|
if (!proto.regularIndexes) {
|
|
|
|
|
proto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.regularIndexes) {
|
|
|
|
|
origProto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize searchableFields on constructor (not prototype)
|
|
|
|
|
if (metadata.searchableFields && !Array.isArray((decoratedClass as any).searchableFields)) {
|
|
|
|
|
(decoratedClass as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
// Initialize searchableFields on BOTH constructors
|
|
|
|
|
if (metadata.searchableFields) {
|
|
|
|
|
if (!Array.isArray((decoratedClass as any).searchableFields)) {
|
|
|
|
|
(decoratedClass as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray((constructor as any).searchableFields)) {
|
|
|
|
|
(constructor as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize _svDbOptions from metadata
|
|
|
|
|
@@ -230,30 +273,56 @@ export function managed<TManager extends IManager>(managerArg?: TManager | TDela
|
|
|
|
|
const metadata = context.metadata as any;
|
|
|
|
|
if (metadata) {
|
|
|
|
|
const proto = decoratedClass.prototype;
|
|
|
|
|
const origProto = constructor.prototype;
|
|
|
|
|
|
|
|
|
|
// Initialize globalSaveableProperties
|
|
|
|
|
if (metadata.globalSaveableProperties && !proto.globalSaveableProperties) {
|
|
|
|
|
proto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
// Initialize globalSaveableProperties on BOTH prototypes
|
|
|
|
|
if (metadata.globalSaveableProperties) {
|
|
|
|
|
if (!proto.globalSaveableProperties) {
|
|
|
|
|
proto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.globalSaveableProperties) {
|
|
|
|
|
origProto.globalSaveableProperties = [...metadata.globalSaveableProperties];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize saveableProperties
|
|
|
|
|
if (metadata.saveableProperties && !proto.saveableProperties) {
|
|
|
|
|
proto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
// Initialize saveableProperties on BOTH prototypes (closure fix)
|
|
|
|
|
if (metadata.saveableProperties) {
|
|
|
|
|
if (!proto.saveableProperties) {
|
|
|
|
|
proto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.saveableProperties) {
|
|
|
|
|
origProto.saveableProperties = [...metadata.saveableProperties];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize uniqueIndexes
|
|
|
|
|
if (metadata.uniqueIndexes && !proto.uniqueIndexes) {
|
|
|
|
|
proto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
// Initialize uniqueIndexes on BOTH prototypes
|
|
|
|
|
if (metadata.uniqueIndexes) {
|
|
|
|
|
if (!proto.uniqueIndexes) {
|
|
|
|
|
proto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.uniqueIndexes) {
|
|
|
|
|
origProto.uniqueIndexes = [...metadata.uniqueIndexes];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize regularIndexes
|
|
|
|
|
if (metadata.regularIndexes && !proto.regularIndexes) {
|
|
|
|
|
proto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
// Initialize regularIndexes on BOTH prototypes
|
|
|
|
|
if (metadata.regularIndexes) {
|
|
|
|
|
if (!proto.regularIndexes) {
|
|
|
|
|
proto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
}
|
|
|
|
|
if (!origProto.regularIndexes) {
|
|
|
|
|
origProto.regularIndexes = [...metadata.regularIndexes];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize searchableFields on constructor (not prototype)
|
|
|
|
|
if (metadata.searchableFields && !Array.isArray((decoratedClass as any).searchableFields)) {
|
|
|
|
|
(decoratedClass as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
// Initialize searchableFields on BOTH constructors
|
|
|
|
|
if (metadata.searchableFields) {
|
|
|
|
|
if (!Array.isArray((decoratedClass as any).searchableFields)) {
|
|
|
|
|
(decoratedClass as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray((constructor as any).searchableFields)) {
|
|
|
|
|
(constructor as any).searchableFields = [...metadata.searchableFields];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize _svDbOptions from metadata
|
|
|
|
|
|