@rws-framework/db 2.1.11 → 2.2.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/README.md +26 -17
- package/dist/decorators/TrackType.d.ts +1 -1
- package/dist/models/_model.d.ts +5 -116
- package/dist/models/_model.js +7 -497
- package/dist/models/core/RWSModel.d.ts +65 -0
- package/dist/models/core/RWSModel.js +375 -0
- package/dist/models/core/TimeSeriesModel.d.ts +7 -0
- package/dist/models/core/TimeSeriesModel.js +34 -0
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +12 -0
- package/dist/models/interfaces/IModel.d.ts +10 -0
- package/dist/models/interfaces/IModel.js +2 -0
- package/dist/models/interfaces/IRWSModelServices.d.ts +6 -0
- package/dist/models/interfaces/IRWSModelServices.js +2 -0
- package/dist/models/interfaces/OpModelType.d.ts +32 -0
- package/dist/models/interfaces/OpModelType.js +2 -0
- package/dist/models/types/RelationTypes.d.ts +24 -0
- package/dist/models/types/RelationTypes.js +2 -0
- package/dist/models/utils/ModelUtils.d.ts +10 -0
- package/dist/models/utils/ModelUtils.js +56 -0
- package/dist/models/utils/PaginationUtils.d.ts +5 -0
- package/dist/models/utils/PaginationUtils.js +32 -0
- package/dist/models/utils/RelationUtils copy.d.ts +13 -0
- package/dist/models/utils/RelationUtils copy.js +65 -0
- package/dist/models/utils/RelationUtils.d.ts +14 -0
- package/dist/models/utils/RelationUtils.js +65 -0
- package/dist/models/utils/TimeSeriesUtils.d.ts +11 -0
- package/dist/models/utils/TimeSeriesUtils.js +35 -0
- package/dist/services/DBService.d.ts +3 -2
- package/dist/services/DBService.js +5 -1
- package/dist/types/DbConfigHandler.d.ts +1 -1
- package/dist/types/FindParams.d.ts +5 -0
- package/package.json +1 -1
- package/src/decorators/TrackType.ts +2 -2
- package/src/models/_model.ts +26 -675
- package/src/models/core/RWSModel.ts +482 -0
- package/src/models/core/TimeSeriesModel.ts +19 -0
- package/src/models/index.ts +20 -0
- package/src/models/interfaces/IModel.ts +11 -0
- package/src/models/interfaces/IRWSModelServices.ts +7 -0
- package/src/models/interfaces/OpModelType.ts +49 -0
- package/src/models/types/RelationTypes.ts +25 -0
- package/src/models/utils/ModelUtils.ts +65 -0
- package/src/models/utils/PaginationUtils.ts +42 -0
- package/src/models/utils/RelationUtils.ts +76 -0
- package/src/models/utils/TimeSeriesUtils.ts +38 -0
- package/src/services/DBService.ts +14 -3
- package/src/types/DbConfigHandler.ts +2 -2
- package/src/types/FindParams.ts +10 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
|
|
2
|
+
import { IRWSModel } from '../../types/IRWSModel';
|
|
3
|
+
import { OpModelType, RWSModel } from '../_model';
|
|
4
|
+
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
5
|
+
|
|
6
|
+
export class PaginationUtils {
|
|
7
|
+
|
|
8
|
+
public static async paginate<ChildClass extends RWSModel<ChildClass>>(
|
|
9
|
+
this: OpModelType<ChildClass>,
|
|
10
|
+
paginationParams: IPaginationParams = { page: 0, per_page: 50 },
|
|
11
|
+
findParams: FindByType = {},
|
|
12
|
+
): Promise<ChildClass[]> {
|
|
13
|
+
const conditions = findParams?.conditions ?? {};
|
|
14
|
+
const ordering = findParams?.ordering ?? null;
|
|
15
|
+
const fields = findParams?.fields ?? null;
|
|
16
|
+
const allowRelations = findParams?.allowRelations ?? true;
|
|
17
|
+
const fullData = findParams?.fullData ?? false;
|
|
18
|
+
|
|
19
|
+
const collection = Reflect.get(this, '_collection');
|
|
20
|
+
this.checkForInclusionWithThrow(this.name);
|
|
21
|
+
try {
|
|
22
|
+
const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginationParams);
|
|
23
|
+
if (dbData.length) {
|
|
24
|
+
const instanced: ChildClass[] = [];
|
|
25
|
+
|
|
26
|
+
for (const data of dbData) {
|
|
27
|
+
const inst: ChildClass = new (this as { new(): ChildClass })();
|
|
28
|
+
instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as ChildClass);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return instanced;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return [];
|
|
35
|
+
} catch (rwsError: Error | any) {
|
|
36
|
+
console.error(rwsError);
|
|
37
|
+
|
|
38
|
+
throw rwsError;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
|
|
2
|
+
import { IRWSModel } from '../../types/IRWSModel';
|
|
3
|
+
import { RWSModel } from '../_model';
|
|
4
|
+
|
|
5
|
+
export class RelationUtils {
|
|
6
|
+
static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
|
|
7
|
+
const relIds: RelOneMetaType<IRWSModel> = {};
|
|
8
|
+
const relationFields = classFields
|
|
9
|
+
.filter((item: string) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
|
|
10
|
+
.map((item: string) => item.split(':').at(-1));
|
|
11
|
+
|
|
12
|
+
for (const key of relationFields) {
|
|
13
|
+
const metadataKey = `Relation:${key}`;
|
|
14
|
+
const metadata = Reflect.getMetadata(metadataKey, model);
|
|
15
|
+
|
|
16
|
+
if (metadata && metadata.promise) {
|
|
17
|
+
const resolvedMetadata = await metadata.promise;
|
|
18
|
+
if (!relIds[key]) {
|
|
19
|
+
relIds[key] = {
|
|
20
|
+
key: resolvedMetadata.key,
|
|
21
|
+
required: resolvedMetadata.required,
|
|
22
|
+
model: resolvedMetadata.relatedTo,
|
|
23
|
+
hydrationField: resolvedMetadata.relationField,
|
|
24
|
+
foreignKey: resolvedMetadata.relatedToField
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return relIds;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static async getRelationManyMeta(model: RWSModel<any>, classFields: string[]): Promise<RelManyMetaType<IRWSModel>> {
|
|
34
|
+
const relIds: RelManyMetaType<IRWSModel> = {};
|
|
35
|
+
|
|
36
|
+
const inverseFields = classFields
|
|
37
|
+
.filter((item: string) => item.indexOf('InverseRelation') === 0)
|
|
38
|
+
.map((item: string) => item.split(':').at(-1));
|
|
39
|
+
|
|
40
|
+
for (const key of inverseFields) {
|
|
41
|
+
const metadataKey = `InverseRelation:${key}`;
|
|
42
|
+
const metadata = Reflect.getMetadata(metadataKey, model);
|
|
43
|
+
|
|
44
|
+
if (metadata && metadata.promise) {
|
|
45
|
+
const resolvedMetadata = await metadata.promise;
|
|
46
|
+
if (!relIds[key]) {
|
|
47
|
+
relIds[key] = {
|
|
48
|
+
key: resolvedMetadata.key,
|
|
49
|
+
inversionModel: resolvedMetadata.inversionModel,
|
|
50
|
+
foreignKey: resolvedMetadata.foreignKey
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return relIds;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string } } {
|
|
60
|
+
return {
|
|
61
|
+
connect: {
|
|
62
|
+
id: relatedModel.id
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static hasRelation(model: RWSModel<any>, key: string): boolean {
|
|
68
|
+
// Check if the property exists and is an object with an id property
|
|
69
|
+
return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static checkRelEnabled(model: RWSModel<any>, key: string): boolean {
|
|
73
|
+
return Object.keys((model.constructor as any)._RELATIONS).includes(key) &&
|
|
74
|
+
(model.constructor as any)._RELATIONS[key] === true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RWSModel } from "../_model";
|
|
2
|
+
|
|
3
|
+
export class TimeSeriesUtils {
|
|
4
|
+
static getTimeSeriesModelFields(model: RWSModel<any>): {[key: string]: {collection: string, hydrationField: string, ids: string[]}} {
|
|
5
|
+
const timeSeriesIds: {[key: string]: {collection: string, hydrationField: string, ids: string[]}} = {};
|
|
6
|
+
|
|
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
|
+
|
|
22
|
+
return timeSeriesIds;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static checkTimeSeries(constructor: any): boolean {
|
|
26
|
+
const data = constructor.prototype as any;
|
|
27
|
+
|
|
28
|
+
for (const key in data) {
|
|
29
|
+
if (data.hasOwnProperty(key)) {
|
|
30
|
+
if(Reflect.getMetadata(`InverseTimeSeries:${key}`, constructor.prototype)){
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { PrismaClient } from '@prisma/client';
|
|
2
2
|
import { Collection, Db, MongoClient } from 'mongodb';
|
|
3
3
|
import {ITimeSeries} from '../types/ITimeSeries';
|
|
4
|
-
import { IModel } from '../models/
|
|
4
|
+
import { IModel } from '../models/interfaces/IModel';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
7
|
+
import { IPaginationParams } from 'src/types/FindParams';
|
|
7
8
|
|
|
8
9
|
interface IDBClientCreate {
|
|
9
10
|
dbUrl?: string;
|
|
@@ -174,7 +175,12 @@ class DBService {
|
|
|
174
175
|
return;
|
|
175
176
|
}
|
|
176
177
|
|
|
177
|
-
async findBy(
|
|
178
|
+
async findBy(
|
|
179
|
+
collection: string,
|
|
180
|
+
conditions: any,
|
|
181
|
+
fields: string[] | null = null,
|
|
182
|
+
ordering: { [fieldName: string]: string } = null,
|
|
183
|
+
pagination: IPaginationParams = null): Promise<IModel[]>
|
|
178
184
|
{
|
|
179
185
|
const params: any ={ where: conditions };
|
|
180
186
|
|
|
@@ -189,6 +195,11 @@ class DBService {
|
|
|
189
195
|
params.orderBy = ordering;
|
|
190
196
|
}
|
|
191
197
|
|
|
198
|
+
if(pagination){
|
|
199
|
+
params.skip = pagination.page * pagination.per_page;
|
|
200
|
+
params.take = pagination.per_page;
|
|
201
|
+
}
|
|
202
|
+
|
|
192
203
|
const retData = await this.getCollectionHandler(collection).findMany(params);
|
|
193
204
|
|
|
194
205
|
return retData;
|
|
@@ -255,4 +266,4 @@ class DBService {
|
|
|
255
266
|
}
|
|
256
267
|
}
|
|
257
268
|
|
|
258
|
-
export { DBService, IDBClientCreate };
|
|
269
|
+
export { DBService, IDBClientCreate };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OpModelType } from "../models/
|
|
1
|
+
import { OpModelType } from "../models/interfaces/OpModelType";
|
|
2
2
|
|
|
3
3
|
export interface IDbConfigParams {
|
|
4
4
|
mongo_url?: string;
|
|
@@ -8,4 +8,4 @@ export interface IDbConfigParams {
|
|
|
8
8
|
|
|
9
9
|
export interface IDbConfigHandler {
|
|
10
10
|
get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K];
|
|
11
|
-
}
|
|
11
|
+
}
|
package/src/types/FindParams.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export type FindByType = {
|
|
2
|
-
conditions?: any
|
|
3
|
-
ordering?: { [fieldName: string]: string }
|
|
4
|
-
fields?: string[]
|
|
5
|
-
allowRelations?: boolean
|
|
2
|
+
conditions?: any
|
|
3
|
+
ordering?: { [fieldName: string]: string }
|
|
4
|
+
fields?: string[]
|
|
5
|
+
allowRelations?: boolean
|
|
6
6
|
fullData?: boolean
|
|
7
|
+
pagination?: IPaginationParams
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IPaginationParams {
|
|
11
|
+
page: number,
|
|
12
|
+
per_page: number
|
|
7
13
|
}
|