@rws-framework/db 2.1.11 → 2.2.1

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.
Files changed (71) hide show
  1. package/dist/decorators/InverseRelation.d.ts +0 -0
  2. package/dist/decorators/InverseRelation.js +0 -0
  3. package/dist/decorators/InverseTimeSeries.d.ts +0 -0
  4. package/dist/decorators/InverseTimeSeries.js +0 -0
  5. package/dist/decorators/RWSCollection.d.ts +0 -0
  6. package/dist/decorators/RWSCollection.js +0 -0
  7. package/dist/decorators/Relation.d.ts +0 -0
  8. package/dist/decorators/Relation.js +0 -0
  9. package/dist/decorators/TrackType.d.ts +1 -1
  10. package/dist/decorators/TrackType.js +0 -0
  11. package/dist/decorators/index.d.ts +0 -0
  12. package/dist/decorators/index.js +0 -0
  13. package/dist/helper/DbHelper.d.ts +0 -0
  14. package/dist/helper/DbHelper.js +0 -0
  15. package/dist/helper/FieldsHelper.d.ts +0 -0
  16. package/dist/helper/FieldsHelper.js +0 -0
  17. package/dist/index.d.ts +0 -0
  18. package/dist/index.js +0 -0
  19. package/dist/models/TimeSeriesModel.d.ts +0 -0
  20. package/dist/models/TimeSeriesModel.js +0 -0
  21. package/dist/models/_model.d.ts +5 -116
  22. package/dist/models/_model.js +10 -42
  23. package/dist/models/core/RWSModel.d.ts +65 -0
  24. package/dist/models/core/RWSModel.js +375 -0
  25. package/dist/models/core/TimeSeriesModel.d.ts +7 -0
  26. package/dist/models/core/TimeSeriesModel.js +34 -0
  27. package/dist/models/index.d.ts +8 -0
  28. package/dist/models/index.js +12 -0
  29. package/dist/models/interfaces/IModel.d.ts +10 -0
  30. package/dist/models/interfaces/IModel.js +2 -0
  31. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -0
  32. package/dist/models/interfaces/IRWSModelServices.js +2 -0
  33. package/dist/models/interfaces/OpModelType.d.ts +32 -0
  34. package/dist/models/interfaces/OpModelType.js +2 -0
  35. package/dist/models/types/RelationTypes.d.ts +24 -0
  36. package/dist/models/types/RelationTypes.js +2 -0
  37. package/dist/models/utils/ModelUtils.d.ts +10 -0
  38. package/dist/models/utils/ModelUtils.js +56 -0
  39. package/dist/models/utils/PaginationUtils.d.ts +5 -0
  40. package/dist/models/utils/PaginationUtils.js +32 -0
  41. package/dist/models/utils/RelationUtils.d.ts +14 -0
  42. package/dist/models/utils/RelationUtils.js +65 -0
  43. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -0
  44. package/dist/models/utils/TimeSeriesUtils.js +35 -0
  45. package/dist/services/DBService.d.ts +3 -2
  46. package/dist/services/DBService.js +5 -1
  47. package/dist/types/DbConfigHandler.d.ts +1 -1
  48. package/dist/types/DbConfigHandler.js +0 -0
  49. package/dist/types/FindParams.d.ts +5 -0
  50. package/dist/types/FindParams.js +0 -0
  51. package/dist/types/IRWSModel.d.ts +0 -0
  52. package/dist/types/IRWSModel.js +0 -0
  53. package/dist/types/ITimeSeries.d.ts +0 -0
  54. package/dist/types/ITimeSeries.js +0 -0
  55. package/package.json +1 -1
  56. package/src/decorators/TrackType.ts +2 -2
  57. package/src/models/_model.ts +29 -24
  58. package/src/models/core/RWSModel.ts +482 -0
  59. package/src/models/core/TimeSeriesModel.ts +19 -0
  60. package/src/models/index.ts +20 -0
  61. package/src/models/interfaces/IModel.ts +11 -0
  62. package/src/models/interfaces/IRWSModelServices.ts +7 -0
  63. package/src/models/interfaces/OpModelType.ts +49 -0
  64. package/src/models/types/RelationTypes.ts +25 -0
  65. package/src/models/utils/ModelUtils.ts +65 -0
  66. package/src/models/utils/PaginationUtils.ts +42 -0
  67. package/src/models/utils/RelationUtils.ts +76 -0
  68. package/src/models/utils/TimeSeriesUtils.ts +38 -0
  69. package/src/services/DBService.ts +14 -3
  70. package/src/types/DbConfigHandler.ts +2 -2
  71. package/src/types/FindParams.ts +10 -4
