fix(collection): Ensure TC39 decorator metadata is initialized on both original and decorated constructors/prototypes and add debug logging
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartdata',
|
||||
version: '7.0.9',
|
||||
version: '7.0.12',
|
||||
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -951,6 +951,7 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
const globalProps = this.globalSaveableProperties || [];
|
||||
const specificProps = this.saveableProperties || [];
|
||||
const saveableProperties = [...globalProps, ...specificProps];
|
||||
logger.log('debug', `createSavableObject: globalProps=${globalProps.length}, specificProps=${specificProps.length}, total=${saveableProperties.length}`);
|
||||
// apply custom serialization if configured
|
||||
const optionsMap = (this.constructor as any)._svDbOptions || {};
|
||||
for (const propertyNameString of saveableProperties) {
|
||||
|
||||
Reference in New Issue
Block a user