@rws-framework/db 2.4.0 → 2.4.2
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/InverseRelation.d.ts +12 -12
- package/dist/decorators/InverseRelation.js +24 -24
- package/dist/decorators/InverseTimeSeries.d.ts +8 -8
- package/dist/decorators/InverseTimeSeries.js +13 -13
- package/dist/decorators/RWSCollection.d.ts +12 -12
- package/dist/decorators/RWSCollection.js +16 -16
- package/dist/decorators/Relation.d.ts +19 -19
- package/dist/decorators/Relation.js +26 -26
- package/dist/decorators/TrackType.d.ts +20 -20
- package/dist/decorators/TrackType.js +41 -41
- package/dist/decorators/index.d.ts +5 -5
- package/dist/decorators/index.js +14 -14
- package/dist/helper/DbHelper.d.ts +12 -8
- package/dist/helper/DbHelper.js +185 -137
- package/dist/helper/FieldsHelper.d.ts +4 -4
- package/dist/helper/FieldsHelper.js +35 -35
- package/dist/index.d.ts +12 -12
- package/dist/index.js +19 -19
- package/dist/models/_model.d.ts +6 -6
- package/dist/models/_model.js +9 -9
- package/dist/models/core/RWSModel.d.ts +66 -66
- package/dist/models/core/RWSModel.js +400 -400
- package/dist/models/core/TimeSeriesModel.d.ts +1 -1
- package/dist/models/core/TimeSeriesModel.js +14 -14
- package/dist/models/index.d.ts +7 -7
- package/dist/models/index.js +8 -8
- package/dist/models/interfaces/IModel.d.ts +11 -11
- package/dist/models/interfaces/IModel.js +2 -2
- package/dist/models/interfaces/IRWSModelServices.d.ts +6 -6
- package/dist/models/interfaces/IRWSModelServices.js +2 -2
- package/dist/models/interfaces/OpModelType.d.ts +31 -31
- package/dist/models/interfaces/OpModelType.js +2 -2
- package/dist/models/types/RelationTypes.d.ts +24 -24
- package/dist/models/types/RelationTypes.js +2 -2
- package/dist/models/utils/ModelUtils.d.ts +10 -10
- package/dist/models/utils/ModelUtils.js +56 -56
- package/dist/models/utils/PaginationUtils.d.ts +5 -5
- package/dist/models/utils/PaginationUtils.js +32 -32
- package/dist/models/utils/RelationUtils.d.ts +14 -14
- package/dist/models/utils/RelationUtils.js +65 -65
- package/dist/models/utils/TimeSeriesUtils.d.ts +11 -11
- package/dist/models/utils/TimeSeriesUtils.js +35 -35
- package/dist/services/DBService.d.ts +37 -37
- package/dist/services/DBService.js +198 -198
- package/dist/types/DbConfigHandler.d.ts +14 -9
- package/dist/types/DbConfigHandler.js +2 -2
- package/dist/types/FindParams.d.ts +14 -14
- package/dist/types/FindParams.js +2 -2
- package/dist/types/IRWSModel.d.ts +3 -3
- package/dist/types/IRWSModel.js +2 -2
- package/dist/types/ITimeSeries.d.ts +6 -6
- package/dist/types/ITimeSeries.js +2 -2
- package/exec/src/cli.ts +1 -0
- package/package.json +1 -1
- package/src/helper/DbHelper.ts +77 -12
- package/src/index.ts +2 -1
- package/src/models/core/RWSModel.ts +3 -3
- package/src/models/interfaces/IModel.ts +1 -1
- package/src/models/utils/RelationUtils.ts +1 -1
- package/src/types/DbConfigHandler.ts +6 -1
|
@@ -1,11 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,35 +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
|
+
"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,37 +1,37 @@
|
|
|
1
|
-
import { Collection, Db, MongoClient } from 'mongodb';
|
|
2
|
-
import { ITimeSeries } from '../types/ITimeSeries';
|
|
3
|
-
import { IModel } from '../models/interfaces/IModel';
|
|
4
|
-
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
5
|
-
import { IPaginationParams } from '
|
|
6
|
-
interface IDBClientCreate {
|
|
7
|
-
dbUrl?: string;
|
|
8
|
-
dbName?: string;
|
|
9
|
-
}
|
|
10
|
-
declare class DBService {
|
|
11
|
-
private configService;
|
|
12
|
-
private client;
|
|
13
|
-
private opts;
|
|
14
|
-
private connected;
|
|
15
|
-
constructor(configService: IDbConfigHandler);
|
|
16
|
-
private connectToDB;
|
|
17
|
-
reconnect(opts?: IDBClientCreate): void;
|
|
18
|
-
static baseClientConstruct(dbUrl: string): MongoClient;
|
|
19
|
-
createBaseMongoClient(): Promise<MongoClient>;
|
|
20
|
-
createBaseMongoClientDB(): Promise<[MongoClient, Db]>;
|
|
21
|
-
cloneDatabase(source: string, target: string): Promise<void>;
|
|
22
|
-
watchCollection(collectionName: string, preRun: () => void): Promise<any>;
|
|
23
|
-
insert(data: any, collection: string, isTimeSeries?: boolean): Promise<any>;
|
|
24
|
-
update(data: any, collection: string): Promise<IModel>;
|
|
25
|
-
findOneBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
26
|
-
[fieldName: string]: string;
|
|
27
|
-
}, allowRelations?: boolean): Promise<IModel | null>;
|
|
28
|
-
delete(collection: string, conditions: any): Promise<void>;
|
|
29
|
-
findBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
30
|
-
[fieldName: string]: string;
|
|
31
|
-
}, pagination?: IPaginationParams): Promise<IModel[]>;
|
|
32
|
-
collectionExists(collection_name: string): Promise<boolean>;
|
|
33
|
-
createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>;
|
|
34
|
-
private getCollectionHandler;
|
|
35
|
-
private setOpts;
|
|
36
|
-
}
|
|
37
|
-
export { DBService, IDBClientCreate };
|
|
1
|
+
import { Collection, Db, MongoClient } from 'mongodb';
|
|
2
|
+
import { ITimeSeries } from '../types/ITimeSeries';
|
|
3
|
+
import { IModel } from '../models/interfaces/IModel';
|
|
4
|
+
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
5
|
+
import { IPaginationParams } from '../types/FindParams';
|
|
6
|
+
interface IDBClientCreate {
|
|
7
|
+
dbUrl?: string;
|
|
8
|
+
dbName?: string;
|
|
9
|
+
}
|
|
10
|
+
declare class DBService {
|
|
11
|
+
private configService;
|
|
12
|
+
private client;
|
|
13
|
+
private opts;
|
|
14
|
+
private connected;
|
|
15
|
+
constructor(configService: IDbConfigHandler);
|
|
16
|
+
private connectToDB;
|
|
17
|
+
reconnect(opts?: IDBClientCreate): void;
|
|
18
|
+
static baseClientConstruct(dbUrl: string): MongoClient;
|
|
19
|
+
createBaseMongoClient(): Promise<MongoClient>;
|
|
20
|
+
createBaseMongoClientDB(): Promise<[MongoClient, Db]>;
|
|
21
|
+
cloneDatabase(source: string, target: string): Promise<void>;
|
|
22
|
+
watchCollection(collectionName: string, preRun: () => void): Promise<any>;
|
|
23
|
+
insert(data: any, collection: string, isTimeSeries?: boolean): Promise<any>;
|
|
24
|
+
update(data: any, collection: string): Promise<IModel>;
|
|
25
|
+
findOneBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
26
|
+
[fieldName: string]: string;
|
|
27
|
+
}, allowRelations?: boolean): Promise<IModel | null>;
|
|
28
|
+
delete(collection: string, conditions: any): Promise<void>;
|
|
29
|
+
findBy(collection: string, conditions: any, fields?: string[] | null, ordering?: {
|
|
30
|
+
[fieldName: string]: string;
|
|
31
|
+
}, pagination?: IPaginationParams): Promise<IModel[]>;
|
|
32
|
+
collectionExists(collection_name: string): Promise<boolean>;
|
|
33
|
+
createTimeSeriesCollection(collection_name: string): Promise<Collection<ITimeSeries>>;
|
|
34
|
+
private getCollectionHandler;
|
|
35
|
+
private setOpts;
|
|
36
|
+
}
|
|
37
|
+
export { DBService, IDBClientCreate };
|
|
@@ -1,198 +1,198 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DBService = void 0;
|
|
7
|
-
const client_1 = require("@prisma/client");
|
|
8
|
-
const mongodb_1 = require("mongodb");
|
|
9
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
-
class DBService {
|
|
11
|
-
constructor(configService) {
|
|
12
|
-
this.configService = configService;
|
|
13
|
-
this.opts = null;
|
|
14
|
-
this.connected = false;
|
|
15
|
-
}
|
|
16
|
-
connectToDB(opts = null) {
|
|
17
|
-
if (opts) {
|
|
18
|
-
this.opts = opts;
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
this.opts = {
|
|
22
|
-
dbUrl: this.configService.get('
|
|
23
|
-
dbName: this.configService.get('
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!this.opts.dbUrl) {
|
|
27
|
-
console.log(chalk_1.default.red('No database config set in @rws-framework/db'));
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
this.client = new client_1.PrismaClient({
|
|
32
|
-
datasources: {
|
|
33
|
-
db: {
|
|
34
|
-
url: this.opts.dbUrl
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
this.connected = true;
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
console.error(e);
|
|
42
|
-
throw new Error('PRISMA CONNECTION ERROR');
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
reconnect(opts = null) {
|
|
46
|
-
this.connectToDB(opts);
|
|
47
|
-
}
|
|
48
|
-
static baseClientConstruct(dbUrl) {
|
|
49
|
-
const client = new mongodb_1.MongoClient(dbUrl);
|
|
50
|
-
return client;
|
|
51
|
-
}
|
|
52
|
-
async createBaseMongoClient() {
|
|
53
|
-
var _a;
|
|
54
|
-
const dbUrl = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbUrl) || this.configService.get('
|
|
55
|
-
const client = DBService.baseClientConstruct(dbUrl);
|
|
56
|
-
await client.connect();
|
|
57
|
-
return client;
|
|
58
|
-
}
|
|
59
|
-
async createBaseMongoClientDB() {
|
|
60
|
-
var _a;
|
|
61
|
-
const dbName = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbName) || this.configService.get('
|
|
62
|
-
const client = await this.createBaseMongoClient();
|
|
63
|
-
return [client, client.db(dbName)];
|
|
64
|
-
}
|
|
65
|
-
async cloneDatabase(source, target) {
|
|
66
|
-
const client = await this.createBaseMongoClient();
|
|
67
|
-
// Source and target DB
|
|
68
|
-
const sourceDb = client.db(source);
|
|
69
|
-
const targetDb = client.db(target);
|
|
70
|
-
// Get all collections from source DB
|
|
71
|
-
const collections = await sourceDb.listCollections().toArray();
|
|
72
|
-
// Loop over all collections and copy them to the target DB
|
|
73
|
-
for (const collection of collections) {
|
|
74
|
-
const docs = await sourceDb.collection(collection.name).find({}).toArray();
|
|
75
|
-
await targetDb.collection(collection.name).insertMany(docs);
|
|
76
|
-
}
|
|
77
|
-
await client.close();
|
|
78
|
-
}
|
|
79
|
-
async watchCollection(collectionName, preRun) {
|
|
80
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
81
|
-
const collection = db.collection(collectionName);
|
|
82
|
-
const changeStream = collection.watch();
|
|
83
|
-
return new Promise((resolve) => {
|
|
84
|
-
changeStream.on('change', (change) => {
|
|
85
|
-
resolve(change);
|
|
86
|
-
});
|
|
87
|
-
preRun();
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async insert(data, collection, isTimeSeries = false) {
|
|
91
|
-
let result = data;
|
|
92
|
-
// Insert time-series data outside of the transaction
|
|
93
|
-
if (isTimeSeries) {
|
|
94
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
95
|
-
const collectionHandler = db.collection(collection);
|
|
96
|
-
const insert = await collectionHandler.insertOne(data);
|
|
97
|
-
result = await this.findOneBy(collection, { id: insert.insertedId.toString() });
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
const prismaCollection = this.getCollectionHandler(collection);
|
|
101
|
-
result = await prismaCollection.create({ data });
|
|
102
|
-
return await this.findOneBy(collection, { id: result.id });
|
|
103
|
-
}
|
|
104
|
-
async update(data, collection) {
|
|
105
|
-
const model_id = data.id;
|
|
106
|
-
delete data['id'];
|
|
107
|
-
const prismaCollection = this.getCollectionHandler(collection);
|
|
108
|
-
await prismaCollection.update({
|
|
109
|
-
where: {
|
|
110
|
-
id: model_id,
|
|
111
|
-
},
|
|
112
|
-
data: data,
|
|
113
|
-
});
|
|
114
|
-
return await this.findOneBy(collection, { id: model_id });
|
|
115
|
-
}
|
|
116
|
-
async findOneBy(collection, conditions, fields = null, ordering = null, allowRelations = true) {
|
|
117
|
-
const params = { where: conditions };
|
|
118
|
-
if (fields) {
|
|
119
|
-
params.select = {};
|
|
120
|
-
fields.forEach((fieldName) => {
|
|
121
|
-
params.select[fieldName] = true;
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
if (ordering) {
|
|
125
|
-
params.orderBy = ordering;
|
|
126
|
-
}
|
|
127
|
-
const retData = await this.getCollectionHandler(collection).findFirst(params);
|
|
128
|
-
return retData;
|
|
129
|
-
}
|
|
130
|
-
async delete(collection, conditions) {
|
|
131
|
-
await this.getCollectionHandler(collection).deleteMany({ where: conditions });
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
async findBy(collection, conditions, fields = null, ordering = null, pagination = null) {
|
|
135
|
-
const params = { where: conditions };
|
|
136
|
-
if (fields) {
|
|
137
|
-
params.select = {};
|
|
138
|
-
fields.forEach((fieldName) => {
|
|
139
|
-
params.select[fieldName] = true;
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
if (ordering) {
|
|
143
|
-
params.orderBy = ordering;
|
|
144
|
-
}
|
|
145
|
-
if (pagination) {
|
|
146
|
-
const perPage = pagination.per_page || 50;
|
|
147
|
-
params.skip = (pagination.page || 0) * perPage;
|
|
148
|
-
params.take = perPage;
|
|
149
|
-
}
|
|
150
|
-
const retData = await this.getCollectionHandler(collection).findMany(params);
|
|
151
|
-
return retData;
|
|
152
|
-
}
|
|
153
|
-
async collectionExists(collection_name) {
|
|
154
|
-
var _a;
|
|
155
|
-
const dbUrl = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbUrl) || this.configService.get('
|
|
156
|
-
const client = new mongodb_1.MongoClient(dbUrl);
|
|
157
|
-
try {
|
|
158
|
-
await client.connect();
|
|
159
|
-
const db = client.db(this.configService.get('
|
|
160
|
-
const collections = await db.listCollections().toArray();
|
|
161
|
-
const existingCollectionNames = collections.map((collection) => collection.name);
|
|
162
|
-
return existingCollectionNames.includes(collection_name);
|
|
163
|
-
}
|
|
164
|
-
catch (error) {
|
|
165
|
-
console.error('Error connecting to MongoDB:', error);
|
|
166
|
-
throw error;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
async createTimeSeriesCollection(collection_name) {
|
|
170
|
-
try {
|
|
171
|
-
const [client, db] = await this.createBaseMongoClientDB();
|
|
172
|
-
// Create a time series collection
|
|
173
|
-
const options = {
|
|
174
|
-
timeseries: {
|
|
175
|
-
timeField: 'timestamp', // Replace with your timestamp field
|
|
176
|
-
metaField: 'params' // Replace with your metadata field
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
await db.createCollection(collection_name, options); // Replace with your collection name
|
|
180
|
-
return db.collection(collection_name);
|
|
181
|
-
}
|
|
182
|
-
catch (error) {
|
|
183
|
-
console.error('Error connecting to MongoDB:', error);
|
|
184
|
-
throw error;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
getCollectionHandler(collection) {
|
|
188
|
-
if (!this.client || !this.connected) {
|
|
189
|
-
this.connectToDB();
|
|
190
|
-
}
|
|
191
|
-
return this.client[collection];
|
|
192
|
-
}
|
|
193
|
-
setOpts(opts = null) {
|
|
194
|
-
this.opts = opts;
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
exports.DBService = DBService;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DBService = void 0;
|
|
7
|
+
const client_1 = require("@prisma/client");
|
|
8
|
+
const mongodb_1 = require("mongodb");
|
|
9
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
+
class DBService {
|
|
11
|
+
constructor(configService) {
|
|
12
|
+
this.configService = configService;
|
|
13
|
+
this.opts = null;
|
|
14
|
+
this.connected = false;
|
|
15
|
+
}
|
|
16
|
+
connectToDB(opts = null) {
|
|
17
|
+
if (opts) {
|
|
18
|
+
this.opts = opts;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.opts = {
|
|
22
|
+
dbUrl: this.configService.get('db_url'),
|
|
23
|
+
dbName: this.configService.get('db_name'),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (!this.opts.dbUrl) {
|
|
27
|
+
console.log(chalk_1.default.red('No database config set in @rws-framework/db'));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
this.client = new client_1.PrismaClient({
|
|
32
|
+
datasources: {
|
|
33
|
+
db: {
|
|
34
|
+
url: this.opts.dbUrl
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
this.connected = true;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
console.error(e);
|
|
42
|
+
throw new Error('PRISMA CONNECTION ERROR');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
reconnect(opts = null) {
|
|
46
|
+
this.connectToDB(opts);
|
|
47
|
+
}
|
|
48
|
+
static baseClientConstruct(dbUrl) {
|
|
49
|
+
const client = new mongodb_1.MongoClient(dbUrl);
|
|
50
|
+
return client;
|
|
51
|
+
}
|
|
52
|
+
async createBaseMongoClient() {
|
|
53
|
+
var _a;
|
|
54
|
+
const dbUrl = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbUrl) || this.configService.get('db_url');
|
|
55
|
+
const client = DBService.baseClientConstruct(dbUrl);
|
|
56
|
+
await client.connect();
|
|
57
|
+
return client;
|
|
58
|
+
}
|
|
59
|
+
async createBaseMongoClientDB() {
|
|
60
|
+
var _a;
|
|
61
|
+
const dbName = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbName) || this.configService.get('db_name');
|
|
62
|
+
const client = await this.createBaseMongoClient();
|
|
63
|
+
return [client, client.db(dbName)];
|
|
64
|
+
}
|
|
65
|
+
async cloneDatabase(source, target) {
|
|
66
|
+
const client = await this.createBaseMongoClient();
|
|
67
|
+
// Source and target DB
|
|
68
|
+
const sourceDb = client.db(source);
|
|
69
|
+
const targetDb = client.db(target);
|
|
70
|
+
// Get all collections from source DB
|
|
71
|
+
const collections = await sourceDb.listCollections().toArray();
|
|
72
|
+
// Loop over all collections and copy them to the target DB
|
|
73
|
+
for (const collection of collections) {
|
|
74
|
+
const docs = await sourceDb.collection(collection.name).find({}).toArray();
|
|
75
|
+
await targetDb.collection(collection.name).insertMany(docs);
|
|
76
|
+
}
|
|
77
|
+
await client.close();
|
|
78
|
+
}
|
|
79
|
+
async watchCollection(collectionName, preRun) {
|
|
80
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
81
|
+
const collection = db.collection(collectionName);
|
|
82
|
+
const changeStream = collection.watch();
|
|
83
|
+
return new Promise((resolve) => {
|
|
84
|
+
changeStream.on('change', (change) => {
|
|
85
|
+
resolve(change);
|
|
86
|
+
});
|
|
87
|
+
preRun();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async insert(data, collection, isTimeSeries = false) {
|
|
91
|
+
let result = data;
|
|
92
|
+
// Insert time-series data outside of the transaction
|
|
93
|
+
if (isTimeSeries) {
|
|
94
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
95
|
+
const collectionHandler = db.collection(collection);
|
|
96
|
+
const insert = await collectionHandler.insertOne(data);
|
|
97
|
+
result = await this.findOneBy(collection, { id: insert.insertedId.toString() });
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
const prismaCollection = this.getCollectionHandler(collection);
|
|
101
|
+
result = await prismaCollection.create({ data });
|
|
102
|
+
return await this.findOneBy(collection, { id: result.id });
|
|
103
|
+
}
|
|
104
|
+
async update(data, collection) {
|
|
105
|
+
const model_id = data.id;
|
|
106
|
+
delete data['id'];
|
|
107
|
+
const prismaCollection = this.getCollectionHandler(collection);
|
|
108
|
+
await prismaCollection.update({
|
|
109
|
+
where: {
|
|
110
|
+
id: model_id,
|
|
111
|
+
},
|
|
112
|
+
data: data,
|
|
113
|
+
});
|
|
114
|
+
return await this.findOneBy(collection, { id: model_id });
|
|
115
|
+
}
|
|
116
|
+
async findOneBy(collection, conditions, fields = null, ordering = null, allowRelations = true) {
|
|
117
|
+
const params = { where: conditions };
|
|
118
|
+
if (fields) {
|
|
119
|
+
params.select = {};
|
|
120
|
+
fields.forEach((fieldName) => {
|
|
121
|
+
params.select[fieldName] = true;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (ordering) {
|
|
125
|
+
params.orderBy = ordering;
|
|
126
|
+
}
|
|
127
|
+
const retData = await this.getCollectionHandler(collection).findFirst(params);
|
|
128
|
+
return retData;
|
|
129
|
+
}
|
|
130
|
+
async delete(collection, conditions) {
|
|
131
|
+
await this.getCollectionHandler(collection).deleteMany({ where: conditions });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
async findBy(collection, conditions, fields = null, ordering = null, pagination = null) {
|
|
135
|
+
const params = { where: conditions };
|
|
136
|
+
if (fields) {
|
|
137
|
+
params.select = {};
|
|
138
|
+
fields.forEach((fieldName) => {
|
|
139
|
+
params.select[fieldName] = true;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (ordering) {
|
|
143
|
+
params.orderBy = ordering;
|
|
144
|
+
}
|
|
145
|
+
if (pagination) {
|
|
146
|
+
const perPage = pagination.per_page || 50;
|
|
147
|
+
params.skip = (pagination.page || 0) * perPage;
|
|
148
|
+
params.take = perPage;
|
|
149
|
+
}
|
|
150
|
+
const retData = await this.getCollectionHandler(collection).findMany(params);
|
|
151
|
+
return retData;
|
|
152
|
+
}
|
|
153
|
+
async collectionExists(collection_name) {
|
|
154
|
+
var _a;
|
|
155
|
+
const dbUrl = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbUrl) || this.configService.get('db_url');
|
|
156
|
+
const client = new mongodb_1.MongoClient(dbUrl);
|
|
157
|
+
try {
|
|
158
|
+
await client.connect();
|
|
159
|
+
const db = client.db(this.configService.get('db_name'));
|
|
160
|
+
const collections = await db.listCollections().toArray();
|
|
161
|
+
const existingCollectionNames = collections.map((collection) => collection.name);
|
|
162
|
+
return existingCollectionNames.includes(collection_name);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
console.error('Error connecting to MongoDB:', error);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async createTimeSeriesCollection(collection_name) {
|
|
170
|
+
try {
|
|
171
|
+
const [client, db] = await this.createBaseMongoClientDB();
|
|
172
|
+
// Create a time series collection
|
|
173
|
+
const options = {
|
|
174
|
+
timeseries: {
|
|
175
|
+
timeField: 'timestamp', // Replace with your timestamp field
|
|
176
|
+
metaField: 'params' // Replace with your metadata field
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
await db.createCollection(collection_name, options); // Replace with your collection name
|
|
180
|
+
return db.collection(collection_name);
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
console.error('Error connecting to MongoDB:', error);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
getCollectionHandler(collection) {
|
|
188
|
+
if (!this.client || !this.connected) {
|
|
189
|
+
this.connectToDB();
|
|
190
|
+
}
|
|
191
|
+
return this.client[collection];
|
|
192
|
+
}
|
|
193
|
+
setOpts(opts = null) {
|
|
194
|
+
this.opts = opts;
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.DBService = DBService;
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { OpModelType } from "../models/interfaces/OpModelType";
|
|
2
|
-
export interface IDbConfigParams {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { OpModelType } from "../models/interfaces/OpModelType";
|
|
2
|
+
export interface IDbConfigParams {
|
|
3
|
+
db_url?: string;
|
|
4
|
+
db_name?: string;
|
|
5
|
+
db_type?: 'mongodb' | 'mysql' | 'sqlite';
|
|
6
|
+
db_models?: OpModelType<any>[];
|
|
7
|
+
}
|
|
8
|
+
export interface IdGeneratorOptions {
|
|
9
|
+
useUuid?: boolean;
|
|
10
|
+
customType?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface IDbConfigHandler {
|
|
13
|
+
get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K];
|
|
14
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export type FindByType = {
|
|
2
|
-
conditions?: any;
|
|
3
|
-
ordering?: {
|
|
4
|
-
[fieldName: string]: string;
|
|
5
|
-
};
|
|
6
|
-
fields?: string[];
|
|
7
|
-
allowRelations?: boolean;
|
|
8
|
-
fullData?: boolean;
|
|
9
|
-
pagination?: IPaginationParams;
|
|
10
|
-
};
|
|
11
|
-
export interface IPaginationParams {
|
|
12
|
-
page: number;
|
|
13
|
-
per_page?: number;
|
|
14
|
-
}
|
|
1
|
+
export type FindByType = {
|
|
2
|
+
conditions?: any;
|
|
3
|
+
ordering?: {
|
|
4
|
+
[fieldName: string]: string;
|
|
5
|
+
};
|
|
6
|
+
fields?: string[];
|
|
7
|
+
allowRelations?: boolean;
|
|
8
|
+
fullData?: boolean;
|
|
9
|
+
pagination?: IPaginationParams;
|
|
10
|
+
};
|
|
11
|
+
export interface IPaginationParams {
|
|
12
|
+
page: number;
|
|
13
|
+
per_page?: number;
|
|
14
|
+
}
|
package/dist/types/FindParams.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export interface IRWSModel {
|
|
2
|
-
id?: string;
|
|
3
|
-
}
|
|
1
|
+
export interface IRWSModel {
|
|
2
|
+
id?: string;
|
|
3
|
+
}
|
package/dist/types/IRWSModel.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export interface ITimeSeries {
|
|
2
|
-
value: number;
|
|
3
|
-
timestamp?: Date;
|
|
4
|
-
params?: any;
|
|
5
|
-
time_tracker_id?: string;
|
|
6
|
-
}
|
|
1
|
+
export interface ITimeSeries {
|
|
2
|
+
value: number;
|
|
3
|
+
timestamp?: Date;
|
|
4
|
+
params?: any;
|
|
5
|
+
time_tracker_id?: string;
|
|
6
|
+
}
|