@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,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/exec/src/cli.ts
CHANGED
package/package.json
CHANGED
package/src/helper/DbHelper.ts
CHANGED
|
@@ -3,7 +3,7 @@ import chalk from 'chalk';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
|
|
6
|
-
import { IDbConfigHandler } from '../types/DbConfigHandler';
|
|
6
|
+
import { IDbConfigHandler, IDbConfigParams, IdGeneratorOptions } from '../types/DbConfigHandler';
|
|
7
7
|
import { IMetaOpts, OpModelType, RWSModel } from '../models/_model';
|
|
8
8
|
// import TimeSeriesModel from '../models/core/TimeSeriesModel';
|
|
9
9
|
import { DBService } from '../services/DBService';
|
|
@@ -15,6 +15,8 @@ const workspaceRoot = rwsPath.findRootWorkspacePath();
|
|
|
15
15
|
const moduleDir = path.resolve(workspaceRoot, 'node_modules', '@rws-framework', 'db');
|
|
16
16
|
|
|
17
17
|
export class DbHelper {
|
|
18
|
+
static dbUrlVarName: string = 'PRISMA_DB_URL';
|
|
19
|
+
|
|
18
20
|
static async installPrisma(configService: IDbConfigHandler, dbService: DBService, leaveFile = false): Promise<void>
|
|
19
21
|
{
|
|
20
22
|
const dbUrl = configService.get('db_url');
|
|
@@ -26,7 +28,7 @@ export class DbHelper {
|
|
|
26
28
|
|
|
27
29
|
template += `\ndatasource db {\n
|
|
28
30
|
provider = "${dbType}"\n
|
|
29
|
-
url = ${
|
|
31
|
+
url = env("${this.dbUrlVarName}")\n
|
|
30
32
|
}\n\n`;
|
|
31
33
|
|
|
32
34
|
const dbModels: OpModelType<unknown>[] | null = configService.get('db_models');
|
|
@@ -34,7 +36,7 @@ export class DbHelper {
|
|
|
34
36
|
if(dbModels){
|
|
35
37
|
|
|
36
38
|
for (const model of dbModels){
|
|
37
|
-
const modelSection = await DbHelper.generateModelSections(model);
|
|
39
|
+
const modelSection = await DbHelper.generateModelSections(model, configService);
|
|
38
40
|
|
|
39
41
|
template += '\n\n' + modelSection;
|
|
40
42
|
|
|
@@ -65,12 +67,25 @@ export class DbHelper {
|
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
fs.writeFileSync(schemaPath, template);
|
|
68
|
-
process.env
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
process.env = { ...process.env, [this.dbUrlVarName]: dbUrl }
|
|
71
|
+
|
|
72
|
+
// Use npx directly with the full path to prisma
|
|
73
|
+
const npxPath = path.join(workspaceRoot, 'node_modules', '.bin', 'npx');
|
|
74
|
+
const prismaPath = path.join(workspaceRoot, 'node_modules', '.bin', 'prisma');
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
// Try using npx with the full path
|
|
78
|
+
await rwsShell.runCommand(`"${npxPath}" prisma generate --schema="${schemaPath}"`, process.cwd());
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// If that fails, try using the prisma binary directly
|
|
81
|
+
try {
|
|
82
|
+
await rwsShell.runCommand(`"${prismaPath}" generate --schema="${schemaPath}"`, process.cwd());
|
|
83
|
+
} catch (innerError) {
|
|
84
|
+
// If both fail, try using node to run prisma
|
|
85
|
+
const nodePrismaPath = path.join(workspaceRoot, 'node_modules', 'prisma', 'build', 'index.js');
|
|
86
|
+
await rwsShell.runCommand(`node "${nodePrismaPath}" generate --schema="${schemaPath}"`, process.cwd());
|
|
87
|
+
}
|
|
88
|
+
}
|
|
74
89
|
|
|
75
90
|
leaveFile = false;
|
|
76
91
|
log(chalk.green('[RWS Init]') + ' prisma schema generated from ', schemaPath);
|
|
@@ -81,14 +96,64 @@ export class DbHelper {
|
|
|
81
96
|
}
|
|
82
97
|
}
|
|
83
98
|
|
|
84
|
-
static
|
|
99
|
+
static generateId(
|
|
100
|
+
dbType: IDbConfigParams['db_type'],
|
|
101
|
+
options: IdGeneratorOptions = {}
|
|
102
|
+
): string {
|
|
103
|
+
const { useUuid = false, customType } = options;
|
|
104
|
+
|
|
105
|
+
if (customType) {
|
|
106
|
+
return `id ${customType} @id`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
switch(dbType) {
|
|
110
|
+
case 'mongodb':
|
|
111
|
+
return 'id String @id @default(auto()) @map("_id") @db.ObjectId';
|
|
112
|
+
|
|
113
|
+
case 'mysql':
|
|
114
|
+
return useUuid
|
|
115
|
+
? 'id String @id @default(uuid())'
|
|
116
|
+
: 'id Int @id @default(autoincrement())';
|
|
117
|
+
|
|
118
|
+
case 'sqlite':
|
|
119
|
+
return 'id Int @id @default(autoincrement())';
|
|
120
|
+
|
|
121
|
+
default:
|
|
122
|
+
throw new Error('Kurwa, nieobsługiwany typ bazy danych!');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static detectInstaller(): string
|
|
127
|
+
{
|
|
128
|
+
|
|
129
|
+
if (fs.existsSync(path.join(workspaceRoot, 'yarn.lock'))){
|
|
130
|
+
return 'yarn';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return 'npx';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static async pushDBModels(configService: IDbConfigHandler, dbService: DBService, leaveFile = false){
|
|
137
|
+
process.env = { ...process.env, [this.dbUrlVarName]: configService.get('db_url') }
|
|
138
|
+
const schemaDir = path.join(moduleDir, 'prisma');
|
|
139
|
+
const schemaPath = path.join(schemaDir, 'schema.prisma');
|
|
140
|
+
|
|
141
|
+
// Use npx directly with the full path to prisma
|
|
142
|
+
const execCmdPath = path.join(workspaceRoot, 'node_modules', '.bin', 'yarn.cmd');
|
|
143
|
+
const execPrismaPath = path.join(workspaceRoot, 'node_modules', '.bin', 'yarn.cmd');
|
|
144
|
+
|
|
145
|
+
await rwsShell.runCommand(`${this.detectInstaller()} prisma db push --schema="${schemaPath}"`, process.cwd());
|
|
146
|
+
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static async generateModelSections(model: OpModelType<any>, configService: IDbConfigHandler): Promise<string> {
|
|
85
150
|
let section = '';
|
|
86
151
|
const modelMetadatas: Record<string, {annotationType: string, metadata: any}> = await RWSModel.getModelAnnotations(model);
|
|
87
152
|
|
|
88
153
|
const modelName: string = (model as any)._collection;
|
|
89
154
|
|
|
90
155
|
section += `model ${modelName} {\n`;
|
|
91
|
-
section +=
|
|
156
|
+
section += `\t${this.generateId(configService.get('db_type'))}\n`;
|
|
92
157
|
|
|
93
158
|
for (const key in modelMetadatas) {
|
|
94
159
|
const modelMetadata = modelMetadatas[key].metadata;
|
|
@@ -175,4 +240,4 @@ export class DbHelper {
|
|
|
175
240
|
|
|
176
241
|
return resultField;
|
|
177
242
|
}
|
|
178
|
-
}
|
|
243
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { FieldsHelper } from './helper/FieldsHelper';
|
|
|
8
8
|
|
|
9
9
|
import type { FindByType } from './types/FindParams';
|
|
10
10
|
import type { ITimeSeries } from './types/ITimeSeries';
|
|
11
|
-
import type { IDbConfigHandler } from './types/DbConfigHandler';
|
|
11
|
+
import type { IDbConfigHandler, IDbConfigParams } from './types/DbConfigHandler';
|
|
12
12
|
import type { IRWSModel } from './types/IRWSModel';
|
|
13
13
|
import { RWSCollection, IRWSCollectionMeta, IRWSCollectionOpts } from "./decorators/RWSCollection";
|
|
14
14
|
|
|
@@ -18,6 +18,7 @@ export type {
|
|
|
18
18
|
IMetaOpts,
|
|
19
19
|
OpModelType,
|
|
20
20
|
IDbConfigHandler,
|
|
21
|
+
IDbConfigParams,
|
|
21
22
|
ITimeSeries,
|
|
22
23
|
}
|
|
23
24
|
|
|
@@ -16,14 +16,14 @@ class RWSModel<T> implements IModel {
|
|
|
16
16
|
|
|
17
17
|
[key: string]: any;
|
|
18
18
|
@TrackType(String)
|
|
19
|
-
id: string;
|
|
19
|
+
id: string | number;
|
|
20
20
|
static _collection: string = null;
|
|
21
21
|
static _RELATIONS = {};
|
|
22
22
|
static _BANNED_KEYS = ['_collection'];
|
|
23
23
|
static allModels: OpModelType<any>[] = [];
|
|
24
24
|
static _CUT_KEYS: string[] = [];
|
|
25
25
|
|
|
26
|
-
constructor(data: any) {
|
|
26
|
+
constructor(data: any = null) {
|
|
27
27
|
if(!this.getCollection()){
|
|
28
28
|
throw new Error('Model must have a collection defined');
|
|
29
29
|
}
|
|
@@ -89,7 +89,7 @@ class RWSModel<T> implements IModel {
|
|
|
89
89
|
return RelationUtils.hasRelation(this, key);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
protected bindRelation(key: string, relatedModel: RWSModel<any>): { connect: { id: string } } {
|
|
92
|
+
protected bindRelation(key: string, relatedModel: RWSModel<any>): { connect: { id: string | number } } {
|
|
93
93
|
return RelationUtils.bindRelation(relatedModel);
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -56,7 +56,7 @@ export class RelationUtils {
|
|
|
56
56
|
return relIds;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string } } {
|
|
59
|
+
static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } {
|
|
60
60
|
return {
|
|
61
61
|
connect: {
|
|
62
62
|
id: relatedModel.id
|
|
@@ -3,10 +3,15 @@ import { OpModelType } from "../models/interfaces/OpModelType";
|
|
|
3
3
|
export interface IDbConfigParams {
|
|
4
4
|
db_url?: string;
|
|
5
5
|
db_name?: string;
|
|
6
|
-
db_type?:
|
|
6
|
+
db_type?: 'mongodb' | 'mysql' | 'sqlite';
|
|
7
7
|
db_models?: OpModelType<any>[]
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
export interface IdGeneratorOptions {
|
|
11
|
+
useUuid?: boolean; // dla MySQL
|
|
12
|
+
customType?: string; // dla custom typów
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
export interface IDbConfigHandler {
|
|
11
16
|
get<K extends keyof IDbConfigParams>(key: K): IDbConfigParams[K];
|
|
12
17
|
}
|