@rws-framework/db 2.2.10 → 2.3.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.
- package/dist/decorators/TrackType.d.ts +3 -0
- package/dist/decorators/TrackType.js +7 -2
- package/dist/helper/DbHelper.d.ts +2 -2
- package/dist/helper/DbHelper.js +14 -7
- package/dist/services/DBService.d.ts +3 -3
- package/dist/services/DBService.js +4 -4
- package/package.json +1 -1
- package/src/decorators/TrackType.ts +11 -4
- package/src/helper/DbHelper.ts +18 -10
- package/src/services/DBService.ts +6 -6
|
@@ -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,16 @@ 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
|
+
else if (opts === null || opts === void 0 ? void 0 : opts.isArray) {
|
|
12
|
+
opts.isArray = false;
|
|
13
|
+
}
|
|
10
14
|
const required = opts.required;
|
|
11
|
-
const
|
|
15
|
+
const isArray = opts.isArray;
|
|
16
|
+
const metaOpts = { type, tags, required, isArray };
|
|
12
17
|
if (opts.relatedToField && opts.relatedTo) {
|
|
13
18
|
metaOpts.relatedToField = opts.relatedToField;
|
|
14
19
|
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:
|
|
7
|
+
static toConfigCase(modelType: IMetaOpts): string;
|
|
8
8
|
}
|
package/dist/helper/DbHelper.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
119
|
+
let input = type.name;
|
|
117
120
|
if (input == 'Number') {
|
|
118
|
-
|
|
121
|
+
input = 'Int';
|
|
119
122
|
}
|
|
120
123
|
if (input == 'Object') {
|
|
121
|
-
|
|
124
|
+
input = 'Json';
|
|
122
125
|
}
|
|
123
126
|
if (input == 'Date') {
|
|
124
|
-
|
|
127
|
+
input = 'DateTime';
|
|
125
128
|
}
|
|
126
129
|
if (input == 'Array') {
|
|
127
|
-
|
|
130
|
+
input = 'Json[]';
|
|
128
131
|
}
|
|
129
132
|
const firstChar = input.charAt(0).toUpperCase();
|
|
130
133
|
const restOfString = input.slice(1);
|
|
131
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
@@ -3,7 +3,8 @@ import { OpModelType } from '../models/interfaces/OpModelType';
|
|
|
3
3
|
|
|
4
4
|
interface ITrackerOpts{
|
|
5
5
|
required?: boolean,
|
|
6
|
-
|
|
6
|
+
isArray?: boolean,
|
|
7
|
+
relationField?: string,
|
|
7
8
|
relatedToField?: string,
|
|
8
9
|
relatedTo?: OpModelType<any>,
|
|
9
10
|
inversionModel?: OpModelType<any>,
|
|
@@ -12,19 +13,25 @@ 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
|
};
|
|
27
|
+
}else if(opts?.isArray){
|
|
28
|
+
opts.isArray = false;
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
const required = opts.required;
|
|
32
|
+
const isArray = opts.isArray;
|
|
26
33
|
|
|
27
|
-
const metaOpts: IMetaOpts = {type, tags, required};
|
|
34
|
+
const metaOpts: IMetaOpts = {type, tags, required, isArray};
|
|
28
35
|
|
|
29
36
|
if(opts.relatedToField && opts.relatedTo){
|
|
30
37
|
metaOpts.relatedToField = opts.relatedToField;
|
package/src/helper/DbHelper.ts
CHANGED
|
@@ -92,7 +92,7 @@ export class DbHelper {
|
|
|
92
92
|
|
|
93
93
|
for (const key in modelMetadatas) {
|
|
94
94
|
const modelMetadata = modelMetadatas[key].metadata;
|
|
95
|
-
|
|
95
|
+
let requiredString = modelMetadata.required ? '' : '?';
|
|
96
96
|
const annotationType: string = modelMetadatas[key].annotationType;
|
|
97
97
|
|
|
98
98
|
if(key === 'id'){
|
|
@@ -131,7 +131,10 @@ 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
|
+
if(modelMetadata.isArray || modelMetadata.type.name === 'Array'){
|
|
136
|
+
requiredString = '';
|
|
137
|
+
}
|
|
135
138
|
section += `\t${key} ${DbHelper.toConfigCase(modelMetadata)}${requiredString} ${tags.join(' ')}\n`;
|
|
136
139
|
}
|
|
137
140
|
}
|
|
@@ -140,29 +143,34 @@ export class DbHelper {
|
|
|
140
143
|
return section;
|
|
141
144
|
}
|
|
142
145
|
|
|
143
|
-
static toConfigCase(modelType:
|
|
146
|
+
static toConfigCase(modelType: IMetaOpts): string {
|
|
144
147
|
const type = modelType.type;
|
|
145
|
-
|
|
148
|
+
let input = type.name;
|
|
146
149
|
|
|
147
150
|
if(input == 'Number'){
|
|
148
|
-
|
|
151
|
+
input = 'Int';
|
|
149
152
|
}
|
|
150
153
|
|
|
151
154
|
if(input == 'Object'){
|
|
152
|
-
|
|
155
|
+
input = 'Json';
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
if(input == 'Date'){
|
|
156
|
-
|
|
159
|
+
input = 'DateTime';
|
|
157
160
|
}
|
|
158
161
|
|
|
159
162
|
if(input == 'Array'){
|
|
160
|
-
|
|
163
|
+
input = 'Json[]';
|
|
161
164
|
}
|
|
162
165
|
|
|
163
|
-
|
|
164
166
|
const firstChar = input.charAt(0).toUpperCase();
|
|
165
167
|
const restOfString = input.slice(1);
|
|
166
|
-
|
|
168
|
+
let resultField = firstChar + restOfString;
|
|
169
|
+
|
|
170
|
+
if(modelType.isArray){
|
|
171
|
+
resultField += '[]';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return resultField;
|
|
167
175
|
}
|
|
168
176
|
}
|
|
@@ -63,7 +63,7 @@ class DBService {
|
|
|
63
63
|
return client;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
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
|
-
|
|
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 = {
|