@rws-framework/db 2.1.9 → 2.1.11
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 +17 -26
- package/dist/helper/DbHelper.js +3 -2
- package/dist/models/_model.d.ts +1 -1
- package/dist/models/_model.js +0 -2
- package/dist/services/DBService.d.ts +1 -0
- package/dist/services/DBService.js +3 -0
- package/package.json +1 -1
- package/src/helper/DbHelper.ts +3 -2
- package/src/models/_model.ts +4 -6
- package/src/services/DBService.ts +5 -0
package/README.md
CHANGED
|
@@ -10,34 +10,38 @@ import User from "./User";
|
|
|
10
10
|
|
|
11
11
|
export const models = [ User, ApiKey];
|
|
12
12
|
```
|
|
13
|
-
|
|
14
|
-
|
|
15
13
|
## Example user model
|
|
16
14
|
|
|
17
15
|
```typescript
|
|
18
|
-
import {
|
|
16
|
+
import { TrackType, InverseRelation, RWSCollection, RWSModel } from '@rws-framework/db';
|
|
19
17
|
|
|
20
|
-
import IUser from './interfaces/IUser';
|
|
21
|
-
import 'reflect-metadata';
|
|
18
|
+
import IUser from './interfaces/IUser';
|
|
19
|
+
import 'reflect-metadata';
|
|
22
20
|
|
|
23
|
-
import ApiKey from './ApiKey';
|
|
24
|
-
import IApiKey from './interfaces/IApiKey';
|
|
25
|
-
const { RWSTrackType, InverseRelation } = RWSannotations.modelAnnotations;
|
|
21
|
+
import ApiKey from './ApiKey';
|
|
22
|
+
import IApiKey from './interfaces/IApiKey';
|
|
26
23
|
|
|
24
|
+
@RWSCollection('users', {
|
|
25
|
+
relations: {
|
|
26
|
+
transcriptions: true,
|
|
27
|
+
apiKeys: true
|
|
28
|
+
},
|
|
29
|
+
ignored_keys: ['passwd']
|
|
30
|
+
})
|
|
27
31
|
class User extends RWSModel<User> implements IUser {
|
|
28
|
-
@
|
|
32
|
+
@TrackType(String)
|
|
29
33
|
username: string;
|
|
30
34
|
|
|
31
|
-
@
|
|
35
|
+
@TrackType(String) // Can also handle Object and Number
|
|
32
36
|
passwd: string;
|
|
33
37
|
|
|
34
|
-
@
|
|
38
|
+
@TrackType(Boolean)
|
|
35
39
|
active: boolean;
|
|
36
40
|
|
|
37
|
-
@
|
|
41
|
+
@TrackType(Date, { required: true })
|
|
38
42
|
created_at: Date;
|
|
39
43
|
|
|
40
|
-
@
|
|
44
|
+
@TrackType(Date)
|
|
41
45
|
updated_at: Date;
|
|
42
46
|
|
|
43
47
|
/**
|
|
@@ -47,25 +51,12 @@ class User extends RWSModel<User> implements IUser {
|
|
|
47
51
|
@InverseRelation(() => ApiKey, () => User)
|
|
48
52
|
apiKeys: IApiKey[];
|
|
49
53
|
|
|
50
|
-
static _collection = 'user';
|
|
51
|
-
|
|
52
|
-
static _RELATIONS = {
|
|
53
|
-
transcriptions: true,
|
|
54
|
-
apiKeys: true
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
static _CUT_KEYS = ['passwd'];
|
|
58
|
-
|
|
59
54
|
constructor(data?: IUser) {
|
|
60
55
|
super(data);
|
|
61
56
|
|
|
62
57
|
if(!this.created_at){
|
|
63
58
|
this.created_at = new Date();
|
|
64
59
|
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
addMessage(message: string){
|
|
68
|
-
this.messages.push(message);
|
|
69
60
|
}
|
|
70
61
|
}
|
|
71
62
|
|
package/dist/helper/DbHelper.js
CHANGED
|
@@ -51,9 +51,10 @@ class DbHelper {
|
|
|
51
51
|
fs_1.default.writeFileSync(schemaPath, template);
|
|
52
52
|
process.env.DB_URL = dbUrl;
|
|
53
53
|
const endPrisma = 'npx prisma';
|
|
54
|
-
|
|
54
|
+
// console.log({cwd: process.cwd()})
|
|
55
|
+
// const clientPath = path.join(rwsPath.findRootWorkspacePath(), 'node_modules', '.prisma', 'client');
|
|
55
56
|
await console_1.rwsShell.runCommand(`${endPrisma} generate --schema=${schemaPath}`, process.cwd());
|
|
56
|
-
leaveFile =
|
|
57
|
+
leaveFile = false;
|
|
57
58
|
log(chalk_1.default.green('[RWS Init]') + ' prisma schema generated from ', schemaPath);
|
|
58
59
|
if (!leaveFile) {
|
|
59
60
|
fs_1.default.unlinkSync(schemaPath);
|
package/dist/models/_model.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ declare class RWSModel<ChildClass> implements IModel {
|
|
|
73
73
|
static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void;
|
|
74
74
|
checkForInclusion(): boolean;
|
|
75
75
|
static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean;
|
|
76
|
-
|
|
76
|
+
_fill(data: any): RWSModel<ChildClass>;
|
|
77
77
|
protected hasRelation(key: string): boolean;
|
|
78
78
|
protected bindRelation(key: string, relatedModel: RWSModel<any>): RelationBindType;
|
|
79
79
|
_asyncFill(data: any, fullDataMode?: boolean, allowRelations?: boolean): Promise<ChildClass>;
|
package/dist/models/_model.js
CHANGED
|
@@ -119,7 +119,6 @@ class RWSModel {
|
|
|
119
119
|
});
|
|
120
120
|
const seriesHydrationfields = [];
|
|
121
121
|
if (allowRelations) {
|
|
122
|
-
// Handle many-to-many relations
|
|
123
122
|
for (const key in relManyData) {
|
|
124
123
|
if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
|
|
125
124
|
continue;
|
|
@@ -135,7 +134,6 @@ class RWSModel {
|
|
|
135
134
|
});
|
|
136
135
|
}
|
|
137
136
|
}
|
|
138
|
-
// Handle one-to-one relations
|
|
139
137
|
for (const key in relOneData) {
|
|
140
138
|
if (!fullDataMode && this.constructor._CUT_KEYS.includes(key)) {
|
|
141
139
|
continue;
|
|
@@ -13,6 +13,7 @@ declare class DBService {
|
|
|
13
13
|
private connected;
|
|
14
14
|
constructor(configService: IDbConfigHandler);
|
|
15
15
|
private connectToDB;
|
|
16
|
+
reconnect(opts?: IDBClientCreate): void;
|
|
16
17
|
private createBaseMongoClient;
|
|
17
18
|
private createBaseMongoClientDB;
|
|
18
19
|
cloneDatabase(source: string, target: string): Promise<void>;
|
|
@@ -42,6 +42,9 @@ class DBService {
|
|
|
42
42
|
throw new Error('PRISMA CONNECTION ERROR');
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
+
reconnect(opts = null) {
|
|
46
|
+
this.connectToDB(opts);
|
|
47
|
+
}
|
|
45
48
|
async createBaseMongoClient() {
|
|
46
49
|
var _a;
|
|
47
50
|
const dbUrl = ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.dbUrl) || this.configService.get('mongo_url');
|
package/package.json
CHANGED
package/src/helper/DbHelper.ts
CHANGED
|
@@ -68,10 +68,11 @@ export class DbHelper {
|
|
|
68
68
|
process.env.DB_URL = dbUrl;
|
|
69
69
|
const endPrisma = 'npx prisma';
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
// console.log({cwd: process.cwd()})
|
|
72
|
+
// const clientPath = path.join(rwsPath.findRootWorkspacePath(), 'node_modules', '.prisma', 'client');
|
|
72
73
|
await rwsShell.runCommand(`${endPrisma} generate --schema=${schemaPath}`, process.cwd());
|
|
73
74
|
|
|
74
|
-
leaveFile =
|
|
75
|
+
leaveFile = false;
|
|
75
76
|
log(chalk.green('[RWS Init]') + ' prisma schema generated from ', schemaPath);
|
|
76
77
|
|
|
77
78
|
if(!leaveFile){
|
package/src/models/_model.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DBService} from '../services/DBService';
|
|
1
|
+
import {DBService, IDBClientCreate} from '../services/DBService';
|
|
2
2
|
import { IRWSModel } from '../types/IRWSModel';
|
|
3
3
|
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
4
4
|
|
|
@@ -59,7 +59,7 @@ export interface OpModelType<ChildClass> {
|
|
|
59
59
|
getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
|
|
60
60
|
getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
|
|
61
61
|
getCollection(): string;
|
|
62
|
-
setServices(services: IRWSModelServices): void;
|
|
62
|
+
setServices(services: IRWSModelServices): void;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
class RWSModel<ChildClass> implements IModel{
|
|
@@ -118,7 +118,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
118
118
|
}) !== undefined;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
|
|
121
|
+
_fill(data: any): RWSModel<ChildClass>{
|
|
122
122
|
for (const key in data) {
|
|
123
123
|
if (data.hasOwnProperty(key)) {
|
|
124
124
|
|
|
@@ -170,7 +170,6 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
170
170
|
const seriesHydrationfields: string[] = [];
|
|
171
171
|
|
|
172
172
|
if (allowRelations) {
|
|
173
|
-
// Handle many-to-many relations
|
|
174
173
|
for (const key in relManyData) {
|
|
175
174
|
if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
|
|
176
175
|
continue;
|
|
@@ -188,8 +187,7 @@ class RWSModel<ChildClass> implements IModel{
|
|
|
188
187
|
});
|
|
189
188
|
}
|
|
190
189
|
}
|
|
191
|
-
|
|
192
|
-
// Handle one-to-one relations
|
|
190
|
+
|
|
193
191
|
for (const key in relOneData) {
|
|
194
192
|
if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
|
|
195
193
|
continue;
|
|
@@ -50,6 +50,11 @@ class DBService {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
reconnect(opts: IDBClientCreate = null)
|
|
54
|
+
{
|
|
55
|
+
this.connectToDB(opts);
|
|
56
|
+
}
|
|
57
|
+
|
|
53
58
|
private async createBaseMongoClient(): Promise<MongoClient>
|
|
54
59
|
{
|
|
55
60
|
const dbUrl = this.opts?.dbUrl || this.configService.get('mongo_url');
|