@geekmidas/testkit 10.0.0-alpha.3 → 10.0.0-alpha.31
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/README.md +74 -0
- package/dist/Factory.cjs +51 -2
- package/dist/Factory.cjs.map +1 -0
- package/dist/Factory.d.cts +143 -3
- package/dist/Factory.d.cts.map +1 -0
- package/dist/Factory.d.mts +143 -3
- package/dist/Factory.d.mts.map +1 -0
- package/dist/Factory.mjs +50 -2
- package/dist/Factory.mjs.map +1 -0
- package/dist/KyselyFactory.cjs +237 -4
- package/dist/KyselyFactory.cjs.map +1 -0
- package/dist/KyselyFactory.d.cts +224 -4
- package/dist/KyselyFactory.d.cts.map +1 -0
- package/dist/KyselyFactory.d.mts +224 -4
- package/dist/KyselyFactory.d.mts.map +1 -0
- package/dist/KyselyFactory.mjs +236 -4
- package/dist/KyselyFactory.mjs.map +1 -0
- package/dist/ObjectionFactory.cjs +265 -4
- package/dist/ObjectionFactory.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +237 -4
- package/dist/ObjectionFactory.d.cts.map +1 -0
- package/dist/ObjectionFactory.d.mts +237 -4
- package/dist/ObjectionFactory.d.mts.map +1 -0
- package/dist/ObjectionFactory.mjs +264 -4
- package/dist/ObjectionFactory.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +94 -3
- package/dist/PostgresKyselyMigrator.cjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.cts +80 -3
- package/dist/PostgresKyselyMigrator.d.cts.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +80 -3
- package/dist/PostgresKyselyMigrator.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.mjs +93 -3
- package/dist/PostgresKyselyMigrator.mjs.map +1 -0
- package/dist/{PostgresMigrator-C_QQ6q35.d.mts → PostgresMigrator-CDPnnwMJ.d.cts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
- package/dist/{PostgresMigrator-CeYy-eHF.d.cts → PostgresMigrator-CDPnnwMJ.d.mts} +11 -5
- package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +178 -2
- package/dist/PostgresMigrator.cjs.map +1 -0
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +175 -2
- package/dist/PostgresMigrator.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +118 -3
- package/dist/PostgresObjectionMigrator.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator.d.cts +75 -3
- package/dist/PostgresObjectionMigrator.d.cts.map +1 -0
- package/dist/PostgresObjectionMigrator.d.mts +75 -3
- package/dist/PostgresObjectionMigrator.d.mts.map +1 -0
- package/dist/PostgresObjectionMigrator.mjs +117 -3
- package/dist/PostgresObjectionMigrator.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +66 -3
- package/dist/VitestKyselyTransactionIsolator.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.cts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.d.mts +60 -3
- package/dist/VitestKyselyTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +65 -3
- package/dist/VitestKyselyTransactionIsolator.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +62 -3
- package/dist/VitestObjectionTransactionIsolator.cjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.cts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.d.mts +56 -3
- package/dist/VitestObjectionTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.mjs +61 -3
- package/dist/VitestObjectionTransactionIsolator.mjs.map +1 -0
- package/dist/VitestTransactionIsolator.cjs +187 -4
- package/dist/VitestTransactionIsolator.cjs.map +1 -0
- package/dist/VitestTransactionIsolator.d.cts +219 -2
- package/dist/VitestTransactionIsolator.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator.d.mts +219 -2
- package/dist/VitestTransactionIsolator.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator.mjs +184 -2
- package/dist/VitestTransactionIsolator.mjs.map +1 -0
- package/dist/aws.cjs +3 -4
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +3 -8
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +3 -8
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +1 -2
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs +2 -2
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +4 -6
- package/dist/benchmark.d.cts.map +1 -1
- package/dist/benchmark.d.mts +4 -6
- package/dist/benchmark.d.mts.map +1 -1
- package/dist/benchmark.mjs +1 -1
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +31 -15
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +13142 -4
- package/dist/better-auth.d.cts.map +1 -1
- package/dist/better-auth.d.mts +13142 -4
- package/dist/better-auth.d.mts.map +1 -1
- package/dist/better-auth.mjs +29 -13
- package/dist/better-auth.mjs.map +1 -1
- package/dist/browser.cjs +127 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +47 -0
- package/dist/browser.d.cts.map +1 -0
- package/dist/browser.d.mts +47 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +125 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/context.cjs +80 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +55 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.mts +55 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.mjs +73 -0
- package/dist/context.mjs.map +1 -0
- package/dist/faker.cjs +262 -8
- package/dist/faker.cjs.map +1 -0
- package/dist/faker.d.cts +162 -2
- package/dist/faker.d.cts.map +1 -0
- package/dist/faker.d.mts +162 -2
- package/dist/faker.d.mts.map +1 -0
- package/dist/faker.mjs +255 -2
- package/dist/faker.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +1 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +1 -2
- package/dist/helpers.mjs.map +1 -1
- package/dist/initScript.cjs +15 -15
- package/dist/initScript.cjs.map +1 -1
- package/dist/initScript.d.cts +2 -4
- package/dist/initScript.d.cts.map +1 -1
- package/dist/initScript.d.mts +2 -4
- package/dist/initScript.d.mts.map +1 -1
- package/dist/initScript.mjs +10 -12
- package/dist/initScript.mjs.map +1 -1
- package/dist/kysely.cjs +10 -13
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +10 -14
- package/dist/kysely.d.cts.map +1 -1
- package/dist/kysely.d.mts +10 -14
- package/dist/kysely.d.mts.map +1 -1
- package/dist/kysely.mjs +10 -14
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs +3 -4
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -5
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.mts +1 -5
- package/dist/logger.d.mts.map +1 -1
- package/dist/logger.mjs +1 -2
- package/dist/logger.mjs.map +1 -1
- package/dist/mailbox.cjs +95 -0
- package/dist/mailbox.cjs.map +1 -0
- package/dist/mailbox.d.cts +58 -0
- package/dist/mailbox.d.cts.map +1 -0
- package/dist/mailbox.d.mts +58 -0
- package/dist/mailbox.d.mts.map +1 -0
- package/dist/mailbox.mjs +92 -0
- package/dist/mailbox.mjs.map +1 -0
- package/dist/objection.cjs +10 -13
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +10 -14
- package/dist/objection.d.cts.map +1 -1
- package/dist/objection.d.mts +10 -14
- package/dist/objection.d.mts.map +1 -1
- package/dist/objection.mjs +10 -14
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.cjs +31 -2
- package/dist/os/directory.cjs.map +1 -0
- package/dist/os/directory.d.cts +9 -2
- package/dist/os/directory.d.cts.map +1 -0
- package/dist/os/directory.d.mts +9 -2
- package/dist/os/directory.d.mts.map +1 -0
- package/dist/os/directory.mjs +25 -2
- package/dist/os/directory.mjs.map +1 -0
- package/dist/os/index.cjs +3 -3
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/os/index.mjs +2 -3
- package/dist/requestContext.cjs +10 -11
- package/dist/requestContext.cjs.map +1 -1
- package/dist/requestContext.d.cts +4 -8
- package/dist/requestContext.d.cts.map +1 -1
- package/dist/requestContext.d.mts +4 -8
- package/dist/requestContext.d.mts.map +1 -1
- package/dist/requestContext.mjs +1 -2
- package/dist/requestContext.mjs.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → rolldown-runtime-VH7oDXx4.cjs} +8 -10
- package/dist/timer.cjs +2 -2
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +1 -4
- package/dist/timer.d.cts.map +1 -1
- package/dist/timer.d.mts +1 -4
- package/dist/timer.d.mts.map +1 -1
- package/dist/timer.mjs +1 -1
- package/dist/timer.mjs.map +1 -1
- package/dist/transactions.cjs +123 -0
- package/dist/transactions.cjs.map +1 -0
- package/dist/transactions.d.cts +44 -0
- package/dist/transactions.d.cts.map +1 -0
- package/dist/transactions.d.mts +44 -0
- package/dist/transactions.d.mts.map +1 -0
- package/dist/transactions.mjs +118 -0
- package/dist/transactions.mjs.map +1 -0
- package/package.json +59 -16
- package/dist/Factory-BFVnMMCC.mjs +0 -51
- package/dist/Factory-BFVnMMCC.mjs.map +0 -1
- package/dist/Factory-BOX312yd.d.cts +0 -147
- package/dist/Factory-BOX312yd.d.cts.map +0 -1
- package/dist/Factory-BhjUOBWN.cjs +0 -57
- package/dist/Factory-BhjUOBWN.cjs.map +0 -1
- package/dist/Factory-C6W78ulZ.d.mts +0 -147
- package/dist/Factory-C6W78ulZ.d.mts.map +0 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs +0 -246
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +0 -1
- package/dist/KyselyFactory-Cc2UmOJk.d.mts +0 -228
- package/dist/KyselyFactory-Cc2UmOJk.d.mts.map +0 -1
- package/dist/KyselyFactory-DMswpwji.mjs +0 -241
- package/dist/KyselyFactory-DMswpwji.mjs.map +0 -1
- package/dist/KyselyFactory-Dy5zzV4B.d.cts +0 -228
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +0 -1
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts +0 -241
- package/dist/ObjectionFactory-BYnPr9ZP.d.mts.map +0 -1
- package/dist/ObjectionFactory-BeFBYcan.cjs +0 -272
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +0 -1
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts +0 -241
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +0 -1
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs +0 -267
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +0 -1
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts +0 -84
- package/dist/PostgresKyselyMigrator-B2M6Gjvv.d.cts.map +0 -1
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs +0 -101
- package/dist/PostgresKyselyMigrator-BGBxjnFv.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts +0 -84
- package/dist/PostgresKyselyMigrator-CvwXycJE.d.mts.map +0 -1
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs +0 -95
- package/dist/PostgresKyselyMigrator-PI0QhvB1.mjs.map +0 -1
- package/dist/PostgresMigrator-C_QQ6q35.d.mts.map +0 -1
- package/dist/PostgresMigrator-CeYy-eHF.d.cts.map +0 -1
- package/dist/PostgresMigrator-DVAY04qN.mjs +0 -166
- package/dist/PostgresMigrator-DVAY04qN.mjs.map +0 -1
- package/dist/PostgresMigrator-M9jpzOvN.cjs +0 -172
- package/dist/PostgresMigrator-M9jpzOvN.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts +0 -79
- package/dist/PostgresObjectionMigrator-1j6YIB1c.d.mts.map +0 -1
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts +0 -79
- package/dist/PostgresObjectionMigrator-DHVC9h_P.d.cts.map +0 -1
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs +0 -123
- package/dist/PostgresObjectionMigrator-DSaPhwjY.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs +0 -118
- package/dist/PostgresObjectionMigrator-DjPKdUbm.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs +0 -67
- package/dist/VitestKyselyTransactionIsolator-1Saieke7.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs +0 -72
- package/dist/VitestKyselyTransactionIsolator-BjJSXryR.cjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts +0 -64
- package/dist/VitestKyselyTransactionIsolator-CwlTo6yf.d.mts.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts +0 -64
- package/dist/VitestKyselyTransactionIsolator-DJasVuIZ.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs +0 -63
- package/dist/VitestObjectionTransactionIsolator-B_VSWFum.mjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CTTjvTPO.d.cts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts +0 -60
- package/dist/VitestObjectionTransactionIsolator-CyZG6nq4.d.mts.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs +0 -68
- package/dist/VitestObjectionTransactionIsolator-DDoJTu7e.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts +0 -223
- package/dist/VitestTransactionIsolator-BLaw80cx.d.mts.map +0 -1
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs +0 -188
- package/dist/VitestTransactionIsolator-CvwPecpl.mjs.map +0 -1
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs +0 -206
- package/dist/VitestTransactionIsolator-DyiX-QtK.cjs.map +0 -1
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts +0 -223
- package/dist/VitestTransactionIsolator-glcxyS_R.d.cts.map +0 -1
- package/dist/directory-BxjqmtCT.mjs +0 -27
- package/dist/directory-BxjqmtCT.mjs.map +0 -1
- package/dist/directory-DD_xQoSh.cjs +0 -33
- package/dist/directory-DD_xQoSh.cjs.map +0 -1
- package/dist/directory-DGOcVlKD.d.cts +0 -13
- package/dist/directory-DGOcVlKD.d.cts.map +0 -1
- package/dist/directory-DiQ_DkaF.d.mts +0 -13
- package/dist/directory-DiQ_DkaF.d.mts.map +0 -1
- package/dist/faker-B14IEMIN.cjs +0 -304
- package/dist/faker-B14IEMIN.cjs.map +0 -1
- package/dist/faker-BGKYFoCT.mjs +0 -262
- package/dist/faker-BGKYFoCT.mjs.map +0 -1
- package/dist/faker-Cg76aFNO.d.cts +0 -166
- package/dist/faker-Cg76aFNO.d.cts.map +0 -1
- package/dist/faker-D9gz7KjY.d.mts +0 -166
- package/dist/faker-D9gz7KjY.d.mts.map +0 -1
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
import "./PostgresMigrator-
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
|
|
2
|
+
import { Kysely } from "kysely";
|
|
3
|
+
import { MigrationProvider } from "kysely/migration";
|
|
4
|
+
//#region src/PostgresKyselyMigrator.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL migrator implementation for Kysely ORM.
|
|
7
|
+
* Extends PostgresMigrator to provide Kysely-specific migration functionality.
|
|
8
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { FileMigrationProvider } from 'kysely';
|
|
13
|
+
* import { PostgresKyselyMigrator } from '@geekmidas/testkit';
|
|
14
|
+
*
|
|
15
|
+
* // Create migration provider
|
|
16
|
+
* const provider = new FileMigrationProvider({
|
|
17
|
+
* fs: require('fs'),
|
|
18
|
+
* path: require('path'),
|
|
19
|
+
* migrationFolder: path.join(__dirname, 'migrations')
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Create Kysely instance
|
|
23
|
+
* const db = new Kysely<Database>({
|
|
24
|
+
* dialect: new PostgresDialect({
|
|
25
|
+
* pool: new Pool({ connectionString: uri })
|
|
26
|
+
* })
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Create and use migrator
|
|
30
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
31
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
32
|
+
* db,
|
|
33
|
+
* provider
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const cleanup = await migrator.start();
|
|
37
|
+
* // Run tests...
|
|
38
|
+
* await cleanup();
|
|
39
|
+
*
|
|
40
|
+
* // With afterCreate hook to create per-app users
|
|
41
|
+
* import { runInitScript } from '@geekmidas/testkit/postgres';
|
|
42
|
+
*
|
|
43
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
44
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
45
|
+
* db,
|
|
46
|
+
* provider,
|
|
47
|
+
* afterCreate: async (uri) => {
|
|
48
|
+
* await runInitScript('docker/postgres/init.sh', uri, env);
|
|
49
|
+
* },
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare class PostgresKyselyMigrator extends PostgresMigrator {
|
|
54
|
+
private options;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
57
|
+
*
|
|
58
|
+
* @param options - Configuration options
|
|
59
|
+
* @param options.uri - PostgreSQL connection URI
|
|
60
|
+
* @param options.db - Kysely database instance
|
|
61
|
+
* @param options.provider - Migration provider for locating migration files
|
|
62
|
+
* @param options.afterCreate - Optional hook called after database creation but before migrations
|
|
63
|
+
*/
|
|
64
|
+
constructor(options: {
|
|
65
|
+
uri: string;
|
|
66
|
+
db: Kysely<any>;
|
|
67
|
+
provider: MigrationProvider;
|
|
68
|
+
afterCreate?: (uri: string) => Promise<void>;
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* Executes Kysely migrations to the latest version.
|
|
72
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
73
|
+
*
|
|
74
|
+
* @throws Error if migrations fail to apply
|
|
75
|
+
* @returns Promise that resolves when all migrations are applied
|
|
76
|
+
*/
|
|
77
|
+
migrate(): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//# sourceMappingURL=PostgresKyselyMigrator.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresKyselyMigrator.d.cts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EADT,YACS;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
|
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
import "./PostgresMigrator-
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
|
|
2
|
+
import { MigrationProvider } from "kysely/migration";
|
|
3
|
+
import { Kysely } from "kysely";
|
|
4
|
+
//#region src/PostgresKyselyMigrator.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL migrator implementation for Kysely ORM.
|
|
7
|
+
* Extends PostgresMigrator to provide Kysely-specific migration functionality.
|
|
8
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { FileMigrationProvider } from 'kysely';
|
|
13
|
+
* import { PostgresKyselyMigrator } from '@geekmidas/testkit';
|
|
14
|
+
*
|
|
15
|
+
* // Create migration provider
|
|
16
|
+
* const provider = new FileMigrationProvider({
|
|
17
|
+
* fs: require('fs'),
|
|
18
|
+
* path: require('path'),
|
|
19
|
+
* migrationFolder: path.join(__dirname, 'migrations')
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Create Kysely instance
|
|
23
|
+
* const db = new Kysely<Database>({
|
|
24
|
+
* dialect: new PostgresDialect({
|
|
25
|
+
* pool: new Pool({ connectionString: uri })
|
|
26
|
+
* })
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Create and use migrator
|
|
30
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
31
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
32
|
+
* db,
|
|
33
|
+
* provider
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const cleanup = await migrator.start();
|
|
37
|
+
* // Run tests...
|
|
38
|
+
* await cleanup();
|
|
39
|
+
*
|
|
40
|
+
* // With afterCreate hook to create per-app users
|
|
41
|
+
* import { runInitScript } from '@geekmidas/testkit/postgres';
|
|
42
|
+
*
|
|
43
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
44
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
45
|
+
* db,
|
|
46
|
+
* provider,
|
|
47
|
+
* afterCreate: async (uri) => {
|
|
48
|
+
* await runInitScript('docker/postgres/init.sh', uri, env);
|
|
49
|
+
* },
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare class PostgresKyselyMigrator extends PostgresMigrator {
|
|
54
|
+
private options;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
57
|
+
*
|
|
58
|
+
* @param options - Configuration options
|
|
59
|
+
* @param options.uri - PostgreSQL connection URI
|
|
60
|
+
* @param options.db - Kysely database instance
|
|
61
|
+
* @param options.provider - Migration provider for locating migration files
|
|
62
|
+
* @param options.afterCreate - Optional hook called after database creation but before migrations
|
|
63
|
+
*/
|
|
64
|
+
constructor(options: {
|
|
65
|
+
uri: string;
|
|
66
|
+
db: Kysely<any>;
|
|
67
|
+
provider: MigrationProvider;
|
|
68
|
+
afterCreate?: (uri: string) => Promise<void>;
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* Executes Kysely migrations to the latest version.
|
|
72
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
73
|
+
*
|
|
74
|
+
* @throws Error if migrations fail to apply
|
|
75
|
+
* @returns Promise that resolves when all migrations are applied
|
|
76
|
+
*/
|
|
77
|
+
migrate(): Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//# sourceMappingURL=PostgresKyselyMigrator.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresKyselyMigrator.d.mts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EADT,YACS;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
|
|
@@ -1,4 +1,94 @@
|
|
|
1
|
-
import "./PostgresMigrator
|
|
2
|
-
import {
|
|
1
|
+
import { PostgresMigrator } from "./PostgresMigrator.mjs";
|
|
2
|
+
import { Migrator } from "kysely/migration";
|
|
3
|
+
//#region src/PostgresKyselyMigrator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default logger instance for migration operations.
|
|
6
|
+
*/
|
|
7
|
+
const logger = console;
|
|
8
|
+
/**
|
|
9
|
+
* PostgreSQL migrator implementation for Kysely ORM.
|
|
10
|
+
* Extends PostgresMigrator to provide Kysely-specific migration functionality.
|
|
11
|
+
* Automatically creates test databases and applies migrations for testing environments.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { FileMigrationProvider } from 'kysely';
|
|
16
|
+
* import { PostgresKyselyMigrator } from '@geekmidas/testkit';
|
|
17
|
+
*
|
|
18
|
+
* // Create migration provider
|
|
19
|
+
* const provider = new FileMigrationProvider({
|
|
20
|
+
* fs: require('fs'),
|
|
21
|
+
* path: require('path'),
|
|
22
|
+
* migrationFolder: path.join(__dirname, 'migrations')
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Create Kysely instance
|
|
26
|
+
* const db = new Kysely<Database>({
|
|
27
|
+
* dialect: new PostgresDialect({
|
|
28
|
+
* pool: new Pool({ connectionString: uri })
|
|
29
|
+
* })
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // Create and use migrator
|
|
33
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
34
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
35
|
+
* db,
|
|
36
|
+
* provider
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* const cleanup = await migrator.start();
|
|
40
|
+
* // Run tests...
|
|
41
|
+
* await cleanup();
|
|
42
|
+
*
|
|
43
|
+
* // With afterCreate hook to create per-app users
|
|
44
|
+
* import { runInitScript } from '@geekmidas/testkit/postgres';
|
|
45
|
+
*
|
|
46
|
+
* const migrator = new PostgresKyselyMigrator({
|
|
47
|
+
* uri: 'postgresql://localhost:5432/test_db',
|
|
48
|
+
* db,
|
|
49
|
+
* provider,
|
|
50
|
+
* afterCreate: async (uri) => {
|
|
51
|
+
* await runInitScript('docker/postgres/init.sh', uri, env);
|
|
52
|
+
* },
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
var PostgresKyselyMigrator = class extends PostgresMigrator {
|
|
57
|
+
options;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
60
|
+
*
|
|
61
|
+
* @param options - Configuration options
|
|
62
|
+
* @param options.uri - PostgreSQL connection URI
|
|
63
|
+
* @param options.db - Kysely database instance
|
|
64
|
+
* @param options.provider - Migration provider for locating migration files
|
|
65
|
+
* @param options.afterCreate - Optional hook called after database creation but before migrations
|
|
66
|
+
*/
|
|
67
|
+
constructor(options) {
|
|
68
|
+
super(options.uri, options.afterCreate);
|
|
69
|
+
this.options = options;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Executes Kysely migrations to the latest version.
|
|
73
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
74
|
+
*
|
|
75
|
+
* @throws Error if migrations fail to apply
|
|
76
|
+
* @returns Promise that resolves when all migrations are applied
|
|
77
|
+
*/
|
|
78
|
+
async migrate() {
|
|
79
|
+
const migrations = await new Migrator({
|
|
80
|
+
db: this.options.db,
|
|
81
|
+
provider: this.options.provider
|
|
82
|
+
}).migrateToLatest();
|
|
83
|
+
if (migrations.error) {
|
|
84
|
+
logger.error(migrations.error, `Failed to apply migrations`);
|
|
85
|
+
throw migrations.error;
|
|
86
|
+
}
|
|
87
|
+
await this.options.db.destroy();
|
|
88
|
+
logger.log(`Applied ${migrations.results?.length} migrations successfully`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
//#endregion
|
|
92
|
+
export { PostgresKyselyMigrator };
|
|
3
93
|
|
|
4
|
-
|
|
94
|
+
//# sourceMappingURL=PostgresKyselyMigrator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresKyselyMigrator.mjs","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"sourcesContent":["import type { Kysely } from 'kysely';\n// `Migrator` and the `MigrationProvider` type moved from the root barrel ('kysely')\n// to the 'kysely/migration' subpath in kysely 0.29+.\nimport { type MigrationProvider, Migrator } from 'kysely/migration';\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 * // With afterCreate hook to create per-app users\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new PostgresKyselyMigrator({\n * uri: 'postgresql://localhost:5432/test_db',\n * db,\n * provider,\n * afterCreate: async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * });\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 * @param options.afterCreate - Optional hook called after database creation but before migrations\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\tafterCreate?: (uri: string) => Promise<void>;\n\t\t},\n\t) {\n\t\tsuper(options.uri, options.afterCreate);\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":";;;;;;AASA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDf,IAAa,yBAAb,cAA4C,iBAAiB;CAWnD;;;;;;;;;;CADT,YACC,SAMC;EACD,MAAM,QAAQ,KAAK,QAAQ,WAAW;EAP9B,KAAA,UAAA;CAQT;;;;;;;;CASA,MAAM,UAAyB;EAK9B,MAAM,aAAa,MAAM,IAJJ,SAAS;GAC7B,IAAI,KAAK,QAAQ;GACjB,UAAU,KAAK,QAAQ;EACxB,CACgC,CAAC,CAAC,gBAAgB;EAElD,IAAI,WAAW,OAAO;GACrB,OAAO,MAAM,WAAW,OAAO,4BAA4B;GAC3D,MAAM,WAAW;EAClB;EAEA,MAAM,KAAK,QAAQ,GAAG,QAAQ;EAE9B,OAAO,IAAI,WAAW,WAAW,SAAS,OAAO,yBAAyB;CAC3E;AACD"}
|
|
@@ -71,9 +71,16 @@ declare abstract class PostgresMigrator {
|
|
|
71
71
|
private static drop;
|
|
72
72
|
/**
|
|
73
73
|
* Starts the migration process by creating the database and running migrations.
|
|
74
|
-
* Returns a cleanup function that
|
|
74
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
75
|
+
* it.
|
|
75
76
|
*
|
|
76
|
-
*
|
|
77
|
+
* A database that was already there belongs to whoever made it, and the
|
|
78
|
+
* cleanup leaves it alone. `gkm test` provisions the test database before
|
|
79
|
+
* the suite starts, with an owner role that may migrate it but not drop it:
|
|
80
|
+
* dropping it was never the suite's to do, and failed there as a permission
|
|
81
|
+
* error on the way out of an otherwise green run.
|
|
82
|
+
*
|
|
83
|
+
* @returns Async cleanup function that drops the database if it created it
|
|
77
84
|
*
|
|
78
85
|
* @example
|
|
79
86
|
* ```typescript
|
|
@@ -93,7 +100,6 @@ declare abstract class PostgresMigrator {
|
|
|
93
100
|
*/
|
|
94
101
|
start(): Promise<() => Promise<void>>;
|
|
95
102
|
}
|
|
96
|
-
//# sourceMappingURL=PostgresMigrator.d.ts.map
|
|
97
103
|
//#endregion
|
|
98
|
-
export { PostgresMigrator };
|
|
99
|
-
//# sourceMappingURL=PostgresMigrator-
|
|
104
|
+
export { PostgresMigrator as t };
|
|
105
|
+
//# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresMigrator-CDPnnwMJ.d.cts","names":[],"sources":["../src/PostgresMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA4EsB;UAQZ;UACA;;;;;;;EAFT,YACS,aACA,gBAAe,gBAAgB;;;;;;;WAS/B,WAAW;;;;;;;;;iBAUC;;;;;;;;;iBAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCf,SAAK,cAAA"}
|
|
@@ -71,9 +71,16 @@ declare abstract class PostgresMigrator {
|
|
|
71
71
|
private static drop;
|
|
72
72
|
/**
|
|
73
73
|
* Starts the migration process by creating the database and running migrations.
|
|
74
|
-
* Returns a cleanup function that
|
|
74
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
75
|
+
* it.
|
|
75
76
|
*
|
|
76
|
-
*
|
|
77
|
+
* A database that was already there belongs to whoever made it, and the
|
|
78
|
+
* cleanup leaves it alone. `gkm test` provisions the test database before
|
|
79
|
+
* the suite starts, with an owner role that may migrate it but not drop it:
|
|
80
|
+
* dropping it was never the suite's to do, and failed there as a permission
|
|
81
|
+
* error on the way out of an otherwise green run.
|
|
82
|
+
*
|
|
83
|
+
* @returns Async cleanup function that drops the database if it created it
|
|
77
84
|
*
|
|
78
85
|
* @example
|
|
79
86
|
* ```typescript
|
|
@@ -93,7 +100,6 @@ declare abstract class PostgresMigrator {
|
|
|
93
100
|
*/
|
|
94
101
|
start(): Promise<() => Promise<void>>;
|
|
95
102
|
}
|
|
96
|
-
//# sourceMappingURL=PostgresMigrator.d.ts.map
|
|
97
103
|
//#endregion
|
|
98
|
-
export { PostgresMigrator };
|
|
99
|
-
//# sourceMappingURL=PostgresMigrator-
|
|
104
|
+
export { PostgresMigrator as t };
|
|
105
|
+
//# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresMigrator-CDPnnwMJ.d.mts","names":[],"sources":["../src/PostgresMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA4EsB;UAQZ;UACA;;;;;;;EAFT,YACS,aACA,gBAAe,gBAAgB;;;;;;;WAS/B,WAAW;;;;;;;;;iBAUC;;;;;;;;;iBAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCf,SAAK,cAAA"}
|
|
@@ -1,3 +1,179 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_rolldown_runtime = require("./rolldown-runtime-VH7oDXx4.cjs");
|
|
3
|
+
let pg = require("pg");
|
|
4
|
+
pg = require_rolldown_runtime.__toESM(pg, 1);
|
|
5
|
+
//#region src/PostgresMigrator.ts
|
|
6
|
+
const { Client } = pg.default;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a PostgreSQL client connected to the 'postgres' database.
|
|
9
|
+
* Extracts connection details from the provided URI.
|
|
10
|
+
*
|
|
11
|
+
* @param uri - PostgreSQL connection URI
|
|
12
|
+
* @returns Object containing the target database name and client instance
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');
|
|
17
|
+
* // database = 'mydb'
|
|
18
|
+
* // db = Client instance connected to 'postgres' database
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
async function setupClient(uri) {
|
|
22
|
+
const url = new URL(uri);
|
|
23
|
+
const db = new Client({
|
|
24
|
+
user: url.username,
|
|
25
|
+
password: url.password,
|
|
26
|
+
host: url.hostname,
|
|
27
|
+
port: parseInt(url.port, 10),
|
|
28
|
+
database: "postgres"
|
|
29
|
+
});
|
|
30
|
+
let database = url.pathname.slice(1);
|
|
31
|
+
if (database.includes("?")) database = database.substring(0, database.indexOf("?"));
|
|
32
|
+
return {
|
|
33
|
+
database,
|
|
34
|
+
db
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Default logger instance for migration operations.
|
|
39
|
+
*/
|
|
40
|
+
const logger = console;
|
|
41
|
+
/**
|
|
42
|
+
* Abstract base class for PostgreSQL database migration utilities.
|
|
43
|
+
* Provides database creation, migration, and cleanup functionality for testing.
|
|
44
|
+
* Subclasses must implement the migrate() method to define migration logic.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* class MyMigrator extends PostgresMigrator {
|
|
49
|
+
* async migrate(): Promise<void> {
|
|
50
|
+
* // Run your migrations here
|
|
51
|
+
* await this.runMigrations();
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Use in tests
|
|
56
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
57
|
+
* const cleanup = await migrator.start();
|
|
58
|
+
*
|
|
59
|
+
* // With afterCreate hook (e.g. run init script to create per-app users)
|
|
60
|
+
* import { runInitScript } from '@geekmidas/testkit/postgres';
|
|
61
|
+
*
|
|
62
|
+
* const migrator = new MyMigrator(
|
|
63
|
+
* 'postgresql://localhost:5432/test_db',
|
|
64
|
+
* async (uri) => {
|
|
65
|
+
* await runInitScript('docker/postgres/init.sh', uri, env);
|
|
66
|
+
* },
|
|
67
|
+
* );
|
|
68
|
+
* const cleanup = await migrator.start();
|
|
69
|
+
*
|
|
70
|
+
* // Run tests...
|
|
71
|
+
*
|
|
72
|
+
* // Clean up
|
|
73
|
+
* await cleanup();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
var PostgresMigrator = class PostgresMigrator {
|
|
77
|
+
uri;
|
|
78
|
+
afterCreate;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new PostgresMigrator instance.
|
|
81
|
+
*
|
|
82
|
+
* @param uri - PostgreSQL connection URI
|
|
83
|
+
* @param afterCreate - Optional hook called after database creation but before migrations
|
|
84
|
+
*/
|
|
85
|
+
constructor(uri, afterCreate) {
|
|
86
|
+
this.uri = uri;
|
|
87
|
+
this.afterCreate = afterCreate;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
91
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
92
|
+
*
|
|
93
|
+
* @param uri - PostgreSQL connection URI
|
|
94
|
+
* @returns Object indicating whether the database already existed
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
static async create(uri) {
|
|
98
|
+
const { database, db } = await setupClient(uri);
|
|
99
|
+
try {
|
|
100
|
+
await db.connect();
|
|
101
|
+
const result = await db.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`);
|
|
102
|
+
if (result.rowCount === 0) try {
|
|
103
|
+
await db.query(`CREATE DATABASE "${database}"`);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error?.code === "42P04") return { alreadyExisted: true };
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
|
|
109
|
+
} finally {
|
|
110
|
+
await db.end();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Drops a PostgreSQL database.
|
|
115
|
+
* Used for cleanup after tests are complete.
|
|
116
|
+
*
|
|
117
|
+
* @param uri - PostgreSQL connection URI
|
|
118
|
+
* @throws Error if database cannot be dropped
|
|
119
|
+
* @private
|
|
120
|
+
*/
|
|
121
|
+
static async drop(uri) {
|
|
122
|
+
const { database, db } = await setupClient(uri);
|
|
123
|
+
try {
|
|
124
|
+
await db.connect();
|
|
125
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
126
|
+
} finally {
|
|
127
|
+
await db.end();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Starts the migration process by creating the database and running migrations.
|
|
132
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
133
|
+
* it.
|
|
134
|
+
*
|
|
135
|
+
* A database that was already there belongs to whoever made it, and the
|
|
136
|
+
* cleanup leaves it alone. `gkm test` provisions the test database before
|
|
137
|
+
* the suite starts, with an owner role that may migrate it but not drop it:
|
|
138
|
+
* dropping it was never the suite's to do, and failed there as a permission
|
|
139
|
+
* error on the way out of an otherwise green run.
|
|
140
|
+
*
|
|
141
|
+
* @returns Async cleanup function that drops the database if it created it
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
146
|
+
*
|
|
147
|
+
* // Start migrations and get cleanup function
|
|
148
|
+
* const cleanup = await migrator.start();
|
|
149
|
+
*
|
|
150
|
+
* try {
|
|
151
|
+
* // Run your tests here
|
|
152
|
+
* await runTests();
|
|
153
|
+
* } finally {
|
|
154
|
+
* // Always clean up
|
|
155
|
+
* await cleanup();
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
async start() {
|
|
160
|
+
const { database, db } = await setupClient(this.uri);
|
|
161
|
+
let created = false;
|
|
162
|
+
try {
|
|
163
|
+
const { alreadyExisted } = await PostgresMigrator.create(this.uri);
|
|
164
|
+
created = !alreadyExisted;
|
|
165
|
+
if (this.afterCreate) await this.afterCreate(this.uri);
|
|
166
|
+
await this.migrate();
|
|
167
|
+
logger.log(`Migrating database: ${database}`);
|
|
168
|
+
} finally {
|
|
169
|
+
await db.end();
|
|
170
|
+
}
|
|
171
|
+
return async () => {
|
|
172
|
+
if (created) await PostgresMigrator.drop(this.uri);
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
//#endregion
|
|
177
|
+
exports.PostgresMigrator = PostgresMigrator;
|
|
2
178
|
|
|
3
|
-
|
|
179
|
+
//# sourceMappingURL=PostgresMigrator.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresMigrator.cjs","names":[],"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 * // With afterCreate hook (e.g. run init script to create per-app users)\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new MyMigrator(\n * 'postgresql://localhost:5432/test_db',\n * async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * );\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 * @param afterCreate - Optional hook called after database creation but before migrations\n\t */\n\tconstructor(\n\t\tprivate uri: string,\n\t\tprivate afterCreate?: (uri: string) => Promise<void>,\n\t) {}\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 drops the database — if this call created\n\t * it.\n\t *\n\t * A database that was already there belongs to whoever made it, and the\n\t * cleanup leaves it alone. `gkm test` provisions the test database before\n\t * the suite starts, with an owner role that may migrate it but not drop it:\n\t * dropping it was never the suite's to do, and failed there as a permission\n\t * error on the way out of an otherwise green run.\n\t *\n\t * @returns Async cleanup function that drops the database if it created it\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\tlet created = false;\n\t\ttry {\n\t\t\tconst { alreadyExisted } = await PostgresMigrator.create(this.uri);\n\t\t\tcreated = !alreadyExisted;\n\t\t\tif (this.afterCreate) {\n\t\t\t\tawait this.afterCreate(this.uri);\n\t\t\t}\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\tif (created) await PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;;;;AAEA,MAAM,EAAE,WAAW,GAAA;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAY,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI,GAAG;CAEvB,MAAM,KAAK,IAAI,OAAO;EACrB,MAAM,IAAI;EACV,UAAU,IAAI;EACd,MAAM,IAAI;EACV,MAAM,SAAS,IAAI,MAAM,EAAE;EAC3B,UAAU;CACX,CAAC;CAED,IAAI,WAAW,IAAI,SAAS,MAAM,CAAC;CACnC,IAAI,SAAS,SAAS,GAAG,GACxB,WAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,GAAG,CAAC;CAEvD,OAAO;EAAE;EAAU;CAAG;AACvB;;;;AAKA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCf,IAAsB,mBAAtB,MAAsB,iBAAiB;CAQ7B;CACA;;;;;;;CAFT,YACC,KACA,aACC;EAFO,KAAA,MAAA;EACA,KAAA,cAAA;CACN;;;;;;;;;CAkBH,aAAqB,OACpB,KACuC;EACvC,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,GAAG;EAC9C,IAAI;GACH,MAAM,GAAG,QAAQ;GACjB,MAAM,SAAS,MAAM,GAAG,MACvB,yDAAyD,SAAS,EACnE;GAEA,IAAI,OAAO,aAAa,GACvB,IAAI;IACH,MAAM,GAAG,MAAM,oBAAoB,SAAS,EAAE;GAC/C,SAAS,OAAY;IAEpB,IAAI,OAAO,SAAS,SACnB,OAAO,EAAE,gBAAgB,KAAK;IAE/B,MAAM;GACP;GAGD,OAAO,EACN,gBAAgB,OAAO,WAAW,OAAO,WAAW,IAAI,MACzD;EACD,UAAU;GACT,MAAM,GAAG,IAAI;EACd;CACD;;;;;;;;;CAUA,aAAqB,KAAK,KAA4B;EACrD,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,GAAG;EAC9C,IAAI;GACH,MAAM,GAAG,QAAQ;GACjB,MAAM,GAAG,MAAM,kBAAkB,SAAS,EAAE;EAC7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;CACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BA,MAAM,QAAQ;EACb,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,KAAK,GAAG;EACnD,IAAI,UAAU;EACd,IAAI;GACH,MAAM,EAAE,mBAAmB,MAAM,iBAAiB,OAAO,KAAK,GAAG;GACjE,UAAU,CAAC;GACX,IAAI,KAAK,aACR,MAAM,KAAK,YAAY,KAAK,GAAG;GAEhC,MAAM,KAAK,QAAQ;GACnB,OAAO,IAAI,uBAAuB,UAAU;EAE7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;EAEA,OAAO,YAAY;GAClB,IAAI,SAAS,MAAM,iBAAiB,KAAK,KAAK,GAAG;EAClD;CACD;AACD"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { PostgresMigrator } from "./PostgresMigrator-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
|
|
2
2
|
export { PostgresMigrator };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { PostgresMigrator } from "./PostgresMigrator-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
|
|
2
2
|
export { PostgresMigrator };
|