@geekmidas/testkit 1.0.3 → 1.0.4

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 (79) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/{Factory-BOX312yd.d.cts → Factory-Cwzho3c8.d.cts} +2 -2
  3. package/dist/{Factory-BOX312yd.d.cts.map → Factory-Cwzho3c8.d.cts.map} +1 -1
  4. package/dist/Factory.d.cts +2 -2
  5. package/dist/{KyselyFactory-Dy5zzV4B.d.cts → KyselyFactory-hMcVtvJq.d.cts} +3 -3
  6. package/dist/{KyselyFactory-Dy5zzV4B.d.cts.map → KyselyFactory-hMcVtvJq.d.cts.map} +1 -1
  7. package/dist/KyselyFactory.d.cts +3 -3
  8. package/dist/{ObjectionFactory-CD-WFuMJ.d.cts → ObjectionFactory-DW-qwnqO.d.cts} +3 -3
  9. package/dist/{ObjectionFactory-CD-WFuMJ.d.cts.map → ObjectionFactory-DW-qwnqO.d.cts.map} +1 -1
  10. package/dist/ObjectionFactory.d.cts +3 -3
  11. package/dist/{PostgresKyselyMigrator-B4pScubb.mjs → PostgresKyselyMigrator-BIKd7G5C.mjs} +16 -3
  12. package/dist/PostgresKyselyMigrator-BIKd7G5C.mjs.map +1 -0
  13. package/dist/{PostgresKyselyMigrator-CBltSOq5.d.cts → PostgresKyselyMigrator-CTNftoZK.d.cts} +16 -2
  14. package/dist/PostgresKyselyMigrator-CTNftoZK.d.cts.map +1 -0
  15. package/dist/{PostgresKyselyMigrator-C7ljZYvq.cjs → PostgresKyselyMigrator-CqNs3qX8.cjs} +16 -3
  16. package/dist/PostgresKyselyMigrator-CqNs3qX8.cjs.map +1 -0
  17. package/dist/{PostgresKyselyMigrator-DrVWncqd.d.mts → PostgresKyselyMigrator-x0uvWs3U.d.mts} +16 -2
  18. package/dist/PostgresKyselyMigrator-x0uvWs3U.d.mts.map +1 -0
  19. package/dist/PostgresKyselyMigrator.cjs +2 -2
  20. package/dist/PostgresKyselyMigrator.d.cts +2 -2
  21. package/dist/PostgresKyselyMigrator.d.mts +2 -2
  22. package/dist/PostgresKyselyMigrator.mjs +2 -2
  23. package/dist/{PostgresMigrator-S-YYosAC.d.mts → PostgresMigrator-C_QQ6q35.d.mts} +15 -2
  24. package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +1 -0
  25. package/dist/{PostgresMigrator-Bres0U6E.d.cts → PostgresMigrator-CeYy-eHF.d.cts} +15 -2
  26. package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +1 -0
  27. package/dist/{PostgresMigrator-DcP1o-T6.mjs → PostgresMigrator-DVAY04qN.mjs} +16 -2
  28. package/dist/PostgresMigrator-DVAY04qN.mjs.map +1 -0
  29. package/dist/{PostgresMigrator-CHiBYEg_.cjs → PostgresMigrator-M9jpzOvN.cjs} +16 -2
  30. package/dist/PostgresMigrator-M9jpzOvN.cjs.map +1 -0
  31. package/dist/PostgresMigrator.cjs +1 -1
  32. package/dist/PostgresMigrator.d.cts +1 -1
  33. package/dist/PostgresMigrator.d.mts +1 -1
  34. package/dist/PostgresMigrator.mjs +1 -1
  35. package/dist/{PostgresObjectionMigrator-CPfBAP7r.d.cts → PostgresObjectionMigrator-1j6YIB1c.d.mts} +2 -2
  36. package/dist/{PostgresObjectionMigrator-CPfBAP7r.d.cts.map → PostgresObjectionMigrator-1j6YIB1c.d.mts.map} +1 -1
  37. package/dist/{PostgresObjectionMigrator-DVEqB5tp.d.mts → PostgresObjectionMigrator-DHVC9h_P.d.cts} +2 -2
  38. package/dist/{PostgresObjectionMigrator-DVEqB5tp.d.mts.map → PostgresObjectionMigrator-DHVC9h_P.d.cts.map} +1 -1
  39. package/dist/{PostgresObjectionMigrator-BXLAVVwm.cjs → PostgresObjectionMigrator-DSaPhwjY.cjs} +2 -2
  40. package/dist/{PostgresObjectionMigrator-BXLAVVwm.cjs.map → PostgresObjectionMigrator-DSaPhwjY.cjs.map} +1 -1
  41. package/dist/{PostgresObjectionMigrator-BJ5X48U8.mjs → PostgresObjectionMigrator-DjPKdUbm.mjs} +2 -2
  42. package/dist/{PostgresObjectionMigrator-BJ5X48U8.mjs.map → PostgresObjectionMigrator-DjPKdUbm.mjs.map} +1 -1
  43. package/dist/PostgresObjectionMigrator.cjs +2 -2
  44. package/dist/PostgresObjectionMigrator.d.cts +2 -2
  45. package/dist/PostgresObjectionMigrator.d.mts +2 -2
  46. package/dist/PostgresObjectionMigrator.mjs +2 -2
  47. package/dist/better-auth.d.cts +2 -2
  48. package/dist/{directory-DGOcVlKD.d.cts → directory-CYXmolu1.d.mts} +3 -3
  49. package/dist/{directory-DGOcVlKD.d.cts.map → directory-CYXmolu1.d.mts.map} +1 -1
  50. package/dist/{directory-YzQUGC5g.d.mts → directory-DAnMWi50.d.cts} +3 -3
  51. package/dist/{directory-YzQUGC5g.d.mts.map → directory-DAnMWi50.d.cts.map} +1 -1
  52. package/dist/{faker-Cg76aFNO.d.cts → faker-Dg3trU4a.d.cts} +3 -3
  53. package/dist/{faker-Cg76aFNO.d.cts.map → faker-Dg3trU4a.d.cts.map} +1 -1
  54. package/dist/faker.d.cts +1 -1
  55. package/dist/kysely.cjs +2 -2
  56. package/dist/kysely.d.cts +5 -5
  57. package/dist/kysely.d.mts +2 -2
  58. package/dist/kysely.mjs +2 -2
  59. package/dist/objection.cjs +2 -2
  60. package/dist/objection.d.cts +5 -5
  61. package/dist/objection.d.mts +2 -2
  62. package/dist/objection.mjs +2 -2
  63. package/dist/os/directory.d.cts +1 -1
  64. package/dist/os/directory.d.mts +1 -1
  65. package/dist/os/index.d.cts +1 -1
  66. package/dist/os/index.d.mts +1 -1
  67. package/package.json +3 -3
  68. package/src/PostgresKyselyMigrator.ts +15 -1
  69. package/src/PostgresMigrator.ts +19 -2
  70. package/src/__tests__/PostgresKyselyMigrator.spec.ts +104 -0
  71. package/src/__tests__/PostgresMigrator.spec.ts +109 -0
  72. package/dist/PostgresKyselyMigrator-B4pScubb.mjs.map +0 -1
  73. package/dist/PostgresKyselyMigrator-C7ljZYvq.cjs.map +0 -1
  74. package/dist/PostgresKyselyMigrator-CBltSOq5.d.cts.map +0 -1
  75. package/dist/PostgresKyselyMigrator-DrVWncqd.d.mts.map +0 -1
  76. package/dist/PostgresMigrator-Bres0U6E.d.cts.map +0 -1
  77. package/dist/PostgresMigrator-CHiBYEg_.cjs.map +0 -1
  78. package/dist/PostgresMigrator-DcP1o-T6.mjs.map +0 -1
  79. package/dist/PostgresMigrator-S-YYosAC.d.mts.map +0 -1
