@rws-framework/db 2.0.3 → 2.0.4
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.
- package/dist/decorators/InverseRelation.d.ts +10 -0
- package/dist/decorators/InverseRelation.js +24 -0
- package/dist/decorators/InverseTimeSeries.d.ts +8 -0
- package/dist/decorators/InverseTimeSeries.js +13 -0
- package/dist/decorators/Relation.d.ts +12 -0
- package/dist/decorators/Relation.js +26 -0
- package/dist/decorators/TrackType.d.ts +17 -0
- package/dist/decorators/TrackType.js +33 -0
- package/dist/decorators/index.d.ts +5 -0
- package/dist/decorators/index.js +14 -0
- package/dist/helper/DbHelper.d.ts +8 -0
- package/dist/helper/DbHelper.js +113 -0
- package/dist/helper/FieldsHelper.d.ts +4 -0
- package/dist/helper/FieldsHelper.js +35 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +21 -0
- package/dist/models/TimeSeriesModel.d.ts +7 -0
- package/dist/models/TimeSeriesModel.js +33 -0
- package/dist/models/_model.d.ts +116 -0
- package/dist/models/_model.js +501 -0
- package/dist/services/DBService.d.ts +34 -0
- package/dist/services/DBService.js +186 -0
- package/dist/types/DbConfigHandler.d.ts +9 -0
- package/dist/types/DbConfigHandler.js +2 -0
- package/dist/types/FindParams.d.ts +9 -0
- package/dist/types/FindParams.js +2 -0
- package/dist/types/IRWSModel.d.ts +3 -0
- package/dist/types/IRWSModel.js +2 -0
- package/dist/types/ITimeSeries.d.ts +6 -0
- package/dist/types/ITimeSeries.js +2 -0
- package/package.json +7 -8
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.RWSModel = exports.TrackType = void 0;
|
|
46
|
+
const decorators_1 = require("../decorators");
|
|
47
|
+
Object.defineProperty(exports, "TrackType", { enumerable: true, get: function () { return decorators_1.TrackType; } });
|
|
48
|
+
const FieldsHelper_1 = require("../helper/FieldsHelper");
|
|
49
|
+
class RWSModel {
|
|
50
|
+
constructor(data) {
|
|
51
|
+
if (!this.getCollection()) {
|
|
52
|
+
throw new Error('Model must have a collection defined');
|
|
53
|
+
}
|
|
54
|
+
this.dbService = RWSModel.services.dbService;
|
|
55
|
+
this.configService = RWSModel.services.configService;
|
|
56
|
+
if (!data) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!this.hasTimeSeries()) {
|
|
60
|
+
this._fill(data);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
throw new Error('Time Series not supported in synchronous constructor. Use `await Model.create(data)` static method to instantiate this model.');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
checkForInclusionWithThrow() {
|
|
67
|
+
this.checkForInclusionWithThrow();
|
|
68
|
+
}
|
|
69
|
+
static checkForInclusionWithThrow(checkModelType) {
|
|
70
|
+
if (!this.checkForInclusion(this.name)) {
|
|
71
|
+
throw new Error('Model undefined: ' + this.name);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
checkForInclusion() {
|
|
75
|
+
return this.checkForInclusion();
|
|
76
|
+
}
|
|
77
|
+
static checkForInclusion(checkModelType) {
|
|
78
|
+
return this.loadModels().find((definedModel) => {
|
|
79
|
+
return definedModel.name === checkModelType;
|
|
80
|
+
}) !== undefined;
|
|
81
|
+
}
|
|
82
|
+
_fill(data) {
|
|
83
|
+
for (const key in data) {
|
|
84
|
+
if (data.hasOwnProperty(key)) {
|
|
85
|
+
const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, this.constructor.prototype);
|
|
86
|
+
if (meta) {
|
|
87
|
+
data[key] = {
|
|
88
|
+
create: data[key]
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this[key] = data[key];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
hasRelation(key) {
|
|
99
|
+
return !!this[key] && this[key] instanceof RWSModel;
|
|
100
|
+
}
|
|
101
|
+
bindRelation(key, relatedModel) {
|
|
102
|
+
return {
|
|
103
|
+
connect: {
|
|
104
|
+
id: relatedModel.id
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async _asyncFill(data, fullDataMode = false, allowRelations = true) {
|
|
109
|
+
const collections_to_models = {};
|
|
110
|
+
const timeSeriesIds = this.getTimeSeriesModelFields();
|
|
111
|
+
const classFields = FieldsHelper_1.FieldsHelper.getAllClassFields(this.constructor);
|
|
112
|
+
// Get both relation metadata types asynchronously
|
|
113
|
+
const [relOneData, relManyData] = await Promise.all([
|
|
114
|
+
this.getRelationOneMeta(classFields),
|
|
115
|
+
this.getRelationManyMeta(classFields)
|
|
116
|
+
]);
|
|
117
|
+
this.loadModels().forEach((model) => {
|
|
118
|
+
collections_to_models[model.getCollection()] = model;
|
|
119
|
+
});
|
|
120
|
+
const seriesHydrationfields = [];
|
|
121
|
+
if (allowRelations) {
|
|
122
|
+
// Handle many-to-many relations
|
|
123
|
+
for (const key in relManyData) {
|
|
124
|
+
if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const relMeta = relManyData[key];
|
|
128
|
+
const relationEnabled = this.checkRelEnabled(relMeta.key);
|
|
129
|
+
if (relationEnabled) {
|
|
130
|
+
this[relMeta.key] = await relMeta.inversionModel.findBy({
|
|
131
|
+
conditions: {
|
|
132
|
+
[relMeta.foreignKey]: data.id
|
|
133
|
+
},
|
|
134
|
+
allowRelations: false
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Handle one-to-one relations
|
|
139
|
+
for (const key in relOneData) {
|
|
140
|
+
if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const relMeta = relOneData[key];
|
|
144
|
+
const relationEnabled = this.checkRelEnabled(relMeta.key);
|
|
145
|
+
if (!data[relMeta.hydrationField] && relMeta.required) {
|
|
146
|
+
throw new Error(`Relation field "${relMeta.hydrationField}" is required in model ${this.constructor.name}.`);
|
|
147
|
+
}
|
|
148
|
+
if (relationEnabled && data[relMeta.hydrationField]) {
|
|
149
|
+
this[relMeta.key] = await relMeta.model.find(data[relMeta.hydrationField], { allowRelations: false });
|
|
150
|
+
}
|
|
151
|
+
else if (relationEnabled && !data[relMeta.hydrationField] && data[relMeta.key]) {
|
|
152
|
+
const newRelModel = await relMeta.model.create(data[relMeta.key]);
|
|
153
|
+
this[relMeta.key] = await newRelModel.save();
|
|
154
|
+
}
|
|
155
|
+
const cutKeys = this.constructor._CUT_KEYS;
|
|
156
|
+
if (!cutKeys.includes(relMeta.hydrationField)) {
|
|
157
|
+
cutKeys.push(relMeta.hydrationField);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Process regular fields and time series
|
|
162
|
+
for (const key in data) {
|
|
163
|
+
if (data.hasOwnProperty(key)) {
|
|
164
|
+
if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (Object.keys(relOneData).includes(key)) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (seriesHydrationfields.includes(key)) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const timeSeriesMetaData = timeSeriesIds[key];
|
|
174
|
+
if (timeSeriesMetaData) {
|
|
175
|
+
this[key] = data[key];
|
|
176
|
+
const seriesModel = collections_to_models[timeSeriesMetaData.collection];
|
|
177
|
+
const dataModels = await seriesModel.findBy({
|
|
178
|
+
id: { in: data[key] }
|
|
179
|
+
});
|
|
180
|
+
seriesHydrationfields.push(timeSeriesMetaData.hydrationField);
|
|
181
|
+
this[timeSeriesMetaData.hydrationField] = dataModels;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
this[key] = data[key];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
getModelScalarFields(model) {
|
|
191
|
+
return FieldsHelper_1.FieldsHelper.getAllClassFields(model)
|
|
192
|
+
.filter(item => item.indexOf('TrackType') === 0)
|
|
193
|
+
.map(item => item.split(':').at(-1));
|
|
194
|
+
}
|
|
195
|
+
getTimeSeriesModelFields() {
|
|
196
|
+
const timeSeriesIds = {};
|
|
197
|
+
for (const key in this) {
|
|
198
|
+
if (this.hasOwnProperty(key)) {
|
|
199
|
+
const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, this);
|
|
200
|
+
if (meta) {
|
|
201
|
+
if (!timeSeriesIds[key]) {
|
|
202
|
+
timeSeriesIds[key] = {
|
|
203
|
+
collection: meta.timeSeriesModel,
|
|
204
|
+
hydrationField: meta.hydrationField,
|
|
205
|
+
ids: this[key]
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return timeSeriesIds;
|
|
212
|
+
}
|
|
213
|
+
async getRelationOneMeta(classFields) {
|
|
214
|
+
return RWSModel.getRelationOneMeta(this, classFields);
|
|
215
|
+
}
|
|
216
|
+
static async getRelationOneMeta(model, classFields) {
|
|
217
|
+
const relIds = {};
|
|
218
|
+
const relationFields = classFields
|
|
219
|
+
.filter((item) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
|
|
220
|
+
.map((item) => item.split(':').at(-1));
|
|
221
|
+
for (const key of relationFields) {
|
|
222
|
+
const metadataKey = `Relation:${key}`;
|
|
223
|
+
const metadata = Reflect.getMetadata(metadataKey, model);
|
|
224
|
+
if (metadata && metadata.promise) {
|
|
225
|
+
const resolvedMetadata = await metadata.promise;
|
|
226
|
+
if (!relIds[key]) {
|
|
227
|
+
relIds[key] = {
|
|
228
|
+
key: resolvedMetadata.key,
|
|
229
|
+
required: resolvedMetadata.required,
|
|
230
|
+
model: resolvedMetadata.relatedTo,
|
|
231
|
+
hydrationField: resolvedMetadata.relationField,
|
|
232
|
+
foreignKey: resolvedMetadata.relatedToField
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return relIds;
|
|
238
|
+
}
|
|
239
|
+
async getRelationManyMeta(classFields) {
|
|
240
|
+
return RWSModel.getRelationManyMeta(this, classFields);
|
|
241
|
+
}
|
|
242
|
+
static async getRelationManyMeta(model, classFields) {
|
|
243
|
+
const relIds = {};
|
|
244
|
+
const inverseFields = classFields
|
|
245
|
+
.filter((item) => item.indexOf('InverseRelation') === 0)
|
|
246
|
+
.map((item) => item.split(':').at(-1));
|
|
247
|
+
for (const key of inverseFields) {
|
|
248
|
+
const metadataKey = `InverseRelation:${key}`;
|
|
249
|
+
const metadata = Reflect.getMetadata(metadataKey, model);
|
|
250
|
+
if (metadata && metadata.promise) {
|
|
251
|
+
const resolvedMetadata = await metadata.promise;
|
|
252
|
+
if (!relIds[key]) {
|
|
253
|
+
relIds[key] = {
|
|
254
|
+
key: resolvedMetadata.key,
|
|
255
|
+
inversionModel: resolvedMetadata.inversionModel,
|
|
256
|
+
foreignKey: resolvedMetadata.foreignKey
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return relIds;
|
|
262
|
+
}
|
|
263
|
+
async toMongo() {
|
|
264
|
+
const data = {};
|
|
265
|
+
const timeSeriesIds = this.getTimeSeriesModelFields();
|
|
266
|
+
const timeSeriesHydrationFields = [];
|
|
267
|
+
for (const key in this) {
|
|
268
|
+
if (this.hasRelation(key)) {
|
|
269
|
+
data[key] = this.bindRelation(key, this[key]);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (!(await this.isDbVariable(key))) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
const passedFieldCondition = this.hasOwnProperty(key) &&
|
|
276
|
+
!(this.constructor._BANNED_KEYS
|
|
277
|
+
|| RWSModel._BANNED_KEYS).includes(key) &&
|
|
278
|
+
!timeSeriesHydrationFields.includes(key);
|
|
279
|
+
if (passedFieldCondition) {
|
|
280
|
+
data[key] = this[key];
|
|
281
|
+
}
|
|
282
|
+
if (timeSeriesIds[key]) {
|
|
283
|
+
data[key] = this[key];
|
|
284
|
+
timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return data;
|
|
288
|
+
}
|
|
289
|
+
getCollection() {
|
|
290
|
+
return this.constructor._collection || this._collection;
|
|
291
|
+
}
|
|
292
|
+
static getCollection() {
|
|
293
|
+
return this.constructor._collection || this._collection;
|
|
294
|
+
}
|
|
295
|
+
async save() {
|
|
296
|
+
const data = await this.toMongo();
|
|
297
|
+
let updatedModelData = data;
|
|
298
|
+
if (this.id) {
|
|
299
|
+
this.preUpdate();
|
|
300
|
+
updatedModelData = await this.dbService.update(data, this.getCollection());
|
|
301
|
+
await this._asyncFill(updatedModelData);
|
|
302
|
+
this.postUpdate();
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
this.preCreate();
|
|
306
|
+
const timeSeriesModel = await Promise.resolve().then(() => __importStar(require('./TimeSeriesModel')));
|
|
307
|
+
const isTimeSeries = this instanceof timeSeriesModel.default;
|
|
308
|
+
updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
|
|
309
|
+
await this._asyncFill(updatedModelData);
|
|
310
|
+
this.postCreate();
|
|
311
|
+
}
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
static async getModelAnnotations(constructor) {
|
|
315
|
+
const annotationsData = {};
|
|
316
|
+
const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
|
|
317
|
+
// Process all metadata keys and collect promises
|
|
318
|
+
const metadataPromises = metadataKeys.map(async (fullKey) => {
|
|
319
|
+
const [annotationType, propertyKey] = fullKey.split(':');
|
|
320
|
+
const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
|
|
321
|
+
if (metadata) {
|
|
322
|
+
// If this is a relation metadata with a promise
|
|
323
|
+
if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
|
|
324
|
+
const resolvedMetadata = await metadata.promise;
|
|
325
|
+
annotationsData[propertyKey] = {
|
|
326
|
+
annotationType,
|
|
327
|
+
metadata: resolvedMetadata
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
// Handle non-relation metadata as before
|
|
332
|
+
const key = metadata.key || propertyKey;
|
|
333
|
+
annotationsData[key] = {
|
|
334
|
+
annotationType,
|
|
335
|
+
metadata
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
// Wait for all metadata to be processed
|
|
341
|
+
await Promise.all(metadataPromises);
|
|
342
|
+
return annotationsData;
|
|
343
|
+
}
|
|
344
|
+
preUpdate() {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
postUpdate() {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
preCreate() {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
postCreate() {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
static isSubclass(constructor, baseClass) {
|
|
357
|
+
return baseClass.prototype.isPrototypeOf(constructor.prototype);
|
|
358
|
+
}
|
|
359
|
+
hasTimeSeries() {
|
|
360
|
+
return RWSModel.checkTimeSeries(this.constructor);
|
|
361
|
+
}
|
|
362
|
+
static checkTimeSeries(constructor) {
|
|
363
|
+
const data = constructor.prototype;
|
|
364
|
+
for (const key in data) {
|
|
365
|
+
if (data.hasOwnProperty(key)) {
|
|
366
|
+
if (Reflect.getMetadata(`InverseTimeSeries:${key}`, constructor.prototype)) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
async isDbVariable(variable) {
|
|
374
|
+
return RWSModel.checkDbVariable(this.constructor, variable);
|
|
375
|
+
}
|
|
376
|
+
static async checkDbVariable(constructor, variable) {
|
|
377
|
+
if (variable === 'id') {
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
const dbAnnotations = await RWSModel.getModelAnnotations(constructor);
|
|
381
|
+
const dbProperties = Object.keys(dbAnnotations)
|
|
382
|
+
.map((key) => { return { ...dbAnnotations[key], key }; })
|
|
383
|
+
.filter((element) => element.annotationType === 'TrackType')
|
|
384
|
+
.map((element) => element.key);
|
|
385
|
+
return dbProperties.includes(variable);
|
|
386
|
+
}
|
|
387
|
+
sanitizeDBData(data) {
|
|
388
|
+
const dataKeys = Object.keys(data);
|
|
389
|
+
const sanitizedData = {};
|
|
390
|
+
for (const key of dataKeys) {
|
|
391
|
+
if (this.isDbVariable(key)) {
|
|
392
|
+
sanitizedData[key] = data[key];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return sanitizedData;
|
|
396
|
+
}
|
|
397
|
+
static async watchCollection(preRun) {
|
|
398
|
+
const collection = Reflect.get(this, '_collection');
|
|
399
|
+
this.checkForInclusionWithThrow(this.name);
|
|
400
|
+
return await this.services.dbService.watchCollection(collection, preRun);
|
|
401
|
+
}
|
|
402
|
+
static async findOneBy(findParams) {
|
|
403
|
+
var _a, _b, _c, _d, _e;
|
|
404
|
+
const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
|
|
405
|
+
const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
|
|
406
|
+
const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
|
|
407
|
+
const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
|
|
408
|
+
const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
|
|
409
|
+
this.checkForInclusionWithThrow('');
|
|
410
|
+
const collection = Reflect.get(this, '_collection');
|
|
411
|
+
const dbData = await this.services.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
|
|
412
|
+
if (dbData) {
|
|
413
|
+
const inst = new this();
|
|
414
|
+
return await inst._asyncFill(dbData, fullData, allowRelations);
|
|
415
|
+
}
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
static async find(id, findParams = null) {
|
|
419
|
+
var _a, _b, _c, _d;
|
|
420
|
+
const ordering = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _a !== void 0 ? _a : null;
|
|
421
|
+
const fields = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _b !== void 0 ? _b : null;
|
|
422
|
+
const allowRelations = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _c !== void 0 ? _c : true;
|
|
423
|
+
const fullData = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _d !== void 0 ? _d : false;
|
|
424
|
+
const collection = Reflect.get(this, '_collection');
|
|
425
|
+
this.checkForInclusionWithThrow(this.name);
|
|
426
|
+
const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
|
|
427
|
+
if (dbData) {
|
|
428
|
+
const inst = new this();
|
|
429
|
+
return await inst._asyncFill(dbData, fullData, allowRelations);
|
|
430
|
+
}
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
static async findBy(findParams) {
|
|
434
|
+
var _a, _b, _c, _d, _e;
|
|
435
|
+
const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
|
|
436
|
+
const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
|
|
437
|
+
const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
|
|
438
|
+
const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
|
|
439
|
+
const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
|
|
440
|
+
const collection = Reflect.get(this, '_collection');
|
|
441
|
+
this.checkForInclusionWithThrow(this.name);
|
|
442
|
+
try {
|
|
443
|
+
const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, allowRelations);
|
|
444
|
+
if (dbData.length) {
|
|
445
|
+
const instanced = [];
|
|
446
|
+
for (const data of dbData) {
|
|
447
|
+
const inst = new this();
|
|
448
|
+
instanced.push((await inst._asyncFill(data, fullData, allowRelations)));
|
|
449
|
+
}
|
|
450
|
+
return instanced;
|
|
451
|
+
}
|
|
452
|
+
return [];
|
|
453
|
+
}
|
|
454
|
+
catch (rwsError) {
|
|
455
|
+
console.error(rwsError);
|
|
456
|
+
throw rwsError;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
static async delete(conditions) {
|
|
460
|
+
const collection = Reflect.get(this, '_collection');
|
|
461
|
+
this.checkForInclusionWithThrow(this.name);
|
|
462
|
+
return await this.services.dbService.delete(collection, conditions);
|
|
463
|
+
}
|
|
464
|
+
async delete() {
|
|
465
|
+
const collection = Reflect.get(this, '_collection');
|
|
466
|
+
this.checkForInclusionWithThrow();
|
|
467
|
+
return await this.dbService.delete(collection, {
|
|
468
|
+
id: this.id
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
static async create(data) {
|
|
472
|
+
const newModel = new this();
|
|
473
|
+
const sanitizedData = newModel.sanitizeDBData(data);
|
|
474
|
+
await newModel._asyncFill(sanitizedData);
|
|
475
|
+
return newModel;
|
|
476
|
+
}
|
|
477
|
+
static loadModels() {
|
|
478
|
+
return this.allModels || [];
|
|
479
|
+
}
|
|
480
|
+
loadModels() {
|
|
481
|
+
return RWSModel.loadModels();
|
|
482
|
+
}
|
|
483
|
+
checkRelEnabled(key) {
|
|
484
|
+
return Object.keys(this.constructor._RELATIONS).includes(key) && this.constructor._RELATIONS[key] === true;
|
|
485
|
+
}
|
|
486
|
+
static setServices(services) {
|
|
487
|
+
this.allModels = services.configService.get('db_models');
|
|
488
|
+
this.services = services;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
exports.RWSModel = RWSModel;
|
|
492
|
+
RWSModel.services = {};
|
|
493
|
+
RWSModel._collection = null;
|
|
494
|
+
RWSModel._RELATIONS = {};
|
|
495
|
+
RWSModel._BANNED_KEYS = ['_collection'];
|
|
496
|
+
RWSModel.allModels = [];
|
|
497
|
+
RWSModel._CUT_KEYS = [];
|
|
498
|
+
__decorate([
|
|
499
|
+
(0, decorators_1.TrackType)(String),
|
|
500
|
+
__metadata("design:type", String)
|
|
501
|
+
], RWSModel.prototype, "id", void 0);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Collection } from 'mongodb';
|
|
2
|
+
import { ITimeSeries } from '../types/ITimeSeries';
|
|
3
|
+
import { IModel } from '../models/_model';
|
|
4
|
+
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
5
|
+
interface IDBClientCreate {
|
|
6
|
+
dbUrl?: string;
|
|
7
|
+
dbName?: string;
|
|
8
|
+
}
|
|
9
|
+
declare class DBService {
|
|
10
|
+
private configService;
|
|
11
|
+
private client;
|
|
12
|
+
private opts;
|
|
13
|
+
private connected;
|
|
14
|
+
constructor(configService: IDbConfigHandler);
|
|
15
|
+
private connectToDB;
|
|
16
|
+
private createBaseMongoClient;
|
|
17
|
+
private createBaseMongoClientDB;
|
|
18
|
+
cloneDatabase(source: string, target: string): Promise<void>;
|
|
19
|
+
watchCollection(collectionName: string, preRun: () => void): Promise<any>;
|
|
20
|
+
insert(data: any, collection: string, isTimeSeries?: boolean): Promise<any>;
|
|
21
|
+
update(data: any, collection: string): Promise<IModel>;
|
|
22
|
+
findOneBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
23
|
+
[fieldName: string]: string;
|
|
24
|
+
}, allowRelations?: boolean): Promise<IModel | null>;
|
|
25
|
+
delete(collection: string, conditions: any): Promise<void>;
|
|
26
|
+
findBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
27
|
+
[fieldName: string]: string;
|
|
28
|
+
}, allowRelations?: boolean): Promise<IModel[]>;
|
|
29
|
+
collectionExists(collection_name: string): Promise<boolean>;
|
|
30
|
+
createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>;
|
|
31
|
+
private getCollectionHandler;
|
|
32
|
+
private setOpts;
|
|
33
|
+
}
|
|
34
|
+
export { DBService, IDBClientCreate };
|