@rws-framework/db 2.1.10 → 2.2.0

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 (48) hide show
  1. package/dist/decorators/TrackType.d.ts +1 -1
  2. package/dist/models/_model.d.ts +5 -116
  3. package/dist/models/_model.js +7 -499
  4. package/dist/models/core/RWSModel.d.ts +65 -0
  5. package/dist/models/core/RWSModel.js +375 -0
  6. package/dist/models/core/TimeSeriesModel.d.ts +7 -0
  7. package/dist/models/core/TimeSeriesModel.js +34 -0
  8. package/dist/models/index.d.ts +8 -0
  9. package/dist/models/index.js +12 -0
  10. package/dist/models/interfaces/IModel.d.ts +10 -0
  11. package/dist/models/interfaces/IModel.js +2 -0
  12. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -0
  13. package/dist/models/interfaces/IRWSModelServices.js +2 -0
  14. package/dist/models/interfaces/OpModelType.d.ts +32 -0
  15. package/dist/models/interfaces/OpModelType.js +2 -0
  16. package/dist/models/types/RelationTypes.d.ts +24 -0
  17. package/dist/models/types/RelationTypes.js +2 -0
  18. package/dist/models/utils/ModelUtils.d.ts +10 -0
  19. package/dist/models/utils/ModelUtils.js +56 -0
  20. package/dist/models/utils/PaginationUtils.d.ts +5 -0
  21. package/dist/models/utils/PaginationUtils.js +32 -0
  22. package/dist/models/utils/RelationUtils copy.d.ts +13 -0
  23. package/dist/models/utils/RelationUtils copy.js +65 -0
  24. package/dist/models/utils/RelationUtils.d.ts +14 -0
  25. package/dist/models/utils/RelationUtils.js +65 -0
  26. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -0
  27. package/dist/models/utils/TimeSeriesUtils.js +35 -0
  28. package/dist/services/DBService.d.ts +3 -2
  29. package/dist/services/DBService.js +5 -1
  30. package/dist/types/DbConfigHandler.d.ts +1 -1
  31. package/dist/types/FindParams.d.ts +5 -0
  32. package/package.json +1 -1
  33. package/src/decorators/TrackType.ts +2 -2
  34. package/src/models/_model.ts +26 -677
  35. package/src/models/core/RWSModel.ts +482 -0
  36. package/src/models/core/TimeSeriesModel.ts +19 -0
  37. package/src/models/index.ts +20 -0
  38. package/src/models/interfaces/IModel.ts +11 -0
  39. package/src/models/interfaces/IRWSModelServices.ts +7 -0
  40. package/src/models/interfaces/OpModelType.ts +49 -0
  41. package/src/models/types/RelationTypes.ts +25 -0
  42. package/src/models/utils/ModelUtils.ts +65 -0
  43. package/src/models/utils/PaginationUtils.ts +42 -0
  44. package/src/models/utils/RelationUtils.ts +76 -0
  45. package/src/models/utils/TimeSeriesUtils.ts +38 -0
  46. package/src/services/DBService.ts +14 -3
  47. package/src/types/DbConfigHandler.ts +2 -2
  48. package/src/types/FindParams.ts +10 -4
