@rws-framework/db 2.0.2 → 2.0.3
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/package.json +1 -1
- package/src/helper/DbHelper.ts +2 -2
- package/src/models/_model.ts +18 -20
package/package.json
CHANGED
package/src/helper/DbHelper.ts
CHANGED
|
@@ -76,7 +76,7 @@ export class DbHelper {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
static async generateModelSections
|
|
79
|
+
static async generateModelSections(model: OpModelType<any>): Promise<string> {
|
|
80
80
|
let section = '';
|
|
81
81
|
const modelMetadatas: Record<string, {annotationType: string, metadata: any}> = await RWSModel.getModelAnnotations(model);
|
|
82
82
|
|
|
@@ -95,7 +95,7 @@ export class DbHelper {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
if(annotationType === 'Relation'){
|
|
98
|
-
const relatedModel = modelMetadata.relatedTo as OpModelType<
|
|
98
|
+
const relatedModel = modelMetadata.relatedTo as OpModelType<any>;
|
|
99
99
|
// Handle direct relation (many-to-one or one-to-one)
|
|
100
100
|
section += `\t${key} ${relatedModel._collection}${requiredString} @relation("${modelName}_${relatedModel._collection}", fields: [${modelMetadata.relationField}], references: [${modelMetadata.relatedToField}], onDelete: Cascade)\n`;
|
|
101
101
|
section += `\t${modelMetadata.relationField} String${requiredString} @db.ObjectId\n`;
|
package/src/models/_model.ts
CHANGED
|
@@ -29,6 +29,7 @@ type RelManyMetaType<T extends IRWSModel> = {[key: string]: {key: string, invers
|
|
|
29
29
|
|
|
30
30
|
export interface OpModelType<ChildClass> {
|
|
31
31
|
new(data?: any | null): ChildClass;
|
|
32
|
+
services: IRWSModelServices;
|
|
32
33
|
name: string
|
|
33
34
|
_collection: string;
|
|
34
35
|
_RELATIONS: {[key: string]: boolean}
|
|
@@ -37,8 +38,6 @@ export interface OpModelType<ChildClass> {
|
|
|
37
38
|
loadModels: () => OpModelType<any>[];
|
|
38
39
|
checkForInclusionWithThrow: (className: string) => void;
|
|
39
40
|
checkForInclusion: (className: string) => boolean;
|
|
40
|
-
configService?: IDbConfigHandler;
|
|
41
|
-
dbService?: DBService;
|
|
42
41
|
findOneBy<T extends RWSModel<T>>(
|
|
43
42
|
this: OpModelType<T>,
|
|
44
43
|
findParams: FindByType
|
|
@@ -60,14 +59,12 @@ export interface OpModelType<ChildClass> {
|
|
|
60
59
|
getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
|
|
61
60
|
getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
|
|
62
61
|
getCollection(): string;
|
|
62
|
+
setServices(services: IRWSModelServices): void;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
class RWSModel<ChildClass> implements IModel{
|
|
66
|
-
static services: IRWSModelServices = {}
|
|
67
|
-
|
|
68
|
-
static configService: IDbConfigHandler;
|
|
69
|
-
static dbService: DBService
|
|
70
|
-
|
|
66
|
+
static services: IRWSModelServices = {};
|
|
67
|
+
|
|
71
68
|
[key: string]: any;
|
|
72
69
|
@TrackType(String)
|
|
73
70
|
id: string;
|
|
@@ -83,8 +80,8 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
83
80
|
|
|
84
81
|
}
|
|
85
82
|
|
|
86
|
-
this.dbService = RWSModel.dbService;
|
|
87
|
-
this.configService = RWSModel.configService;
|
|
83
|
+
this.dbService = RWSModel.services.dbService;
|
|
84
|
+
this.configService = RWSModel.services.configService;
|
|
88
85
|
|
|
89
86
|
if(!data){
|
|
90
87
|
return;
|
|
@@ -99,7 +96,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
99
96
|
|
|
100
97
|
checkForInclusionWithThrow(): void
|
|
101
98
|
{
|
|
102
|
-
this.checkForInclusionWithThrow()
|
|
99
|
+
this.checkForInclusionWithThrow();
|
|
103
100
|
}
|
|
104
101
|
|
|
105
102
|
static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void
|
|
@@ -117,8 +114,8 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
117
114
|
static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean
|
|
118
115
|
{
|
|
119
116
|
return this.loadModels().find((definedModel: OpModelType<any>) => {
|
|
120
|
-
return definedModel.name === checkModelType
|
|
121
|
-
}) !== undefined
|
|
117
|
+
return definedModel.name === checkModelType;
|
|
118
|
+
}) !== undefined;
|
|
122
119
|
}
|
|
123
120
|
|
|
124
121
|
protected _fill(data: any): RWSModel<ChildClass>{
|
|
@@ -543,7 +540,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
543
540
|
){
|
|
544
541
|
const collection = Reflect.get(this, '_collection');
|
|
545
542
|
this.checkForInclusionWithThrow(this.name);
|
|
546
|
-
return await this.dbService.watchCollection(collection, preRun);
|
|
543
|
+
return await this.services.dbService.watchCollection(collection, preRun);
|
|
547
544
|
}
|
|
548
545
|
|
|
549
546
|
public static async findOneBy<ChildClass extends RWSModel<ChildClass>>(
|
|
@@ -560,7 +557,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
560
557
|
|
|
561
558
|
|
|
562
559
|
const collection = Reflect.get(this, '_collection');
|
|
563
|
-
const dbData = await this.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
|
|
560
|
+
const dbData = await this.services.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
|
|
564
561
|
|
|
565
562
|
|
|
566
563
|
if (dbData) {
|
|
@@ -584,7 +581,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
584
581
|
const collection = Reflect.get(this, '_collection');
|
|
585
582
|
this.checkForInclusionWithThrow(this.name);
|
|
586
583
|
|
|
587
|
-
const dbData = await this.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
|
|
584
|
+
const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
|
|
588
585
|
|
|
589
586
|
if (dbData) {
|
|
590
587
|
const inst: ChildClass = new (this as { new(): ChildClass })();
|
|
@@ -607,7 +604,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
607
604
|
const collection = Reflect.get(this, '_collection');
|
|
608
605
|
this.checkForInclusionWithThrow(this.name);
|
|
609
606
|
try {
|
|
610
|
-
const dbData = await this.dbService.findBy(collection, conditions, fields, ordering, allowRelations);
|
|
607
|
+
const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, allowRelations);
|
|
611
608
|
if (dbData.length) {
|
|
612
609
|
const instanced: ChildClass[] = [];
|
|
613
610
|
|
|
@@ -633,7 +630,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
633
630
|
): Promise<void> {
|
|
634
631
|
const collection = Reflect.get(this, '_collection');
|
|
635
632
|
this.checkForInclusionWithThrow(this.name);
|
|
636
|
-
return await this.dbService.delete(collection, conditions);
|
|
633
|
+
return await this.services.dbService.delete(collection, conditions);
|
|
637
634
|
}
|
|
638
635
|
|
|
639
636
|
public async delete<ChildClass extends RWSModel<ChildClass>>(): Promise<void> {
|
|
@@ -657,7 +654,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
657
654
|
|
|
658
655
|
static loadModels(): OpModelType<any>[]
|
|
659
656
|
{
|
|
660
|
-
return
|
|
657
|
+
return this.allModels || [];
|
|
661
658
|
}
|
|
662
659
|
|
|
663
660
|
loadModels(): OpModelType<any>[]
|
|
@@ -670,8 +667,9 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
670
667
|
return Object.keys((this as any).constructor._RELATIONS).includes(key) && (this as any).constructor._RELATIONS[key] === true
|
|
671
668
|
}
|
|
672
669
|
|
|
673
|
-
public static setServices(services: IRWSModelServices){
|
|
674
|
-
|
|
670
|
+
public static setServices(services: IRWSModelServices){
|
|
671
|
+
this.allModels = services.configService.get('db_models');
|
|
672
|
+
this.services = services;
|
|
675
673
|
}
|
|
676
674
|
}
|
|
677
675
|
|