fix(collection): Ensure TC39 decorator metadata is initialized on both original and decorated constructors/prototypes and add debug logging

This commit is contained in:
2025-11-28 11:27:36 +00:00
parent 2fe3a72eaf
commit 11a1345891
4 changed files with 95 additions and 31 deletions

View File

@@ -93,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
@@ -244,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