pgpm 4.34.2 → 4.35.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.
@@ -0,0 +1,78 @@
1
+ import { PGPM_DRIVER_EXPORT, } from '@pgpmjs/types';
2
+ import { createRequire } from 'node:module';
3
+ import { join } from 'node:path';
4
+ /** Package name of the built-in PGlite driver plugin (the `--pglite` alias). */
5
+ export const PGLITE_DRIVER_PLUGIN = '@pgpmjs/pglite-adapter';
6
+ /**
7
+ * Resolve the effective driver config from `pgpm.json` and CLI overrides.
8
+ *
9
+ * Precedence: an explicit override (from `--driver <pkg>` or the `--pglite`
10
+ * alias) beats the configured `driver` block. `--pglite=<dataDir>` sets
11
+ * `options.dataDir`. Returns undefined for the built-in `pg` (server) path.
12
+ */
13
+ export function resolveDriverConfig(configured, override) {
14
+ if (override) {
15
+ // Merge override options over configured options when targeting the same plugin.
16
+ const base = configured && configured.plugin === override.plugin ? configured.options : undefined;
17
+ return {
18
+ plugin: override.plugin,
19
+ options: { ...base, ...override.options },
20
+ };
21
+ }
22
+ return configured;
23
+ }
24
+ /**
25
+ * Translate the `--pglite` / `--driver` argv into a driver override.
26
+ *
27
+ * - `--pglite` (bare) → PGlite in-memory
28
+ * - `--pglite=./local.db` → PGlite persisted to `./local.db`
29
+ * - `--driver <pkg>` → arbitrary driver plugin
30
+ */
31
+ export function driverOverrideFromArgv(argv) {
32
+ if (argv.pglite !== undefined && argv.pglite !== false) {
33
+ const options = typeof argv.pglite === 'string' && argv.pglite.length > 0
34
+ ? { dataDir: argv.pglite }
35
+ : undefined;
36
+ return { plugin: PGLITE_DRIVER_PLUGIN, options };
37
+ }
38
+ if (argv.driver) {
39
+ return { plugin: argv.driver };
40
+ }
41
+ return undefined;
42
+ }
43
+ /**
44
+ * Load and activate the configured driver plugin, resolving it from the
45
+ * *consumer's* project (`cwd`) — the same way ESLint/Babel resolve plugins.
46
+ * pgpm ships no backend dependencies; the plugin is declared by the user's
47
+ * project. Returns undefined on the built-in server path.
48
+ *
49
+ * Resolution uses `createRequire` from `node:module` (not the ambient
50
+ * `require`) so it behaves identically in both makage build outputs: the CJS
51
+ * bundle and the `es2022` ESM bundle, where a bare `require`/`require.resolve`
52
+ * is undefined at runtime.
53
+ */
54
+ export async function activateDriver(configured, override, cwd) {
55
+ const cfg = resolveDriverConfig(configured, override);
56
+ if (!cfg)
57
+ return undefined;
58
+ // A require bound to the consumer's project directory. `createRequire` is the
59
+ // one primitive available and identical in both CJS and ESM runtimes.
60
+ const requireFrom = createRequire(join(cwd, 'package.json'));
61
+ let mod;
62
+ try {
63
+ const entry = requireFrom.resolve(cfg.plugin);
64
+ mod = requireFrom(entry);
65
+ }
66
+ catch {
67
+ const extra = cfg.plugin.includes('pglite') ? ' @electric-sql/pglite' : '';
68
+ throw new Error(`pgpm driver "${cfg.plugin}" is not installed in this project.\n` +
69
+ `Run: npm i ${cfg.plugin}${extra}`);
70
+ }
71
+ // Support both a direct named export and an interop `default` wrapper.
72
+ const factory = (mod[PGPM_DRIVER_EXPORT] ??
73
+ mod.default?.[PGPM_DRIVER_EXPORT]);
74
+ if (typeof factory !== 'function') {
75
+ throw new Error(`"${cfg.plugin}" does not export ${PGPM_DRIVER_EXPORT} — it is not a pgpm driver plugin.`);
76
+ }
77
+ return factory(cfg.options);
78
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './database';
2
+ export * from './driver';
2
3
  export * from './display';
3
4
  export * from './deployed-changes';
4
5
  export * from './module-utils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pgpm",
3
- "version": "4.34.2",
3
+ "version": "4.35.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PostgreSQL Package Manager - Database migration and package management CLI",
6
6
  "main": "index.js",
@@ -46,18 +46,18 @@
46
46
  },