@@ -0,0 +1,375 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RWSModel = void 0;
16
+ const decorators_1 = require("../../decorators");
17
+ const FieldsHelper_1 = require("../../helper/FieldsHelper");
18
+ const RelationUtils_1 = require("../utils/RelationUtils");
19
+ const PaginationUtils_1 = require("../utils/PaginationUtils");
20
+ const TimeSeriesUtils_1 = require("../utils/TimeSeriesUtils");
21
+ const ModelUtils_1 = require("../utils/ModelUtils");
22
+ const TimeSeriesModel_1 = __importDefault(require("./TimeSeriesModel"));
23
+ class RWSModel {
24
+ constructor(data) {
25
+ if (!this.getCollection()) {
26
+ throw new Error('Model must have a collection defined');
27
+ }
28
+ this.dbService = RWSModel.services.dbService;
29
+ this.configService = RWSModel.services.configService;
30
+ if (!data) {
31
+ return;
32
+ }
33
+ if (!this.hasTimeSeries()) {
34
+ this._fill(data);
35
+ }
36
+ else {
37
+ throw new Error('Time Series not supported in synchronous constructor. Use `await Model.create(data)` static method to instantiate this model.');
38
+ }
39
+ }
40
+ checkForInclusionWithThrow() {
41
+ this.checkForInclusionWithThrow();
42
+ }
43
+ static checkForInclusionWithThrow(checkModelType) {
44
+ if (!this.checkForInclusion(this.name)) {
45
+ throw new Error('Model undefined: ' + this.name);
46
+ }
47
+ }
48
+ checkForInclusion() {
49
+ return this.checkForInclusion();
50
+ }
51
+ static checkForInclusion(checkModelType) {
52
+ return this.loadModels().find((definedModel) => {
53
+ return definedModel.name === checkModelType;
54
+ }) !== undefined;
55
+ }
56
+ _fill(data) {
57
+ for (const key in data) {
58
+ if (data.hasOwnProperty(key)) {
59
+ const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, this.constructor.prototype);
60
+ if (meta) {
61
+ data[key] = {
62
+ create: data[key]
63
+ };
64
+ }
65
+ else {
66
+ this[key] = data[key];
67
+ }
68
+ }
69
+ }
70
+ return this;
71
+ }
72
+ hasRelation(key) {
73
+ return RelationUtils_1.RelationUtils.hasRelation(this, key);
74
+ }
75
+ bindRelation(key, relatedModel) {
76
+ return RelationUtils_1.RelationUtils.bindRelation(relatedModel);
77
+ }
78
+ async _asyncFill(data, fullDataMode = false, allowRelations = true) {
79
+ const collections_to_models = {};
80
+ const timeSeriesIds = TimeSeriesUtils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
81
+ const classFields = FieldsHelper_1.FieldsHelper.getAllClassFields(this.constructor);
82
+ // Get both relation metadata types asynchronously
83
+ const [relOneData, relManyData] = await Promise.all([
84
+ this.getRelationOneMeta(classFields),
85
+ this.getRelationManyMeta(classFields)
86
+ ]);
87
+ this.loadModels().forEach((model) => {
88
+ collections_to_models[model.getCollection()] = model;
89
+ });
90
+ const seriesHydrationfields = [];
91
+ if (allowRelations) {
92
+ // Handle many-to-many relations
93
+ for (const key in relManyData) {
94
+ if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
95
+ continue;
96
+ }
97
+ const relMeta = relManyData[key];
98
+ const relationEnabled = RelationUtils_1.RelationUtils.checkRelEnabled(this, relMeta.key);
99
+ if (relationEnabled) {
100
+ this[relMeta.key] = await relMeta.inversionModel.findBy({
101
+ conditions: {
102
+ [relMeta.foreignKey]: data.id
103
+ },
104
+ allowRelations: false
105
+ });
106
+ }
107
+ }
108
+ // Handle one-to-one relations
109
+ for (const key in relOneData) {
110
+ if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
111
+ continue;
112
+ }
113
+ const relMeta = relOneData[key];
114
+ const relationEnabled = RelationUtils_1.RelationUtils.checkRelEnabled(this, relMeta.key);
115
+ if (!data[relMeta.hydrationField] && relMeta.required) {
116
+ throw new Error(`Relation field "${relMeta.hydrationField}" is required in model ${this.constructor.name}.`);
117
+ }
118
+ if (relationEnabled && data[relMeta.hydrationField]) {
119
+ this[relMeta.key] = await relMeta.model.find(data[relMeta.hydrationField], { allowRelations: false });
120
+ }
121
+ else if (relationEnabled && !data[relMeta.hydrationField] && data[relMeta.key]) {
122
+ const newRelModel = await relMeta.model.create(data[relMeta.key]);
123
+ this[relMeta.key] = await newRelModel.save();
124
+ }
125
+ const cutKeys = this.constructor._CUT_KEYS;
126
+ if (!cutKeys.includes(relMeta.hydrationField)) {
127
+ cutKeys.push(relMeta.hydrationField);
128
+ }
129
+ }
130
+ }
131
+ // Process regular fields and time series
132
+ for (const key in data) {
133
+ if (data.hasOwnProperty(key)) {
134
+ if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
135
+ continue;
136
+ }
137
+ if (Object.keys(relOneData).includes(key)) {
138
+ continue;
139
+ }
140
+ if (seriesHydrationfields.includes(key)) {
141
+ continue;
142
+ }
143
+ const timeSeriesMetaData = timeSeriesIds[key];
144
+ if (timeSeriesMetaData) {
145
+ this[key] = data[key];
146
+ const seriesModel = collections_to_models[timeSeriesMetaData.collection];
147
+ const dataModels = await seriesModel.findBy({
148
+ id: { in: data[key] }
149
+ });
150
+ seriesHydrationfields.push(timeSeriesMetaData.hydrationField);
151
+ this[timeSeriesMetaData.hydrationField] = dataModels;
152
+ }
153
+ else {
154
+ this[key] = data[key];
155
+ }
156
+ }
157
+ }
158
+ return this;
159
+ }
160
+ getModelScalarFields(model) {
161
+ return ModelUtils_1.ModelUtils.getModelScalarFields(model);
162
+ }
163
+ async getRelationOneMeta(classFields) {
164
+ return RelationUtils_1.RelationUtils.getRelationOneMeta(this, classFields);
165
+ }
166
+ static async getRelationOneMeta(model, classFields) {
167
+ return RelationUtils_1.RelationUtils.getRelationOneMeta(model, classFields);
168
+ }
169
+ async getRelationManyMeta(classFields) {
170
+ return RelationUtils_1.RelationUtils.getRelationManyMeta(this, classFields);
171
+ }
172
+ static async getRelationManyMeta(model, classFields) {
173
+ return RelationUtils_1.RelationUtils.getRelationManyMeta(model, classFields);
174
+ }
175
+ static paginate(pageParams, findParams) {
176
+ return PaginationUtils_1.PaginationUtils.paginate.bind(this)(pageParams, findParams);
177
+ }
178
+ async toMongo() {
179
+ const data = {};
180
+ const timeSeriesIds = TimeSeriesUtils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
181
+ const timeSeriesHydrationFields = [];
182
+ for (const key in this) {
183
+ if (this.hasRelation(key)) {
184
+ data[key] = this.bindRelation(key, this[key]);
185
+ continue;
186
+ }
187
+ if (!(await this.isDbVariable(key))) {
188
+ continue;
189
+ }
190
+ const passedFieldCondition = this.hasOwnProperty(key) &&
191
+ !(this.constructor._BANNED_KEYS
192
+ || RWSModel._BANNED_KEYS).includes(key) &&
193
+ !timeSeriesHydrationFields.includes(key);
194
+ if (passedFieldCondition) {
195
+ data[key] = this[key];
196
+ }
197
+ if (timeSeriesIds[key]) {
198
+ data[key] = this[key];
199
+ timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
200
+ }
201
+ }
202
+ return data;
203
+ }
204
+ getCollection() {
205
+ return this.constructor._collection || this._collection;
206
+ }
207
+ static getCollection() {
208
+ return this.constructor._collection || this._collection;
209
+ }
210
+ async save() {
211
+ const data = await this.toMongo();
212
+ let updatedModelData = data;
213
+ if (this.id) {
214
+ this.preUpdate();
215
+ updatedModelData = await this.dbService.update(data, this.getCollection());
216
+ await this._asyncFill(updatedModelData);
217
+ this.postUpdate();
218
+ }
219
+ else {
220
+ this.preCreate();
221
+ const isTimeSeries = this instanceof TimeSeriesModel_1.default;
222
+ updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
223
+ await this._asyncFill(updatedModelData);
224
+ this.postCreate();
225
+ }
226
+ return this;
227
+ }
228
+ static async getModelAnnotations(constructor) {
229
+ return ModelUtils_1.ModelUtils.getModelAnnotations(constructor);
230
+ }
231
+ preUpdate() {
232
+ return;
233
+ }
234
+ postUpdate() {
235
+ return;
236
+ }
237
+ preCreate() {
238
+ return;
239
+ }
240
+ postCreate() {
241
+ return;
242
+ }
243
+ static isSubclass(constructor, baseClass) {
244
+ return ModelUtils_1.ModelUtils.isSubclass(constructor, baseClass);
245
+ }
246
+ hasTimeSeries() {
247
+ return TimeSeriesUtils_1.TimeSeriesUtils.checkTimeSeries(this.constructor);
248
+ }
249
+ static checkTimeSeries(constructor) {
250
+ return TimeSeriesUtils_1.TimeSeriesUtils.checkTimeSeries(constructor);
251
+ }
252
+ async isDbVariable(variable) {
253
+ return ModelUtils_1.ModelUtils.checkDbVariable(this.constructor, variable);
254
+ }
255
+ static async checkDbVariable(constructor, variable) {
256
+ return ModelUtils_1.ModelUtils.checkDbVariable(constructor, variable);
257
+ }
258
+ sanitizeDBData(data) {
259
+ const dataKeys = Object.keys(data);
260
+ const sanitizedData = {};
261
+ for (const key of dataKeys) {
262
+ if (this.isDbVariable(key)) {
263
+ sanitizedData[key] = data[key];
264
+ }
265
+ }
266
+ return sanitizedData;
267
+ }
268
+ static async watchCollection(preRun) {
269
+ const collection = Reflect.get(this, '_collection');
270
+ this.checkForInclusionWithThrow(this.name);
271
+ return await this.services.dbService.watchCollection(collection, preRun);
272
+ }
273
+ static async findOneBy(findParams) {
274
+ var _a, _b, _c, _d, _e;
275
+ const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
276
+ const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
277
+ const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
278
+ const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
279
+ const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
280
+ this.checkForInclusionWithThrow('');
281
+ const collection = Reflect.get(this, '_collection');
282
+ const dbData = await this.services.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
283
+ if (dbData) {
284
+ const inst = new this();
285
+ return await inst._asyncFill(dbData, fullData, allowRelations);
286
+ }
287
+ return null;
288
+ }
289
+ static async find(id, findParams = null) {
290
+ var _a, _b, _c, _d;
291
+ const ordering = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _a !== void 0 ? _a : null;
292
+ const fields = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _b !== void 0 ? _b : null;
293
+ const allowRelations = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _c !== void 0 ? _c : true;
294
+ const fullData = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _d !== void 0 ? _d : false;
295
+ const collection = Reflect.get(this, '_collection');
296
+ this.checkForInclusionWithThrow(this.name);
297
+ const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
298
+ if (dbData) {
299
+ const inst = new this();
300
+ return await inst._asyncFill(dbData, fullData, allowRelations);
301
+ }
302
+ return null;
303
+ }
304
+ static async findBy(findParams) {
305
+ var _a, _b, _c, _d, _e;
306
+ const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
307
+ const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
308
+ const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
309
+ const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
310
+ const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
311
+ const collection = Reflect.get(this, '_collection');
312
+ this.checkForInclusionWithThrow(this.name);
313
+ try {
314
+ const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering);
315
+ if (dbData.length) {
316
+ const instanced = [];
317
+ for (const data of dbData) {
318
+ const inst = new this();
319
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations)));
320
+ }
321
+ return instanced;
322
+ }
323
+ return [];
324
+ }
325
+ catch (rwsError) {
326
+ console.error(rwsError);
327
+ throw rwsError;
328
+ }
329
+ }
330
+ static async delete(conditions) {
331
+ const collection = Reflect.get(this, '_collection');
332
+ this.checkForInclusionWithThrow(this.name);
333
+ return await this.services.dbService.delete(collection, conditions);
334
+ }
335
+ async delete() {
336
+ const collection = Reflect.get(this, '_collection');
337
+ this.checkForInclusionWithThrow();
338
+ return await this.dbService.delete(collection, {
339
+ id: this.id
340
+ });
341
+ }
342
+ static async create(data) {
343
+ const newModel = new this();
344
+ const sanitizedData = newModel.sanitizeDBData(data);
345
+ await newModel._asyncFill(sanitizedData);
346
+ return newModel;
347
+ }
348
+ static loadModels() {
349
+ return this.allModels || [];
350
+ }
351
+ loadModels() {
352
+ return RWSModel.loadModels();
353
+ }
354
+ checkRelEnabled(key) {
355
+ return RelationUtils_1.RelationUtils.checkRelEnabled(this, key);
356
+ }
357
+ static setServices(services) {
358
+ this.allModels = services.configService.get('db_models');
359
+ this.services = services;
360
+ }
361
+ getDb() {
362
+ return this.services.dbService;
363
+ }
364
+ }
365
+ exports.RWSModel = RWSModel;
366
+ RWSModel.services = {};
367
+ RWSModel._collection = null;
368
+ RWSModel._RELATIONS = {};
369
+ RWSModel._BANNED_KEYS = ['_collection'];
370
+ RWSModel.allModels = [];
371
+ RWSModel._CUT_KEYS = [];
372
+ __decorate([
373
+ (0, decorators_1.TrackType)(String),
374
+ __metadata("design:type", String)
375
+ ], RWSModel.prototype, "id", void 0);
@@ -0,0 +1,7 @@
1
+ import { RWSModel } from './RWSModel';
2
+ export default class TimeSeriesModel<ChildClass> extends RWSModel<TimeSeriesModel<ChildClass>> {
3
+ value: number;
4
+ timestamp: Date;
5
+ params: any;
6
+ constructor(data?: any);
7
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const RWSModel_1 = require("./RWSModel");
13
+ const decorators_1 = require("../../decorators");
14
+ class TimeSeriesModel extends RWSModel_1.RWSModel {
15
+ constructor(data) {
16
+ super(data);
17
+ if (!this.timestamp) {
18
+ this.timestamp = new Date();
19
+ }
20
+ }
21
+ }
22
+ exports.default = TimeSeriesModel;
23
+ __decorate([
24
+ (0, decorators_1.TrackType)(Number),
25
+ __metadata("design:type", Number)
26
+ ], TimeSeriesModel.prototype, "value", void 0);
27
+ __decorate([
28
+ (0, decorators_1.TrackType)(Date),
29
+ __metadata("design:type", Date)
30
+ ], TimeSeriesModel.prototype, "timestamp", void 0);
31
+ __decorate([
32
+ (0, decorators_1.TrackType)(Object),
33
+ __metadata("design:type", Object)
34
+ ], TimeSeriesModel.prototype, "params", void 0);
@@ -0,0 +1,8 @@
1
+ import { IModel } from './interfaces/IModel';
2
+ import { IRWSModelServices } from './interfaces/IRWSModelServices';
3
+ import { OpModelType } from './interfaces/OpModelType';
4
+ import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
5
+ import { RWSModel } from './core/RWSModel';
6
+ import TimeSeriesModel from './core/TimeSeriesModel';
7
+ import { TrackType, IMetaOpts } from '../decorators';
8
+ export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TimeSeriesModel, TrackType, IMetaOpts };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TrackType = exports.TimeSeriesModel = exports.RWSModel = void 0;
7
+ const RWSModel_1 = require("./core/RWSModel");
8
+ Object.defineProperty(exports, "RWSModel", { enumerable: true, get: function () { return RWSModel_1.RWSModel; } });
9
+ const TimeSeriesModel_1 = __importDefault(require("./core/TimeSeriesModel"));
10
+ exports.TimeSeriesModel = TimeSeriesModel_1.default;
11
+ const decorators_1 = require("../decorators");
12
+ Object.defineProperty(exports, "TrackType", { enumerable: true, get: function () { return decorators_1.TrackType; } });
@@ -0,0 +1,10 @@
1
+ import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
+ import { DBService } from '../../services/DBService';
3
+ export interface IModel {
4
+ [key: string]: any;
5
+ id: string | null;
6
+ save: () => void;
7
+ getCollection: () => string | null;
8
+ configService?: IDbConfigHandler;
9
+ dbService?: DBService;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
+ import { DBService } from '../../services/DBService';
3
+ export interface IRWSModelServices {
4
+ configService?: IDbConfigHandler;
5
+ dbService?: DBService;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,32 @@
1
+ import { IRWSModel } from '../../types/IRWSModel';
2
+ import { FindByType, IPaginationParams } from '../../types/FindParams';
3
+ import { IRWSModelServices } from './IRWSModelServices';
4
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
5
+ import { DBService } from '../../services/DBService';
6
+ type RWSModel<T> = any;
7
+ export interface OpModelType<ChildClass> {
8
+ new (data?: any | null): ChildClass;
9
+ services: IRWSModelServices;
10
+ name: string;
11
+ _collection: string;
12
+ _RELATIONS: {
13
+ [key: string]: boolean;
14
+ };
15
+ _CUT_KEYS: string[];
16
+ allModels: OpModelType<any>[];
17
+ loadModels: () => OpModelType<any>[];
18
+ checkForInclusionWithThrow: (className: string) => void;
19
+ checkForInclusion: (className: string) => boolean;
20
+ findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams: FindByType): Promise<T | null>;
21
+ find<T extends RWSModel<T>>(this: OpModelType<T>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
22
+ findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams: FindByType): Promise<T[]>;
23
+ paginate<T extends RWSModel<T>>(this: OpModelType<T>, paginateParams?: IPaginationParams, findParams?: FindByType): Promise<T[]>;
24
+ delete<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, conditions: any): Promise<void>;
25
+ create<T extends RWSModel<T>>(this: OpModelType<T>, data: T): Promise<T>;
26
+ getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
27
+ getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
28
+ getCollection(): string;
29
+ getDb(): DBService;
30
+ setServices(services: IRWSModelServices): void;
31
+ }
32
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ import { IRWSModel } from '../../types/IRWSModel';
2
+ type OpModelType<T> = any;
3
+ export type RelationBindType = {
4
+ connect: {
5
+ id: string;
6
+ };
7
+ };
8
+ export type RelOneMetaType<T extends IRWSModel> = {
9
+ [key: string]: {
10
+ required: boolean;
11
+ key?: string;
12
+ model: OpModelType<T>;
13
+ hydrationField: string;
14
+ foreignKey: string;
15
+ };
16
+ };
17
+ export type RelManyMetaType<T extends IRWSModel> = {
18
+ [key: string]: {
19
+ key: string;
20
+ inversionModel: OpModelType<T>;
21
+ foreignKey: string;
22
+ };
23
+ };
24
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { RWSModel } from "../core/RWSModel";
2
+ export declare class ModelUtils {
3
+ static getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {
4
+ annotationType: string;
5
+ metadata: any;
6
+ }>>;
7
+ static checkDbVariable(constructor: any, variable: string): Promise<boolean>;
8
+ static isSubclass<T, C extends new () => T>(constructor: C, baseClass: new () => T): boolean;
9
+ static getModelScalarFields(model: RWSModel<any>): string[];
10
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModelUtils = void 0;
4
+ const FieldsHelper_1 = require("../../helper/FieldsHelper");
5
+ class ModelUtils {
6
+ static async getModelAnnotations(constructor) {
7
+ const annotationsData = {};
8
+ const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
9
+ // Process all metadata keys and collect promises
10
+ const metadataPromises = metadataKeys.map(async (fullKey) => {
11
+ const [annotationType, propertyKey] = fullKey.split(':');
12
+ const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
13
+ if (metadata) {
14
+ // If this is a relation metadata with a promise
15
+ if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
16
+ const resolvedMetadata = await metadata.promise;
17
+ annotationsData[propertyKey] = {
18
+ annotationType,
19
+ metadata: resolvedMetadata
20
+ };
21
+ }
22
+ else {
23
+ // Handle non-relation metadata as before
24
+ const key = metadata.key || propertyKey;
25
+ annotationsData[key] = {
26
+ annotationType,
27
+ metadata
28
+ };
29
+ }
30
+ }
31
+ });
32
+ // Wait for all metadata to be processed
33
+ await Promise.all(metadataPromises);
34
+ return annotationsData;
35
+ }
36
+ static async checkDbVariable(constructor, variable) {
37
+ if (variable === 'id') {
38
+ return true;
39
+ }
40
+ const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
41
+ const dbProperties = Object.keys(dbAnnotations)
42
+ .map((key) => { return { ...dbAnnotations[key], key }; })
43
+ .filter((element) => element.annotationType === 'TrackType')
44
+ .map((element) => element.key);
45
+ return dbProperties.includes(variable);
46
+ }
47
+ static isSubclass(constructor, baseClass) {
48
+ return baseClass.prototype.isPrototypeOf(constructor.prototype);
49
+ }
50
+ static getModelScalarFields(model) {
51
+ return FieldsHelper_1.FieldsHelper.getAllClassFields(model)
52
+ .filter((item) => item.indexOf('TrackType') === 0)
53
+ .map((item) => item.split(':').at(-1));
54
+ }
55
+ }
56
+ exports.ModelUtils = ModelUtils;
@@ -0,0 +1,5 @@
1
+ import { OpModelType, RWSModel } from '../_model';
2
+ import { FindByType, IPaginationParams } from '../../types/FindParams';
3
+ export declare class PaginationUtils {
4
+ static paginate<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, paginationParams?: IPaginationParams, findParams?: FindByType): Promise<ChildClass[]>;
5
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaginationUtils = void 0;
4
+ class PaginationUtils {
5
+ static async paginate(paginationParams = { page: 0, per_page: 50 }, findParams = {}) {
6
+ var _a, _b, _c, _d, _e;
7
+ const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
8
+ const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
9
+ const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
10
+ const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
11
+ const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
12
+ const collection = Reflect.get(this, '_collection');
13
+ this.checkForInclusionWithThrow(this.name);
14
+ try {
15
+ const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginationParams);
16
+ if (dbData.length) {
17
+ const instanced = [];
18
+ for (const data of dbData) {
19
+ const inst = new this();
20
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations)));
21
+ }
22
+ return instanced;
23
+ }
24
+ return [];
25
+ }
26
+ catch (rwsError) {
27
+ console.error(rwsError);
28
+ throw rwsError;
29
+ }
30
+ }
31
+ }
32
+ exports.PaginationUtils = PaginationUtils;
@@ -0,0 +1,14 @@
1
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
+ import { IRWSModel } from '../../types/IRWSModel';
3
+ import { RWSModel } from '../_model';
4
+ export declare class RelationUtils {
5
+ static getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
6
+ static getRelationManyMeta(model: RWSModel<any>, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
7
+ static bindRelation(relatedModel: RWSModel<any>): {
8
+ connect: {
9
+ id: string;
10
+ };
11
+ };
12
+ static hasRelation(model: RWSModel<any>, key: string): boolean;
13
+ static checkRelEnabled(model: RWSModel<any>, key: string): boolean;
14
+ }