@rws-framework/db 3.9.0 → 3.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -73,6 +73,6 @@ declare class RWSModel<T> implements IModel {
73
73
  [k: string]: any;
74
74
  }): Promise<number>;
75
75
  static getDb(): DBService;
76
- reload(inPostLoad?: boolean): Promise<RWSModel<T> | null>;
76
+ reload(): Promise<RWSModel<T> | null>;
77
77
  }
78
78
  export { RWSModel };
@@ -12,7 +12,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.RWSModel = void 0;
13
13
  const decorators_1 = require("../../decorators");
14
14
  const FieldsHelper_1 = require("../../helper/FieldsHelper");
15
- const utils_1 = require("../utils");
15
+ const RelationUtils_1 = require("../utils/RelationUtils");
16
+ const TimeSeriesUtils_1 = require("../utils/TimeSeriesUtils");
17
+ const ModelUtils_1 = require("../utils/ModelUtils");
18
+ const HydrateUtils_1 = require("../utils/HydrateUtils");
19
+ const FindUtils_1 = require("../utils/FindUtils");
16
20
  class RWSModel {
17
21
  static services = {};
18
22
  id;
@@ -83,13 +87,13 @@ class RWSModel {
83
87
  return this;
84
88
  }
85
89
  async hasRelation(key) {
86
- return utils_1.RelationUtils.hasRelation(this.constructor, key);
90
+ return RelationUtils_1.RelationUtils.hasRelation(this.constructor, key);
87
91
  }
88
92
  async getRelationKey(key) {
89
- return utils_1.RelationUtils.getRelationKey(this.constructor, key);
93
+ return RelationUtils_1.RelationUtils.getRelationKey(this.constructor, key);
90
94
  }
91
95
  bindRelation(key, relatedModel) {
92
- return utils_1.RelationUtils.bindRelation(relatedModel);
96
+ return RelationUtils_1.RelationUtils.bindRelation(relatedModel);
93
97
  }
94
98
  async _asyncFill(data, fullDataMode = false, allowRelations = true, postLoadExecute = true) {
95
99
  const collections_to_models = {};
@@ -104,36 +108,37 @@ class RWSModel {
104
108
  });
105
109
  const seriesHydrationfields = [];
106
110
  if (allowRelations) {
107
- await utils_1.HydrateUtils.hydrateRelations(this, relManyData, relOneData, seriesHydrationfields, fullDataMode, data);
111
+ await HydrateUtils_1.HydrateUtils.hydrateRelations(this, relManyData, relOneData, seriesHydrationfields, fullDataMode, data);
108
112
  }
109
113
  // Process regular fields and time series
110
- await utils_1.HydrateUtils.hydrateDataFields(this, collections_to_models, relOneData, seriesHydrationfields, fullDataMode, data);
114
+ await HydrateUtils_1.HydrateUtils.hydrateDataFields(this, collections_to_models, relOneData, seriesHydrationfields, fullDataMode, data);
111
115
  if (!this.isPostLoadExecuted() && postLoadExecute) {
112
116
  await this.postLoad();
117
+ this.setPostLoadExecuted();
113
118
  }
114
119
  return this;
115
120
  }
116
121
  getModelScalarFields(model) {
117
- return utils_1.ModelUtils.getModelScalarFields(model);
122
+ return ModelUtils_1.ModelUtils.getModelScalarFields(model);
118
123
  }
119
124
  async getRelationOneMeta(classFields) {
120
- return utils_1.RelationUtils.getRelationOneMeta(this, classFields);
125
+ return RelationUtils_1.RelationUtils.getRelationOneMeta(this, classFields);
121
126
  }
122
127
  static async getRelationOneMeta(model, classFields) {
123
- return utils_1.RelationUtils.getRelationOneMeta(model, classFields);
128
+ return RelationUtils_1.RelationUtils.getRelationOneMeta(model, classFields);
124
129
  }
