@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.
Files changed (60) hide show
  1. package/dist/decorators/InverseRelation.d.ts +12 -12
  2. package/dist/decorators/InverseRelation.js +24 -24
  3. package/dist/decorators/InverseTimeSeries.d.ts +8 -8
  4. package/dist/decorators/InverseTimeSeries.js +13 -13
  5. package/dist/decorators/RWSCollection.d.ts +12 -12
  6. package/dist/decorators/RWSCollection.js +16 -16
  7. package/dist/decorators/Relation.d.ts +19 -19
  8. package/dist/decorators/Relation.js +26 -26
  9. package/dist/decorators/TrackType.d.ts +20 -20
  10. package/dist/decorators/TrackType.js +41 -41
  11. package/dist/decorators/index.d.ts +5 -5
  12. package/dist/decorators/index.js +14 -14
  13. package/dist/helper/DbHelper.d.ts +12 -8
  14. package/dist/helper/DbHelper.js +185 -137
  15. package/dist/helper/FieldsHelper.d.ts +4 -4
  16. package/dist/helper/FieldsHelper.js +35 -35
  17. package/dist/index.d.ts +12 -12
  18. package/dist/index.js +19 -19
  19. package/dist/models/_model.d.ts +6 -6
  20. package/dist/models/_model.js +9 -9
  21. package/dist/models/core/RWSModel.d.ts +66 -66
  22. package/dist/models/core/RWSModel.js +400 -400
  23. package/dist/models/core/TimeSeriesModel.d.ts +1 -1
  24. package/dist/models/core/TimeSeriesModel.js +14 -14
  25. package/dist/models/index.d.ts +7 -7
  26. package/dist/models/index.js +8 -8
  27. package/dist/models/interfaces/IModel.d.ts +11 -11
  28. package/dist/models/interfaces/IModel.js +2 -2
  29. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -6
  30. package/dist/models/interfaces/IRWSModelServices.js +2 -2
  31. package/dist/models/interfaces/OpModelType.d.ts +31 -31
  32. package/dist/models/interfaces/OpModelType.js +2 -2
  33. package/dist/models/types/RelationTypes.d.ts +24 -24
  34. package/dist/models/types/RelationTypes.js +2 -2
  35. package/dist/models/utils/ModelUtils.d.ts +10 -10
  36. package/dist/models/utils/ModelUtils.js +56 -56
  37. package/dist/models/utils/PaginationUtils.d.ts +5 -5
  38. package/dist/models/utils/PaginationUtils.js +32 -32
  39. package/dist/models/utils/RelationUtils.d.ts +14 -14
  40. package/dist/models/utils/RelationUtils.js +65 -65
  41. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -11
  42. package/dist/models/utils/TimeSeriesUtils.js +35 -35
  43. package/dist/services/DBService.d.ts +37 -37
  44. package/dist/services/DBService.js +198 -198
  45. package/dist/types/DbConfigHandler.d.ts +14 -9
  46. package/dist/types/DbConfigHandler.js +2 -2
  47. package/dist/types/FindParams.d.ts +14 -14
  48. package/dist/types/FindParams.js +2 -2
  49. package/dist/types/IRWSModel.d.ts +3 -3
  50. package/dist/types/IRWSModel.js +2 -2
  51. package/dist/types/ITimeSeries.d.ts +6 -6
  52. package/dist/types/ITimeSeries.js +2 -2
  53. package/exec/src/cli.ts +1 -0
  54. package/package.json +1 -1
  55. package/src/helper/DbHelper.ts +77 -12
  56. package/src/index.ts +2 -1
  57. package/src/models/core/RWSModel.ts +3 -3
  58. package/src/models/interfaces/IModel.ts +1 -1
  59. package/src/models/utils/RelationUtils.ts +1 -1
  60. 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
@@ -66,6 +66,7 @@ async function main(): Promise<void>
66
66
  {
67
67
  console.log('INSTALL PRISMA');
68
68
  const cfg = await Config.getInstance();
69
+ process.env.PRISMA_DB_URL = cfg.get('db_url');
69
70
  DbHelper.installPrisma(cfg, new DBService(cfg), false);
70
71
  }
71
72
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "2.4.0",
4
+ "version": "2.4.2",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -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 = ${configService.get('db_models')}\n
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.DB_URL = dbUrl;
69
- const endPrisma = 'npx prisma';
70
-
71
- // console.log({cwd: process.cwd()})
72
- // const clientPath = path.join(rwsPath.findRootWorkspacePath(), 'node_modules', '.prisma', 'client');
73
- await rwsShell.runCommand(`${endPrisma} generate --schema=${schemaPath}`, process.cwd());
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 async generateModelSections(model: OpModelType<any>): Promise<string> {
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 += '\tid String @map("_id") @id @default(auto()) @db.ObjectId\n';
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
 
@@ -3,7 +3,7 @@ import { DBService } from '../../services/DBService';
3
3
 
4
4
  export interface IModel {
5
5
  [key: string]: any;
6
- id: string | null;
6
+ id: string |number | null;
7
7
  save: () => void;
8
8
  getDb: () => DBService;
9
9
  getCollection: () => string | null;
@@ -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?: string;
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
  }