@@ -18,6 +18,10 @@ class TestPostgresMigrator extends PostgresMigrator {
18
18
  public migrateError?: Error;
19
19
  public customMigrations: Array<() => Promise<void>> = [];
20
20
 
21
+ constructor(uri: string, afterCreate?: (uri: string) => Promise<void>) {
22
+ super(uri, afterCreate);
23
+ }
24
+
21
25
  async migrate(): Promise<void> {
22
26
  this.migrateCalled = true;
23
27
  if (this.migrateError) {
@@ -285,6 +289,111 @@ describe('PostgresMigrator', () => {
285
289
  });
286
290
  });
287
291
 
292
+ describe('afterCreate hook', () => {
293
+ it('should call afterCreate after database creation and before migrate', async () => {
294
+ const hookDbName = `test_after_create_${Date.now()}`;
295
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${hookDbName}`;
296
+ const callOrder: string[] = [];
297
+
298
+ const migrator = new TestPostgresMigrator(uri, async (receivedUri) => {
299
+ callOrder.push('afterCreate');
300
+ expect(receivedUri).toBe(uri);
301
+ });
302
+
303
+ migrator.addMigration(async () => {
304
+ callOrder.push('migrate');
305
+ });
306
+
307
+ const cleanup = await migrator.start();
308
+
309
+ expect(callOrder).toEqual(['afterCreate', 'migrate']);
310
+
311
+ await cleanup();
312
+ });
313
+
314
+ it('should skip afterCreate when not provided', async () => {
315
+ const noHookDbName = `test_no_hook_${Date.now()}`;
316
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${noHookDbName}`;
317
+ const migrator = new TestPostgresMigrator(uri);
318
+
319
+ const cleanup = await migrator.start();
320
+
321
+ expect(migrator.migrateCalled).toBe(true);
322
+
323
+ await cleanup();
324
+ });
325
+
326
+ it('should propagate afterCreate errors and not run migrate', async () => {
327
+ const errorHookDbName = `test_hook_error_${Date.now()}`;
328
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorHookDbName}`;
329
+
330
+ const migrator = new TestPostgresMigrator(uri, async () => {
331
+ throw new Error('afterCreate failed');
332
+ });
333
+
334
+ await expect(migrator.start()).rejects.toThrow('afterCreate failed');
335
+ expect(migrator.migrateCalled).toBe(false);
336
+
337
+ // Cleanup the created database
338
+ const cleanupClient = new Client({
339
+ host: 'localhost',
340
+ port: 5432,
341
+ user: 'geekmidas',
342
+ password: 'geekmidas',
343
+ database: 'postgres',
344
+ });
345
+ try {
346
+ await cleanupClient.connect();
347
+ await cleanupClient.query(
348
+ `DROP DATABASE IF EXISTS "${errorHookDbName}"`,
349
+ );
350
+ } finally {
351
+ await cleanupClient.end();
352
+ }
353
+ });
354
+
355
+ it('should allow afterCreate to set up database objects before migrations', async () => {
356
+ const setupDbName = `test_hook_setup_${Date.now()}`;
357
+ const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${setupDbName}`;
358
+
359
+ const migrator = new TestPostgresMigrator(uri, async () => {
360
+ const client = new Client({
361
+ host: 'localhost',
362
+ port: 5432,
363
+ user: 'geekmidas',
364
+ password: 'geekmidas',
365
+ database: setupDbName,
366
+ });
367
+ await client.connect();
368
+ await client.query(
369
+ "CREATE SCHEMA IF NOT EXISTS app; COMMENT ON SCHEMA app IS 'Created by afterCreate hook';",
370
+ );
371
+ await client.end();
372
+ });
373
+
374
+ migrator.addMigration(async () => {
375
+ // Verify schema exists during migration
376
+ const client = new Client({
377
+ host: 'localhost',
378
+ port: 5432,
379
+ user: 'geekmidas',
380
+ password: 'geekmidas',
381
+ database: setupDbName,
382
+ });
383
+ await client.connect();
384
+ const result = await client.query(
385
+ "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'app'",
386
+ );
387
+ expect(result.rowCount).toBe(1);
388
+ await client.end();
389
+ });
390
+
391
+ const cleanup = await migrator.start();
392
+
393
+ await cleanup();
394
+ });
395
+ });
396
+
288
397
  describe('abstract method', () => {
289
398
  it('should require concrete implementation of migrate method', () => {
290
399
  // TypeScript ensures abstract methods are implemented
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresKyselyMigrator-B4pScubb.mjs","names":["options: {\n\t\t\turi: string;\n\t\t\tdb: Kysely<any>;\n\t\t\tprovider: MigrationProvider;\n\t\t}"],"sources":["../src/PostgresKyselyMigrator.ts"],"sourcesContent":["import { type Kysely, type MigrationProvider, Migrator } from 'kysely';\nimport { PostgresMigrator } from './PostgresMigrator';\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * PostgreSQL migrator implementation for Kysely ORM.\n * Extends PostgresMigrator to provide Kysely-specific migration functionality.\n * Automatically creates test databases and applies migrations for testing environments.\n *\n * @example\n * ```typescript\n * import { FileMigrationProvider } from 'kysely';\n * import { PostgresKyselyMigrator } from '@geekmidas/testkit';\n *\n * // Create migration provider\n * const provider = new FileMigrationProvider({\n * fs: require('fs'),\n * path: require('path'),\n * migrationFolder: path.join(__dirname, 'migrations')\n * });\n *\n * // Create Kysely instance\n * const db = new Kysely<Database>({\n * dialect: new PostgresDialect({\n * pool: new Pool({ connectionString: uri })\n * })\n * });\n *\n * // Create and use migrator\n * const migrator = new PostgresKyselyMigrator({\n * uri: 'postgresql://localhost:5432/test_db',\n * db,\n * provider\n * });\n *\n * const cleanup = await migrator.start();\n * // Run tests...\n * await cleanup();\n * ```\n */\nexport class PostgresKyselyMigrator extends PostgresMigrator {\n\t/**\n\t * Creates a new PostgresKyselyMigrator instance.\n\t *\n\t * @param options - Configuration options\n\t * @param options.uri - PostgreSQL connection URI\n\t * @param options.db - Kysely database instance\n\t * @param options.provider - Migration provider for locating migration files\n\t */\n\tconstructor(\n\t\tprivate options: {\n\t\t\turi: string;\n\t\t\tdb: Kysely<any>;\n\t\t\tprovider: MigrationProvider;\n\t\t},\n\t) {\n\t\tsuper(options.uri);\n\t}\n\n\t/**\n\t * Executes Kysely migrations to the latest version.\n\t * Implements the abstract migrate() method from PostgresMigrator.\n\t *\n\t * @throws Error if migrations fail to apply\n\t * @returns Promise that resolves when all migrations are applied\n\t */\n\tasync migrate(): Promise<void> {\n\t\tconst migrator = new Migrator({\n\t\t\tdb: this.options.db,\n\t\t\tprovider: this.options.provider,\n\t\t});\n\t\tconst migrations = await migrator.migrateToLatest();\n\n\t\tif (migrations.error) {\n\t\t\tlogger.error(migrations.error, `Failed to apply migrations`);\n\t\t\tthrow migrations.error;\n\t\t}\n\n\t\tawait this.options.db.destroy();\n\n\t\tlogger.log(`Applied ${migrations.results?.length} migrations successfully`);\n\t}\n}\n"],"mappings":";;;;;;;AAMA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCf,IAAa,yBAAb,cAA4C,iBAAiB;;;;;;;;;CAS5D,YACSA,SAKP;AACD,QAAM,QAAQ,IAAI;EANV;CAOR;;;;;;;;CASD,MAAM,UAAyB;EAC9B,MAAM,WAAW,IAAI,SAAS;GAC7B,IAAI,KAAK,QAAQ;GACjB,UAAU,KAAK,QAAQ;EACvB;EACD,MAAM,aAAa,MAAM,SAAS,iBAAiB;AAEnD,MAAI,WAAW,OAAO;AACrB,UAAO,MAAM,WAAW,QAAQ,4BAA4B;AAC5D,SAAM,WAAW;EACjB;AAED,QAAM,KAAK,QAAQ,GAAG,SAAS;AAE/B,SAAO,KAAK,UAAU,WAAW,SAAS,OAAO,0BAA0B;CAC3E;AACD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresKyselyMigrator-C7ljZYvq.cjs","names":["PostgresMigrator","options: {\n\t\t\turi: string;\n\t\t\tdb: Kysely<any>;\n\t\t\tprovider: MigrationProvider;\n\t\t}","Migrator"],"sources":["../src/PostgresKyselyMigrator.ts"],"sourcesContent":["import { type Kysely, type MigrationProvider, Migrator } from 'kysely';\nimport { PostgresMigrator } from './PostgresMigrator';\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * PostgreSQL migrator implementation for Kysely ORM.\n * Extends PostgresMigrator to provide Kysely-specific migration functionality.\n * Automatically creates test databases and applies migrations for testing environments.\n *\n * @example\n * ```typescript\n * import { FileMigrationProvider } from 'kysely';\n * import { PostgresKyselyMigrator } from '@geekmidas/testkit';\n *\n * // Create migration provider\n * const provider = new FileMigrationProvider({\n * fs: require('fs'),\n * path: require('path'),\n * migrationFolder: path.join(__dirname, 'migrations')\n * });\n *\n * // Create Kysely instance\n * const db = new Kysely<Database>({\n * dialect: new PostgresDialect({\n * pool: new Pool({ connectionString: uri })\n * })\n * });\n *\n * // Create and use migrator\n * const migrator = new PostgresKyselyMigrator({\n * uri: 'postgresql://localhost:5432/test_db',\n * db,\n * provider\n * });\n *\n * const cleanup = await migrator.start();\n * // Run tests...\n * await cleanup();\n * ```\n */\nexport class PostgresKyselyMigrator extends PostgresMigrator {\n\t/**\n\t * Creates a new PostgresKyselyMigrator instance.\n\t *\n\t * @param options - Configuration options\n\t * @param options.uri - PostgreSQL connection URI\n\t * @param options.db - Kysely database instance\n\t * @param options.provider - Migration provider for locating migration files\n\t */\n\tconstructor(\n\t\tprivate options: {\n\t\t\turi: string;\n\t\t\tdb: Kysely<any>;\n\t\t\tprovider: MigrationProvider;\n\t\t},\n\t) {\n\t\tsuper(options.uri);\n\t}\n\n\t/**\n\t * Executes Kysely migrations to the latest version.\n\t * Implements the abstract migrate() method from PostgresMigrator.\n\t *\n\t * @throws Error if migrations fail to apply\n\t * @returns Promise that resolves when all migrations are applied\n\t */\n\tasync migrate(): Promise<void> {\n\t\tconst migrator = new Migrator({\n\t\t\tdb: this.options.db,\n\t\t\tprovider: this.options.provider,\n\t\t});\n\t\tconst migrations = await migrator.migrateToLatest();\n\n\t\tif (migrations.error) {\n\t\t\tlogger.error(migrations.error, `Failed to apply migrations`);\n\t\t\tthrow migrations.error;\n\t\t}\n\n\t\tawait this.options.db.destroy();\n\n\t\tlogger.log(`Applied ${migrations.results?.length} migrations successfully`);\n\t}\n}\n"],"mappings":";;;;;;;;AAMA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCf,IAAa,yBAAb,cAA4CA,0CAAiB;;;;;;;;;CAS5D,YACSC,SAKP;AACD,QAAM,QAAQ,IAAI;EANV;CAOR;;;;;;;;CASD,MAAM,UAAyB;EAC9B,MAAM,WAAW,IAAIC,gBAAS;GAC7B,IAAI,KAAK,QAAQ;GACjB,UAAU,KAAK,QAAQ;EACvB;EACD,MAAM,aAAa,MAAM,SAAS,iBAAiB;AAEnD,MAAI,WAAW,OAAO;AACrB,UAAO,MAAM,WAAW,QAAQ,4BAA4B;AAC5D,SAAM,WAAW;EACjB;AAED,QAAM,KAAK,QAAQ,GAAG,SAAS;AAE/B,SAAO,KAAK,UAAU,WAAW,SAAS,OAAO,0BAA0B;CAC3E;AACD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresKyselyMigrator-CBltSOq5.d.cts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"sourcesContent":[],"mappings":";;;;;;;AA4CA;;;;;;AAA4D;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAA/C,sBAAA,SAA+B,gBAAA;;;;;;;;;;;;QAYrC;cACM;;;;;;;;;aAaK"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresKyselyMigrator-DrVWncqd.d.mts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"sourcesContent":[],"mappings":";;;;;;;AA4CA;;;;;;AAA4D;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAA/C,sBAAA,SAA+B,gBAAA;;;;;;;;;;;;QAYrC;cACM;;;;;;;;;aAaK"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresMigrator-Bres0U6E.d.cts","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":[],"mappings":";;AAiEA;;;;;AA8FY;;;;;;;;;;;;;;;;;;uBA9FU,gBAAA;;;;;;;;;;;;;;sBAcD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAgFT,cAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresMigrator-CHiBYEg_.cjs","names":["uri: string","error: any"],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t */\n\tconstructor(private uri: string) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// 42P04 = duplicate_database — another process created it between our check and create\n\t\t\t\t\tif (error?.code === '42P04') {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that will drop the database when called.\n\t *\n\t * @returns Async cleanup function that drops the created database\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\ttry {\n\t\t\tawait PostgresMigrator.create(this.uri);\n\t\t\t// Implement migration logic here\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tawait PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;;;AAEA,MAAM,EAAE,QAAQ,GAAG;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAYA,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI;CAEpB,MAAM,KAAK,IAAI,OAAO;EACrB,MAAM,IAAI;EACV,UAAU,IAAI;EACd,MAAM,IAAI;EACV,MAAM,SAAS,IAAI,MAAM,GAAG;EAC5B,UAAU;CACV;CAED,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE;AACpC,KAAI,SAAS,SAAS,IAAI,CACzB,YAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,IAAI,CAAC;AAExD,QAAO;EAAE;EAAU;CAAI;AACvB;;;;AAKD,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;AA0Bf,IAAsB,mBAAtB,MAAsB,iBAAiB;;;;;;CAMtC,YAAoBA,KAAa;EAAb;CAAe;;;;;;;;;CAkBnC,aAAqB,OACpBA,KACuC;EACvC,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,IAAI;AAC/C,MAAI;AACH,SAAM,GAAG,SAAS;GAClB,MAAM,SAAS,MAAM,GAAG,OACtB,wDAAwD,SAAS,GAClE;AAED,OAAI,OAAO,aAAa,EACvB,KAAI;AACH,UAAM,GAAG,OAAO,mBAAmB,SAAS,GAAG;GAC/C,SAAQC,OAAY;AAEpB,QAAI,OAAO,SAAS,QACnB,QAAO,EAAE,gBAAgB,KAAM;AAEhC,UAAM;GACN;AAGF,UAAO,EACN,gBAAgB,OAAO,WAAW,OAAO,WAAW,IAAI,MACxD;EACD,UAAS;AACT,SAAM,GAAG,KAAK;EACd;CACD;;;;;;;;;CAUD,aAAqB,KAAKD,KAA4B;EACrD,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,IAAI;AAC/C,MAAI;AACH,SAAM,GAAG,SAAS;AAClB,SAAM,GAAG,OAAO,iBAAiB,SAAS,GAAG;EAC7C,UAAS;AACT,SAAM,GAAG,KAAK;EACd;CACD;;;;;;;;;;;;;;;;;;;;;;;CAwBD,MAAM,QAAQ;EACb,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,KAAK,IAAI;AACpD,MAAI;AACH,SAAM,iBAAiB,OAAO,KAAK,IAAI;AAEvC,SAAM,KAAK,SAAS;AACpB,UAAO,KAAK,sBAAsB,SAAS,EAAE;EAE7C,UAAS;AACT,SAAM,GAAG,KAAK;EACd;AAED,SAAO,YAAY;AAClB,SAAM,iBAAiB,KAAK,KAAK,IAAI;EACrC;CACD;AACD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresMigrator-DcP1o-T6.mjs","names":["uri: string","error: any"],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t */\n\tconstructor(private uri: string) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// 42P04 = duplicate_database — another process created it between our check and create\n\t\t\t\t\tif (error?.code === '42P04') {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that will drop the database when called.\n\t *\n\t * @returns Async cleanup function that drops the created database\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\ttry {\n\t\t\tawait PostgresMigrator.create(this.uri);\n\t\t\t// Implement migration logic here\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tawait PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;;AAEA,MAAM,EAAE,QAAQ,GAAG;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAYA,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI;CAEpB,MAAM,KAAK,IAAI,OAAO;EACrB,MAAM,IAAI;EACV,UAAU,IAAI;EACd,MAAM,IAAI;EACV,MAAM,SAAS,IAAI,MAAM,GAAG;EAC5B,UAAU;CACV;CAED,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE;AACpC,KAAI,SAAS,SAAS,IAAI,CACzB,YAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,IAAI,CAAC;AAExD,QAAO;EAAE;EAAU;CAAI;AACvB;;;;AAKD,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;AA0Bf,IAAsB,mBAAtB,MAAsB,iBAAiB;;;;;;CAMtC,YAAoBA,KAAa;EAAb;CAAe;;;;;;;;;CAkBnC,aAAqB,OACpBA,KACuC;EACvC,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,IAAI;AAC/C,MAAI;AACH,SAAM,GAAG,SAAS;GAClB,MAAM,SAAS,MAAM,GAAG,OACtB,wDAAwD,SAAS,GAClE;AAED,OAAI,OAAO,aAAa,EACvB,KAAI;AACH,UAAM,GAAG,OAAO,mBAAmB,SAAS,GAAG;GAC/C,SAAQC,OAAY;AAEpB,QAAI,OAAO,SAAS,QACnB,QAAO,EAAE,gBAAgB,KAAM;AAEhC,UAAM;GACN;AAGF,UAAO,EACN,gBAAgB,OAAO,WAAW,OAAO,WAAW,IAAI,MACxD;EACD,UAAS;AACT,SAAM,GAAG,KAAK;EACd;CACD;;;;;;;;;CAUD,aAAqB,KAAKD,KAA4B;EACrD,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,IAAI;AAC/C,MAAI;AACH,SAAM,GAAG,SAAS;AAClB,SAAM,GAAG,OAAO,iBAAiB,SAAS,GAAG;EAC7C,UAAS;AACT,SAAM,GAAG,KAAK;EACd;CACD;;;;;;;;;;;;;;;;;;;;;;;CAwBD,MAAM,QAAQ;EACb,MAAM,EAAE,UAAU,IAAI,GAAG,MAAM,YAAY,KAAK,IAAI;AACpD,MAAI;AACH,SAAM,iBAAiB,OAAO,KAAK,IAAI;AAEvC,SAAM,KAAK,SAAS;AACpB,UAAO,KAAK,sBAAsB,SAAS,EAAE;EAE7C,UAAS;AACT,SAAM,GAAG,KAAK;EACd;AAED,SAAO,YAAY;AAClB,SAAM,iBAAiB,KAAK,KAAK,IAAI;EACrC;CACD;AACD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"PostgresMigrator-S-YYosAC.d.mts","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":[],"mappings":";;AAiEA;;;;;AA8FY;;;;;;;;;;;;;;;;;;uBA9FU,gBAAA;;;;;;;;;;;;;;sBAcD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAgFT,cAAA"}