@reldens/storage 0.86.0 → 0.88.0
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/CLAUDE.md +92 -18
- package/README.md +64 -21
- package/bin/reldens-storage.js +4 -25
- package/index.js +2 -0
- package/lib/prisma/prisma-client-loader.js +45 -0
- package/package.json +1 -1
package/CLAUDE.md
CHANGED
|
@@ -111,6 +111,7 @@ npx reldens-storage-prisma --host=<host> --database=<db> --user=<user> --passwor
|
|
|
111
111
|
- `prisma-metadata-loader.js`: Loads field metadata including defaults
|
|
112
112
|
- `prisma-type-caster.js`: Type casting and normalization
|
|
113
113
|
- `prisma-relation-resolver.js`: Relation mapping and transformations
|
|
114
|
+
- `prisma-client-loader.js`: Utility for loading Prisma Client instances
|
|
114
115
|
- Features:
|
|
115
116
|
- Schema-first approach with auto-introspection
|
|
116
117
|
- Type-safe queries with Prisma Client
|
|
@@ -212,24 +213,22 @@ Templates use placeholder replacement with `{{placeholderName}}` syntax.
|
|
|
212
213
|
|
|
213
214
|
## Generated File Structure
|
|
214
215
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
└── entities-translations.js # Translation keys
|
|
232
|
-
```
|
|
216
|
+
All generated files are created in the **generated-entities/** directory:
|
|
217
|
+
|
|
218
|
+
**Entity Definitions:**
|
|
219
|
+
- entities/[table-name]-entity.js
|
|
220
|
+
|
|
221
|
+
**Driver Models:**
|
|
222
|
+
- models/objection-js/[table-name]-model.js
|
|
223
|
+
- models/objection-js/registered-models-objection-js.js
|
|
224
|
+
- models/mikro-orm/[table-name]-model.js
|
|
225
|
+
- models/mikro-orm/registered-models-mikro-orm.js
|
|
226
|
+
- models/prisma/[table-name]-model.js
|
|
227
|
+
- models/prisma/registered-models-prisma.js
|
|
228
|
+
|
|
229
|
+
**Configuration Files:**
|
|
230
|
+
- entities-config.js (entity relation configuration)
|
|
231
|
+
- entities-translations.js (translation keys)
|
|
233
232
|
|
|
234
233
|
## Relation Keys Pattern
|
|
235
234
|
|
|
@@ -398,3 +397,78 @@ The `prepareDataWithRelations()` method (lines 156-199) automatically converts F
|
|
|
398
397
|
- `RELDENS_DB_PARAMS`: Database connection parameters (used by Prisma)
|
|
399
398
|
- Format: `key1=value1&key2=value2`
|
|
400
399
|
- Example: `authPlugin=mysql_native_password&sslmode=require`
|
|
400
|
+
|
|
401
|
+
## PrismaClientLoader Utility
|
|
402
|
+
|
|
403
|
+
**Location:** `lib/prisma/prisma-client-loader.js`
|
|
404
|
+
|
|
405
|
+
**Purpose:** Shared utility for loading Prisma Client instances in CLI tools and applications.
|
|
406
|
+
|
|
407
|
+
**Exported From Package:** Yes, available via `const { PrismaClientLoader } = require('@reldens/storage');`
|
|
408
|
+
|
|
409
|
+
**Method:**
|
|
410
|
+
```javascript
|
|
411
|
+
PrismaClientLoader.load(projectPath, customPath, connectionData)
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
**Parameters:**
|
|
415
|
+
- `projectPath` (string): Project root directory path
|
|
416
|
+
- `customPath` (string|null): Optional custom path to a Prisma client (overrides default)
|
|
417
|
+
- `connectionData` (object|null): Optional database connection configuration with properties:
|
|
418
|
+
- `client` (string): Database client type (mysql, postgresql, etc.)
|
|
419
|
+
- `user` (string): Database username
|
|
420
|
+
- `password` (string): Database password
|
|
421
|
+
- `host` (string): Database host
|
|
422
|
+
- `port` (number): Database port
|
|
423
|
+
- `database` (string): Database name
|
|
424
|
+
|
|
425
|
+
**Returns:** PrismaClient instance or null on error
|
|
426
|
+
|
|
427
|
+
**Behavior:**
|
|
428
|
+
- If `customPath` is provided, uses that path
|
|
429
|
+
- Otherwise, uses a default path: `projectPath/prisma/client`
|
|
430
|
+
- Validates that Prisma Client exists at the path
|
|
431
|
+
- Requires `prismaModule.PrismaClient` export
|
|
432
|
+
- If `connectionData` is null: Uses default connection from Prisma schema datasource
|
|
433
|
+
- If `connectionData` is provided: Builds custom connection string and overrides datasource
|
|
434
|
+
- Returns initialized PrismaClient instance
|
|
435
|
+
|
|
436
|
+
**Usage Examples:**
|
|
437
|
+
|
|
438
|
+
Using the default connection from schema:
|
|
439
|
+
```javascript
|
|
440
|
+
const { PrismaClientLoader } = require('@reldens/storage');
|
|
441
|
+
|
|
442
|
+
const prismaClient = PrismaClientLoader.load(process.cwd(), null, null);
|
|
443
|
+
if(!prismaClient){
|
|
444
|
+
console.error('Failed to load Prisma client');
|
|
445
|
+
process.exit(1);
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
Using custom connection:
|
|
450
|
+
```javascript
|
|
451
|
+
const { PrismaClientLoader } = require('@reldens/storage');
|
|
452
|
+
|
|
453
|
+
const prismaClient = PrismaClientLoader.load(
|
|
454
|
+
process.cwd(),
|
|
455
|
+
null,
|
|
456
|
+
{
|
|
457
|
+
client: 'mysql',
|
|
458
|
+
user: 'dbuser',
|
|
459
|
+
password: 'dbpass',
|
|
460
|
+
host: 'localhost',
|
|
461
|
+
port: 3306,
|
|
462
|
+
database: 'mydb'
|
|
463
|
+
}
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
if(!prismaClient){
|
|
467
|
+
console.error('Failed to load Prisma client');
|
|
468
|
+
process.exit(1);
|
|
469
|
+
}
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
**Used By:**
|
|
473
|
+
- `bin/reldens-storage.js`: CLI entity generator
|
|
474
|
+
- External packages: `@reldens/cms` CLI tools (update-password, generate-entities, generate-sitemap)
|
package/README.md
CHANGED
|
@@ -182,6 +182,49 @@ const entities = server.generateEntities();
|
|
|
182
182
|
|
|
183
183
|
Note: The PrismaDataServer requires the Prisma schema to be generated first. Make sure to run the `reldens-generate-prisma-schema` command before using PrismaDataServer.
|
|
184
184
|
|
|
185
|
+
### Loading Prisma Client Programmatically
|
|
186
|
+
|
|
187
|
+
If you need to load a Prisma Client instance in your CLI tools or applications:
|
|
188
|
+
|
|
189
|
+
Using the default connection from schema:
|
|
190
|
+
```javascript
|
|
191
|
+
const { PrismaClientLoader } = require('@reldens/storage');
|
|
192
|
+
|
|
193
|
+
const prismaClient = PrismaClientLoader.load(process.cwd(), null, null);
|
|
194
|
+
if(!prismaClient){
|
|
195
|
+
console.error('Failed to load Prisma client');
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Using custom connection:
|
|
201
|
+
```javascript
|
|
202
|
+
const { PrismaClientLoader } = require('@reldens/storage');
|
|
203
|
+
|
|
204
|
+
const prismaClient = PrismaClientLoader.load(
|
|
205
|
+
process.cwd(),
|
|
206
|
+
null,
|
|
207
|
+
{
|
|
208
|
+
client: 'mysql',
|
|
209
|
+
user: 'dbuser',
|
|
210
|
+
password: 'dbpass',
|
|
211
|
+
host: 'localhost',
|
|
212
|
+
port: 3306,
|
|
213
|
+
database: 'mydb'
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
if(!prismaClient){
|
|
218
|
+
console.error('Failed to load Prisma client');
|
|
219
|
+
process.exit(1);
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Parameters:
|
|
224
|
+
- `projectPath`: Project root directory
|
|
225
|
+
- `customPath`: Optional custom path to a Prisma client (null for default)
|
|
226
|
+
- `connectionData`: Optional database connection configuration object (null to use schema default)
|
|
227
|
+
|
|
185
228
|
## Custom Drivers
|
|
186
229
|
|
|
187
230
|
You can create custom storage drivers by extending the base classes:
|
|
@@ -237,28 +280,28 @@ All drivers must implement the methods defined in `BaseDriver`:
|
|
|
237
280
|
|
|
238
281
|
## Generated File Structure
|
|
239
282
|
|
|
240
|
-
When you run entity generation, files are created in the
|
|
283
|
+
When you run entity generation, all files are created in the **generated-entities/** directory:
|
|
241
284
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
285
|
+
**Entity Definitions:**
|
|
286
|
+
- entities/users-entity.js
|
|
287
|
+
- entities/players-entity.js
|
|
288
|
+
|
|
289
|
+
**ObjectionJS Models:**
|
|
290
|
+
- models/objection-js/users-model.js
|
|
291
|
+
- models/objection-js/players-model.js
|
|
292
|
+
- models/objection-js/registered-models-objection-js.js
|
|
293
|
+
|
|
294
|
+
**MikroORM Models:**
|
|
295
|
+
- models/mikro-orm/users-model.js
|
|
296
|
+
- models/mikro-orm/registered-models-mikro-orm.js
|
|
297
|
+
|
|
298
|
+
**Prisma Models:**
|
|
299
|
+
- models/prisma/users-model.js
|
|
300
|
+
- models/prisma/registered-models-prisma.js
|
|
301
|
+
|
|
302
|
+
**Configuration Files:**
|
|
303
|
+
- entities-config.js (entity relations)
|
|
304
|
+
- entities-translations.js (i18n keys)
|
|
262
305
|
|
|
263
306
|
### Entity Files
|
|
264
307
|
- **Entity classes**: Define properties, types, validations
|
package/bin/reldens-storage.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
const { EntitiesGenerator } = require('../lib/entities-generator');
|
|
10
|
-
const {
|
|
10
|
+
const { PrismaClientLoader } = require('../lib/prisma/prisma-client-loader');
|
|
11
11
|
const { Logger, sc } = require('@reldens/utils');
|
|
12
12
|
|
|
13
13
|
class StorageEntitiesGenerator
|
|
@@ -113,32 +113,11 @@ class StorageEntitiesGenerator
|
|
|
113
113
|
|
|
114
114
|
loadPrismaClient(connectionData)
|
|
115
115
|
{
|
|
116
|
-
let
|
|
117
|
-
if(!
|
|
118
|
-
prismaClientPath = FileHandler.joinPaths(this.projectPath, 'prisma', 'client');
|
|
119
|
-
}
|
|
120
|
-
if(!FileHandler.exists(prismaClientPath)){
|
|
121
|
-
Logger.critical('PrismaClient path does not exist: '+prismaClientPath);
|
|
116
|
+
let loadedClient = PrismaClientLoader.load(this.projectPath, this.prismaClientPath, connectionData);
|
|
117
|
+
if(!loadedClient){
|
|
122
118
|
Logger.info('Please run "npx prisma generate" first or provide --prismaClientPath argument.');
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
Logger.info('Loading PrismaClient from: '+prismaClientPath);
|
|
126
|
-
let prismaModule = require(prismaClientPath);
|
|
127
|
-
if(!prismaModule.PrismaClient){
|
|
128
|
-
Logger.critical('PrismaClient class not found at: '+prismaClientPath);
|
|
129
|
-
return null;
|
|
130
119
|
}
|
|
131
|
-
|
|
132
|
-
+connectionData.user
|
|
133
|
-
+(connectionData.password ? ':'+connectionData.password : '')
|
|
134
|
-
+'@'+connectionData.host
|
|
135
|
-
+':'+connectionData.port
|
|
136
|
-
+'/'+connectionData.database;
|
|
137
|
-
Logger.info('Creating PrismaClient with connection to: '+connectionData.database);
|
|
138
|
-
return new prismaModule.PrismaClient({
|
|
139
|
-
datasources: {db: {url: connectionString}},
|
|
140
|
-
log: ['error']
|
|
141
|
-
});
|
|
120
|
+
return loadedClient;
|
|
142
121
|
}
|
|
143
122
|
|
|
144
123
|
async run()
|
package/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const { MySQLTablesProvider } = require('./lib/mysql-tables-provider');
|
|
|
19
19
|
const { PrismaDriver } = require('./lib/prisma/prisma-driver');
|
|
20
20
|
const { PrismaDataServer } = require('./lib/prisma/prisma-data-server');
|
|
21
21
|
const { PrismaSchemaGenerator } = require('./lib/prisma/prisma-schema-generator');
|
|
22
|
+
const { PrismaClientLoader } = require('./lib/prisma/prisma-client-loader');
|
|
22
23
|
|
|
23
24
|
module.exports = {
|
|
24
25
|
// base:
|
|
@@ -46,6 +47,7 @@ module.exports = {
|
|
|
46
47
|
PrismaDataServer,
|
|
47
48
|
PrismaDriver,
|
|
48
49
|
PrismaSchemaGenerator,
|
|
50
|
+
PrismaClientLoader,
|
|
49
51
|
// entities:
|
|
50
52
|
EntitiesGenerator,
|
|
51
53
|
EntityProperties,
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Reldens - Storage - PrismaClientLoader
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { Logger } = require('@reldens/utils');
|
|
8
|
+
const { FileHandler } = require('@reldens/server-utils');
|
|
9
|
+
|
|
10
|
+
class PrismaClientLoader
|
|
11
|
+
{
|
|
12
|
+
|
|
13
|
+
static load(projectPath, customPath, connectionData)
|
|
14
|
+
{
|
|
15
|
+
let prismaClientPath = customPath;
|
|
16
|
+
if(!prismaClientPath){
|
|
17
|
+
prismaClientPath = FileHandler.joinPaths(projectPath, 'prisma', 'client');
|
|
18
|
+
}
|
|
19
|
+
if(!FileHandler.exists(prismaClientPath)){
|
|
20
|
+
Logger.critical('PrismaClient path does not exist: '+prismaClientPath);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
Logger.info('Loading PrismaClient from: '+prismaClientPath);
|
|
24
|
+
let prismaModule = require(prismaClientPath);
|
|
25
|
+
if(!prismaModule.PrismaClient){
|
|
26
|
+
Logger.critical('PrismaClient class not found at: '+prismaClientPath);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if(!connectionData){
|
|
30
|
+
Logger.info('Creating PrismaClient with default connection from schema');
|
|
31
|
+
return new prismaModule.PrismaClient({log: ['error']});
|
|
32
|
+
}
|
|
33
|
+
let connectionString = connectionData.client+'://'
|
|
34
|
+
+connectionData.user
|
|
35
|
+
+(connectionData.password ? ':'+connectionData.password : '')
|
|
36
|
+
+'@'+connectionData.host
|
|
37
|
+
+':'+connectionData.port
|
|
38
|
+
+'/'+connectionData.database;
|
|
39
|
+
Logger.info('Creating PrismaClient with connection to: '+connectionData.database);
|
|
40
|
+
return new prismaModule.PrismaClient({datasources: {db: {url: connectionString}}, log: ['error']});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports.PrismaClientLoader = PrismaClientLoader;
|