Compare commits

..

6 Commits

Author SHA1 Message Date
7606e074a5 5.2.5 2024-06-18 20:11:41 +02:00
7ec39e397e fix(core): update 2024-06-18 20:11:40 +02:00
21d8d3dc32 5.2.4 2024-05-31 18:47:49 +02:00
6d456955d8 fix(core): update 2024-05-31 18:47:48 +02:00
d08544c782 5.2.3 2024-05-31 18:39:34 +02:00
bda9ac8a07 fix(saveableProperties): fix issue where _createdAt and _updatedAt registered saveableProperties for all document types 2024-05-31 18:39:33 +02:00
3 changed files with 35 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartdata", "name": "@push.rocks/smartdata",
"version": "5.2.2", "version": "5.2.5",
"private": false, "private": false,
"description": "An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.", "description": "An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartdata', name: '@push.rocks/smartdata',
version: '5.2.2', version: '5.2.5',
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.' description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
} }

View File

@ -7,16 +7,26 @@ import { SmartdataDbWatcher } from './smartdata.classes.watcher.js';
export type TDocCreation = 'db' | 'new' | 'mixed'; export type TDocCreation = 'db' | 'new' | 'mixed';
export function globalSvDb() {
return (target: SmartDataDbDoc<unknown, unknown>, 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 * saveable - saveable decorator to be used on class properties
*/ */
export function svDb() { export function svDb() {
return (target: SmartDataDbDoc<unknown, unknown>, key: string) => { return (target: SmartDataDbDoc<unknown, unknown>, key: string) => {
console.log(`called svDb() on >${target.constructor.name}.${key}<`); console.log(`called svDb() on >${target.constructor.name}.${key}<`);
if (!target.constructor.prototype.saveableProperties) { if (!target.saveableProperties) {
target.constructor.prototype.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}<<`); console.log(`called unI on >>${target.constructor.name}.${key}<<`);
// mark the index as unique // mark the index as unique
if (!target.constructor.prototype.uniqueIndexes) { if (!target.uniqueIndexes) {
target.constructor.prototype.uniqueIndexes = []; target.uniqueIndexes = [];
} }
target.constructor.prototype.uniqueIndexes.push(key); target.uniqueIndexes.push(key);
// and also save it // and also save it
if (!target.constructor.prototype.saveableProperties) { if (!target.saveableProperties) {
target.constructor.prototype.saveableProperties = []; target.saveableProperties = [];
} }
target.constructor.prototype.saveableProperties.push(key); target.saveableProperties.push(key);
}; };
} }
@ -66,7 +76,7 @@ export const convertFilterForMongoDb = (filterArg: { [key: string]: any }) => {
return convertedFilter; return convertedFilter;
}; };
export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends IManager = any> { export class SmartDataDbDoc<T extends TImplements, TImplements, TManager = any> {
/** /**
* the collection object an Doc belongs to * the collection object an Doc belongs to
*/ */
@ -202,22 +212,27 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
/** /**
* updated from db in any case where doc comes from db * updated from db in any case where doc comes from db
*/ */
@svDb() @globalSvDb()
_createdAt: string = (new Date()).toISOString(); _createdAt: string = (new Date()).toISOString();
/** /**
* will be updated everytime the doc is saved * will be updated everytime the doc is saved
*/ */
@svDb() @globalSvDb()
_updatedAt: string = (new Date()).toISOString(); _updatedAt: string = (new Date()).toISOString();
/**
* an array of saveable properties of ALL doc
*/
public globalSaveableProperties: string[];
/** /**
* unique indexes * unique indexes
*/ */
public uniqueIndexes: string[]; public uniqueIndexes: string[];
/** /**
* an array of saveable properties of a doc * an array of saveable properties of a specific doc
*/ */
public saveableProperties: string[]; public saveableProperties: string[];
@ -301,7 +316,11 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
*/ */
public async createSavableObject(): Promise<TImplements> { public async createSavableObject(): Promise<TImplements> {
const saveableObject: unknown = {}; // is not exposed to outside, so any is ok here const saveableObject: unknown = {}; // is not exposed to outside, so any is ok here
for (const propertyNameString of this.constructor.prototype.saveableProperties) { const saveableProperties = [
...this.globalSaveableProperties,
...this.saveableProperties
]
for (const propertyNameString of saveableProperties) {
saveableObject[propertyNameString] = this[propertyNameString]; saveableObject[propertyNameString] = this[propertyNameString];
} }
return saveableObject as TImplements; return saveableObject as TImplements;