@rws-framework/db 2.2.5 → 2.2.6

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 (70) 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 +0 -0
  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 +9 -10
  15. package/dist/helper/FieldsHelper.d.ts +0 -0
  16. package/dist/helper/FieldsHelper.js +0 -0
  17. package/dist/index.d.ts +1 -2
  18. package/dist/index.js +2 -6
  19. package/dist/models/TimeSeriesModel.d.ts +0 -0
  20. package/dist/models/TimeSeriesModel.js +0 -0
  21. package/dist/models/_model.d.ts +0 -0
  22. package/dist/models/_model.js +0 -0
  23. package/dist/models/core/RWSModel.d.ts +10 -10
  24. package/dist/models/core/RWSModel.js +32 -10
  25. package/dist/models/core/TimeSeriesModel.d.ts +1 -7
  26. package/dist/models/core/TimeSeriesModel.js +12 -32
  27. package/dist/models/index.d.ts +1 -2
  28. package/dist/models/index.js +2 -6
  29. package/dist/models/interfaces/IModel.d.ts +0 -0
  30. package/dist/models/interfaces/IModel.js +0 -0
  31. package/dist/models/interfaces/IRWSModelServices.d.ts +0 -0
  32. package/dist/models/interfaces/IRWSModelServices.js +0 -0
  33. package/dist/models/interfaces/OpModelType.d.ts +7 -8
  34. package/dist/models/interfaces/OpModelType.js +0 -0
  35. package/dist/models/types/RelationTypes.d.ts +0 -0
  36. package/dist/models/types/RelationTypes.js +0 -0
  37. package/dist/models/utils/ModelUtils.d.ts +0 -0
  38. package/dist/models/utils/ModelUtils.js +0 -0
  39. package/dist/models/utils/PaginationUtils.d.ts +1 -1
  40. package/dist/models/utils/PaginationUtils.js +0 -0
  41. package/dist/models/utils/RelationUtils.d.ts +0 -0
  42. package/dist/models/utils/RelationUtils.js +0 -0
  43. package/dist/models/utils/TimeSeriesUtils.d.ts +0 -0
  44. package/dist/models/utils/TimeSeriesUtils.js +0 -0
  45. package/dist/services/DBService.d.ts +0 -0
  46. package/dist/services/DBService.js +3 -2
  47. package/dist/types/DbConfigHandler.d.ts +0 -0
  48. package/dist/types/DbConfigHandler.js +0 -0
  49. package/dist/types/FindParams.d.ts +1 -1
  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/helper/DbHelper.ts +10 -10
  57. package/src/index.ts +2 -2
  58. package/src/models/core/RWSModel.ts +64 -31
  59. package/src/models/core/TimeSeriesModel.ts +12 -12
  60. package/src/models/index.ts +2 -2
  61. package/src/models/interfaces/IModel.ts +0 -0
  62. package/src/models/interfaces/IRWSModelServices.ts +0 -0
  63. package/src/models/interfaces/OpModelType.ts +9 -9
  64. package/src/models/types/RelationTypes.ts +0 -0
  65. package/src/models/utils/ModelUtils.ts +0 -0
  66. package/src/models/utils/PaginationUtils.ts +6 -6
  67. package/src/models/utils/RelationUtils.ts +0 -0
  68. package/src/models/utils/TimeSeriesUtils.ts +0 -0
  69. package/src/services/DBService.ts +3 -2
  70. package/src/types/FindParams.ts +2 -2
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -9,7 +9,6 @@ const chalk_1 = __importDefault(require("chalk"));
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const fs_1 = __importDefault(require("fs"));
11
11
  const _model_1 = require("../models/_model");
12
- const TimeSeriesModel_1 = __importDefault(require("../models/core/TimeSeriesModel"));
13
12
  const log = console.log;
14
13
  const workspaceRoot = console_1.rwsPath.findRootWorkspacePath();
15
14
  const moduleDir = path_1.default.resolve(workspaceRoot, 'node_modules', '@rws-framework', 'db');
@@ -30,15 +29,15 @@ class DbHelper {
30
29
  const modelSection = await DbHelper.generateModelSections(model);
31
30
  template += '\n\n' + modelSection;
32
31
  log(chalk_1.default.green('[RWS]'), chalk_1.default.blue('Building DB Model'), model.name);
33
- if (_model_1.RWSModel.isSubclass(model, TimeSeriesModel_1.default)) {
34
- dbService.collectionExists(model._collection).then((exists) => {
35
- if (exists) {
36
- return;
37
- }
38
- log(chalk_1.default.green('[RWS Init]') + ` creating TimeSeries type collection from ${model} model`);
39
- dbService.createTimeSeriesCollection(model._collection);
40
- });
41
- }
32
+ // if(RWSModel.isSubclass(model as any, TimeSeriesModel)){
33
+ // dbService.collectionExists(model._collection).then((exists: boolean) => {
34
+ // if (exists){
35
+ // return;
36
+ // }
37
+ // log(chalk.green('[RWS Init]') + ` creating TimeSeries type collection from ${model} model`);
38
+ // dbService.createTimeSeriesCollection(model._collection);
39
+ // });
40
+ // }
42
41
  }
