@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,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;
File without changes
@@ -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
+ }
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.1.11",
4
+ "version": "2.2.1",
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};
@@ -1,28 +1,31 @@
1
- import {DBService, IDBClientCreate} from '../services/DBService';
2
- import { IRWSModel } from '../types/IRWSModel';
3
- import { IDbConfigHandler } from '../types/DbConfigHandler';
4
-
5
- import {TrackType, IMetaOpts } from '../decorators';
6
- import { FieldsHelper } from '../helper/FieldsHelper';
7
- import { FindByType } from '../types/FindParams';
8
-
9
- interface IModel{
10
- [key: string]: any;
11
- id: string | null;
12
- save: () => void;
13
- getCollection: () => string | null;
14
- configService?: IDbConfigHandler;
15
- dbService?: DBService;
16
- }
17
-
18
- interface IRWSModelServices {
19
- configService?: IDbConfigHandler
20
- dbService?: DBService
21
- }
22
-
23
- type RelationBindType = {
24
- connect: { id: string }
1
+ /**
2
+ * @deprecated This file is deprecated. Import from 'models' directory instead.
3
+ */
4
+
5
+ import {
6
+ IModel,
7
+ IRWSModelServices,
8
+ OpModelType,
9
+ RelationBindType,
10
+ RelOneMetaType,
11
+ RelManyMetaType,
12
+ RWSModel,
13
+ TrackType,
14
+ IMetaOpts
15
+ } from './index';
16
+
17
+ export {
18
+ IModel,
19
+ IRWSModelServices,
20
+ OpModelType,
21
+ RelationBindType,
22
+ RelOneMetaType,
23
+ RelManyMetaType,
24
+ RWSModel,
25
+ TrackType,
26
+ IMetaOpts
25
27
  };
28
+ <<<<<<< HEAD
26
29
 
27
30
  type RelOneMetaType<T extends IRWSModel> = {[key: string]: {required: boolean, key?: string, model: OpModelType<T>, hydrationField: string, foreignKey: string}};
28
31
  type RelManyMetaType<T extends IRWSModel> = {[key: string]: {key: string, inversionModel: OpModelType<T>, foreignKey: string}};
@@ -674,3 +677,5 @@ class RWSModel<ChildClass> implements IModel{
674
677
  export { IModel, TrackType, IMetaOpts, RWSModel };
675
678
 
676
679
 
680
+ =======
681
+ >>>>>>> 7c50786 (v2.2.0)