125
130
  async getRelationManyMeta(classFields) {
126
- return utils_1.RelationUtils.getRelationManyMeta(this, classFields);
131
+ return RelationUtils_1.RelationUtils.getRelationManyMeta(this, classFields);
127
132
  }
128
133
  static async getRelationManyMeta(model, classFields) {
129
- return utils_1.RelationUtils.getRelationManyMeta(model, classFields);
134
+ return RelationUtils_1.RelationUtils.getRelationManyMeta(model, classFields);
130
135
  }
131
136
  static async paginate(paginateParams, findParams) {
132
- return await utils_1.FindUtils.paginate(this, paginateParams, findParams);
137
+ return await FindUtils_1.FindUtils.paginate(this, paginateParams, findParams);
133
138
  }
134
139
  async toMongo() {
135
140
  const data = {};
136
- const timeSeriesIds = utils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
141
+ const timeSeriesIds = TimeSeriesUtils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
137
142
  const timeSeriesHydrationFields = [];
138
143
  for (const key in this) {
139
144
  if (await this.hasRelation(key)) {
@@ -175,10 +180,10 @@ class RWSModel {
175
180
  async save() {
176
181
  const data = await this.toMongo();
177
182
  let updatedModelData = data;
178
- const entryExists = await utils_1.ModelUtils.entryExists(this);
183
+ const entryExists = await ModelUtils_1.ModelUtils.entryExists(this);
179
184
  if (entryExists) {
180
185
  await this.preUpdate();
181
- const pk = utils_1.ModelUtils.findPrimaryKeyFields(this.constructor);
186
+ const pk = ModelUtils_1.ModelUtils.findPrimaryKeyFields(this.constructor);
182
187
  updatedModelData = await this.dbService.update(data, this.getCollection(), pk);
183
188
  await this._asyncFill(updatedModelData);
184
189
  await this.postUpdate();
@@ -193,13 +198,12 @@ class RWSModel {
193
198
  return this;
194
199
  }
195
200
  static async getModelAnnotations(constructor) {
196
- return utils_1.ModelUtils.getModelAnnotations(constructor);
201
+ return ModelUtils_1.ModelUtils.getModelAnnotations(constructor);
197
202
  }
198
203
  async preUpdate() {
199
204
  return;
200
205
  }
201
206
  async postLoad() {
202
- this.setPostLoadExecuted();
203
207
  return;
204
208
  }
205
209
  async postUpdate() {
@@ -212,19 +216,19 @@ class RWSModel {
212
216
  return;
213
217
  }
214
218
  static isSubclass(constructor, baseClass) {
215
- return utils_1.ModelUtils.isSubclass(constructor, baseClass);
219
+ return ModelUtils_1.ModelUtils.isSubclass(constructor, baseClass);
216
220
  }
217
221
  hasTimeSeries() {
218
- return utils_1.TimeSeriesUtils.checkTimeSeries(this.constructor);
222
+ return TimeSeriesUtils_1.TimeSeriesUtils.checkTimeSeries(this.constructor);
219
223
  }
220
224
  static checkTimeSeries(constructor) {
221
- return utils_1.TimeSeriesUtils.checkTimeSeries(constructor);
225
+ return TimeSeriesUtils_1.TimeSeriesUtils.checkTimeSeries(constructor);
222
226
  }
223
227
  async isDbVariable(variable) {
224
- return utils_1.ModelUtils.checkDbVariable(this.constructor, variable);
228
+ return ModelUtils_1.ModelUtils.checkDbVariable(this.constructor, variable);
225
229
  }
226
230
  static async checkDbVariable(constructor, variable) {
227
- return utils_1.ModelUtils.checkDbVariable(constructor, variable);
231
+ return ModelUtils_1.ModelUtils.checkDbVariable(constructor, variable);
228
232
  }
229
233
  sanitizeDBData(data) {
230
234
  const dataKeys = Object.keys(data);
@@ -242,13 +246,13 @@ class RWSModel {
242
246
  return await this.services.dbService.watchCollection(collection, preRun);
243
247
  }
244
248
  static async findOneBy(findParams) {
245
- return await utils_1.FindUtils.findOneBy(this, findParams);
249
+ return await FindUtils_1.FindUtils.findOneBy(this, findParams);
246
250
  }
247
251
  static async find(id, findParams = null) {
248
- return await utils_1.FindUtils.find(this, id, findParams);
252
+ return await FindUtils_1.FindUtils.find(this, id, findParams);
249
253
  }
250
254
  static async findBy(findParams) {
251
- return await utils_1.FindUtils.findBy(this, findParams);
255
+ return await FindUtils_1.FindUtils.findBy(this, findParams);
252
256
  }
253
257
  static async delete(conditions) {
254
258
  const collection = Reflect.get(this, '_collection');
@@ -275,7 +279,7 @@ class RWSModel {
275
279
  return RWSModel.loadModels();
276
280
  }
277
281
  checkRelDisabled(key) {
278
- return utils_1.RelationUtils.checkRelDisabled(this, key);
282
+ return RelationUtils_1.RelationUtils.checkRelDisabled(this, key);
279
283
  }
280
284
  static setServices(services) {
281
285
  this.allModels = services.configService.get('db_models');
@@ -290,8 +294,8 @@ class RWSModel {
290
294
  static getDb() {
291
295
  return this.services.dbService;
292
296
  }
293
- async reload(inPostLoad = false) {
294
- const pk = utils_1.ModelUtils.findPrimaryKeyFields(this.constructor);
297
+ async reload() {
298
+ const pk = ModelUtils_1.ModelUtils.findPrimaryKeyFields(this.constructor);
295
299
  const where = {};
296
300
  if (Array.isArray(pk)) {
297
301
  for (const pkElem of pk) {
@@ -301,7 +305,7 @@ class RWSModel {
301
305
  else {
302
306
  where[pk] = this[pk];
303
307
  }
304
- return await utils_1.FindUtils.findOneBy(this.constructor, { conditions: where, cancelPostLoad: inPostLoad });
308
+ return await FindUtils_1.FindUtils.findOneBy(this.constructor, { conditions: where });
305
309
  }
306
310
  }
307
311
  exports.RWSModel = RWSModel;
@@ -9,7 +9,7 @@ export interface IModel {
9
9
  save: () => Promise<this>;
10
10
  getDb: () => DBService;
11
11
  getCollection: () => string | null;
12
- reload: (inPostLoad: boolean) => Promise<RWSModel<any>>;
12
+ reload: () => Promise<RWSModel<any>>;
13
13
  delete: () => Promise<void>;
14
14
  hasTimeSeries: () => boolean;
15
15
  _asyncFill: (data: any, fullDataMode?: boolean, allowRelations?: boolean) => Promise<any>;
@@ -1,149 +1,88 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.FindUtils = void 0;
7
- const LoadingContext_1 = require("./LoadingContext");
8
- const chalk_1 = __importDefault(require("chalk"));
9
- function circularReferenceWarning(modelType, id) {
10
- console.warn(chalk_1.default.yellow(`Circular reference detected: ${modelType}:${id} is already being loaded. Breaking cycle.`));
11
- }
12
4
  class FindUtils {
13
5
  static async findOneBy(opModel, findParams) {
14
- // Wrap in new execution context to ensure clean loading stack per operation
15
- return LoadingContext_1.LoadingContext.withNewExecutionContext(async () => {
16
- const conditions = findParams?.conditions ?? {};
17
- const ordering = findParams?.ordering ?? null;
18
- const fields = findParams?.fields ?? null;
19
- const allowRelations = findParams?.allowRelations ?? true;
20
- const fullData = findParams?.fullData ?? false;
21
- opModel.checkForInclusionWithThrow('');
22
- const collection = Reflect.get(opModel, '_collection');
23
- const dbData = await opModel.services.dbService.findOneBy(collection, conditions, fields, ordering);
24
- if (dbData) {
25
- const modelType = opModel.name;
26
- const id = dbData.id;
27
- // Check if this model is already being loaded to prevent circular references
28
- if (LoadingContext_1.LoadingContext.isLoading(modelType, id)) {
29
- circularReferenceWarning(modelType, id);
30
- return null;
31
- }
32
- return await LoadingContext_1.LoadingContext.withLoadingContext(modelType, id, async () => {
33
- const inst = new opModel();
34
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
35
- return loaded;
36
- });
37
- }
38
- return null;
39
- });
6
+ const conditions = findParams?.conditions ?? {};
7
+ const ordering = findParams?.ordering ?? null;
8
+ const fields = findParams?.fields ?? null;
9
+ const allowRelations = findParams?.allowRelations ?? true;
10
+ const fullData = findParams?.fullData ?? false;
11
+ opModel.checkForInclusionWithThrow('');
12
+ const collection = Reflect.get(opModel, '_collection');
13
+ const dbData = await opModel.services.dbService.findOneBy(collection, conditions, fields, ordering);
14
+ if (dbData) {
15
+ const inst = new opModel();
16
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
17
+ return loaded;
18
+ }
19
+ return null;
40
20
  }
41
21
  static async find(opModel, id, findParams = null) {
42
- // Wrap in new execution context to ensure clean loading stack per operation
43
- return LoadingContext_1.LoadingContext.withNewExecutionContext(async () => {
44
- const ordering = findParams?.ordering ?? null;
45
- const fields = findParams?.fields ?? null;
46
- const allowRelations = findParams?.allowRelations ?? true;
47
- const fullData = findParams?.fullData ?? false;
48
- const collection = Reflect.get(opModel, '_collection');
49
- opModel.checkForInclusionWithThrow(opModel.name);
50
- const dbData = await opModel.services.dbService.findOneBy(collection, { id }, fields, ordering);
51
- if (dbData) {
52
- const modelType = opModel.name;
53
- // Check if this model is already being loaded to prevent circular references
54
- if (LoadingContext_1.LoadingContext.isLoading(modelType, id)) {
55
- circularReferenceWarning(modelType, id);
56
- return null;
57
- }
58
- return await LoadingContext_1.LoadingContext.withLoadingContext(modelType, id, async () => {
59
- const inst = new opModel();
60
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
61
- return loaded;
62
- });
63
- }
64
- return null;
65
- });
22
+ const ordering = findParams?.ordering ?? null;
23
+ const fields = findParams?.fields ?? null;
24
+ const allowRelations = findParams?.allowRelations ?? true;
25
+ const fullData = findParams?.fullData ?? false;
26
+ const collection = Reflect.get(opModel, '_collection');
27
+ opModel.checkForInclusionWithThrow(opModel.name);
28
+ const dbData = await opModel.services.dbService.findOneBy(collection, { id }, fields, ordering);
29
+ if (dbData) {
30
+ const inst = new opModel();
31
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
32
+ return loaded;
33
+ }
34
+ return null;
66
35
  }
67
36
  static async findBy(opModel, findParams) {
68
- // Wrap in new execution context to ensure clean loading stack per operation
69
- return LoadingContext_1.LoadingContext.withNewExecutionContext(async () => {
70
- const conditions = findParams?.conditions ?? {};
71
- const ordering = findParams?.ordering ?? null;
72
- const fields = findParams?.fields ?? null;
73
- const allowRelations = findParams?.allowRelations ?? true;
74
- const fullData = findParams?.fullData ?? false;
75
- const collection = Reflect.get(opModel, '_collection');
76
- opModel.checkForInclusionWithThrow(opModel.name);
77
- try {
78
- const paginateParams = findParams?.pagination ? findParams?.pagination : undefined;
79
- const dbData = await opModel.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
80
- if (dbData.length) {
81
- const instanced = [];
82
- for (const data of dbData) {
83
- const modelType = opModel.name;
84
- const id = data.id;
85
- // Check if this model is already being loaded to prevent circular references
86
- if (LoadingContext_1.LoadingContext.isLoading(modelType, id)) {
87
- circularReferenceWarning(modelType, id);
88
- continue;
89
- }
90
- const loaded = await LoadingContext_1.LoadingContext.withLoadingContext(modelType, id, async () => {
91
- const inst = new opModel();
92
- return await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
93
- });
94
- if (loaded) {
95
- instanced.push(loaded);
96
- }
97
- }
98
- return instanced;
37
+ const conditions = findParams?.conditions ?? {};
38
+ const ordering = findParams?.ordering ?? null;
39
+ const fields = findParams?.fields ?? null;
40
+ const allowRelations = findParams?.allowRelations ?? true;
41
+ const fullData = findParams?.fullData ?? false;
42
+ const collection = Reflect.get(opModel, '_collection');
43
+ opModel.checkForInclusionWithThrow(opModel.name);
44
+ try {
45
+ const paginateParams = findParams?.pagination ? findParams?.pagination : undefined;
46
+ const dbData = await opModel.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
47
+ if (dbData.length) {
48
+ const instanced = [];
49
+ for (const data of dbData) {
50
+ const inst = new opModel();
51
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)));
99
52
  }
100
- return [];
101
- }
102
- catch (rwsError) {
103
- console.error(rwsError);
104
- throw rwsError;
53
+ return instanced;
105
54
  }
106
- });
55
+ return [];
56
+ }
57
+ catch (rwsError) {
58
+ console.error(rwsError);
59
+ throw rwsError;
60
+ }
107
61
  }
108
62
  static async paginate(opModel, paginateParams, findParams) {
109
- // Wrap in new execution context to ensure clean loading stack per operation
110
- return LoadingContext_1.LoadingContext.withNewExecutionContext(async () => {
111
- const conditions = findParams?.conditions ?? {};
112
- const ordering = findParams?.ordering ?? null;
113
- const fields = findParams?.fields ?? null;
114
- const allowRelations = findParams?.allowRelations ?? true;
115
- const fullData = findParams?.fullData ?? false;
116
- const collection = Reflect.get(opModel, '_collection');
117
- opModel.checkForInclusionWithThrow(opModel.name);
118
- try {
119
- const dbData = await opModel.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
120
- if (dbData.length) {
121
- const instanced = [];
122
- for (const data of dbData) {
123
- const modelType = opModel.name;
124
- const id = data.id;
125
- // Check if this model is already being loaded to prevent circular references
126
- if (LoadingContext_1.LoadingContext.isLoading(modelType, id)) {
127
- circularReferenceWarning(modelType, id);
128
- continue;
129
- }
130
- const loaded = await LoadingContext_1.LoadingContext.withLoadingContext(modelType, id, async () => {
131
- const inst = new opModel();
132
- return await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
133
- });
134
- if (loaded) {
135
- instanced.push(loaded);
136
- }
137
- }
138
- return instanced;
63
+ const conditions = findParams?.conditions ?? {};
64
+ const ordering = findParams?.ordering ?? null;
65
+ const fields = findParams?.fields ?? null;
66
+ const allowRelations = findParams?.allowRelations ?? true;
67
+ const fullData = findParams?.fullData ?? false;
68
+ const collection = Reflect.get(opModel, '_collection');
69
+ opModel.checkForInclusionWithThrow(opModel.name);
70
+ try {
71
+ const dbData = await opModel.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
72
+ if (dbData.length) {
73
+ const instanced = [];
74
+ for (const data of dbData) {
75
+ const inst = new opModel();
76
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)));
139
77
  }
140
- return [];
141
- }
142
- catch (rwsError) {
143
- console.error(rwsError);
144
- throw rwsError;
78
+ return instanced;
145
79
  }
146
- });
80
+ return [];
81
+ }
82
+ catch (rwsError) {
83
+ console.error(rwsError);
84
+ throw rwsError;
85
+ }
147
86
  }
148
87
  }
149
88
  exports.FindUtils = FindUtils;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "3.9.0",
4
+ "version": "3.9.3",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -4,16 +4,15 @@ import { OpModelType } from '../interfaces/OpModelType';
4
4
  import { TrackType } from '../../decorators';
5
5
  import { FieldsHelper } from '../../helper/FieldsHelper';
6
6
  import { FindByType, IPaginationParams } from '../../types/FindParams';
7
+ import { RelationUtils } from '../utils/RelationUtils';
8
+
9
+ import { TimeSeriesUtils } from '../utils/TimeSeriesUtils';
10
+ import { ModelUtils } from '../utils/ModelUtils';
11
+ // import timeSeriesModel from './TimeSeriesModel';
7
12
  import { DBService } from '../../services/DBService';
8
13
  import { ISuperTagData } from '../../decorators/RWSCollection';
9
- import {
10
- RelationUtils,
11
- TimeSeriesUtils,
12
- ModelUtils,
13
- HydrateUtils,
14
- FindUtils,
15
- LoadingContext
16
- } from '../utils';
14
+ import { HydrateUtils } from '../utils/HydrateUtils';
15
+ import { FindUtils } from '../utils/FindUtils';
17
16
 
18
17
  class RWSModel<T> implements IModel {
19
18
  static services: IRWSModelServices = {};
@@ -138,9 +137,9 @@ class RWSModel<T> implements IModel {
138
137
  // Process regular fields and time series
139
138
  await HydrateUtils.hydrateDataFields(this, collections_to_models, relOneData, seriesHydrationfields, fullDataMode, data);
140
139
 
141
- if(!this.isPostLoadExecuted() && postLoadExecute){
142
-
143
- await this.postLoad();
140
+ if(!this.isPostLoadExecuted() && postLoadExecute){
141
+ await this.postLoad();
142
+ this.setPostLoadExecuted();
144
143
  }
145
144
 
146
145
  return this as any as T;
@@ -267,8 +266,7 @@ class RWSModel<T> implements IModel {
267
266
  return;
268
267
  }
269
268
 
270
- public async postLoad(): Promise<void> {
271
- this.setPostLoadExecuted();
269
+ public async postLoad(): Promise<void> {
272
270
  return;
273
271
  }
274
272
 
@@ -406,7 +404,7 @@ class RWSModel<T> implements IModel {
406
404
  return this.services.dbService;
407
405
  }
408
406
 
409
- public async reload(inPostLoad = false): Promise<RWSModel<T> | null>
407
+ public async reload(): Promise<RWSModel<T> | null>
410
408
  {
411
409
  const pk = ModelUtils.findPrimaryKeyFields(this.constructor as OpModelType<T>);
412
410
  const where: any = {};
@@ -419,7 +417,7 @@ class RWSModel<T> implements IModel {
419
417
  where[pk as string] = this[pk as string]
420
418
  }
421
419
 
422
- return await FindUtils.findOneBy(this.constructor as OpModelType<any>, { conditions: where, cancelPostLoad: inPostLoad });
420
+ return await FindUtils.findOneBy(this.constructor as OpModelType<any>, { conditions: where });
423
421
  }
424
422
  }
425
423
 
@@ -11,7 +11,7 @@ export interface IModel {
11
11
  save: () => Promise<this>;
12
12
  getDb: () => DBService;
13
13
  getCollection: () => string | null;
14
- reload: (inPostLoad: boolean) => Promise<RWSModel<any>>;
14
+ reload: () => Promise<RWSModel<any>>;
15
15
  delete: () => Promise<void>;
16
16
  hasTimeSeries: () => boolean;
17
17