47
47
  "dependencies": {
48
48
  "@inquirerer/utils": "^3.3.9",
49
- "@pgpmjs/core": "^6.27.1",
50
- "@pgpmjs/env": "^2.26.1",
51
- "@pgpmjs/export": "^0.26.5",
49
+ "@pgpmjs/core": "^6.27.2",
50
+ "@pgpmjs/env": "^2.26.2",
51
+ "@pgpmjs/export": "^0.26.6",
52
52
  "@pgpmjs/logger": "^2.13.0",
53
- "@pgpmjs/types": "^2.31.0",
53
+ "@pgpmjs/types": "^2.32.0",
54
54
  "@pgsql/quotes": "^17.1.0",
55
55
  "appstash": "^0.7.0",
56
56
  "find-and-require-package-json": "^0.9.1",
57
57
  "genomic": "^5.6.2",
58
58
  "inquirerer": "^4.9.1",
59
59
  "js-yaml": "^4.1.0",
60
- "pg-cache": "^3.14.0",
60
+ "pg-cache": "^3.14.1",
61
61
  "pg-env": "^1.17.0",
62
62
  "pgsql-deparser": "^17.18.3",
63
63
  "semver": "^7.8.1",
@@ -76,5 +76,5 @@
76
76
  "pg",
77
77
  "pgsql"
78
78
  ],
79
- "gitHead": "7cbe15fac21a9345b169203ba9542ba9a50c866c"
79
+ "gitHead": "87e65cd31d4e23c367ca4d398416b19ac863c7de"
80
80
  }
