fix(classes): Add Deno compatibility, prototype-safe decorators and safe collection accessor; bump a few deps
This commit is contained in:
@@ -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