fragment-ts 1.0.6 → 1.0.7

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.
@@ -99,11 +99,15 @@ export class TypeORMModule {
99
99
 
100
100
  switch (dbType) {
101
101
  case "sqlite":
102
- case "better-sqlite3":
103
102
  return this.buildSqliteConfig(
104
103
  merged as Partial<SqliteConnectionOptions>,
105
104
  );
106
105
 
106
+ case "better-sqlite3":
107
+ return this.buildBetterSqlite3Config(
108
+ merged as Partial<BetterSqlite3ConnectionOptions>,
109
+ );
110
+
107
111
  case "mysql":
108
112
  case "mariadb":
109
113
  return this.buildMysqlConfig(merged as Partial<MysqlConnectionOptions>);
@@ -113,13 +117,108 @@ export class TypeORMModule {
113
117
  merged as Partial<PostgresConnectionOptions>,
114
118
  );
115
119
 
120
+ case "cockroachdb":
121
+ return this.buildCockroachConfig(
122
+ merged as Partial<CockroachConnectionOptions>,
123
+ );
124
+
125
+ case "mssql":
126
+ return this.buildSqlServerConfig(
127
+ merged as Partial<SqlServerConnectionOptions>,
128
+ );
129
+
130
+ case "sap":
131
+ return this.buildSapConfig(merged as Partial<SapConnectionOptions>);
132
+
133
+ case "oracle":
134
+ return this.buildOracleConfig(
135
+ merged as Partial<OracleConnectionOptions>,
136
+ );
137
+
138
+ case "mongodb":
139
+ return this.buildMongoConfig(merged as Partial<MongoConnectionOptions>);
140
+
141
+ case "aurora-mysql":
142
+ return this.buildAuroraMysqlConfig(
143
+ merged as Partial<AuroraMysqlConnectionOptions>,
144
+ );
145
+
146
+ case "aurora-postgres":
147
+ return this.buildAuroraPostgresConfig(
148
+ merged as Partial<AuroraPostgresConnectionOptions>,
149
+ );
150
+
151
+ case "cordova":
152
+ return this.buildCordovaConfig(
153
+ merged as Partial<CordovaConnectionOptions>,
154
+ );
155
+
156
+ case "nativescript":
157
+ return this.buildNativescriptConfig(
158
+ merged as Partial<NativescriptConnectionOptions>,
159
+ );
160
+
161
+ case "react-native":
162
+ return this.buildReactNativeConfig(
163
+ merged as Partial<ReactNativeConnectionOptions>,
164
+ );
165
+
166
+ case "sqljs":
167
+ return this.buildSqljsConfig(merged as Partial<SqljsConnectionOptions>);
168
+
169
+ case "expo":
170
+ return this.buildExpoConfig(merged as Partial<ExpoConnectionOptions>);
171
+
172
+ case "capacitor":
173
+ return this.buildCapacitorConfig(
174
+ merged as Partial<CapacitorConnectionOptions>,
175
+ );
176
+
177
+ case "spanner":
178
+ return this.buildSpannerConfig(
179
+ merged as Partial<SpannerConnectionOptions>,
180
+ );
181
+
116
182
  default:
117
183
  throw new Error(`Unsupported database type: ${String(dbType)}`);
118
184
  }
119
185
  }
120
186
 
121
187
  /* ======================================================
122
- * Builders
188
+ * Environment Variable Interpolation
189
+ * ====================================================== */
190
+ private static interpolateEnvVars(value: any): any {
191
+ if (typeof value === "string") {
192
+ // Match ${VAR_NAME} or {VAR_NAME}
193
+ return value.replace(/\$?\{([^}]+)\}/g, (match, varName) => {
194
+ const envValue = process.env[varName];
195
+ if (envValue === undefined) {
196
+ console.warn(
197
+ `Warning: Environment variable ${varName} is not defined, using empty string`,
198
+ );
199
+ return "";
200
+ }
201
+ return envValue;
202
+ });
203
+ }
204
+
205
+ if (Array.isArray(value)) {
206
+ return value.map((item) => this.interpolateEnvVars(item));
207
+ }
208
+
209
+ if (value !== null && typeof value === "object") {
210
+ const result: any = {};
211
+ for (const key in value) {
212
+ result[key] = this.interpolateEnvVars(value[key]);
213
+ }
214
+ return result;
215
+ }
216
+
217
+ return value;
218
+ }
219
+
220
+ /* ======================================================
221
+ * Builders - SQL Databases
123
222
  * ====================================================== */
