pgsql-client 1.5.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ephemeral.d.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { PgConfig } from 'pg-env';
2
+ import { DbAdmin } from './admin';
3
+ /**
4
+ * Options for creating an ephemeral database
5
+ */
6
+ export interface EphemeralDbOptions {
7
+ /**
8
+ * Database name prefix (default: 'ephemeral_')
9
+ */
10
+ prefix?: string;
11
+ /**
12
+ * PostgreSQL extensions to install after creation
13
+ */
14
+ extensions?: string[];
15
+ /**
16
+ * Base PostgreSQL configuration (host, port, user, password)
17
+ * If not provided, uses environment variables via pg-env
18
+ */
19
+ baseConfig?: Partial<PgConfig>;
20
+ /**
21
+ * Enable verbose logging
22
+ */
23
+ verbose?: boolean;
24
+ }
25
+ /**
26
+ * Options for tearing down an ephemeral database
27
+ */
28
+ export interface TeardownOptions {
29
+ /**
30
+ * If true, keeps the database instead of dropping it (useful for debugging)
31
+ */
32
+ keepDb?: boolean;
33
+ }
34
+ /**
35
+ * Result of creating an ephemeral database
36
+ */
37
+ export interface EphemeralDbResult {
38
+ /**
39
+ * The name of the created database
40
+ */
41
+ name: string;
42
+ /**
43
+ * Full PostgreSQL configuration for connecting to the ephemeral database
44
+ */
45
+ config: PgConfig;
46
+ /**
47
+ * Database admin instance for additional operations
48
+ */
49
+ admin: DbAdmin;
50
+ /**
51
+ * Teardown function to clean up the ephemeral database
52
+ * Call this when done to drop the database (unless keepDb is true)
53
+ */
54
+ teardown: (opts?: TeardownOptions) => void;
55
+ }
56
+ /**
57
+ * Create an ephemeral (temporary) PostgreSQL database
58
+ *
59
+ * Creates a new database with a unique UUID-based name. The database
60
+ * can be used for testing, code generation, or other temporary purposes.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const { config, teardown } = createEphemeralDb();
65
+ *
66
+ * // Use the database...
67
+ * const pool = new Pool(config);
68
+ * await pool.query('SELECT 1');
69
+ * await pool.end();
70
+ *
71
+ * // Clean up
72
+ * teardown();
73
+ *
74
+ * // Or keep for debugging
75
+ * teardown({ keepDb: true });
76
+ * ```
77
+ */
78
+ export declare function createEphemeralDb(options?: EphemeralDbOptions): EphemeralDbResult;
79
+ /**
80
+ * Create an ephemeral database asynchronously
81
+ *
82
+ * Same as createEphemeralDb but returns a Promise for consistency
83
+ * with async workflows.
84
+ */
85
+ export declare function createEphemeralDbAsync(options?: EphemeralDbOptions): Promise<EphemeralDbResult>;
package/ephemeral.js ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEphemeralDb = createEphemeralDb;
4
+ exports.createEphemeralDbAsync = createEphemeralDbAsync;
5
+ /**
6
+ * Ephemeral Database Utilities
7
+ *
8
+ * Provides utilities for creating and managing temporary PostgreSQL databases
9
+ * for testing, code generation, and other ephemeral use cases.
10
+ */
11
+ const crypto_1 = require("crypto");
12
+ const pg_env_1 = require("pg-env");
13
+ const admin_1 = require("./admin");
14
+ /**
15
+ * Create an ephemeral (temporary) PostgreSQL database
16
+ *
17
+ * Creates a new database with a unique UUID-based name. The database
18
+ * can be used for testing, code generation, or other temporary purposes.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const { config, teardown } = createEphemeralDb();
23
+ *
24
+ * // Use the database...
25
+ * const pool = new Pool(config);
26
+ * await pool.query('SELECT 1');
27
+ * await pool.end();
28
+ *
29
+ * // Clean up
30
+ * teardown();
31
+ *
32
+ * // Or keep for debugging
33
+ * teardown({ keepDb: true });
34
+ * ```
35
+ */
36
+ function createEphemeralDb(options = {}) {
37
+ const { prefix = 'ephemeral_', extensions = [], baseConfig = {}, verbose = false, } = options;
38
+ // Generate unique database name
39
+ const dbName = `${prefix}${(0, crypto_1.randomUUID)().replace(/-/g, '_')}`;
40
+ // Get base config from environment, merged with any provided config
41
+ const config = (0, pg_env_1.getPgEnvOptions)({
42
+ ...baseConfig,
43
+ database: dbName,
44
+ });
45
+ // Create admin instance for database operations
46
+ const admin = new admin_1.DbAdmin(config, verbose);
47
+ // Create the database
48
+ admin.create(dbName);
49
+ // Install extensions if specified
50
+ if (extensions.length > 0) {
51
+ admin.installExtensions(extensions, dbName);
52
+ }
53
+ // Create teardown function
54
+ const teardown = (opts = {}) => {
55
+ const { keepDb = false } = opts;
56
+ if (keepDb) {
57
+ if (verbose) {
58
+ console.log(`[ephemeral-db] Keeping database: ${dbName}`);
59
+ }
60
+ return;
61
+ }
62
+ try {
63
+ admin.drop(dbName);
64
+ if (verbose) {
65
+ console.log(`[ephemeral-db] Dropped database: ${dbName}`);
66
+ }
67
+ }
68
+ catch (err) {
69
+ if (verbose) {
70
+ console.error(`[ephemeral-db] Failed to drop database ${dbName}:`, err);
71
+ }
72
+ }
73
+ };
74
+ return {
75
+ name: dbName,
76
+ config,
77
+ admin,
78
+ teardown,
79
+ };
80
+ }
81
+ /**
82
+ * Create an ephemeral database asynchronously
83
+ *
84
+ * Same as createEphemeralDb but returns a Promise for consistency
85
+ * with async workflows.
86
+ */
87
+ async function createEphemeralDbAsync(options = {}) {
88
+ return createEphemeralDb(options);
89
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Ephemeral Database Utilities
3
+ *
4
+ * Provides utilities for creating and managing temporary PostgreSQL databases
5
+ * for testing, code generation, and other ephemeral use cases.
6
+ */
7
+ import { randomUUID } from 'crypto';
8
+ import { getPgEnvOptions } from 'pg-env';
9
+ import { DbAdmin } from './admin';
10
+ /**
11
+ * Create an ephemeral (temporary) PostgreSQL database
12
+ *
13
+ * Creates a new database with a unique UUID-based name. The database
14
+ * can be used for testing, code generation, or other temporary purposes.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const { config, teardown } = createEphemeralDb();
19
+ *
20
+ * // Use the database...
21
+ * const pool = new Pool(config);
22
+ * await pool.query('SELECT 1');
23
+ * await pool.end();
24
+ *
25
+ * // Clean up
26
+ * teardown();
27
+ *
28
+ * // Or keep for debugging
29
+ * teardown({ keepDb: true });
30
+ * ```
31
+ */
32
+ export function createEphemeralDb(options = {}) {
33
+ const { prefix = 'ephemeral_', extensions = [], baseConfig = {}, verbose = false, } = options;
34
+ // Generate unique database name
35
+ const dbName = `${prefix}${randomUUID().replace(/-/g, '_')}`;
36
+ // Get base config from environment, merged with any provided config
37
+ const config = getPgEnvOptions({
38
+ ...baseConfig,
39
+ database: dbName,
40
+ });
41
+ // Create admin instance for database operations
42
+ const admin = new DbAdmin(config, verbose);
43
+ // Create the database
44
+ admin.create(dbName);
45
+ // Install extensions if specified
46
+ if (extensions.length > 0) {
47
+ admin.installExtensions(extensions, dbName);
48
+ }
49
+ // Create teardown function
50
+ const teardown = (opts = {}) => {
51
+ const { keepDb = false } = opts;
52
+ if (keepDb) {
53
+ if (verbose) {
54
+ console.log(`[ephemeral-db] Keeping database: ${dbName}`);
55
+ }
56
+ return;
57
+ }
58
+ try {
59
+ admin.drop(dbName);
60
+ if (verbose) {
61
+ console.log(`[ephemeral-db] Dropped database: ${dbName}`);
62
+ }
63
+ }
64
+ catch (err) {
65
+ if (verbose) {
66
+ console.error(`[ephemeral-db] Failed to drop database ${dbName}:`, err);
67
+ }
68
+ }
69
+ };
70
+ return {
71
+ name: dbName,
72
+ config,
73
+ admin,
74
+ teardown,
75
+ };
76
+ }
77
+ /**
78
+ * Create an ephemeral database asynchronously
79
+ *
80
+ * Same as createEphemeralDb but returns a Promise for consistency
81
+ * with async workflows.
82
+ */
83
+ export async function createEphemeralDbAsync(options = {}) {
84
+ return createEphemeralDb(options);
85
+ }
package/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './admin';
2
2
  export * from './client';
3
3
  export * from './context-utils';
4
+ export * from './ephemeral';
4
5
  export * from './roles';
5
6
  export { streamSql } from './stream';
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './admin';
2
2
  export * from './client';
3
3
  export * from './context-utils';
4
+ export * from './ephemeral';
4
5
  export * from './roles';
5
6
  export { streamSql } from './stream';
package/index.js CHANGED
@@ -18,6 +18,7 @@ exports.streamSql = void 0;
18
18
  __exportStar(require("./admin"), exports);
19
19
  __exportStar(require("./client"), exports);
20
20
  __exportStar(require("./context-utils"), exports);
21
+ __exportStar(require("./ephemeral"), exports);
21
22
  __exportStar(require("./roles"), exports);
22
23
  var stream_1 = require("./stream");
23
24
  Object.defineProperty(exports, "streamSql", { enumerable: true, get: function () { return stream_1.streamSql; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pgsql-client",
3
- "version": "1.5.1",
3
+ "version": "2.0.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PostgreSQL client utilities with query helpers, RLS context management, and database administration",
6
6
  "main": "index.js",
@@ -45,11 +45,11 @@
45
45
  "makage": "^0.1.10"
46
46
  },
47
47
  "dependencies": {
48
- "@pgpmjs/core": "^4.17.1",
49
- "@pgpmjs/logger": "^1.5.0",
48
+ "@pgpmjs/core": "^5.0.0",
49
+ "@pgpmjs/logger": "^2.0.0",
50
50
  "@pgpmjs/types": "^2.15.0",
51
51
  "pg": "^8.17.1",
52
52
  "pg-env": "^1.3.0"
53
53
  },
54
- "gitHead": "39b5c59b01b8ce391dc14daf5a9430ca0ff67574"
54
+ "gitHead": "390f4dd57fc158554518ec454bf2a4856d550552"
55
55
  }