From bda9ac8a07a26cf466026366c09aace4fc92ff8f Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 31 May 2024 18:39:33 +0200 Subject: [PATCH] fix(saveableProperties): fix issue where _createdAt and _updatedAt registered saveableProperties for all document types --- ts/00_commitinfo_data.ts | 2 +- ts/smartdata.classes.doc.ts | 41 +++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 5b83135..5e41500 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartdata', - version: '5.2.2', + version: '5.2.3', description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.' } diff --git a/ts/smartdata.classes.doc.ts b/ts/smartdata.classes.doc.ts index 680988c..55627cc 100644 --- a/ts/smartdata.classes.doc.ts +++ b/ts/smartdata.classes.doc.ts @@ -7,16 +7,26 @@ import { SmartdataDbWatcher } from './smartdata.classes.watcher.js'; export type TDocCreation = 'db' | 'new' | 'mixed'; +export function globalSvDb() { + return (target: SmartDataDbDoc, key: string) => { + console.log(`called svDb() on >${target.constructor.name}.${key}<`); + if (!target.globalSaveableProperties) { + target.globalSaveableProperties = []; + } + target.globalSaveableProperties.push(key); + }; +} + /** * saveable - saveable decorator to be used on class properties */ export function svDb() { return (target: SmartDataDbDoc, key: string) => { console.log(`called svDb() on >${target.constructor.name}.${key}<`); - if (!target.constructor.prototype.saveableProperties) { - target.constructor.prototype.saveableProperties = []; + if (!target.saveableProperties) { + target.saveableProperties = []; } - target.constructor.prototype.saveableProperties.push(key); + target.saveableProperties.push(key); }; } @@ -28,16 +38,16 @@ export function unI() { console.log(`called unI on >>${target.constructor.name}.${key}<<`); // mark the index as unique - if (!target.constructor.prototype.uniqueIndexes) { - target.constructor.prototype.uniqueIndexes = []; + if (!target.uniqueIndexes) { + target.uniqueIndexes = []; } - target.constructor.prototype.uniqueIndexes.push(key); + target.uniqueIndexes.push(key); // and also save it - if (!target.constructor.prototype.saveableProperties) { - target.constructor.prototype.saveableProperties = []; + if (!target.saveableProperties) { + target.saveableProperties = []; } - target.constructor.prototype.saveableProperties.push(key); + target.saveableProperties.push(key); }; } @@ -202,22 +212,27 @@ export class SmartDataDbDoc { const saveableObject: unknown = {}; // is not exposed to outside, so any is ok here - for (const propertyNameString of this.constructor.prototype.saveableProperties) { + for (const propertyNameString of this.saveableProperties) { saveableObject[propertyNameString] = this[propertyNameString]; } return saveableObject as TImplements;