@@ -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,13 @@
1
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
+ import { IRWSModel } from '../../types/IRWSModel';
3
+ export declare class RelationUtils {
4
+ static getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
5
+ static getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
6
+ static bindRelation(relatedModel: any): {
7
+ connect: {
8
+ id: string;
9
+ };
10
+ };
11
+ static hasRelation(model: any, key: string): boolean;
12
+ static checkRelEnabled(model: any, key: string): boolean;
13
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelationUtils = void 0;
4
+ class RelationUtils {
5
+ static async getRelationOneMeta(model, classFields) {
6
+ const relIds = {};
7
+ const relationFields = classFields
8
+ .filter((item) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
9
+ .map((item) => item.split(':').at(-1));
10
+ for (const key of relationFields) {
11
+ const metadataKey = `Relation:${key}`;
12
+ const metadata = Reflect.getMetadata(metadataKey, model);
13
+ if (metadata && metadata.promise) {
14
+ const resolvedMetadata = await metadata.promise;
15
+ if (!relIds[key]) {
16
+ relIds[key] = {
17
+ key: resolvedMetadata.key,
18
+ required: resolvedMetadata.required,
19
+ model: resolvedMetadata.relatedTo,
20
+ hydrationField: resolvedMetadata.relationField,
21
+ foreignKey: resolvedMetadata.relatedToField
22
+ };
23
+ }
24
+ }
25
+ }
26
+ return relIds;
27
+ }
28
+ static async getRelationManyMeta(model, classFields) {
29
+ const relIds = {};
30
+ const inverseFields = classFields
31
+ .filter((item) => item.indexOf('InverseRelation') === 0)
32
+ .map((item) => item.split(':').at(-1));
33
+ for (const key of inverseFields) {
34
+ const metadataKey = `InverseRelation:${key}`;
35
+ const metadata = Reflect.getMetadata(metadataKey, model);
36
+ if (metadata && metadata.promise) {
37
+ const resolvedMetadata = await metadata.promise;
38
+ if (!relIds[key]) {
39
+ relIds[key] = {
40
+ key: resolvedMetadata.key,
41
+ inversionModel: resolvedMetadata.inversionModel,
42
+ foreignKey: resolvedMetadata.foreignKey
43
+ };
44
+ }
45
+ }
46
+ }
47
+ return relIds;
48
+ }
49
+ static bindRelation(relatedModel) {
50
+ return {
51
+ connect: {
52
+ id: relatedModel.id
53
+ }
54
+ };
55
+ }
56
+ static hasRelation(model, key) {
57
+ // Check if the property exists and is an object with an id property
58
+ return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
59
+ }
60
+ static checkRelEnabled(model, key) {
61
+ return Object.keys(model.constructor._RELATIONS).includes(key) &&
62
+ model.constructor._RELATIONS[key] === true;
63
+ }
64
+ }
65
+ exports.RelationUtils = RelationUtils;
@@ -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
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelationUtils = void 0;
4
+ class RelationUtils {
5
+ static async getRelationOneMeta(model, classFields) {
6
+ const relIds = {};
7
+ const relationFields = classFields
8
+ .filter((item) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
9
+ .map((item) => item.split(':').at(-1));
10
+ for (const key of relationFields) {
11
+ const metadataKey = `Relation:${key}`;
12
+ const metadata = Reflect.getMetadata(metadataKey, model);
13
+ if (metadata && metadata.promise) {
14
+ const resolvedMetadata = await metadata.promise;
15
+ if (!relIds[key]) {
16
+ relIds[key] = {
17
+ key: resolvedMetadata.key,
18
+ required: resolvedMetadata.required,
19
+ model: resolvedMetadata.relatedTo,
20
+ hydrationField: resolvedMetadata.relationField,
21
+ foreignKey: resolvedMetadata.relatedToField
22
+ };
23
+ }
24
+ }
25
+ }
26
+ return relIds;
27
+ }
28
+ static async getRelationManyMeta(model, classFields) {
29
+ const relIds = {};
30
+ const inverseFields = classFields
31
+ .filter((item) => item.indexOf('InverseRelation') === 0)
32
+ .map((item) => item.split(':').at(-1));
33
+ for (const key of inverseFields) {
34
+ const metadataKey = `InverseRelation:${key}`;
35
+ const metadata = Reflect.getMetadata(metadataKey, model);
36
+ if (metadata && metadata.promise) {
37
+ const resolvedMetadata = await metadata.promise;
38
+ if (!relIds[key]) {
39
+ relIds[key] = {
40
+ key: resolvedMetadata.key,
41
+ inversionModel: resolvedMetadata.inversionModel,
42
+ foreignKey: resolvedMetadata.foreignKey
43
+ };
44
+ }
45
+ }
46
+ }
47
+ return relIds;
48
+ }
49
+ static bindRelation(relatedModel) {
50
+ return {
51
+ connect: {
52
+ id: relatedModel.id
53
+ }
54
+ };
55
+ }
56
+ static hasRelation(model, key) {
57
+ // Check if the property exists and is an object with an id property
58
+ return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
59
+ }
60
+ static checkRelEnabled(model, key) {
61
+ return Object.keys(model.constructor._RELATIONS).includes(key) &&
62
+ model.constructor._RELATIONS[key] === true;
63
+ }
64
+ }
65
+ exports.RelationUtils = RelationUtils;
@@ -0,0 +1,11 @@
1
+ import { RWSModel } from "../_model";
2
+ export declare class TimeSeriesUtils {
3
+ static getTimeSeriesModelFields(model: RWSModel<any>): {
4
+ [key: string]: {
5
+ collection: string;
6
+ hydrationField: string;
7
+ ids: string[];
8
+ };
9
+ };
10
+ static checkTimeSeries(constructor: any): boolean;
11
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimeSeriesUtils = void 0;
4
+ class TimeSeriesUtils {
5
+ static getTimeSeriesModelFields(model) {
6
+ const timeSeriesIds = {};
7
+ for (const key in model) {
8
+ if (model.hasOwnProperty(key)) {
9
+ const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, model);
10
+ if (meta) {
11
+ if (!timeSeriesIds[key]) {
12
+ timeSeriesIds[key] = {
13
+ collection: meta.timeSeriesModel,
14
+ hydrationField: meta.hydrationField,
15
+ ids: model[key]
16
+ };
17
+ }
18
+ }
19
+ }
20
+ }
21
+ return timeSeriesIds;
22
+ }
23
+ static checkTimeSeries(constructor) {
24
+ const data = constructor.prototype;
25
+ for (const key in data) {
26
+ if (data.hasOwnProperty(key)) {
27
+ if (Reflect.getMetadata(`InverseTimeSeries:${key}`, constructor.prototype)) {
28
+ return true;
29
+ }
30
+ }
31
+ }
32
+ return false;
33
+ }
34
+ }
35
+ exports.TimeSeriesUtils = TimeSeriesUtils;
@@ -1,7 +1,8 @@
1
1
  import { Collection } from 'mongodb';
2
2
  import { ITimeSeries } from '../types/ITimeSeries';
3
- import { IModel } from '../models/_model';
3
+ import { IModel } from '../models/interfaces/IModel';
4
4
  import { IDbConfigHandler } from '../types/DbConfigHandler';
5
+ import { IPaginationParams } from 'src/types/FindParams';
5
6
  interface IDBClientCreate {
6
7
  dbUrl?: string;
7
8
  dbName?: string;
@@ -26,7 +27,7 @@ declare class DBService {
26
27
  delete(collection: string, conditions: any): Promise<void>;
27
28
  findBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
28
29
  [fieldName: string]: string;
29
- }, allowRelations?: boolean): Promise<IModel[]>;
30
+ }, pagination?: IPaginationParams): Promise<IModel[]>;
30
31
  collectionExists(collection_name: string): Promise<boolean>;
31
32
  createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>;
32
33
  private getCollectionHandler;
@@ -127,7 +127,7 @@ class DBService {
127
127
  await this.getCollectionHandler(collection).deleteMany({ where: conditions });
128
128
  return;
129
129
  }
130
- async findBy(collection, conditions, fields = null, ordering = null, allowRelations = true) {
130
+ async findBy(collection, conditions, fields = null, ordering = null, pagination = null) {
131
131
  const params = { where: conditions };
132
132
  if (fields) {
133
133
  params.select = {};
@@ -138,6 +138,10 @@ class DBService {
138
138
  if (ordering) {
139
139
  params.orderBy = ordering;
140
140
  }
141
+ if (pagination) {
142
+ params.skip = pagination.page * pagination.per_page;
143
+ params.take = pagination.per_page;
144
+ }
141
145
  const retData = await this.getCollectionHandler(collection).findMany(params);
142
146
  return retData;
143
147
  }
@@ -1,4 +1,4 @@
1
- import { OpModelType } from "../models/_model";
1
+ import { OpModelType } from "../models/interfaces/OpModelType";
2
2
  export interface IDbConfigParams {
3
3
  mongo_url?: string;
4
4
  mongo_db?: string;
@@ -6,4 +6,9 @@ export type FindByType = {
6
6
  fields?: string[];
7
7
  allowRelations?: boolean;
8
8
  fullData?: boolean;
9
+ pagination?: IPaginationParams;
9
10
  };
11
+ export interface IPaginationParams {
12
+ page: number;
13
+ per_page: number;
14
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "2.1.10",
4
+ "version": "2.2.0",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import 'reflect-metadata';
2
- import { OpModelType } from '../models/_model';
2
+ import { OpModelType } from '../models/interfaces/OpModelType';
3
3
 
4
4
  interface ITrackerOpts{
5
5
  required?: boolean,
@@ -53,4 +53,4 @@ function TrackType(type: any, opts: ITrackerOpts | null = null, tags: string[] =
53
53
  }
54
54
 
55
55
  export default TrackType;
56
- export {IMetaOpts, ITrackerOpts};
56
+ export {IMetaOpts, ITrackerOpts};