@rws-framework/db 2.4.6 → 3.0.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.
- package/.bin/add-v.sh +9 -9
- package/.bin/emerge.sh +10 -10
- package/.eslintrc.json +53 -53
- package/README.md +343 -405
- 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 +255 -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 +5 -2
- package/dist/models/core/RWSModel.js +8 -5
- 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 +4 -1
- 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 +318 -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 +529 -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,19 +1,19 @@
|
|
|
1
|
-
import { RWSModel } from './RWSModel';
|
|
2
|
-
import { TrackType } from '../../decorators';
|
|
3
|
-
|
|
4
|
-
// export default class TimeSeriesModel<T> extends RWSModel<T> {
|
|
5
|
-
// @TrackType(Number) value: number;
|
|
6
|
-
|
|
7
|
-
// @TrackType(Date) timestamp: Date;
|
|
8
|
-
|
|
9
|
-
// @TrackType(Object)
|
|
10
|
-
// params: any;
|
|
11
|
-
|
|
12
|
-
// constructor(data?: any) {
|
|
13
|
-
// super(data);
|
|
14
|
-
|
|
15
|
-
// if(!this.timestamp) {
|
|
16
|
-
// this.timestamp = new Date();
|
|
17
|
-
// }
|
|
18
|
-
// }
|
|
19
|
-
// }
|
|
1
|
+
import { RWSModel } from './RWSModel';
|
|
2
|
+
import { TrackType } from '../../decorators';
|
|
3
|
+
|
|
4
|
+
// export default class TimeSeriesModel<T> extends RWSModel<T> {
|
|
5
|
+
// @TrackType(Number) value: number;
|
|
6
|
+
|
|
7
|
+
// @TrackType(Date) timestamp: Date;
|
|
8
|
+
|
|
9
|
+
// @TrackType(Object)
|
|
10
|
+
// params: any;
|
|
11
|
+
|
|
12
|
+
// constructor(data?: any) {
|
|
13
|
+
// super(data);
|
|
14
|
+
|
|
15
|
+
// if(!this.timestamp) {
|
|
16
|
+
// this.timestamp = new Date();
|
|
17
|
+
// }
|
|
18
|
+
// }
|
|
19
|
+
// }
|
package/src/models/index.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { IModel } from './interfaces/IModel';
|
|
2
|
-
import { IRWSModelServices } from './interfaces/IRWSModelServices';
|
|
3
|
-
import { OpModelType } from './interfaces/OpModelType';
|
|
4
|
-
import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
|
|
5
|
-
import { RWSModel } from './core/RWSModel';
|
|
6
|
-
// import TimeSeriesModel from './core/TimeSeriesModel';
|
|
7
|
-
import { TrackType,
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
IModel,
|
|
11
|
-
IRWSModelServices,
|
|
12
|
-
OpModelType,
|
|
13
|
-
RelationBindType,
|
|
14
|
-
RelOneMetaType,
|
|
15
|
-
RelManyMetaType,
|
|
16
|
-
RWSModel,
|
|
17
|
-
// TimeSeriesModel,
|
|
18
|
-
TrackType,
|
|
19
|
-
|
|
20
|
-
};
|
|
1
|
+
import { IModel } from './interfaces/IModel';
|
|
2
|
+
import { IRWSModelServices } from './interfaces/IRWSModelServices';
|
|
3
|
+
import { OpModelType } from './interfaces/OpModelType';
|
|
4
|
+
import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
|
|
5
|
+
import { RWSModel } from './core/RWSModel';
|
|
6
|
+
// import TimeSeriesModel from './core/TimeSeriesModel';
|
|
7
|
+
import { TrackType, ITrackerMetaOpts } from '../decorators';
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
IModel,
|
|
11
|
+
IRWSModelServices,
|
|
12
|
+
OpModelType,
|
|
13
|
+
RelationBindType,
|
|
14
|
+
RelOneMetaType,
|
|
15
|
+
RelManyMetaType,
|
|
16
|
+
RWSModel,
|
|
17
|
+
// TimeSeriesModel,
|
|
18
|
+
TrackType,
|
|
19
|
+
ITrackerMetaOpts
|
|
20
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface IDbOpts {
|
|
2
|
+
dbOptions?: {
|
|
3
|
+
mysql?: {
|
|
4
|
+
useType?: string;
|
|
5
|
+
useText?: boolean;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
useUuid?: boolean;
|
|
8
|
+
};
|
|
9
|
+
postgres?: {
|
|
10
|
+
useText?: boolean;
|
|
11
|
+
useUuid?: boolean;
|
|
12
|
+
};
|
|
13
|
+
mongodb?: {
|
|
14
|
+
customType?: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { IDbConfigHandler } from '../../types/DbConfigHandler';
|
|
2
|
-
import { DBService } from '../../services/DBService';
|
|
3
|
-
|
|
4
|
-
export interface IModel {
|
|
5
|
-
[key: string]: any;
|
|
6
|
-
id: string |number | null;
|
|
7
|
-
save: () => void;
|
|
8
|
-
getDb: () => DBService;
|
|
9
|
-
getCollection: () => string | null;
|
|
10
|
-
configService?: IDbConfigHandler;
|
|
11
|
-
dbService?: DBService;
|
|
12
|
-
}
|
|
1
|
+
import { IDbConfigHandler } from '../../types/DbConfigHandler';
|
|
2
|
+
import { DBService } from '../../services/DBService';
|
|
3
|
+
|
|
4
|
+
export interface IModel {
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
id: string |number | null;
|
|
7
|
+
save: () => void;
|
|
8
|
+
getDb: () => DBService;
|
|
9
|
+
getCollection: () => string | null;
|
|
10
|
+
configService?: IDbConfigHandler;
|
|
11
|
+
dbService?: DBService;
|
|
12
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { IDbConfigHandler } from '../../types/DbConfigHandler';
|
|
2
|
-
import { DBService } from '../../services/DBService';
|
|
3
|
-
|
|
4
|
-
export interface IRWSModelServices {
|
|
5
|
-
configService?: IDbConfigHandler;
|
|
6
|
-
dbService?: DBService;
|
|
7
|
-
}
|
|
1
|
+
import { IDbConfigHandler } from '../../types/DbConfigHandler';
|
|
2
|
+
import { DBService } from '../../services/DBService';
|
|
3
|
+
|
|
4
|
+
export interface IRWSModelServices {
|
|
5
|
+
configService?: IDbConfigHandler;
|
|
6
|
+
dbService?: DBService;
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IDbOpts } from "./IDbOpts";
|
|
2
|
+
import { OpModelType } from "./OpModelType";
|
|
3
|
+
|
|
4
|
+
export interface ITrackerOpts extends IDbOpts {
|
|
5
|
+
required?: boolean,
|
|
6
|
+
unique?: boolean | string,
|
|
7
|
+
isArray?: boolean,
|
|
8
|
+
relationField?: string,
|
|
9
|
+
relatedToField?: string,
|
|
10
|
+
relatedTo?: OpModelType<any>,
|
|
11
|
+
inversionModel?: OpModelType<any>,
|
|
12
|
+
relationName?: string
|
|
13
|
+
}
|
|
@@ -1,49 +1,52 @@
|
|
|
1
|
-
import { IRWSModel } from '../../types/IRWSModel';
|
|
2
|
-
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
3
|
-
import { IRWSModelServices } from './IRWSModelServices';
|
|
4
|
-
import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
|
|
5
|
-
import { DBService } from '../../services/DBService';
|
|
6
|
-
|
|
7
|
-
// Reference to the RWSModel class to avoid circular dependency
|
|
8
|
-
import type { RWSModel } from '../core/RWSModel';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
import { IRWSModel } from '../../types/IRWSModel';
|
|
2
|
+
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
3
|
+
import { IRWSModelServices } from './IRWSModelServices';
|
|
4
|
+
import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
|
|
5
|
+
import { DBService } from '../../services/DBService';
|
|
6
|
+
|
|
7
|
+
// Reference to the RWSModel class to avoid circular dependency
|
|
8
|
+
import type { RWSModel } from '../core/RWSModel';
|
|
9
|
+
import { ISuperTagData } from '../../decorators/RWSCollection';
|
|
10
|
+
|
|
11
|
+
export interface OpModelType<T> {
|
|
12
|
+
new(data?: any | null): T;
|
|
13
|
+
services: IRWSModelServices;
|
|
14
|
+
name: string;
|
|
15
|
+
_collection: string;
|
|
16
|
+
_NO_ID: boolean;
|
|
17
|
+
_SUPER_TAGS: ISuperTagData[];
|
|
18
|
+
_RELATIONS: {[key: string]: boolean};
|
|
19
|
+
_CUT_KEYS: string[];
|
|
20
|
+
allModels: OpModelType<any>[];
|
|
21
|
+
loadModels: () => OpModelType<any>[];
|
|
22
|
+
checkForInclusionWithThrow: (className: string) => void;
|
|
23
|
+
checkForInclusion: (className: string) => boolean;
|
|
24
|
+
findOneBy<T extends RWSModel<T>>(
|
|
25
|
+
this: OpModelType<T>,
|
|
26
|
+
findParams?: FindByType
|
|
27
|
+
): Promise<T | null>;
|
|
28
|
+
find<T extends RWSModel<T>>(
|
|
29
|
+
this: OpModelType<T>,
|
|
30
|
+
id: string | number,
|
|
31
|
+
findParams?: Omit<FindByType, 'conditions'>
|
|
32
|
+
): Promise<T | null>;
|
|
33
|
+
findBy<T extends RWSModel<T>>(
|
|
34
|
+
this: OpModelType<T>,
|
|
35
|
+
findParams?: FindByType
|
|
36
|
+
): Promise<T[]>;
|
|
37
|
+
paginate<T extends RWSModel<T>>(
|
|
38
|
+
this: OpModelType<T>,
|
|
39
|
+
paginateParams?: IPaginationParams,
|
|
40
|
+
findParams?: FindByType
|
|
41
|
+
): Promise<T[]>;
|
|
42
|
+
delete<T extends RWSModel<T>>(
|
|
43
|
+
this: OpModelType<T>,
|
|
44
|
+
conditions: any
|
|
45
|
+
): Promise<void>;
|
|
46
|
+
create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
|
|
47
|
+
getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
|
|
48
|
+
getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
|
|
49
|
+
getCollection(): string;
|
|
50
|
+
getDb(): DBService;
|
|
51
|
+
setServices(services: IRWSModelServices): void;
|
|
52
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { IRWSModel } from '../../types/IRWSModel';
|
|
2
|
-
// Using a type reference to avoid circular dependency
|
|
3
|
-
type OpModelType<T> = any;
|
|
4
|
-
|
|
5
|
-
export type RelationBindType = {
|
|
6
|
-
connect: { id: string }
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type RelOneMetaType<T extends IRWSModel> = {
|
|
10
|
-
[key: string]: {
|
|
11
|
-
required: boolean,
|
|
12
|
-
key?: string,
|
|
13
|
-
model: OpModelType<T>,
|
|
14
|
-
hydrationField: string,
|
|
15
|
-
foreignKey: string
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type RelManyMetaType<T extends IRWSModel> = {
|
|
20
|
-
[key: string]: {
|
|
21
|
-
key: string,
|
|
22
|
-
inversionModel: OpModelType<T>,
|
|
23
|
-
foreignKey: string
|
|
24
|
-
}
|
|
25
|
-
};
|
|
1
|
+
import { IRWSModel } from '../../types/IRWSModel';
|
|
2
|
+
// Using a type reference to avoid circular dependency
|
|
3
|
+
type OpModelType<T> = any;
|
|
4
|
+
|
|
5
|
+
export type RelationBindType = {
|
|
6
|
+
connect: { id: string }
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type RelOneMetaType<T extends IRWSModel> = {
|
|
10
|
+
[key: string]: {
|
|
11
|
+
required: boolean,
|
|
12
|
+
key?: string,
|
|
13
|
+
model: OpModelType<T>,
|
|
14
|
+
hydrationField: string,
|
|
15
|
+
foreignKey: string
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type RelManyMetaType<T extends IRWSModel> = {
|
|
20
|
+
[key: string]: {
|
|
21
|
+
key: string,
|
|
22
|
+
inversionModel: OpModelType<T>,
|
|
23
|
+
foreignKey: string
|
|
24
|
+
}
|
|
25
|
+
};
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import { RWSModel } from "../core/RWSModel";
|
|
2
|
-
import { FieldsHelper } from '../../helper/FieldsHelper';
|
|
3
|
-
|
|
4
|
-
export class ModelUtils {
|
|
5
|
-
static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
|
|
6
|
-
const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
|
|
7
|
-
|
|
8
|
-
const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
|
|
9
|
-
|
|
10
|
-
// Process all metadata keys and collect promises
|
|
11
|
-
const metadataPromises = metadataKeys.map(async (fullKey: string) => {
|
|
12
|
-
const [annotationType, propertyKey] = fullKey.split(':');
|
|
13
|
-
const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
|
|
14
|
-
|
|
15
|
-
if (metadata) {
|
|
16
|
-
// If this is a relation metadata with a promise
|
|
17
|
-
if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
|
|
18
|
-
const resolvedMetadata = await metadata.promise;
|
|
19
|
-
annotationsData[propertyKey] = {
|
|
20
|
-
annotationType,
|
|
21
|
-
metadata: resolvedMetadata
|
|
22
|
-
};
|
|
23
|
-
} else {
|
|
24
|
-
// Handle non-relation metadata as before
|
|
25
|
-
const key = metadata.key || propertyKey;
|
|
26
|
-
annotationsData[key] = {
|
|
27
|
-
annotationType,
|
|
28
|
-
metadata
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// Wait for all metadata to be processed
|
|
35
|
-
await Promise.all(metadataPromises);
|
|
36
|
-
|
|
37
|
-
return annotationsData;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
|
|
41
|
-
if(variable === 'id'){
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
|
|
46
|
-
type AnnotationType = { annotationType: string, key: string };
|
|
47
|
-
|
|
48
|
-
const dbProperties: string[] = Object.keys(dbAnnotations)
|
|
49
|
-
.map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
|
|
50
|
-
.filter((element: AnnotationType) => element.annotationType === 'TrackType' )
|
|
51
|
-
.map((element: AnnotationType) => element.key);
|
|
52
|
-
|
|
53
|
-
return dbProperties.includes(variable);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static isSubclass<T, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
|
|
57
|
-
return baseClass.prototype.isPrototypeOf(constructor.prototype);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
static getModelScalarFields(model: RWSModel<any>): string[] {
|
|
61
|
-
return FieldsHelper.getAllClassFields(model)
|
|
62
|
-
.filter((item: string) => item.indexOf('TrackType') === 0)
|
|
63
|
-
.map((item: string) => item.split(':').at(-1));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
import { RWSModel } from "../core/RWSModel";
|
|
2
|
+
import { FieldsHelper } from '../../helper/FieldsHelper';
|
|
3
|
+
|
|
4
|
+
export class ModelUtils {
|
|
5
|
+
static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
|
|
6
|
+
const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
|
|
7
|
+
|
|
8
|
+
const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
|
|
9
|
+
|
|
10
|
+
// Process all metadata keys and collect promises
|
|
11
|
+
const metadataPromises = metadataKeys.map(async (fullKey: string) => {
|
|
12
|
+
const [annotationType, propertyKey] = fullKey.split(':');
|
|
13
|
+
const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
|
|
14
|
+
|
|
15
|
+
if (metadata) {
|
|
16
|
+
// If this is a relation metadata with a promise
|
|
17
|
+
if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
|
|
18
|
+
const resolvedMetadata = await metadata.promise;
|
|
19
|
+
annotationsData[propertyKey] = {
|
|
20
|
+
annotationType,
|
|
21
|
+
metadata: resolvedMetadata
|
|
22
|
+
};
|
|
23
|
+
} else {
|
|
24
|
+
// Handle non-relation metadata as before
|
|
25
|
+
const key = metadata.key || propertyKey;
|
|
26
|
+
annotationsData[key] = {
|
|
27
|
+
annotationType,
|
|
28
|
+
metadata
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Wait for all metadata to be processed
|
|
35
|
+
await Promise.all(metadataPromises);
|
|
36
|
+
|
|
37
|
+
return annotationsData;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
|
|
41
|
+
if(variable === 'id'){
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
|
|
46
|
+
type AnnotationType = { annotationType: string, key: string };
|
|
47
|
+
|
|
48
|
+
const dbProperties: string[] = Object.keys(dbAnnotations)
|
|
49
|
+
.map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
|
|
50
|
+
.filter((element: AnnotationType) => element.annotationType === 'TrackType' )
|
|
51
|
+
.map((element: AnnotationType) => element.key);
|
|
52
|
+
|
|
53
|
+
return dbProperties.includes(variable);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static isSubclass<T, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
|
|
57
|
+
return baseClass.prototype.isPrototypeOf(constructor.prototype);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static getModelScalarFields(model: RWSModel<any>): string[] {
|
|
61
|
+
return FieldsHelper.getAllClassFields(model)
|
|
62
|
+
.filter((item: string) => item.indexOf('TrackType') === 0)
|
|
63
|
+
.map((item: string) => item.split(':').at(-1));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,42 +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<T extends RWSModel<T>>(
|
|
9
|
-
this: OpModelType<T>,
|
|
10
|
-
paginationParams: IPaginationParams = { page: 0, per_page: 50 },
|
|
11
|
-
findParams: FindByType = {},
|
|
12
|
-
): Promise<T[]> {
|
|
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: T[] = [];
|
|
25
|
-
|
|
26
|
-
for (const data of dbData) {
|
|
27
|
-
const inst: T = new (this as { new(): T })();
|
|
28
|
-
instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
|
|
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
|
-
}
|
|
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<T extends RWSModel<T>>(
|
|
9
|
+
this: OpModelType<T>,
|
|
10
|
+
paginationParams: IPaginationParams = { page: 0, per_page: 50 },
|
|
11
|
+
findParams: FindByType = {},
|
|
12
|
+
): Promise<T[]> {
|
|
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: T[] = [];
|
|
25
|
+
|
|
26
|
+
for (const data of dbData) {
|
|
27
|
+
const inst: T = new (this as { new(): T })();
|
|
28
|
+
instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
|
|
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
|
+
}
|