@rws-framework/db 2.4.5 → 3.0.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/.bin/add-v.sh +9 -9
- package/.bin/emerge.sh +10 -10
- package/.eslintrc.json +53 -53
- package/README.md +404 -404
- package/dist/decorators/IdType.d.ts +8 -0
- package/dist/decorators/IdType.js +10 -0
- package/dist/decorators/InverseRelation.d.ts +2 -2
- package/dist/decorators/InverseRelation.js +5 -1
- package/dist/decorators/InverseTimeSeries.d.ts +0 -0
- package/dist/decorators/InverseTimeSeries.js +0 -0
- package/dist/decorators/RWSCollection.d.ts +7 -0
- package/dist/decorators/RWSCollection.js +6 -0
- package/dist/decorators/Relation.d.ts +8 -6
- package/dist/decorators/Relation.js +8 -1
- package/dist/decorators/TrackType.d.ts +2 -14
- package/dist/decorators/TrackType.js +7 -0
- package/dist/decorators/TypeFunctions.d.ts +44 -0
- package/dist/decorators/TypeFunctions.js +174 -0
- package/dist/decorators/index.d.ts +4 -2
- package/dist/decorators/index.js +3 -1
- package/dist/helper/DbHelper.d.ts +76 -5
- package/dist/helper/DbHelper.js +93 -154
- package/dist/helper/FieldsHelper.d.ts +0 -0
- package/dist/helper/FieldsHelper.js +0 -0
- package/dist/helper/db/index.d.ts +9 -0
- package/dist/helper/db/index.js +18 -0
- package/dist/helper/db/relation-manager.d.ts +45 -0
- package/dist/helper/db/relation-manager.js +105 -0
- package/dist/helper/db/schema-generator.d.ts +37 -0
- package/dist/helper/db/schema-generator.js +243 -0
- package/dist/helper/db/type-converter.d.ts +22 -0
- package/dist/helper/db/type-converter.js +106 -0
- package/dist/helper/db/utils.d.ts +24 -0
- package/dist/helper/db/utils.js +99 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/models/TimeSeriesModel.d.ts +7 -7
- package/dist/models/TimeSeriesModel.js +33 -33
- package/dist/models/_model.d.ts +2 -2
- package/dist/models/_model.js +0 -0
- package/dist/models/core/RWSModel.d.ts +4 -1
- package/dist/models/core/RWSModel.js +6 -4
- package/dist/models/core/TimeSeriesModel.d.ts +0 -0
- package/dist/models/core/TimeSeriesModel.js +0 -0
- package/dist/models/index.d.ts +2 -2
- package/dist/models/index.js +0 -0
- package/dist/models/interfaces/IDbOpts.d.ts +17 -0
- package/dist/models/interfaces/IDbOpts.js +2 -0
- package/dist/models/interfaces/IIdOpts.d.ts +0 -0
- package/dist/models/interfaces/IIdOpts.js +1 -0
- package/dist/models/interfaces/IIdTypeOpts.d.ts +3 -0
- package/dist/models/interfaces/IIdTypeOpts.js +2 -0
- package/dist/models/interfaces/IModel.d.ts +0 -0
- package/dist/models/interfaces/IModel.js +0 -0
- package/dist/models/interfaces/IRWSModelServices.d.ts +0 -0
- package/dist/models/interfaces/IRWSModelServices.js +0 -0
- package/dist/models/interfaces/ITrackerOpts.d.ts +12 -0
- package/dist/models/interfaces/ITrackerOpts.js +2 -0
- package/dist/models/interfaces/OpModelType.d.ts +3 -0
- package/dist/models/interfaces/OpModelType.js +0 -0
- package/dist/models/types/RelationTypes.d.ts +0 -0
- package/dist/models/types/RelationTypes.js +0 -0
- package/dist/models/utils/ModelUtils.d.ts +0 -0
- package/dist/models/utils/ModelUtils.js +0 -0
- package/dist/models/utils/PaginationUtils.d.ts +0 -0
- package/dist/models/utils/PaginationUtils.js +0 -0
- package/dist/models/utils/RelationUtils.d.ts +1 -1
- package/dist/models/utils/RelationUtils.js +2 -2
- package/dist/models/utils/TimeSeriesUtils.d.ts +0 -0
- package/dist/models/utils/TimeSeriesUtils.js +0 -0
- package/dist/services/DBService.d.ts +0 -0
- package/dist/services/DBService.js +0 -0
- package/dist/types/DbConfigHandler.d.ts +1 -1
- package/dist/types/DbConfigHandler.js +0 -0
- package/dist/types/FindParams.d.ts +0 -0
- package/dist/types/FindParams.js +0 -0
- package/dist/types/IRWSModel.d.ts +1 -1
- package/dist/types/IRWSModel.js +0 -0
- package/dist/types/ITimeSeries.d.ts +0 -0
- package/dist/types/ITimeSeries.js +0 -0
- package/exec/console.js +110 -110
- package/exec/db.rws.webpack.config.js +168 -168
- package/exec/src/cli.ts +73 -75
- package/exec/tsconfig.json +32 -32
- package/exec/webpackFilters.js +17 -17
- package/package.json +36 -36
- package/src/decorators/IdType.ts +17 -0
- package/src/decorators/InverseRelation.ts +41 -37
- package/src/decorators/InverseTimeSeries.ts +21 -21
- package/src/decorators/RWSCollection.ts +45 -27
- package/src/decorators/Relation.ts +61 -48
- package/src/decorators/TrackType.ts +65 -69
- package/src/decorators/index.ts +16 -8
- package/src/empty.js +0 -0
- package/src/helper/DbHelper.ts +133 -223
- package/src/helper/FieldsHelper.ts +34 -34
- package/src/helper/db/index.ts +10 -0
- package/src/helper/db/relation-manager.ts +119 -0
- package/src/helper/db/schema-generator.ts +302 -0
- package/src/helper/db/type-converter.ts +119 -0
- package/src/helper/db/utils.ts +120 -0
- package/src/index.ts +47 -38
- package/src/models/_model.ts +29 -29
- package/src/models/core/RWSModel.ts +523 -520
- package/src/models/core/TimeSeriesModel.ts +19 -19
- package/src/models/index.ts +20 -20
- package/src/models/interfaces/IDbOpts.ts +17 -0
- package/src/models/interfaces/IIdTypeOpts.ts +4 -0
- package/src/models/interfaces/IModel.ts +12 -12
- package/src/models/interfaces/IRWSModelServices.ts +7 -7
- package/src/models/interfaces/ITrackerOpts.ts +13 -0
- package/src/models/interfaces/OpModelType.ts +52 -49
- package/src/models/types/RelationTypes.ts +25 -25
- package/src/models/utils/ModelUtils.ts +65 -65
- package/src/models/utils/PaginationUtils.ts +42 -42
- package/src/models/utils/RelationUtils.ts +76 -76
- package/src/models/utils/TimeSeriesUtils.ts +38 -38
- package/src/services/DBService.ts +277 -277
- package/src/types/DbConfigHandler.ts +17 -17
- package/src/types/FindParams.ts +13 -13
- package/src/types/IRWSModel.ts +2 -2
- package/src/types/ITimeSeries.ts +5 -5
- package/tsconfig.json +22 -22
|
@@ -1,277 +1,277 @@
|
|
|
1
|
-
import { PrismaClient } from '@prisma/client';
|
|
2
|
-
import { Collection, Db, MongoClient } from 'mongodb';
|
|
3
|
-
import {ITimeSeries} from '../types/ITimeSeries';
|
|
4
|
-
import { IModel } from '../models/interfaces/IModel';
|
|
5
|
-
import chalk from 'chalk';
|
|
6
|
-
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
7
|
-
import { IPaginationParams } from '../types/FindParams';
|
|
8
|
-
|
|
9
|
-
interface IDBClientCreate {
|
|
10
|
-
dbUrl?: string;
|
|
11
|
-
dbName?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
class DBService {
|
|
15
|
-
private client: PrismaClient;
|
|
16
|
-
private opts: IDBClientCreate = null;
|
|
17
|
-
private connected = false;
|
|
18
|
-
|
|
19
|
-
constructor(private configService: IDbConfigHandler){}
|
|
20
|
-
|
|
21
|
-
private connectToDB(opts: IDBClientCreate = null) {
|
|
22
|
-
if(opts){
|
|
23
|
-
this.opts = opts;
|
|
24
|
-
}else{
|
|
25
|
-
this.opts = {
|
|
26
|
-
dbUrl: this.configService.get('db_url'),
|
|
27
|
-
dbName: this.configService.get('db_name'),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if(!this.opts.dbUrl){
|
|
32
|
-
console.log(chalk.red('No database config set in @rws-framework/db'));
|
|
33
|
-
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
try{
|
|
38
|
-
this.client = new PrismaClient({
|
|
39
|
-
datasources: {
|
|
40
|
-
db: {
|
|
41
|
-
url: this.opts.dbUrl
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
this.connected = true;
|
|
47
|
-
} catch (e: Error | any){
|
|
48
|
-
console.error(e);
|
|
49
|
-
|
|
50
|
-
throw new Error('PRISMA CONNECTION ERROR');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
reconnect(opts: IDBClientCreate = null)
|
|
55
|
-
{
|
|
56
|
-
this.connectToDB(opts);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static baseClientConstruct(dbUrl: string): MongoClient
|
|
60
|
-
{
|
|
61
|
-
const client = new MongoClient(dbUrl);
|
|
62
|
-
|
|
63
|
-
return client;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public async createBaseMongoClient(): Promise<MongoClient>
|
|
67
|
-
{
|
|
68
|
-
const dbUrl = this.opts?.dbUrl || this.configService.get('db_url');
|
|
69
|
-
const client = DBService.baseClientConstruct(dbUrl);
|
|
70
|
-
|
|
71
|
-
await client.connect();
|
|
72
|
-
|
|
73
|
-
return client;
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async createBaseMongoClientDB(): Promise<[MongoClient, Db]>
|
|
78
|
-
{
|
|
79
|
-
const dbName = this.opts?.dbName || this.configService.get('db_name');
|
|
80
|
-
const client = await this.createBaseMongoClient();
|
|
81
|
-
return [client, client.db(dbName)];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public async cloneDatabase(source: string, target: string): Promise<void> {
|
|
85
|
-
const client = await this.createBaseMongoClient();
|
|
86
|
-
|
|
87
|
-
// Source and target DB
|
|
88
|
-
const sourceDb = client.db(source);
|
|
89
|
-
const targetDb = client.db(target);
|
|
90
|
-
|
|
91
|
-
// Get all collections from source DB
|
|
92
|
-
const collections = await sourceDb.listCollections().toArray();
|
|
93
|
-
|
|
94
|
-
// Loop over all collections and copy them to the target DB
|
|
95
|
-
for (const collection of collections) {
|
|
96
|
-
const docs = await sourceDb.collection(collection.name).find({}).toArray();
|
|
97
|
-
await targetDb.collection(collection.name).insertMany(docs);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
await client.close();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async watchCollection(collectionName: string, preRun: () => void): Promise<any>
|
|
104
|
-
{
|
|
105
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
106
|
-
const collection = db.collection(collectionName);
|
|
107
|
-
|
|
108
|
-
const changeStream = collection.watch();
|
|
109
|
-
return new Promise((resolve) => {
|
|
110
|
-
changeStream.on('change', (change) => {
|
|
111
|
-
resolve(change);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
preRun();
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async insert(data: any, collection: string, isTimeSeries: boolean = false) {
|
|
119
|
-
|
|
120
|
-
let result: any = data;
|
|
121
|
-
// Insert time-series data outside of the transaction
|
|
122
|
-
|
|
123
|
-
if(isTimeSeries){
|
|
124
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
125
|
-
const collectionHandler = db.collection(collection);
|
|
126
|
-
|
|
127
|
-
const insert = await collectionHandler.insertOne(data);
|
|
128
|
-
|
|
129
|
-
result = await this.findOneBy(collection, { id: insert.insertedId.toString() });
|
|
130
|
-
return result;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const prismaCollection = this.getCollectionHandler(collection);
|
|
134
|
-
|
|
135
|
-
result = await prismaCollection.create({ data });
|
|
136
|
-
|
|
137
|
-
return await this.findOneBy(collection, { id: result.id });
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
async update(data: any, collection: string): Promise<IModel>
|
|
141
|
-
{
|
|
142
|
-
const model_id: string = data.id;
|
|
143
|
-
delete data['id'];
|
|
144
|
-
|
|
145
|
-
const prismaCollection = this.getCollectionHandler(collection);
|
|
146
|
-
|
|
147
|
-
await prismaCollection.update({
|
|
148
|
-
where: {
|
|
149
|
-
id: model_id,
|
|
150
|
-
},
|
|
151
|
-
data: data,
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return await this.findOneBy(collection, { id: model_id });
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
async findOneBy(collection: string, conditions: any, fields: string[] | null = null, ordering: { [fieldName: string]: string } = null, allowRelations: boolean = true): Promise<IModel|null>
|
|
160
|
-
{
|
|
161
|
-
const params: any = { where: conditions };
|
|
162
|
-
|
|
163
|
-
if(fields){
|
|
164
|
-
params.select = {};
|
|
165
|
-
fields.forEach((fieldName: string) => {
|
|
166
|
-
params.select[fieldName] = true;
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if(ordering){
|
|
171
|
-
params.orderBy = ordering;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const retData = await this.getCollectionHandler(collection).findFirst(params);
|
|
175
|
-
|
|
176
|
-
return retData;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async delete(collection: string, conditions: any): Promise<void>
|
|
180
|
-
{
|
|
181
|
-
await this.getCollectionHandler(collection).deleteMany({ where: conditions });
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async findBy(
|
|
186
|
-
collection: string,
|
|
187
|
-
conditions: any,
|
|
188
|
-
fields: string[] | null = null,
|
|
189
|
-
ordering: { [fieldName: string]: string } = null,
|
|
190
|
-
pagination: IPaginationParams = null): Promise<IModel[]>
|
|
191
|
-
{
|
|
192
|
-
const params: any ={ where: conditions };
|
|
193
|
-
|
|
194
|
-
if(fields){
|
|
195
|
-
params.select = {};
|
|
196
|
-
fields.forEach((fieldName: string) => {
|
|
197
|
-
params.select[fieldName] = true;
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if(ordering){
|
|
202
|
-
params.orderBy = ordering;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if(pagination){
|
|
206
|
-
const perPage = pagination.per_page || 50;
|
|
207
|
-
params.skip = (pagination.page || 0) * perPage;
|
|
208
|
-
params.take = perPage;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const retData = await this.getCollectionHandler(collection).findMany(params);
|
|
212
|
-
|
|
213
|
-
return retData;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async collectionExists(collection_name: string): Promise<boolean>
|
|
217
|
-
{
|
|
218
|
-
const dbUrl = this.opts?.dbUrl || this.configService.get('db_url');
|
|
219
|
-
const client = new MongoClient(dbUrl);
|
|
220
|
-
|
|
221
|
-
try {
|
|
222
|
-
await client.connect();
|
|
223
|
-
|
|
224
|
-
const db = client.db(this.configService.get('db_name'));
|
|
225
|
-
|
|
226
|
-
const collections = await db.listCollections().toArray();
|
|
227
|
-
const existingCollectionNames = collections.map((collection) => collection.name);
|
|
228
|
-
|
|
229
|
-
return existingCollectionNames.includes(collection_name);
|
|
230
|
-
} catch (error) {
|
|
231
|
-
console.error('Error connecting to MongoDB:', error);
|
|
232
|
-
|
|
233
|
-
throw error;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>
|
|
238
|
-
{
|
|
239
|
-
try {
|
|
240
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
241
|
-
|
|
242
|
-
// Create a time series collection
|
|
243
|
-
const options = {
|
|
244
|
-
timeseries: {
|
|
245
|
-
timeField: 'timestamp', // Replace with your timestamp field
|
|
246
|
-
metaField: 'params' // Replace with your metadata field
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
await db.createCollection(collection_name, options); // Replace with your collection name
|
|
251
|
-
|
|
252
|
-
return db.collection(collection_name);
|
|
253
|
-
|
|
254
|
-
} catch (error) {
|
|
255
|
-
console.error('Error connecting to MongoDB:', error);
|
|
256
|
-
|
|
257
|
-
throw error;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
private getCollectionHandler(collection: string): any
|
|
262
|
-
{
|
|
263
|
-
if(!this.client || !this.connected){
|
|
264
|
-
this.connectToDB();
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return (this.client[collection as keyof PrismaClient] as any);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
private setOpts(opts: IDBClientCreate = null): this
|
|
271
|
-
{
|
|
272
|
-
this.opts = opts;
|
|
273
|
-
return this;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export { DBService, IDBClientCreate };
|
|
1
|
+
import { PrismaClient } from '@prisma/client';
|
|
2
|
+
import { Collection, Db, MongoClient } from 'mongodb';
|
|
3
|
+
import {ITimeSeries} from '../types/ITimeSeries';
|
|
4
|
+
import { IModel } from '../models/interfaces/IModel';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
7
|
+
import { IPaginationParams } from '../types/FindParams';
|
|
8
|
+
|
|
9
|
+
interface IDBClientCreate {
|
|
10
|
+
dbUrl?: string;
|
|
11
|
+
dbName?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class DBService {
|
|
15
|
+
private client: PrismaClient;
|
|
16
|
+
private opts: IDBClientCreate = null;
|
|
17
|
+
private connected = false;
|
|
18
|
+
|
|
19
|
+
constructor(private configService: IDbConfigHandler){}
|
|
20
|
+
|
|
21
|
+
private connectToDB(opts: IDBClientCreate = null) {
|
|
22
|
+
if(opts){
|
|
23
|
+
this.opts = opts;
|
|
24
|
+
}else{
|
|
25
|
+
this.opts = {
|
|
26
|
+
dbUrl: this.configService.get('db_url'),
|
|
27
|
+
dbName: this.configService.get('db_name'),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if(!this.opts.dbUrl){
|
|
32
|
+
console.log(chalk.red('No database config set in @rws-framework/db'));
|
|
33
|
+
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try{
|
|
38
|
+
this.client = new PrismaClient({
|
|
39
|
+
datasources: {
|
|
40
|
+
db: {
|
|
41
|
+
url: this.opts.dbUrl
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
this.connected = true;
|
|
47
|
+
} catch (e: Error | any){
|
|
48
|
+
console.error(e);
|
|
49
|
+
|
|
50
|
+
throw new Error('PRISMA CONNECTION ERROR');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
reconnect(opts: IDBClientCreate = null)
|
|
55
|
+
{
|
|
56
|
+
this.connectToDB(opts);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static baseClientConstruct(dbUrl: string): MongoClient
|
|
60
|
+
{
|
|
61
|
+
const client = new MongoClient(dbUrl);
|
|
62
|
+
|
|
63
|
+
return client;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async createBaseMongoClient(): Promise<MongoClient>
|
|
67
|
+
{
|
|
68
|
+
const dbUrl = this.opts?.dbUrl || this.configService.get('db_url');
|
|
69
|
+
const client = DBService.baseClientConstruct(dbUrl);
|
|
70
|
+
|
|
71
|
+
await client.connect();
|
|
72
|
+
|
|
73
|
+
return client;
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public async createBaseMongoClientDB(): Promise<[MongoClient, Db]>
|
|
78
|
+
{
|
|
79
|
+
const dbName = this.opts?.dbName || this.configService.get('db_name');
|
|
80
|
+
const client = await this.createBaseMongoClient();
|
|
81
|
+
return [client, client.db(dbName)];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async cloneDatabase(source: string, target: string): Promise<void> {
|
|
85
|
+
const client = await this.createBaseMongoClient();
|
|
86
|
+
|
|
87
|
+
// Source and target DB
|
|
88
|
+
const sourceDb = client.db(source);
|
|
89
|
+
const targetDb = client.db(target);
|
|
90
|
+
|
|
91
|
+
// Get all collections from source DB
|
|
92
|
+
const collections = await sourceDb.listCollections().toArray();
|
|
93
|
+
|
|
94
|
+
// Loop over all collections and copy them to the target DB
|
|
95
|
+
for (const collection of collections) {
|
|
96
|
+
const docs = await sourceDb.collection(collection.name).find({}).toArray();
|
|
97
|
+
await targetDb.collection(collection.name).insertMany(docs);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
await client.close();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async watchCollection(collectionName: string, preRun: () => void): Promise<any>
|
|
104
|
+
{
|
|
105
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
106
|
+
const collection = db.collection(collectionName);
|
|
107
|
+
|
|
108
|
+
const changeStream = collection.watch();
|
|
109
|
+
return new Promise((resolve) => {
|
|
110
|
+
changeStream.on('change', (change) => {
|
|
111
|
+
resolve(change);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
preRun();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async insert(data: any, collection: string, isTimeSeries: boolean = false) {
|
|
119
|
+
|
|
120
|
+
let result: any = data;
|
|
121
|
+
// Insert time-series data outside of the transaction
|
|
122
|
+
|
|
123
|
+
if(isTimeSeries){
|
|
124
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
125
|
+
const collectionHandler = db.collection(collection);
|
|
126
|
+
|
|
127
|
+
const insert = await collectionHandler.insertOne(data);
|
|
128
|
+
|
|
129
|
+
result = await this.findOneBy(collection, { id: insert.insertedId.toString() });
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const prismaCollection = this.getCollectionHandler(collection);
|
|
134
|
+
|
|
135
|
+
result = await prismaCollection.create({ data });
|
|
136
|
+
|
|
137
|
+
return await this.findOneBy(collection, { id: result.id });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async update(data: any, collection: string): Promise<IModel>
|
|
141
|
+
{
|
|
142
|
+
const model_id: string = data.id;
|
|
143
|
+
delete data['id'];
|
|
144
|
+
|
|
145
|
+
const prismaCollection = this.getCollectionHandler(collection);
|
|
146
|
+
|
|
147
|
+
await prismaCollection.update({
|
|
148
|
+
where: {
|
|
149
|
+
id: model_id,
|
|
150
|
+
},
|
|
151
|
+
data: data,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
return await this.findOneBy(collection, { id: model_id });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
async findOneBy(collection: string, conditions: any, fields: string[] | null = null, ordering: { [fieldName: string]: string } = null, allowRelations: boolean = true): Promise<IModel|null>
|
|
160
|
+
{
|
|
161
|
+
const params: any = { where: conditions };
|
|
162
|
+
|
|
163
|
+
if(fields){
|
|
164
|
+
params.select = {};
|
|
165
|
+
fields.forEach((fieldName: string) => {
|
|
166
|
+
params.select[fieldName] = true;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if(ordering){
|
|
171
|
+
params.orderBy = ordering;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const retData = await this.getCollectionHandler(collection).findFirst(params);
|
|
175
|
+
|
|
176
|
+
return retData;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async delete(collection: string, conditions: any): Promise<void>
|
|
180
|
+
{
|
|
181
|
+
await this.getCollectionHandler(collection).deleteMany({ where: conditions });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async findBy(
|
|
186
|
+
collection: string,
|
|
187
|
+
conditions: any,
|
|
188
|
+
fields: string[] | null = null,
|
|
189
|
+
ordering: { [fieldName: string]: string } = null,
|
|
190
|
+
pagination: IPaginationParams = null): Promise<IModel[]>
|
|
191
|
+
{
|
|
192
|
+
const params: any ={ where: conditions };
|
|
193
|
+
|
|
194
|
+
if(fields){
|
|
195
|
+
params.select = {};
|
|
196
|
+
fields.forEach((fieldName: string) => {
|
|
197
|
+
params.select[fieldName] = true;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if(ordering){
|
|
202
|
+
params.orderBy = ordering;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if(pagination){
|
|
206
|
+
const perPage = pagination.per_page || 50;
|
|
207
|
+
params.skip = (pagination.page || 0) * perPage;
|
|
208
|
+
params.take = perPage;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const retData = await this.getCollectionHandler(collection).findMany(params);
|
|
212
|
+
|
|
213
|
+
return retData;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async collectionExists(collection_name: string): Promise<boolean>
|
|
217
|
+
{
|
|
218
|
+
const dbUrl = this.opts?.dbUrl || this.configService.get('db_url');
|
|
219
|
+
const client = new MongoClient(dbUrl);
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
await client.connect();
|
|
223
|
+
|
|
224
|
+
const db = client.db(this.configService.get('db_name'));
|
|
225
|
+
|
|
226
|
+
const collections = await db.listCollections().toArray();
|
|
227
|
+
const existingCollectionNames = collections.map((collection) => collection.name);
|
|
228
|
+
|
|
229
|
+
return existingCollectionNames.includes(collection_name);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error('Error connecting to MongoDB:', error);
|
|
232
|
+
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>
|
|
238
|
+
{
|
|
239
|
+
try {
|
|
240
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
241
|
+
|
|
242
|
+
// Create a time series collection
|
|
243
|
+
const options = {
|
|
244
|
+
timeseries: {
|
|
245
|
+
timeField: 'timestamp', // Replace with your timestamp field
|
|
246
|
+
metaField: 'params' // Replace with your metadata field
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
await db.createCollection(collection_name, options); // Replace with your collection name
|
|
251
|
+
|
|
252
|
+
return db.collection(collection_name);
|
|
253
|
+
|
|
254
|
+
} catch (error) {
|
|
255
|
+
console.error('Error connecting to MongoDB:', error);
|
|
256
|
+
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private getCollectionHandler(collection: string): any
|
|
262
|
+
{
|
|
263
|
+
if(!this.client || !this.connected){
|
|
264
|
+
this.connectToDB();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return (this.client[collection as keyof PrismaClient] as any);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private setOpts(opts: IDBClientCreate = null): this
|
|
271
|
+
{
|
|
272
|
+
this.opts = opts;
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export { DBService, IDBClientCreate };
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { OpModelType } from "../models/interfaces/OpModelType";
|
|
2
|
-
|
|
3
|
-
export interface IDbConfigParams {
|
|
4
|
-
db_url?: string;
|
|
5
|
-
db_name?: string;
|
|
6
|
-
db_type?: 'mongodb' | 'mysql' | 'sqlite';
|
|
7
|
-
db_models?: OpModelType<any>[]
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface IdGeneratorOptions {
|
|
11
|
-
useUuid?: boolean; // dla MySQL
|
|
12
|
-
customType?: string; // dla custom typów
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface IDbConfigHandler {
|
|
16
|
-
get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K];
|
|
17
|
-
}
|
|
1
|
+
import { OpModelType } from "../models/interfaces/OpModelType";
|
|
2
|
+
|
|
3
|
+
export interface IDbConfigParams {
|
|
4
|
+
db_url?: string;
|
|
5
|
+
db_name?: string;
|
|
6
|
+
db_type?: 'mongodb' | 'mysql' | 'sqlite' | 'postgresql' | 'postgres';
|
|
7
|
+
db_models?: OpModelType<any>[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IdGeneratorOptions {
|
|
11
|
+
useUuid?: boolean; // dla MySQL
|
|
12
|
+
customType?: string; // dla custom typów
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface IDbConfigHandler {
|
|
16
|
+
get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K];
|
|
17
|
+
}
|
package/src/types/FindParams.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export type FindByType = {
|
|
2
|
-
conditions?: any
|
|
3
|
-
ordering?: { [fieldName: string]: string }
|
|
4
|
-
fields?: string[]
|
|
5
|
-
allowRelations?: boolean
|
|
6
|
-
fullData?: boolean
|
|
7
|
-
pagination?: IPaginationParams
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface IPaginationParams {
|
|
11
|
-
page: number,
|
|
12
|
-
per_page?: number
|
|
13
|
-
}
|
|
1
|
+
export type FindByType = {
|
|
2
|
+
conditions?: any
|
|
3
|
+
ordering?: { [fieldName: string]: string }
|
|
4
|
+
fields?: string[]
|
|
5
|
+
allowRelations?: boolean
|
|
6
|
+
fullData?: boolean
|
|
7
|
+
pagination?: IPaginationParams
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IPaginationParams {
|
|
11
|
+
page: number,
|
|
12
|
+
per_page?: number
|
|
13
|
+
}
|
package/src/types/IRWSModel.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export interface IRWSModel {
|
|
2
|
-
id?: string
|
|
1
|
+
export interface IRWSModel {
|
|
2
|
+
id?: string | number
|
|
3
3
|
}
|
package/src/types/ITimeSeries.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export interface ITimeSeries {
|
|
2
|
-
value: number,
|
|
3
|
-
timestamp?: Date;
|
|
4
|
-
params?: any;
|
|
5
|
-
time_tracker_id?: string
|
|
1
|
+
export interface ITimeSeries {
|
|
2
|
+
value: number,
|
|
3
|
+
timestamp?: Date;
|
|
4
|
+
params?: any;
|
|
5
|
+
time_tracker_id?: string
|
|
6
6
|
}
|