43
42
  const schemaDir = path_1.default.join(moduleDir, 'prisma');
44
43
  const schemaPath = path_1.default.join(schemaDir, 'schema.prisma');
File without changes
File without changes
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { DBService } from "./services/DBService";
2
2
  import { RWSModel, OpModelType } from "./models/_model";
3
- import TimeSeriesModel from './models/core/TimeSeriesModel';
4
3
  import { InverseRelation, Relation, TrackType, InverseTimeSeries, IMetaOpts } from './decorators';
5
4
  import { DbHelper } from './helper/DbHelper';
6
5
  import { FieldsHelper } from './helper/FieldsHelper';
@@ -10,4 +9,4 @@ import type { IDbConfigHandler } from './types/DbConfigHandler';
10
9
  import type { IRWSModel } from './types/IRWSModel';
11
10
  import { RWSCollection, IRWSCollectionMeta, IRWSCollectionOpts } from "./decorators/RWSCollection";
12
11
  export type { IRWSCollectionMeta, IRWSCollectionOpts, IRWSModel, IMetaOpts, OpModelType, IDbConfigHandler, ITimeSeries, };
13
- export { RWSModel, RWSCollection, DBService, FindByType, TimeSeriesModel, InverseRelation, Relation, TrackType, InverseTimeSeries, DbHelper, FieldsHelper };
12
+ export { RWSModel, RWSCollection, DBService, FindByType, InverseRelation, Relation, TrackType, InverseTimeSeries, DbHelper, FieldsHelper };
package/dist/index.js CHANGED
@@ -1,15 +1,11 @@
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
- exports.FieldsHelper = exports.DbHelper = exports.InverseTimeSeries = exports.TrackType = exports.Relation = exports.InverseRelation = exports.TimeSeriesModel = exports.DBService = exports.RWSCollection = exports.RWSModel = void 0;
3
+ exports.FieldsHelper = exports.DbHelper = exports.InverseTimeSeries = exports.TrackType = exports.Relation = exports.InverseRelation = exports.DBService = exports.RWSCollection = exports.RWSModel = void 0;
7
4
  const DBService_1 = require("./services/DBService");
8
5
  Object.defineProperty(exports, "DBService", { enumerable: true, get: function () { return DBService_1.DBService; } });
9
6
  const _model_1 = require("./models/_model");
10
7
  Object.defineProperty(exports, "RWSModel", { enumerable: true, get: function () { return _model_1.RWSModel; } });
11
- const TimeSeriesModel_1 = __importDefault(require("./models/core/TimeSeriesModel"));
12
- exports.TimeSeriesModel = TimeSeriesModel_1.default;
8
+ // import TimeSeriesModel from './models/core/TimeSeriesModel';
13
9
  const decorators_1 = require("./decorators");
14
10
  Object.defineProperty(exports, "InverseRelation", { enumerable: true, get: function () { return decorators_1.InverseRelation; } });
15
11
  Object.defineProperty(exports, "Relation", { enumerable: true, get: function () { return decorators_1.Relation; } });
File without changes
File without changes
File without changes
File without changes
@@ -3,7 +3,7 @@ import { IRWSModelServices } from '../interfaces/IRWSModelServices';
3
3
  import { OpModelType } from '../interfaces/OpModelType';
4
4
  import { FindByType, IPaginationParams } from '../../types/FindParams';
5
5
  import { DBService } from '../../services/DBService';
