fix(classes): Add Deno compatibility, prototype-safe decorators and safe collection accessor; bump a few deps
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartdata',
|
||||
version: '5.16.5',
|
||||
version: '5.16.6',
|
||||
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -339,6 +339,13 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
public static manager;
|
||||
public manager: TManager;
|
||||
|
||||
/**
|
||||
* Helper to get collection with fallback to static for Deno compatibility
|
||||
*/
|
||||
private getCollectionSafe(): SmartdataCollection<any> {
|
||||
return this.collection || (this.constructor as any).collection;
|
||||
}
|
||||
|
||||
// STATIC
|
||||
public static createInstanceFromMongoDbNativeDoc<T>(
|
||||
this: plugins.tsclass.typeFest.Class<T>,
|
||||
@@ -698,23 +705,28 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
|
||||
/**
|
||||
* an array of saveable properties of ALL doc
|
||||
* Note: Set by decorators on prototype - NOT declared as instance property to avoid shadowing in Deno
|
||||
* Declared with definite assignment assertion to satisfy TypeScript without creating instance property
|
||||
*/
|
||||
public globalSaveableProperties: string[];
|
||||
declare globalSaveableProperties: string[];
|
||||
|
||||
/**
|
||||
* unique indexes
|
||||
* Note: Set by decorators on prototype - NOT declared as instance property to avoid shadowing in Deno
|
||||
*/
|
||||
public uniqueIndexes: string[];
|
||||
declare uniqueIndexes: string[];
|
||||
|
||||
/**
|
||||
* regular indexes with their options
|
||||
* Note: Set by decorators on prototype - NOT declared as instance property to avoid shadowing in Deno
|
||||
*/
|
||||
public regularIndexes: Array<{field: string, options: IIndexOptions}> = [];
|
||||
declare regularIndexes: Array<{field: string, options: IIndexOptions}>;
|
||||
|
||||
/**
|
||||
* an array of saveable properties of a specific doc
|
||||
* Note: Set by decorators on prototype - NOT declared as instance property to avoid shadowing in Deno
|
||||
*/
|
||||
public saveableProperties: string[];
|
||||
declare saveableProperties: string[];
|
||||
|
||||
/**
|
||||
* name
|
||||
@@ -747,10 +759,10 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
// perform insert or update
|
||||
switch (this.creationStatus) {
|
||||
case 'db':
|
||||
dbResult = await this.collection.update(self, { session: opts?.session });
|
||||
dbResult = await this.getCollectionSafe().update(self, { session: opts?.session });
|
||||
break;
|
||||
case 'new':
|
||||
dbResult = await this.collection.insert(self, { session: opts?.session });
|
||||
dbResult = await this.getCollectionSafe().insert(self, { session: opts?.session });
|
||||
this.creationStatus = 'db';
|
||||
break;
|
||||
default:
|
||||
@@ -772,7 +784,7 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
await (this as any).beforeDelete();
|
||||
}
|
||||
// perform deletion
|
||||
const result = await this.collection.delete(this, { session: opts?.session });
|
||||
const result = await this.getCollectionSafe().delete(this, { session: opts?.session });
|
||||
// allow hook after delete
|
||||
if (typeof (this as any).afterDelete === 'function') {
|
||||
await (this as any).afterDelete();
|
||||
@@ -802,7 +814,7 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
* updates an object from db
|
||||
*/
|
||||
public async updateFromDb(): Promise<boolean> {
|
||||
const mongoDbNativeDoc = await this.collection.findOne(await this.createIdentifiableObject());
|
||||
const mongoDbNativeDoc = await this.getCollectionSafe().findOne(await this.createIdentifiableObject());
|
||||
if (!mongoDbNativeDoc) {
|
||||
return false; // Document not found in database
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user