@rws-framework/db 2.2.10 → 2.3.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.
@@ -2,6 +2,7 @@ import 'reflect-metadata';
2
2
  import { OpModelType } from '../models/interfaces/OpModelType';
3
3
  interface ITrackerOpts {
4
4
  required?: boolean;
5
+ isArray?: boolean;
5
6
  relationField?: string;
6
7
  relatedToField?: string;
7
8
  relatedTo?: OpModelType<any>;
@@ -11,6 +12,8 @@ interface ITrackerOpts {
11
12
  interface IMetaOpts extends ITrackerOpts {
12
13
  type: any;
13
14
  tags: string[];
15
+ required: boolean;
16
+ isArray: boolean;
14
17
  }
15
18
  declare function TrackType(type: any, opts?: ITrackerOpts | null, tags?: string[]): (target: any, key: string) => void;
16
19
  export default TrackType;
@@ -4,11 +4,19 @@ require("reflect-metadata");
4
4
  function TrackType(type, opts = null, tags = []) {
5
5
  if (!opts) {
6
6
  opts = {
7
- required: false
7
+ required: false,
8
+ isArray: false
8
9
  };
9
10
  }
11
+ if (!(opts === null || opts === void 0 ? void 0 : opts.required)) {
12
+ opts.required = false;
13
+ }
14
+ if (!(opts === null || opts === void 0 ? void 0 : opts.isArray)) {
15
+ opts.isArray = false;
16
+ }
10
17
  const required = opts.required;
11
- const metaOpts = { type, tags, required };
18
+ const isArray = opts.isArray;
19
+ const metaOpts = { type, tags, required, isArray };
12
20
  if (opts.relatedToField && opts.relatedTo) {
13
21
  metaOpts.relatedToField = opts.relatedToField;
14
22
  metaOpts.relatedTo = opts.relatedTo;
@@ -1,8 +1,8 @@
1
1
  import { IDbConfigHandler } from '../types/DbConfigHandler';
2
- import { OpModelType } from '../models/_model';
2
+ import { IMetaOpts, OpModelType } from '../models/_model';
3
3
  import { DBService } from '../services/DBService';
4
4
  export declare class DbHelper {
5
5
  static installPrisma(configService: IDbConfigHandler, dbService: DBService, leaveFile?: boolean): Promise<void>;
6
6
  static generateModelSections(model: OpModelType<any>): Promise<string>;
7
- static toConfigCase(modelType: any): string;
7
+ static toConfigCase(modelType: IMetaOpts): string;
8
8
  }
@@ -69,7 +69,7 @@ class DbHelper {
69
69
  section += '\tid String @map("_id") @id @default(auto()) @db.ObjectId\n';
70
70
  for (const key in modelMetadatas) {
71
71
  const modelMetadata = modelMetadatas[key].metadata;
72
- const requiredString = modelMetadata.required ? '' : '?';
72
+ let requiredString = modelMetadata.required ? '' : '?';
73
73
  const annotationType = modelMetadatas[key].annotationType;
74
74
  if (key === 'id') {
75
75
  continue;
@@ -105,6 +105,9 @@ class DbHelper {
105
105
  }
106
106
  else if (annotationType === 'TrackType') {
107
107
  const tags = modelMetadata.tags.map((item) => '@' + item);
108
+ if (modelMetadata.isArray || modelMetadata.type.name === 'Array') {
109
+ requiredString = '';
110
+ }
108
111
  section += `\t${key} ${DbHelper.toConfigCase(modelMetadata)}${requiredString} ${tags.join(' ')}\n`;
109
112
  }
110
113
  }
@@ -113,22 +116,26 @@ class DbHelper {
113
116
  }
114
117
  static toConfigCase(modelType) {
115
118
  const type = modelType.type;
116
- const input = type.name;
119
+ let input = type.name;
117
120
  if (input == 'Number') {
118
- return 'Int';
121
+ input = 'Int';
119
122
  }
120
123
  if (input == 'Object') {
121
- return 'Json';
124
+ input = 'Json';
122
125
  }
123
126
  if (input == 'Date') {
124
- return 'DateTime';
127
+ input = 'DateTime';
125
128
  }
126
129
  if (input == 'Array') {
127
- return 'Json';
130
+ input = 'Json[]';
128
131
  }
129
132
  const firstChar = input.charAt(0).toUpperCase();
130
133
  const restOfString = input.slice(1);
131
- return firstChar + restOfString;
134
+ let resultField = firstChar + restOfString;
135
+ if (modelType.isArray) {
136
+ resultField += '[]';
137
+ }
138
+ return resultField;
132
139
  }
133
140
  }
134
141
  exports.DbHelper = DbHelper;
@@ -1,4 +1,4 @@
1
- import { Collection, MongoClient } from 'mongodb';
1
+ import { Collection, Db, MongoClient } from 'mongodb';
2
2
  import { ITimeSeries } from '../types/ITimeSeries';
3
3
  import { IModel } from '../models/interfaces/IModel';
4
4
  import { IDbConfigHandler } from '../types/DbConfigHandler';
@@ -16,8 +16,8 @@ declare class DBService {
16
16
  private connectToDB;
17
17
  reconnect(opts?: IDBClientCreate): void;
18
18
  static baseClientConstruct(dbUrl: string): MongoClient;
19
- private createBaseMongoClient;
20
- private createBaseMongoClientDB;
19
+ createBaseMongoClient(): Promise<MongoClient>;
20
+ createBaseMongoClientDB(): Promise<[MongoClient, Db]>;
21
21
  cloneDatabase(source: string, target: string): Promise<void>;
22
22
  watchCollection(collectionName: string, preRun: () => void): Promise<any>;
23
23
  insert(data: any, collection: string, isTimeSeries?: boolean): Promise<any>;
@@ -60,7 +60,7 @@ class DBService {
60
60
  var _a;
61
61
  const dbName = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbName) || this.configService.get('mongo_db');
62
62
  const client = await this.createBaseMongoClient();
63
- return client.db(dbName);
63
+ return [client, client.db(dbName)];
64
64
  }
65
65
  async cloneDatabase(source, target) {
66
66
  const client = await this.createBaseMongoClient();
@@ -77,7 +77,7 @@ class DBService {
77
77
  await client.close();
78
78
  }
79
79
  async watchCollection(collectionName, preRun) {
80
- const db = await this.createBaseMongoClientDB();
80
+ const [client, db] = await this.createBaseMongoClientDB();
81
81
  const collection = db.collection(collectionName);
82
82
  const changeStream = collection.watch();
83
83
  return new Promise((resolve) => {
@@ -91,7 +91,7 @@ class DBService {
91
91
  let result = data;
92
92
  // Insert time-series data outside of the transaction
93
93
  if (isTimeSeries) {
94
- const db = await this.createBaseMongoClientDB();
94
+ const [client, db] = await this.createBaseMongoClientDB();
95
95
  const collectionHandler = db.collection(collection);
96
96
  const insert = await collectionHandler.insertOne(data);
97
97
  result = await this.findOneBy(collection, { id: insert.insertedId.toString() });
@@ -168,7 +168,7 @@ class DBService {
168
168
  }
169
169
  async createTimeSeriesCollection(collection_name) {
170
170
  try {
171
- const db = await this.createBaseMongoClientDB();
171
+ const [client, db] = await this.createBaseMongoClientDB();
172
172
  // Create a time series collection
173
173
  const options = {
174
174
  timeseries: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "2.2.10",
4
+ "version": "2.3.1",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -3,7 +3,8 @@ import { OpModelType } from '../models/interfaces/OpModelType';
3
3
 
4
4
  interface ITrackerOpts{
5
5
  required?: boolean,
6
- relationField?: string
6
+ isArray?: boolean,
7
+ relationField?: string,
7
8
  relatedToField?: string,
8
9
  relatedTo?: OpModelType<any>,
9
10
  inversionModel?: OpModelType<any>,
@@ -12,19 +13,31 @@ interface ITrackerOpts{
12
13
 
13
14
  interface IMetaOpts extends ITrackerOpts{
14
15
  type: any,
15
- tags: string[]
16
+ tags: string[],
17
+ required: boolean,
18
+ isArray: boolean
16
19
  }
17
20
 
18
21
  function TrackType(type: any, opts: ITrackerOpts | null = null, tags: string[] = []) {
19
22
  if(!opts){
20
23
  opts = {
21
- required: false
24
+ required: false,
25
+ isArray: false
22
26
  };
23
27
  }
28
+
29
+ if(!opts?.required){
30
+ opts.required = false;
31
+ }
32
+
33
+ if(!opts?.isArray){
34
+ opts.isArray = false;
35
+ }
24
36
 
25
37
  const required = opts.required;
38
+ const isArray = opts.isArray;
26
39
 
27
- const metaOpts: IMetaOpts = {type, tags, required};
40
+ const metaOpts: IMetaOpts = {type, tags, required, isArray};
28
41
 
29
42
  if(opts.relatedToField && opts.relatedTo){
30
43
  metaOpts.relatedToField = opts.relatedToField;
@@ -92,7 +92,7 @@ export class DbHelper {
92
92
 
93
93
  for (const key in modelMetadatas) {
94
94
  const modelMetadata = modelMetadatas[key].metadata;
95
- const requiredString = modelMetadata.required ? '' : '?';
95
+ let requiredString = modelMetadata.required ? '' : '?';
96
96
  const annotationType: string = modelMetadatas[key].annotationType;
97
97
 
98
98
  if(key === 'id'){
@@ -131,7 +131,11 @@ export class DbHelper {
131
131
  } else if (annotationType === 'InverseTimeSeries'){
132
132
  section += `\t${key} String[] @db.ObjectId\n`;
133
133
  } else if (annotationType === 'TrackType'){
134
- const tags: string[] = modelMetadata.tags.map((item: string) => '@' + item);
134
+ const tags: string[] = modelMetadata.tags.map((item: string) => '@' + item);
135
+
136
+ if(modelMetadata.isArray || modelMetadata.type.name === 'Array'){
137
+ requiredString = '';
138
+ }
135
139
  section += `\t${key} ${DbHelper.toConfigCase(modelMetadata)}${requiredString} ${tags.join(' ')}\n`;
136
140
  }
137
141
  }
@@ -140,29 +144,35 @@ export class DbHelper {
140
144
  return section;
141
145
  }
142
146
 
143
- static toConfigCase(modelType: any): string {
147
+ static toConfigCase(modelType: IMetaOpts): string {
144
148
  const type = modelType.type;
145
- const input = type.name;
149
+ let input = type.name;
150
+
146
151
 
147
152
  if(input == 'Number'){
148
- return 'Int';
153
+ input = 'Int';
149
154
  }
150
155
 
151
156
  if(input == 'Object'){
152
- return 'Json';
157
+ input = 'Json';
153
158
  }
154
159
 
155
160
  if(input == 'Date'){
156
- return 'DateTime';
161
+ input = 'DateTime';
157
162
  }
158
163
 
159
164
  if(input == 'Array'){
160
- return 'Json';
165
+ input = 'Json[]';
161
166
  }
162
167
 
163
-
164
168
  const firstChar = input.charAt(0).toUpperCase();
165
169
  const restOfString = input.slice(1);
166
- return firstChar + restOfString;
170
+ let resultField = firstChar + restOfString;
171
+
172
+ if(modelType.isArray){
173
+ resultField += '[]';
174
+ }
175
+
176
+ return resultField;
167
177
  }
168
178
  }
@@ -63,7 +63,7 @@ class DBService {
63
63
  return client;
64
64
  }
65
65
 
66
- private async createBaseMongoClient(): Promise<MongoClient>
66
+ public async createBaseMongoClient(): Promise<MongoClient>
67
67
  {
68
68
  const dbUrl = this.opts?.dbUrl || this.configService.get('mongo_url');
69
69
  const client = DBService.baseClientConstruct(dbUrl);
@@ -74,11 +74,11 @@ class DBService {
74
74
 
75
75
  }
76
76
 
77
- private async createBaseMongoClientDB(): Promise<Db>
77
+ public async createBaseMongoClientDB(): Promise<[MongoClient, Db]>
78
78
  {
79
79
  const dbName = this.opts?.dbName || this.configService.get('mongo_db');
80
80
  const client = await this.createBaseMongoClient();
81
- return client.db(dbName);
81
+ return [client, client.db(dbName)];
82
82
  }
83
83
 
84
84
  public async cloneDatabase(source: string, target: string): Promise<void> {
@@ -102,7 +102,7 @@ class DBService {
102
102
 
103
103
  async watchCollection(collectionName: string, preRun: () => void): Promise<any>
104
104
  {
105
- const db = await this.createBaseMongoClientDB();
105
+ const [client, db] = await this.createBaseMongoClientDB();
106
106
  const collection = db.collection(collectionName);
107
107
 
108
108
  const changeStream = collection.watch();
@@ -121,7 +121,7 @@ class DBService {
121
121
  // Insert time-series data outside of the transaction
122
122
 
123
123
  if(isTimeSeries){
124
- const db = await this.createBaseMongoClientDB();
124
+ const [client, db] = await this.createBaseMongoClientDB();
125
125
  const collectionHandler = db.collection(collection);
126
126
 
127
127
  const insert = await collectionHandler.insertOne(data);
@@ -237,7 +237,7 @@ class DBService {
237
237
  async createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>
238
238
  {
239
239
  try {
240
- const db = await this.createBaseMongoClientDB();
240
+ const [client, db] = await this.createBaseMongoClientDB();
241
241
 
242
242
  // Create a time series collection
243
243
  const options = {