124
223
  private static buildSqliteConfig(
125
224
  config: Partial<SqliteConnectionOptions>,
@@ -136,11 +235,26 @@ export class TypeORMModule {
136
235
  };
137
236
  }
138
237
 
238
+ private static buildBetterSqlite3Config(
239
+ config: Partial<BetterSqlite3ConnectionOptions>,
240
+ ): BetterSqlite3ConnectionOptions {
241
+ return {
242
+ type: "better-sqlite3",
243
+ database: config.database ?? "database.sqlite",
244
+ synchronize: false,
245
+ logging: false,
246
+ entities: config.entities ?? ["dist/**/*.entity.js"],
247
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
248
+ subscribers: [],
249
+ ...config,
250
+ };
251
+ }
252
+
139
253
  private static buildMysqlConfig(
140
254
  config: Partial<MysqlConnectionOptions>,
141
255
  ): MysqlConnectionOptions {
142
256
  return {
143
- type: config.type ?? "mysql",
257
+ type: (config.type as "mysql" | "mariadb") ?? "mysql",
144
258
  host: config.host ?? "localhost",
145
259
  port: config.port ?? 3306,
146
260
  username: config.username ?? "root",
@@ -172,6 +286,302 @@ export class TypeORMModule {
172
286
  };
173
287
  }
174
288
 
289
+ private static buildCockroachConfig(
290
+ config: Partial<CockroachConnectionOptions>,
291
+ ): CockroachConnectionOptions {
292
+ return {
293
+ type: "cockroachdb",
294
+ host: config.host ?? "localhost",
295
+ port: config.port ?? 26257,
296
+ username: config.username ?? "root",
297
+ password: config.password ?? "",
298
+ database: config.database ?? "defaultdb",
299
+ synchronize: false,
300
+ logging: false,
301
+ entities: config.entities ?? ["dist/**/*.entity.js"],
302
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
303
+ timeTravelQueries: config.timeTravelQueries ?? false,
304
+ ...config,
305
+ };
306
+ }
307
+
308
+ private static buildSqlServerConfig(
309
+ config: Partial<SqlServerConnectionOptions>,
310
+ ): SqlServerConnectionOptions {
311
+ return {
312
+ type: "mssql",
313
+ host: config.host ?? "localhost",
314
+ port: config.port ?? 1433,
315
+ username: config.username ?? "sa",
316
+ password: config.password ?? "",
317
+ database: config.database ?? "master",
318
+ synchronize: false,
319
+ logging: false,
320
+ entities: config.entities ?? ["dist/**/*.entity.js"],
321
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
322
+ options: config.options,
323
+ ...config,
324
+ };
325
+ }
326
+
327
+ private static buildOracleConfig(
328
+ config: Partial<OracleConnectionOptions>,
329
+ ): OracleConnectionOptions {
330
+ return {
331
+ type: "oracle",
332
+ host: config.host ?? "localhost",
333
+ port: config.port ?? 1521,
334
+ username: config.username ?? "system",
335
+ password: config.password ?? "",
336
+ sid: config.sid ?? "xe",
337
+ synchronize: false,
338
+ logging: false,
339
+ entities: config.entities ?? ["dist/**/*.entity.js"],
340
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
341
+ ...config,
342
+ };
343
+ }
344
+
345
+ private static buildSapConfig(
346
+ config: Partial<SapConnectionOptions>,
347
+ ): SapConnectionOptions {
348
+ return {
349
+ type: "sap",
350
+ host: config.host ?? "localhost",
351
+ port: config.port ?? 30015,
352
+ username: config.username ?? "SYSTEM",
353
+ password: config.password ?? "",
354
+ schema: config.schema,
355
+ synchronize: false,
356
+ logging: false,
357
+ entities: config.entities ?? ["dist/**/*.entity.js"],
358
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
359
+ ...config,
360
+ };
361
+ }
362
+
363
+ /* ======================================================
364
+ * Builders - Cloud/Serverless Databases
365
+ * ====================================================== */
366
+ private static buildAuroraMysqlConfig(
367
+ config: Partial<AuroraMysqlConnectionOptions>,
368
+ ): AuroraMysqlConnectionOptions {
369
+ if (!config.region) {
370
+ throw new Error("AuroraMysqlConnectionOptions: region is required");
371
+ }
372
+ if (!config.secretArn) {
373
+ throw new Error("AuroraMysqlConnectionOptions: secretArn is required");
374
+ }
375
+ if (!config.resourceArn) {
376
+ throw new Error("AuroraMysqlConnectionOptions: resourceArn is required");
377
+ }
378
+
379
+ return {
380
+ type: "aurora-mysql",
381
+ region: config.region,
382
+ secretArn: config.secretArn,
383
+ resourceArn: config.resourceArn,
384
+ database: config.database ?? "app",
385
+ host: config.host ?? "localhost",
386
+ port: config.port ?? 3306,
387
+ username: config.username ?? "root",
388
+ password: config.password ?? "",
389
+ synchronize: false,
390
+ logging: false,
391
+ entities: config.entities ?? ["dist/**/*.entity.js"],
392
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
393
+ driver: config.driver,
394
+ serviceConfigOptions: config.serviceConfigOptions,
395
+ formatOptions: config.formatOptions,
396
+ legacySpatialSupport: config.legacySpatialSupport,
397
+ ssl: config.ssl,
398
+ };
399
+ }
400
+
401
+ private static buildAuroraPostgresConfig(
402
+ config: Partial<AuroraPostgresConnectionOptions>,
403
+ ): AuroraPostgresConnectionOptions {
404
+ if (!config.region) {
405
+ throw new Error("AuroraPostgresConnectionOptions: region is required");
406
+ }
407
+ if (!config.secretArn) {
408
+ throw new Error("AuroraPostgresConnectionOptions: secretArn is required");
409
+ }
410
+ if (!config.resourceArn) {
411
+ throw new Error(
412
+ "AuroraPostgresConnectionOptions: resourceArn is required",
413
+ );
414
+ }
415
+
416
+ return {
417
+ type: "aurora-postgres",
418
+ region: config.region,
419
+ secretArn: config.secretArn,
420
+ resourceArn: config.resourceArn,
421
+ database: config.database ?? "app",
422
+ synchronize: false,
423
+ logging: false,
424
+ entities: config.entities ?? ["dist/**/*.entity.js"],
425
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
426
+ driver: config.driver,
427
+ transformParameters: config.transformParameters,
428
+ poolErrorHandler: config.poolErrorHandler,
429
+ serviceConfigOptions: config.serviceConfigOptions,
430
+ formatOptions: config.formatOptions,
431
+ };
432
+ }
433
+
434
+ private static buildSpannerConfig(
435
+ config: Partial<SpannerConnectionOptions>,
436
+ ): SpannerConnectionOptions {
437
+ return {
438
+ type: "spanner",
439
+ projectId: config.projectId ?? "",
440
+ instanceId: config.instanceId ?? "",
441
+ databaseId: config.databaseId ?? "",
442
+ synchronize: false,
443
+ logging: false,
444
+ entities: config.entities ?? ["dist/**/*.entity.js"],
445
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
446
+ ...config,
447
+ };
448
+ }
449
+
450
+ /* ======================================================
451
+ * Builders - NoSQL Databases
452
+ * ====================================================== */
453
+ private static buildMongoConfig(
454
+ config: Partial<MongoConnectionOptions>,
455
+ ): MongoConnectionOptions {
456
+ return {
457
+ type: "mongodb",
458
+ host: config.host ?? "localhost",
459
+ port: config.port ?? 27017,
460
+ database: config.database ?? "app",
461
+ synchronize: false,
462
+ logging: false,
463
+ entities: config.entities ?? ["dist/**/*.entity.js"],
464
+ ...config,
465
+ };
466
+ }
467
+
468
+ /* ======================================================
469
+ * Builders - Mobile/Embedded Databases
470
+ * ====================================================== */
471
+ private static buildCordovaConfig(
472
+ config: Partial<CordovaConnectionOptions>,
473
+ ): CordovaConnectionOptions {
474
+ return {
475
+ type: "cordova",
476
+ database: config.database ?? "app.db",
477
+ location: config.location ?? "default",
478
+ synchronize: false,
479
+ logging: false,
480
+ entities: config.entities ?? ["dist/**/*.entity.js"],
481
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
482
+ ...config,
483
+ };
484
+ }
485
+
486
+ private static buildNativescriptConfig(
487
+ config: Partial<NativescriptConnectionOptions>,
488
+ ): NativescriptConnectionOptions {
489
+ if (!config.driver) {
490
+ throw new Error(
491
+ "NativescriptConnectionOptions: driver is required (e.g. require('nativescript-sqlite'))",
492
+ );
493
+ }
494
+
495
+ return {
496
+ type: "nativescript",
497
+ database: config.database ?? "app.db",
498
+ driver: config.driver,
499
+ readOnly: config.readOnly ?? false,
500
+ key: config.key,
501
+ multithreading: config.multithreading ?? false,
502
+ migrate: config.migrate ?? false,
503
+ iosFlags: config.iosFlags,
504
+ androidFlags: config.androidFlags,
505
+ synchronize: false,
506
+ logging: false,
507
+ entities: config.entities ?? ["dist/**/*.entity.js"],
508
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
509
+ ...config, // optional fields only
510
+ };
511
+ }
512
+
513
+ private static buildReactNativeConfig(
514
+ config: Partial<ReactNativeConnectionOptions>,
515
+ ): ReactNativeConnectionOptions {
516
+ return {
517
+ type: "react-native",
518
+ database: config.database ?? "app.db",
519
+ location: config.location ?? "default",
520
+ synchronize: false,
521
+ logging: false,
522
+ entities: config.entities ?? ["dist/**/*.entity.js"],
523
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
524
+ ...config,
525
+ };
526
+ }
527
+
528
+ private static buildSqljsConfig(
529
+ config: Partial<SqljsConnectionOptions>,
530
+ ): SqljsConnectionOptions {
531
+ return {
532
+ type: "sqljs",
533
+ autoSave: config.autoSave ?? true,
534
+ location: config.location ?? "browser",
535
+ synchronize: false,
536
+ logging: false,
537
+ entities: config.entities ?? ["dist/**/*.entity.js"],
538
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
539
+ ...config,
540
+ };
541
+ }
542
+
543
+ private static buildExpoConfig(
544
+ config: Partial<ExpoConnectionOptions>,
545
+ ): ExpoConnectionOptions {
546
+ if (!config.driver) {
547
+ throw new Error(
548
+ "ExpoConnectionOptions: driver is required (e.g. require('expo-sqlite'))",
549
+ );
550
+ }
551
+
552
+ return {
553
+ type: "expo",
554
+ database: config.database ?? "app.db",
555
+ driver: config.driver, // ✅ guaranteed
556
+ synchronize: false,
557
+ logging: false,
558
+ entities: config.entities ?? ["dist/**/*.entity.js"],
559
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
560
+ ...config, // optional fields
561
+ };
562
+ }
563
+
564
+ private static buildCapacitorConfig(
565
+ config: Partial<CapacitorConnectionOptions>,
566
+ ): CapacitorConnectionOptions {
567
+ if (!config.driver) {
568
+ throw new Error(
569
+ "CapacitorConnectionOptions: driver is required (e.g. new SQLiteConnection(CapacitorSQLite))",
570
+ );
571
+ }
572
+
573
+ return {
574
+ type: "capacitor",
575
+ database: config.database ?? "app.db",
576
+ driver: config.driver, // ✅ guaranteed
577
+ synchronize: false,
578
+ logging: false,
579
+ entities: config.entities ?? ["dist/**/*.entity.js"],
580
+ migrations: config.migrations ?? ["dist/migrations/**/*.js"],
581
+ ...config, // optional fields
582
+ };
583
+ }
584
+
175
585
  /* ======================================================
176
586
  * fragment.json loader
177
587
  * ====================================================== */
@@ -183,7 +593,10 @@ export class TypeORMModule {
183
593
  }
184
594
 
185
595
  const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
186
- return raw.database ?? {};
596
+ const dbConfig = raw.database ?? {};
597
+
598
+ // Interpolate environment variables
599
+ return this.interpolateEnvVars(dbConfig);
187
600
  }
188
601
 
189
602
  /* ======================================================