6
- declare class RWSModel<ChildClass> implements IModel {
6
+ declare class RWSModel<T> implements IModel {
7
7
  static services: IRWSModelServices;
8
8
  [key: string]: any;
9
9
  id: string;
@@ -17,20 +17,20 @@ declare class RWSModel<ChildClass> implements IModel {
17
17
  static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void;
18
18
  checkForInclusion(): boolean;
19
19
  static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean;
20
- protected _fill(data: any): RWSModel<ChildClass>;
20
+ protected _fill(data: any): RWSModel<T>;
21
21
  protected hasRelation(key: string): boolean;
22
22
  protected bindRelation(key: string, relatedModel: RWSModel<any>): {
23
23
  connect: {
24
24
  id: string;
25
25
  };
26
26
  };
27
- _asyncFill(data: any, fullDataMode?: boolean, allowRelations?: boolean): Promise<ChildClass>;
27
+ _asyncFill(data: any, fullDataMode?: boolean, allowRelations?: boolean): Promise<T>;
28
28
  private getModelScalarFields;
29
29
  private getRelationOneMeta;
30
30
  static getRelationOneMeta(model: any, classFields: string[]): Promise<import("..").RelOneMetaType<import("../..").IRWSModel>>;
31
31
  private getRelationManyMeta;
32
32
  static getRelationManyMeta(model: any, classFields: string[]): Promise<import("..").RelManyMetaType<import("../..").IRWSModel>>;
33
- static paginate<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<any>, pageParams: IPaginationParams, findParams?: FindByType): Promise<RWSModel<ChildClass>[]>;
33
+ static paginate<T extends RWSModel<T>>(this: OpModelType<T>, paginateParams: IPaginationParams, findParams?: FindByType): Promise<T[]>;
34
34
  toMongo(): Promise<any>;
35
35
  getCollection(): string | null;
36
36
  static getCollection(): string | null;
@@ -49,12 +49,12 @@ declare class RWSModel<ChildClass> implements IModel {
49
49
  isDbVariable(variable: string): Promise<boolean>;
50
50
  static checkDbVariable(constructor: any, variable: string): Promise<boolean>;
51
51
  sanitizeDBData(data: any): any;
52
- static watchCollection<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, preRun: () => void): Promise<any>;
53
- static findOneBy<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, findParams?: FindByType): Promise<ChildClass | null>;
54
- static find<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<ChildClass | null>;
55
- static findBy<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, findParams?: FindByType): Promise<ChildClass[]>;
56
- static delete<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, conditions: any): Promise<void>;
57
- delete<ChildClass extends RWSModel<ChildClass>>(): Promise<void>;
52
+ static watchCollection<T extends RWSModel<T>>(this: OpModelType<T>, preRun: () => void): Promise<any>;
53
+ static findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T | null>;
54
+ static find<T extends RWSModel<T>>(this: OpModelType<T>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
55
+ static findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T[]>;
56
+ static delete<T extends RWSModel<T>>(this: OpModelType<T>, conditions: any): Promise<void>;
57
+ delete<T extends RWSModel<T>>(): Promise<void>;
58
58
  static create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
59
59
  static loadModels(): OpModelType<any>[];
60
60
  loadModels(): OpModelType<any>[];
@@ -8,18 +8,13 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.RWSModel = void 0;
16
13
  const decorators_1 = require("../../decorators");
17
14
  const FieldsHelper_1 = require("../../helper/FieldsHelper");
18
15
  const RelationUtils_1 = require("../utils/RelationUtils");
19
- const PaginationUtils_1 = require("../utils/PaginationUtils");
20
16
  const TimeSeriesUtils_1 = require("../utils/TimeSeriesUtils");
21
17
  const ModelUtils_1 = require("../utils/ModelUtils");
22
- const TimeSeriesModel_1 = __importDefault(require("./TimeSeriesModel"));
23
18
  class RWSModel {
24
19
  constructor(data) {
25
20
  if (!this.getCollection()) {
@@ -38,7 +33,10 @@ class RWSModel {
38
33
  }
39
34
  }
40
35
  checkForInclusionWithThrow() {
41
- this.checkForInclusionWithThrow();
36
+ const constructor = this.constructor;
37
+ if (!constructor.checkForInclusion(constructor.name)) {
38
+ throw new Error('Model undefined: ' + constructor.name);
39
+ }
42
40
  }
43
41
  static checkForInclusionWithThrow(checkModelType) {
44
42
  if (!this.checkForInclusion(this.name)) {
@@ -46,7 +44,8 @@ class RWSModel {
46
44
  }
47
45
  }
48
46
  checkForInclusion() {
49
- return this.checkForInclusion();
47
+ const constructor = this.constructor;
48
+ return constructor.checkForInclusion(constructor.name);
50
49
  }
51
50
  static checkForInclusion(checkModelType) {
52
51
  return this.loadModels().find((definedModel) => {
@@ -172,8 +171,31 @@ class RWSModel {
172
171
  static async getRelationManyMeta(model, classFields) {
173
172
  return RelationUtils_1.RelationUtils.getRelationManyMeta(model, classFields);
174
173
  }
175
- static paginate(pageParams, findParams) {
176
- return PaginationUtils_1.PaginationUtils.paginate.bind(this)(pageParams, findParams);
174
+ static async paginate(paginateParams, findParams) {
175
+ var _a, _b, _c, _d, _e;
176
+ const conditions = (_a = findParams === null || findParams === void 0 ? void 0 : findParams.conditions) !== null && _a !== void 0 ? _a : {};
177
+ const ordering = (_b = findParams === null || findParams === void 0 ? void 0 : findParams.ordering) !== null && _b !== void 0 ? _b : null;
178
+ const fields = (_c = findParams === null || findParams === void 0 ? void 0 : findParams.fields) !== null && _c !== void 0 ? _c : null;
179
+ const allowRelations = (_d = findParams === null || findParams === void 0 ? void 0 : findParams.allowRelations) !== null && _d !== void 0 ? _d : true;
180
+ const fullData = (_e = findParams === null || findParams === void 0 ? void 0 : findParams.fullData) !== null && _e !== void 0 ? _e : false;
181
+ const collection = Reflect.get(this, '_collection');
182
+ this.checkForInclusionWithThrow(this.name);
183
+ try {
184
+ const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
185
+ if (dbData.length) {
186
+ const instanced = [];
187
+ for (const data of dbData) {
188
+ const inst = new this();
189
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations)));
190
+ }
191
+ return instanced;
192
+ }
193
+ return [];
194
+ }
195
+ catch (rwsError) {
196
+ console.error(rwsError);
197
+ throw rwsError;
198
+ }
177
199
  }
178
200
  async toMongo() {
179
201
  const data = {};
@@ -218,7 +240,7 @@ class RWSModel {
218
240
  }
219
241
  else {
220
242
  this.preCreate();
221
- const isTimeSeries = this instanceof TimeSeriesModel_1.default;
243
+ const isTimeSeries = false; //this instanceof timeSeriesModel;
222
244
  updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
223
245
  await this._asyncFill(updatedModelData);
224
246
  this.postCreate();
@@ -1,7 +1 @@
1
- import { RWSModel } from './RWSModel';
2
- export default class TimeSeriesModel<T> extends RWSModel<T> {
3
- value: number;
4
- timestamp: Date;
5
- params: any;
6
- constructor(data?: any);
7
- }
1
+ export {};
@@ -1,34 +1,14 @@
1
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
2
  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);
3
+ // export default class TimeSeriesModel<T> extends RWSModel<T> {
4
+ // @TrackType(Number) value: number;
5
+ // @TrackType(Date) timestamp: Date;
6
+ // @TrackType(Object)
7
+ // params: any;
8
+ // constructor(data?: any) {
9
+ // super(data);
10
+ // if(!this.timestamp) {
11
+ // this.timestamp = new Date();
12
+ // }
13
+ // }
14
+ // }
@@ -3,6 +3,5 @@ import { IRWSModelServices } from './interfaces/IRWSModelServices';
3
3
  import { OpModelType } from './interfaces/OpModelType';
4
4
  import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
5
5
  import { RWSModel } from './core/RWSModel';
6
- import TimeSeriesModel from './core/TimeSeriesModel';
7
6
  import { TrackType, IMetaOpts } from '../decorators';
8
- export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TimeSeriesModel, TrackType, IMetaOpts };
7
+ export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TrackType, IMetaOpts };
@@ -1,12 +1,8 @@
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
- exports.TrackType = exports.TimeSeriesModel = exports.RWSModel = void 0;
3
+ exports.TrackType = exports.RWSModel = void 0;
7
4
  const RWSModel_1 = require("./core/RWSModel");
8
5
  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;
6
+ // import TimeSeriesModel from './core/TimeSeriesModel';
11
7
  const decorators_1 = require("../decorators");
12
8
  Object.defineProperty(exports, "TrackType", { enumerable: true, get: function () { return decorators_1.TrackType; } });
File without changes
File without changes
File without changes
File without changes
@@ -3,9 +3,9 @@ import { FindByType, IPaginationParams } from '../../types/FindParams';
3
3
  import { IRWSModelServices } from './IRWSModelServices';
4
4
  import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
5
5
  import { DBService } from '../../services/DBService';
6
- type RWSModel<T> = any;
7
- export interface OpModelType<ChildClass> {
8
- new (data?: any | null): ChildClass;
6
+ import type { RWSModel } from '../core/RWSModel';
7
+ export interface OpModelType<T> {
8
+ new (data?: any | null): T;
9
9
  services: IRWSModelServices;
10
10
  name: string;
11
11
  _collection: string;
@@ -17,16 +17,15 @@ export interface OpModelType<ChildClass> {
17
17
  loadModels: () => OpModelType<any>[];
18
18
  checkForInclusionWithThrow: (className: string) => void;
19
19
  checkForInclusion: (className: string) => boolean;
20
- findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams: FindByType): Promise<T | null>;
20
+ findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T | null>;
21
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[]>;
22
+ findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T[]>;
23
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>;
24
+ delete<T extends RWSModel<T>>(this: OpModelType<T>, conditions: any): Promise<void>;
25
+ create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
26
26
  getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
27
27
  getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
28
28
  getCollection(): string;
29
29
  getDb(): DBService;
30
30
  setServices(services: IRWSModelServices): void;
31
31
  }
32
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,5 +1,5 @@
1
1
  import { OpModelType, RWSModel } from '../_model';
2
2
  import { FindByType, IPaginationParams } from '../../types/FindParams';
3
3
  export declare class PaginationUtils {
4
- static paginate<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<ChildClass>, paginationParams?: IPaginationParams, findParams?: FindByType): Promise<ChildClass[]>;
4
+ static paginate<T extends RWSModel<T>>(this: OpModelType<T>, paginationParams?: IPaginationParams, findParams?: FindByType): Promise<T[]>;
5
5
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -139,8 +139,9 @@ class DBService {
139
139
  params.orderBy = ordering;
140
140
  }
141
141
  if (pagination) {
142
- params.skip = pagination.page * pagination.per_page;
143
- params.take = pagination.per_page;
142
+ const perPage = pagination.per_page || 50;
143
+ params.skip = pagination.page * perPage;
144
+ params.take = perPage;
144
145
  }
145
146
  const retData = await this.getCollectionHandler(collection).findMany(params);
146
147
  return retData;
File without changes
File without changes
@@ -10,5 +10,5 @@ export type FindByType = {
10
10
  };
11
11
  export interface IPaginationParams {
12
12
  page: number;
13
- per_page: number;
13
+ per_page?: number;
14
14
  }
File without changes
File without changes
File without changes
File without changes
File without changes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "2.2.5",
4
+ "version": "2.2.6",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -5,7 +5,7 @@ import fs from 'fs';
5
5
 
6
6
  import { IDbConfigHandler } from '../types/DbConfigHandler';
7
7
  import { IMetaOpts, OpModelType, RWSModel } from '../models/_model';
8
- import TimeSeriesModel from '../models/core/TimeSeriesModel';
8
+ // import TimeSeriesModel from '../models/core/TimeSeriesModel';
9
9
  import { DBService } from '../services/DBService';
10
10
  import { IRelationOpts } from '../decorators/Relation';
11
11
  import { InverseRelationOpts } from '../decorators/InverseRelation';
@@ -40,17 +40,17 @@ export class DbHelper {
40
40
 
41
41
  log(chalk.green('[RWS]'), chalk.blue('Building DB Model'), model.name);
42
42
 
43
- if(RWSModel.isSubclass(model as any, TimeSeriesModel)){
44
- dbService.collectionExists(model._collection).then((exists: boolean) => {
45
- if (exists){
46
- return;
47
- }
43
+ // if(RWSModel.isSubclass(model as any, TimeSeriesModel)){
44
+ // dbService.collectionExists(model._collection).then((exists: boolean) => {
45
+ // if (exists){
46
+ // return;
47
+ // }
48
48
 
49
- log(chalk.green('[RWS Init]') + ` creating TimeSeries type collection from ${model} model`);
49
+ // log(chalk.green('[RWS Init]') + ` creating TimeSeries type collection from ${model} model`);
50
50
 
51
- dbService.createTimeSeriesCollection(model._collection);
52
- });
53
- }
51
+ // dbService.createTimeSeriesCollection(model._collection);
52
+ // });
53
+ // }
54
54
  }
55
55
 
56
56
  const schemaDir = path.join(moduleDir, 'prisma');
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DBService } from "./services/DBService";
2
2
  import { RWSModel, OpModelType } from "./models/_model";
3
- import TimeSeriesModel from './models/core/TimeSeriesModel';
3
+ // import TimeSeriesModel from './models/core/TimeSeriesModel';
4
4
  import { InverseRelation, Relation, TrackType, InverseTimeSeries, IMetaOpts } from './decorators';
5
5
 
6
6
  import { DbHelper } from './helper/DbHelper';
@@ -28,7 +28,7 @@ export {
28
28
  DBService,
29
29
 
30
30
  FindByType,
31
- TimeSeriesModel,
31
+ // TimeSeriesModel,
32
32
 
33
33
  InverseRelation, Relation, TrackType, InverseTimeSeries,
34
34
 
@@ -5,14 +5,13 @@ import { TrackType, IMetaOpts } from '../../decorators';
5
5
  import { FieldsHelper } from '../../helper/FieldsHelper';
6
6
  import { FindByType, IPaginationParams } from '../../types/FindParams';
7
7
  import { RelationUtils } from '../utils/RelationUtils';
8
- import { PaginationUtils } from '../utils/PaginationUtils';
9
8
 
10
9
  import { TimeSeriesUtils } from '../utils/TimeSeriesUtils';
11
10
  import { ModelUtils } from '../utils/ModelUtils';
12
- import timeSeriesModel from './TimeSeriesModel';
11
+ // import timeSeriesModel from './TimeSeriesModel';
13
12
  import { DBService } from '../../services/DBService';
14
13
 
15
- class RWSModel<ChildClass> implements IModel {
14
+ class RWSModel<T> implements IModel {
16
15
  static services: IRWSModelServices = {};
17
16
 
18
17
  [key: string]: any;
@@ -44,7 +43,10 @@ class RWSModel<ChildClass> implements IModel {
44
43
  }
45
44
 
46
45
  checkForInclusionWithThrow(): void {
47
- this.checkForInclusionWithThrow();
46
+ const constructor = this.constructor as OpModelType<any>;
47
+ if(!constructor.checkForInclusion(constructor.name)){
48
+ throw new Error('Model undefined: ' + constructor.name);
49
+ }
48
50
  }
49
51
 
50
52
  static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void {
@@ -54,7 +56,8 @@ class RWSModel<ChildClass> implements IModel {
54
56
  }
55
57
 
56
58
  checkForInclusion(): boolean {
57
- return this.checkForInclusion();
59
+ const constructor = this.constructor as OpModelType<any>;
60
+ return constructor.checkForInclusion(constructor.name);
58
61
  }
59
62
 
60
63
  static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean {
@@ -63,7 +66,7 @@ class RWSModel<ChildClass> implements IModel {
63
66
  }) !== undefined;
64
67
  }
65
68
 
66
- protected _fill(data: any): RWSModel<ChildClass> {
69
+ protected _fill(data: any): RWSModel<T> {
67
70
  for (const key in data) {
68
71
  if (data.hasOwnProperty(key)) {
69
72
 
@@ -90,7 +93,7 @@ class RWSModel<ChildClass> implements IModel {
90
93
  return RelationUtils.bindRelation(relatedModel);
91
94
  }
92
95
 
93
- public async _asyncFill(data: any, fullDataMode = false, allowRelations = true): Promise<ChildClass> {
96
+ public async _asyncFill(data: any, fullDataMode = false, allowRelations = true): Promise<T> {
94
97
  const collections_to_models: {[key: string]: any} = {};
95
98
  const timeSeriesIds = TimeSeriesUtils.getTimeSeriesModelFields(this);
96
99
 
@@ -191,10 +194,10 @@ class RWSModel<ChildClass> implements IModel {
191
194
  }
192
195
  }
193
196
 
194
- return this as any as ChildClass;
197
+ return this as any as T;
195
198
  }
196
199
 
197
- private getModelScalarFields(model: RWSModel<ChildClass>): string[] {
200
+ private getModelScalarFields(model: RWSModel<T>): string[] {
198
201
  return ModelUtils.getModelScalarFields(model);
199
202
  }
200
203
 
@@ -214,8 +217,38 @@ class RWSModel<ChildClass> implements IModel {
214
217
  return RelationUtils.getRelationManyMeta(model, classFields);
215
218
  }
216
219
 
217
- static paginate<ChildClass extends RWSModel<ChildClass>>(this: OpModelType<any>,pageParams: IPaginationParams, findParams?: FindByType): Promise<RWSModel<ChildClass>[]> {
218
- return PaginationUtils.paginate.bind(this)(pageParams, findParams);
220
+ public static async paginate<T extends RWSModel<T>>(
221
+ this: OpModelType<T>,
222
+ paginateParams: IPaginationParams,
223
+ findParams?: FindByType
224
+ ): Promise<T[]> {
225
+ const conditions = findParams?.conditions ?? {};
226
+ const ordering = findParams?.ordering ?? null;
227
+ const fields = findParams?.fields ?? null;
228
+ const allowRelations = findParams?.allowRelations ?? true;
229
+ const fullData = findParams?.fullData ?? false;
230
+
231
+ const collection = Reflect.get(this, '_collection');
232
+ this.checkForInclusionWithThrow(this.name);
233
+ try {
234
+ const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
235
+ if (dbData.length) {
236
+ const instanced: T[] = [];
237
+
238
+ for (const data of dbData) {
239
+ const inst: T = new (this as { new(): T })();
240
+ instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
241
+ }
242
+
243
+ return instanced;
244
+ }
245
+
246
+ return [];
247
+ } catch (rwsError: Error | any) {
248
+ console.error(rwsError);
249
+
250
+ throw rwsError;
251
+ }
219
252
  }
220
253
 
221
254
  public async toMongo(): Promise<any> {
@@ -274,7 +307,7 @@ class RWSModel<ChildClass> implements IModel {
274
307
  } else {
275
308
  this.preCreate();
276
309
 
277
- const isTimeSeries = this instanceof timeSeriesModel;
310
+ const isTimeSeries = false;//this instanceof timeSeriesModel;
278
311
 
279
312
  updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
280
313
 
@@ -339,8 +372,8 @@ class RWSModel<ChildClass> implements IModel {
339
372
  return sanitizedData;
340
373
  }
341
374
 
342
- public static async watchCollection<ChildClass extends RWSModel<ChildClass>>(
343
- this: OpModelType<ChildClass>,
375
+ public static async watchCollection<T extends RWSModel<T>>(
376
+ this: OpModelType<T>,
344
377
  preRun: () => void
345
378
  ){
346
379
  const collection = Reflect.get(this, '_collection');
@@ -348,10 +381,10 @@ class RWSModel<ChildClass> implements IModel {
348
381
  return await this.services.dbService.watchCollection(collection, preRun);
349
382
  }
350
383
 
351
- public static async findOneBy<ChildClass extends RWSModel<ChildClass>>(
352
- this: OpModelType<ChildClass>,
384
+ public static async findOneBy<T extends RWSModel<T>>(
385
+ this: OpModelType<T>,
353
386
  findParams?: FindByType
354
- ): Promise<ChildClass | null> {
387
+ ): Promise<T | null> {
355
388
  const conditions = findParams?.conditions ?? {};
356
389
  const ordering = findParams?.ordering ?? null;
357
390
  const fields = findParams?.fields ?? null;
@@ -366,18 +399,18 @@ class RWSModel<ChildClass> implements IModel {
366
399
 
367
400
 
368
401
  if (dbData) {
369
- const inst: ChildClass = new (this as { new(): ChildClass })();
402
+ const inst: T = new (this as { new(): T })();
370
403
  return await inst._asyncFill(dbData, fullData, allowRelations);
371
404
  }
372
405
 
373
406
  return null;
374
407
  }
375
408
 
376
- public static async find<ChildClass extends RWSModel<ChildClass>>(
377
- this: OpModelType<ChildClass>,
409
+ public static async find<T extends RWSModel<T>>(
410
+ this: OpModelType<T>,
378
411
  id: string,
379
412
  findParams: Omit<FindByType, 'conditions'> = null
380
- ): Promise<ChildClass | null> {
413
+ ): Promise<T | null> {
381
414
  const ordering = findParams?.ordering ?? null;
382
415
  const fields = findParams?.fields ?? null;
383
416
  const allowRelations = findParams?.allowRelations ?? true;
@@ -389,17 +422,17 @@ class RWSModel<ChildClass> implements IModel {
389
422
  const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
390
423
 
391
424
  if (dbData) {
392
- const inst: ChildClass = new (this as { new(): ChildClass })();
425
+ const inst: T = new (this as { new(): T })();
393
426
  return await inst._asyncFill(dbData, fullData, allowRelations);
394
427
  }
395
428
 
396
429
  return null;
397
430
  }
398
431
 
399
- public static async findBy<ChildClass extends RWSModel<ChildClass>>(
400
- this: OpModelType<ChildClass>,
432
+ public static async findBy<T extends RWSModel<T>>(
433
+ this: OpModelType<T>,
401
434
  findParams?: FindByType
402
- ): Promise<ChildClass[]> {
435
+ ): Promise<T[]> {
403
436
  const conditions = findParams?.conditions ?? {};
404
437
  const ordering = findParams?.ordering ?? null;
405
438
  const fields = findParams?.fields ?? null;
@@ -411,11 +444,11 @@ class RWSModel<ChildClass> implements IModel {
411
444
  try {
412
445
  const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering);
413
446
  if (dbData.length) {
414
- const instanced: ChildClass[] = [];
447
+ const instanced: T[] = [];
415
448
 
416
449
  for (const data of dbData) {
417
- const inst: ChildClass = new (this as { new(): ChildClass })();
418
- instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as ChildClass);
450
+ const inst: T = new (this as { new(): T })();
451
+ instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
419
452
  }
420
453
 
421
454
  return instanced;
@@ -429,8 +462,8 @@ class RWSModel<ChildClass> implements IModel {
429
462
  }
430
463
  }
431
464
 
432
- public static async delete<ChildClass extends RWSModel<ChildClass>>(
433
- this: OpModelType<ChildClass>,
465
+ public static async delete<T extends RWSModel<T>>(
466
+ this: OpModelType<T>,
434
467
  conditions: any
435
468
  ): Promise<void> {
436
469
  const collection = Reflect.get(this, '_collection');
@@ -438,7 +471,7 @@ class RWSModel<ChildClass> implements IModel {
438
471
  return await this.services.dbService.delete(collection, conditions);
439
472
  }
440
473
 
441
- public async delete<ChildClass extends RWSModel<ChildClass>>(): Promise<void> {
474
+ public async delete<T extends RWSModel<T>>(): Promise<void> {
442
475
  const collection = Reflect.get(this, '_collection');
443
476
  this.checkForInclusionWithThrow();
444
477
  return await this.dbService.delete(collection, {
@@ -1,19 +1,19 @@
1
1
  import { RWSModel } from './RWSModel';
2
2
  import { TrackType } from '../../decorators';
3
3
 
4
- export default class TimeSeriesModel<T> extends RWSModel<T> {
5
- @TrackType(Number) value: number;
4
+ // export default class TimeSeriesModel<T> extends RWSModel<T> {
5
+ // @TrackType(Number) value: number;
6
6
 
7
- @TrackType(Date) timestamp: Date;
7
+ // @TrackType(Date) timestamp: Date;
8
8
 
9
- @TrackType(Object)
10
- params: any;
9
+ // @TrackType(Object)
10
+ // params: any;
11
11
 
12
- constructor(data?: any) {
13
- super(data);
12
+ // constructor(data?: any) {
13
+ // super(data);
14
14
 
15
- if(!this.timestamp) {
16
- this.timestamp = new Date();
17
- }
18
- }
19
- }
15
+ // if(!this.timestamp) {
16
+ // this.timestamp = new Date();
17
+ // }
18
+ // }
19
+ // }
@@ -3,7 +3,7 @@ import { IRWSModelServices } from './interfaces/IRWSModelServices';
3
3
  import { OpModelType } from './interfaces/OpModelType';
4
4
  import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
5
5
  import { RWSModel } from './core/RWSModel';
6
- import TimeSeriesModel from './core/TimeSeriesModel';
6
+ // import TimeSeriesModel from './core/TimeSeriesModel';
7
7
  import { TrackType, IMetaOpts } from '../decorators';
8
8
 
9
9
  export {
@@ -14,7 +14,7 @@ export {
14
14
  RelOneMetaType,
15
15
  RelManyMetaType,
16
16
  RWSModel,
17
- TimeSeriesModel,
17
+ // TimeSeriesModel,
18
18
  TrackType,
19
19
  IMetaOpts
20
20
  };
File without changes
File without changes
@@ -4,11 +4,11 @@ import { IRWSModelServices } from './IRWSModelServices';
4
4
  import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
5
5
  import { DBService } from '../../services/DBService';
6
6
 
7
- // Forward declaration to avoid circular dependency
8
- type RWSModel<T> = any;
7
+ // Reference to the RWSModel class to avoid circular dependency
8
+ import type { RWSModel } from '../core/RWSModel';
9
9
 
10
- export interface OpModelType<ChildClass> {
11
- new(data?: any | null): ChildClass;
10
+ export interface OpModelType<T> {
11
+ new(data?: any | null): T;
12
12
  services: IRWSModelServices;
13
13
  name: string;
14
14
  _collection: string;
@@ -20,7 +20,7 @@ export interface OpModelType<ChildClass> {
20
20
  checkForInclusion: (className: string) => boolean;
21
21
  findOneBy<T extends RWSModel<T>>(
22
22
  this: OpModelType<T>,
23
- findParams: FindByType
23
+ findParams?: FindByType
24
24
  ): Promise<T | null>;
25
25
  find<T extends RWSModel<T>>(
26
26
  this: OpModelType<T>,
@@ -29,18 +29,18 @@ export interface OpModelType<ChildClass> {
29
29
  ): Promise<T | null>;
30
30
  findBy<T extends RWSModel<T>>(
31
31
  this: OpModelType<T>,
32
- findParams: FindByType
32
+ findParams?: FindByType
33
33
  ): Promise<T[]>;
34
34
  paginate<T extends RWSModel<T>>(
35
35
  this: OpModelType<T>,
36
36
  paginateParams?: IPaginationParams,
37
37
  findParams?: FindByType
38
38
  ): Promise<T[]>;
39
- delete<ChildClass extends RWSModel<ChildClass>>(
40
- this: OpModelType<ChildClass>,
39
+ delete<T extends RWSModel<T>>(
40
+ this: OpModelType<T>,
41
41
  conditions: any
42
42
  ): Promise<void>;
43
- create<T extends RWSModel<T>>(this: OpModelType<T>, data: T): Promise<T>;
43
+ create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
44
44
  getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
45
45
  getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
46
46
  getCollection(): string;
File without changes
File without changes
@@ -5,11 +5,11 @@ import { FindByType, IPaginationParams } from '../../types/FindParams';
5
5
 
6
6
  export class PaginationUtils {
7
7
 
8
- public static async paginate<ChildClass extends RWSModel<ChildClass>>(
9
- this: OpModelType<ChildClass>,
8
+ public static async paginate<T extends RWSModel<T>>(
9
+ this: OpModelType<T>,
10
10
  paginationParams: IPaginationParams = { page: 0, per_page: 50 },
11
11
  findParams: FindByType = {},
12
- ): Promise<ChildClass[]> {
12
+ ): Promise<T[]> {
13
13
  const conditions = findParams?.conditions ?? {};
14
14
  const ordering = findParams?.ordering ?? null;
15
15
  const fields = findParams?.fields ?? null;
@@ -21,11 +21,11 @@ export class PaginationUtils {
21
21
  try {
22
22
  const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginationParams);
23
23
  if (dbData.length) {
24
- const instanced: ChildClass[] = [];
24
+ const instanced: T[] = [];
25
25
 
26
26
  for (const data of dbData) {
27
- const inst: ChildClass = new (this as { new(): ChildClass })();
28
- instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as ChildClass);
27
+ const inst: T = new (this as { new(): T })();
28
+ instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
29
29
  }
30
30
 
31
31
  return instanced;
File without changes
File without changes
@@ -196,8 +196,9 @@ class DBService {
196
196
  }
197
197
 
198
198
  if(pagination){
199
- params.skip = pagination.page * pagination.per_page;
200
- params.take = pagination.per_page;
199
+ const perPage = pagination.per_page || 50;
200
+ params.skip = pagination.page * perPage;
201
+ params.take = perPage;
201
202
  }
202
203
 
203
204
  const retData = await this.getCollectionHandler(collection).findMany(params);
@@ -9,5 +9,5 @@ export type FindByType = {
9
9
 
10
10
  export interface IPaginationParams {
11
11
  page: number,
12
- per_page: number
13
- }
12
+ per_page?: number
13
+ }