@@ -0,0 +1,43 @@
1
+ import { PgpmDriverConfig, PgpmDriverSession } from '@pgpmjs/types';
2
+ /** Package name of the built-in PGlite driver plugin (the `--pglite` alias). */
3
+ export declare const PGLITE_DRIVER_PLUGIN = "@pgpmjs/pglite-adapter";
4
+ /**
5
+ * Resolve the effective driver config from `pgpm.json` and CLI overrides.
6
+ *
7
+ * Precedence: an explicit override (from `--driver <pkg>` or the `--pglite`
8
+ * alias) beats the configured `driver` block. `--pglite=<dataDir>` sets
9
+ * `options.dataDir`. Returns undefined for the built-in `pg` (server) path.
10
+ */
11
+ export declare function resolveDriverConfig(configured: PgpmDriverConfig | undefined, override?: {
12
+ plugin: string;
13
+ options?: Record<string, unknown>;
14
+ }): PgpmDriverConfig | undefined;
15
+ /**
16
+ * Translate the `--pglite` / `--driver` argv into a driver override.
17
+ *
18
+ * - `--pglite` (bare) → PGlite in-memory
19
+ * - `--pglite=./local.db` → PGlite persisted to `./local.db`
20
+ * - `--driver <pkg>` → arbitrary driver plugin
21
+ */
22
+ export declare function driverOverrideFromArgv(argv: {
23
+ pglite?: boolean | string;
24
+ driver?: string;
25
+ }): {
26
+ plugin: string;
27
+ options?: Record<string, unknown>;
28
+ } | undefined;
29
+ /**
30
+ * Load and activate the configured driver plugin, resolving it from the
31
+ * *consumer's* project (`cwd`) — the same way ESLint/Babel resolve plugins.
32
+ * pgpm ships no backend dependencies; the plugin is declared by the user's
33
+ * project. Returns undefined on the built-in server path.
34
+ *
35
+ * Resolution uses `createRequire` from `node:module` (not the ambient
36
+ * `require`) so it behaves identically in both makage build outputs: the CJS
37
+ * bundle and the `es2022` ESM bundle, where a bare `require`/`require.resolve`
38
+ * is undefined at runtime.
39
+ */
40
+ export declare function activateDriver(configured: PgpmDriverConfig | undefined, override: {
41
+ plugin: string;
42
+ options?: Record<string, unknown>;
43
+ } | undefined, cwd: string): Promise<PgpmDriverSession | undefined>;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PGLITE_DRIVER_PLUGIN = void 0;
4
+ exports.resolveDriverConfig = resolveDriverConfig;
5
+ exports.driverOverrideFromArgv = driverOverrideFromArgv;
6
+ exports.activateDriver = activateDriver;
7
+ const types_1 = require("@pgpmjs/types");
8
+ const node_module_1 = require("node:module");
9
+ const node_path_1 = require("node:path");
10
+ /** Package name of the built-in PGlite driver plugin (the `--pglite` alias). */
11
+ exports.PGLITE_DRIVER_PLUGIN = '@pgpmjs/pglite-adapter';
12
+ /**
13
+ * Resolve the effective driver config from `pgpm.json` and CLI overrides.
14
+ *
15
+ * Precedence: an explicit override (from `--driver <pkg>` or the `--pglite`
16
+ * alias) beats the configured `driver` block. `--pglite=<dataDir>` sets
17
+ * `options.dataDir`. Returns undefined for the built-in `pg` (server) path.
18
+ */
19
+ function resolveDriverConfig(configured, override) {
20
+ if (override) {
21
+ // Merge override options over configured options when targeting the same plugin.
22
+ const base = configured && configured.plugin === override.plugin ? configured.options : undefined;
23
+ return {
24
+ plugin: override.plugin,
25
+ options: { ...base, ...override.options },
26
+ };
27
+ }
28
+ return configured;
29
+ }
30
+ /**
31
+ * Translate the `--pglite` / `--driver` argv into a driver override.
32
+ *
33
+ * - `--pglite` (bare) → PGlite in-memory
34
+ * - `--pglite=./local.db` → PGlite persisted to `./local.db`
35
+ * - `--driver <pkg>` → arbitrary driver plugin
36
+ */
37
+ function driverOverrideFromArgv(argv) {
38
+ if (argv.pglite !== undefined && argv.pglite !== false) {
39
+ const options = typeof argv.pglite === 'string' && argv.pglite.length > 0
40
+ ? { dataDir: argv.pglite }
41
+ : undefined;
42
+ return { plugin: exports.PGLITE_DRIVER_PLUGIN, options };
43
+ }
44
+ if (argv.driver) {
45
+ return { plugin: argv.driver };
46
+ }
47
+ return undefined;
48
+ }
49
+ /**
50
+ * Load and activate the configured driver plugin, resolving it from the
51
+ * *consumer's* project (`cwd`) — the same way ESLint/Babel resolve plugins.
52
+ * pgpm ships no backend dependencies; the plugin is declared by the user's
53
+ * project. Returns undefined on the built-in server path.
54
+ *
55
+ * Resolution uses `createRequire` from `node:module` (not the ambient
56
+ * `require`) so it behaves identically in both makage build outputs: the CJS
57
+ * bundle and the `es2022` ESM bundle, where a bare `require`/`require.resolve`
58
+ * is undefined at runtime.
59
+ */
60
+ async function activateDriver(configured, override, cwd) {
61
+ const cfg = resolveDriverConfig(configured, override);
62
+ if (!cfg)
63
+ return undefined;
64
+ // A require bound to the consumer's project directory. `createRequire` is the
65
+ // one primitive available and identical in both CJS and ESM runtimes.
66
+ const requireFrom = (0, node_module_1.createRequire)((0, node_path_1.join)(cwd, 'package.json'));
67
+ let mod;
68
+ try {
69
+ const entry = requireFrom.resolve(cfg.plugin);
70
+ mod = requireFrom(entry);
71
+ }
72
+ catch {
73
+ const extra = cfg.plugin.includes('pglite') ? ' @electric-sql/pglite' : '';
74
+ throw new Error(`pgpm driver "${cfg.plugin}" is not installed in this project.\n` +
75
+ `Run: npm i ${cfg.plugin}${extra}`);
76
+ }
77
+ // Support both a direct named export and an interop `default` wrapper.
78
+ const factory = (mod[types_1.PGPM_DRIVER_EXPORT] ??
79
+ mod.default?.[types_1.PGPM_DRIVER_EXPORT]);
80
+ if (typeof factory !== 'function') {
81
+ throw new Error(`"${cfg.plugin}" does not export ${types_1.PGPM_DRIVER_EXPORT} — it is not a pgpm driver plugin.`);
82
+ }
83
+ return factory(cfg.options);
84
+ }
package/utils/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './database';
2
+ export * from './driver';
2
3
  export * from './display';
3
4
  export * from './deployed-changes';
4
5
  export * from './module-utils';
package/utils/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./database"), exports);
18
+ __exportStar(require("./driver"), exports);
18
19
  __exportStar(require("./display"), exports);
19
20
  __exportStar(require("./deployed-changes"), exports);
20
21
  __exportStar(require("./module-utils"), exports);