pgsql-seed 0.0.1 → 0.2.1
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/LICENSE +23 -0
- package/README.md +39 -625
- package/esm/index.d.ts +2 -0
- package/esm/index.js +12 -7
- package/esm/pgpm.d.ts +37 -0
- package/esm/pgpm.js +52 -0
- package/index.d.ts +2 -7
- package/index.js +20 -23
- package/package.json +27 -32
- package/pgpm.d.ts +37 -0
- package/pgpm.js +56 -0
- package/admin.d.ts +0 -26
- package/admin.js +0 -144
- package/connect.d.ts +0 -19
- package/connect.js +0 -95
- package/context-utils.d.ts +0 -8
- package/context-utils.js +0 -28
- package/esm/admin.js +0 -140
- package/esm/connect.js +0 -90
- package/esm/context-utils.js +0 -25
- package/esm/manager.js +0 -138
- package/esm/roles.js +0 -32
- package/esm/seed/adapters.js +0 -23
- package/esm/seed/csv.js +0 -108
- package/esm/seed/index.js +0 -14
- package/esm/seed/json.js +0 -36
- package/esm/seed/pgpm.js +0 -28
- package/esm/seed/sql.js +0 -15
- package/esm/seed/types.js +0 -1
- package/esm/stream.js +0 -96
- package/esm/test-client.js +0 -168
- package/esm/utils.js +0 -91
- package/manager.d.ts +0 -26
- package/manager.js +0 -142
- package/roles.d.ts +0 -17
- package/roles.js +0 -38
- package/seed/adapters.d.ts +0 -4
- package/seed/adapters.js +0 -28
- package/seed/csv.d.ts +0 -15
- package/seed/csv.js +0 -114
- package/seed/index.d.ts +0 -14
- package/seed/index.js +0 -31
- package/seed/json.d.ts +0 -12
- package/seed/json.js +0 -40
- package/seed/pgpm.d.ts +0 -10
- package/seed/pgpm.js +0 -32
- package/seed/sql.d.ts +0 -7
- package/seed/sql.js +0 -18
- package/seed/types.d.ts +0 -13
- package/seed/types.js +0 -2
- package/stream.d.ts +0 -33
- package/stream.js +0 -99
- package/test-client.d.ts +0 -55
- package/test-client.js +0 -172
- package/utils.d.ts +0 -17
- package/utils.js +0 -105
package/esm/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// Re-export everything from pg-seed (core seeding utilities)
|
|
2
|
+
export {
|
|
3
|
+
// CSV utilities
|
|
4
|
+
exportCsv, loadCsv, loadCsvMap,
|
|
5
|
+
// JSON utilities
|
|
6
|
+
insertJson, insertJsonMap,
|
|
7
|
+
// SQL utilities
|
|
8
|
+
execSql, loadSql, loadSqlFiles,
|
|
9
|
+
// Utility
|
|
10
|
+
unwrapClient } from 'pg-seed';
|
|
11
|
+
// pgpm integration (requires @pgpmjs/core)
|
|
12
|
+
export { deployPgpm, loadPgpm } from './pgpm';
|
package/esm/pgpm.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PgConfig } from 'pg-env';
|
|
2
|
+
/**
|
|
3
|
+
* Deploy a pgpm package to a PostgreSQL database.
|
|
4
|
+
*
|
|
5
|
+
* @param config - PostgreSQL configuration (host, port, database, user, password)
|
|
6
|
+
* @param cwd - Current working directory containing the pgpm package (defaults to process.cwd())
|
|
7
|
+
* @param cache - Whether to enable caching (defaults to false)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Client } from 'pg';
|
|
12
|
+
* import { deployPgpm } from 'pgsql-seed';
|
|
13
|
+
*
|
|
14
|
+
* const config = {
|
|
15
|
+
* host: 'localhost',
|
|
16
|
+
* port: 5432,
|
|
17
|
+
* database: 'mydb',
|
|
18
|
+
* user: 'postgres',
|
|
19
|
+
* password: 'password'
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* // Deploy the pgpm package in the current directory
|
|
23
|
+
* await deployPgpm(config);
|
|
24
|
+
*
|
|
25
|
+
* // Deploy from a specific directory
|
|
26
|
+
* await deployPgpm(config, '/path/to/package');
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function deployPgpm(config: PgConfig, cwd?: string, cache?: boolean): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Load/deploy a pgpm package - alias for deployPgpm for consistency with other load* functions.
|
|
32
|
+
*
|
|
33
|
+
* @param config - PostgreSQL configuration
|
|
34
|
+
* @param cwd - Current working directory containing the pgpm package
|
|
35
|
+
* @param cache - Whether to enable caching
|
|
36
|
+
*/
|
|
37
|
+
export declare function loadPgpm(config: PgConfig, cwd?: string, cache?: boolean): Promise<void>;
|
package/esm/pgpm.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PgpmPackage } from '@pgpmjs/core';
|
|
2
|
+
import { getEnvOptions } from '@pgpmjs/env';
|
|
3
|
+
/**
|
|
4
|
+
* Deploy a pgpm package to a PostgreSQL database.
|
|
5
|
+
*
|
|
6
|
+
* @param config - PostgreSQL configuration (host, port, database, user, password)
|
|
7
|
+
* @param cwd - Current working directory containing the pgpm package (defaults to process.cwd())
|
|
8
|
+
* @param cache - Whether to enable caching (defaults to false)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Client } from 'pg';
|
|
13
|
+
* import { deployPgpm } from 'pgsql-seed';
|
|
14
|
+
*
|
|
15
|
+
* const config = {
|
|
16
|
+
* host: 'localhost',
|
|
17
|
+
* port: 5432,
|
|
18
|
+
* database: 'mydb',
|
|
19
|
+
* user: 'postgres',
|
|
20
|
+
* password: 'password'
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* // Deploy the pgpm package in the current directory
|
|
24
|
+
* await deployPgpm(config);
|
|
25
|
+
*
|
|
26
|
+
* // Deploy from a specific directory
|
|
27
|
+
* await deployPgpm(config, '/path/to/package');
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export async function deployPgpm(config, cwd, cache = false) {
|
|
31
|
+
const proj = new PgpmPackage(cwd ?? process.cwd());
|
|
32
|
+
if (!proj.isInModule())
|
|
33
|
+
return;
|
|
34
|
+
await proj.deploy(getEnvOptions({
|
|
35
|
+
pg: config,
|
|
36
|
+
deployment: {
|
|
37
|
+
fast: true,
|
|
38
|
+
usePlan: true,
|
|
39
|
+
cache
|
|
40
|
+
}
|
|
41
|
+
}), proj.getModuleName());
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Load/deploy a pgpm package - alias for deployPgpm for consistency with other load* functions.
|
|
45
|
+
*
|
|
46
|
+
* @param config - PostgreSQL configuration
|
|
47
|
+
* @param cwd - Current working directory containing the pgpm package
|
|
48
|
+
* @param cache - Whether to enable caching
|
|
49
|
+
*/
|
|
50
|
+
export async function loadPgpm(config, cwd, cache = false) {
|
|
51
|
+
return deployPgpm(config, cwd, cache);
|
|
52
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * from './manager';
|
|
4
|
-
export * from './roles';
|
|
5
|
-
export * from './seed';
|
|
6
|
-
export * from './test-client';
|
|
7
|
-
export { snapshot } from './utils';
|
|
1
|
+
export { exportCsv, loadCsv, loadCsvMap, insertJson, insertJsonMap, execSql, loadSql, loadSqlFiles, type ClientInput, type CopyableClient, type CsvSeedMap, type JsonSeedMap, type QueryableClient, unwrapClient } from 'pg-seed';
|
|
2
|
+
export { deployPgpm, loadPgpm } from './pgpm';
|
package/index.js
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.loadPgpm = exports.deployPgpm = exports.unwrapClient = exports.loadSqlFiles = exports.loadSql = exports.execSql = exports.insertJsonMap = exports.insertJson = exports.loadCsvMap = exports.loadCsv = exports.exportCsv = void 0;
|
|
4
|
+
// Re-export everything from pg-seed (core seeding utilities)
|
|
5
|
+
var pg_seed_1 = require("pg-seed");
|
|
6
|
+
// CSV utilities
|
|
7
|
+
Object.defineProperty(exports, "exportCsv", { enumerable: true, get: function () { return pg_seed_1.exportCsv; } });
|
|
8
|
+
Object.defineProperty(exports, "loadCsv", { enumerable: true, get: function () { return pg_seed_1.loadCsv; } });
|
|
9
|
+
Object.defineProperty(exports, "loadCsvMap", { enumerable: true, get: function () { return pg_seed_1.loadCsvMap; } });
|
|
10
|
+
// JSON utilities
|
|
11
|
+
Object.defineProperty(exports, "insertJson", { enumerable: true, get: function () { return pg_seed_1.insertJson; } });
|
|
12
|
+
Object.defineProperty(exports, "insertJsonMap", { enumerable: true, get: function () { return pg_seed_1.insertJsonMap; } });
|
|
13
|
+
// SQL utilities
|
|
14
|
+
Object.defineProperty(exports, "execSql", { enumerable: true, get: function () { return pg_seed_1.execSql; } });
|
|
15
|
+
Object.defineProperty(exports, "loadSql", { enumerable: true, get: function () { return pg_seed_1.loadSql; } });
|
|
16
|
+
Object.defineProperty(exports, "loadSqlFiles", { enumerable: true, get: function () { return pg_seed_1.loadSqlFiles; } });
|
|
17
|
+
// Utility
|
|
18
|
+
Object.defineProperty(exports, "unwrapClient", { enumerable: true, get: function () { return pg_seed_1.unwrapClient; } });
|
|
19
|
+
// pgpm integration (requires @pgpmjs/core)
|
|
20
|
+
var pgpm_1 = require("./pgpm");
|
|
21
|
+
Object.defineProperty(exports, "deployPgpm", { enumerable: true, get: function () { return pgpm_1.deployPgpm; } });
|
|
22
|
+
Object.defineProperty(exports, "loadPgpm", { enumerable: true, get: function () { return pgpm_1.loadPgpm; } });
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pgsql-seed",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "PostgreSQL seeding utilities with pgpm integration - batteries included",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
8
|
"types": "index.d.ts",
|
|
@@ -22,40 +22,35 @@
|
|
|
22
22
|
"keywords": [
|
|
23
23
|
"postgres",
|
|
24
24
|
"postgresql",
|
|
25
|
-
"testing",
|
|
26
|
-
"integration-tests",
|
|
27
|
-
"database-testing",
|
|
28
|
-
"pg",
|
|
29
|
-
"rls",
|
|
30
|
-
"role-based-access",
|
|
31
|
-
"test-database",
|
|
32
|
-
"test-runner",
|
|
33
|
-
"jest",
|
|
34
|
-
"mocha",
|
|
35
|
-
"sqitch",
|
|
36
|
-
"constructive",
|
|
37
|
-
"graphile",
|
|
38
|
-
"typeorm",
|
|
39
|
-
"knex",
|
|
40
25
|
"seed",
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
26
|
+
"seeding",
|
|
27
|
+
"csv",
|
|
28
|
+
"json",
|
|
29
|
+
"sql",
|
|
30
|
+
"pgpm",
|
|
31
|
+
"migrations",
|
|
32
|
+
"data-loading",
|
|
33
|
+
"constructive"
|
|
47
34
|
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"clean": "makage clean",
|
|
37
|
+
"prepack": "npm run build",
|
|
38
|
+
"build": "makage build",
|
|
39
|
+
"build:dev": "makage build --dev",
|
|
40
|
+
"lint": "eslint . --fix",
|
|
41
|
+
"test": "jest --passWithNoTests",
|
|
42
|
+
"test:watch": "jest --watch"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/pg": "^8.16.0",
|
|
46
|
+
"makage": "^0.1.9"
|
|
47
|
+
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@pgpmjs/core": "^4.
|
|
49
|
+
"@pgpmjs/core": "^4.4.0",
|
|
50
50
|
"@pgpmjs/env": "^2.8.11",
|
|
51
|
-
"@pgpmjs/logger": "^1.3.5",
|
|
52
|
-
"@pgpmjs/server-utils": "^2.8.11",
|
|
53
|
-
"@pgpmjs/types": "^2.12.8",
|
|
54
|
-
"csv-parse": "^6.1.0",
|
|
55
51
|
"pg": "^8.16.3",
|
|
56
|
-
"pg-
|
|
57
|
-
"pg-
|
|
58
|
-
"pg-env": "^1.2.4"
|
|
52
|
+
"pg-env": "^1.2.4",
|
|
53
|
+
"pg-seed": "^0.2.0"
|
|
59
54
|
},
|
|
60
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "bda56442f70c77c98276bc7bab0450308c975df8"
|
|
61
56
|
}
|
package/pgpm.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PgConfig } from 'pg-env';
|
|
2
|
+
/**
|
|
3
|
+
* Deploy a pgpm package to a PostgreSQL database.
|
|
4
|
+
*
|
|
5
|
+
* @param config - PostgreSQL configuration (host, port, database, user, password)
|
|
6
|
+
* @param cwd - Current working directory containing the pgpm package (defaults to process.cwd())
|
|
7
|
+
* @param cache - Whether to enable caching (defaults to false)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Client } from 'pg';
|
|
12
|
+
* import { deployPgpm } from 'pgsql-seed';
|
|
13
|
+
*
|
|
14
|
+
* const config = {
|
|
15
|
+
* host: 'localhost',
|
|
16
|
+
* port: 5432,
|
|
17
|
+
* database: 'mydb',
|
|
18
|
+
* user: 'postgres',
|
|
19
|
+
* password: 'password'
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* // Deploy the pgpm package in the current directory
|
|
23
|
+
* await deployPgpm(config);
|
|
24
|
+
*
|
|
25
|
+
* // Deploy from a specific directory
|
|
26
|
+
* await deployPgpm(config, '/path/to/package');
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function deployPgpm(config: PgConfig, cwd?: string, cache?: boolean): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Load/deploy a pgpm package - alias for deployPgpm for consistency with other load* functions.
|
|
32
|
+
*
|
|
33
|
+
* @param config - PostgreSQL configuration
|
|
34
|
+
* @param cwd - Current working directory containing the pgpm package
|
|
35
|
+
* @param cache - Whether to enable caching
|
|
36
|
+
*/
|
|
37
|
+
export declare function loadPgpm(config: PgConfig, cwd?: string, cache?: boolean): Promise<void>;
|
package/pgpm.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deployPgpm = deployPgpm;
|
|
4
|
+
exports.loadPgpm = loadPgpm;
|
|
5
|
+
const core_1 = require("@pgpmjs/core");
|
|
6
|
+
const env_1 = require("@pgpmjs/env");
|
|
7
|
+
/**
|
|
8
|
+
* Deploy a pgpm package to a PostgreSQL database.
|
|
9
|
+
*
|
|
10
|
+
* @param config - PostgreSQL configuration (host, port, database, user, password)
|
|
11
|
+
* @param cwd - Current working directory containing the pgpm package (defaults to process.cwd())
|
|
12
|
+
* @param cache - Whether to enable caching (defaults to false)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { Client } from 'pg';
|
|
17
|
+
* import { deployPgpm } from 'pgsql-seed';
|
|
18
|
+
*
|
|
19
|
+
* const config = {
|
|
20
|
+
* host: 'localhost',
|
|
21
|
+
* port: 5432,
|
|
22
|
+
* database: 'mydb',
|
|
23
|
+
* user: 'postgres',
|
|
24
|
+
* password: 'password'
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* // Deploy the pgpm package in the current directory
|
|
28
|
+
* await deployPgpm(config);
|
|
29
|
+
*
|
|
30
|
+
* // Deploy from a specific directory
|
|
31
|
+
* await deployPgpm(config, '/path/to/package');
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
async function deployPgpm(config, cwd, cache = false) {
|
|
35
|
+
const proj = new core_1.PgpmPackage(cwd ?? process.cwd());
|
|
36
|
+
if (!proj.isInModule())
|
|
37
|
+
return;
|
|
38
|
+
await proj.deploy((0, env_1.getEnvOptions)({
|
|
39
|
+
pg: config,
|
|
40
|
+
deployment: {
|
|
41
|
+
fast: true,
|
|
42
|
+
usePlan: true,
|
|
43
|
+
cache
|
|
44
|
+
}
|
|
45
|
+
}), proj.getModuleName());
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Load/deploy a pgpm package - alias for deployPgpm for consistency with other load* functions.
|
|
49
|
+
*
|
|
50
|
+
* @param config - PostgreSQL configuration
|
|
51
|
+
* @param cwd - Current working directory containing the pgpm package
|
|
52
|
+
* @param cache - Whether to enable caching
|
|
53
|
+
*/
|
|
54
|
+
async function loadPgpm(config, cwd, cache = false) {
|
|
55
|
+
return deployPgpm(config, cwd, cache);
|
|
56
|
+
}
|
package/admin.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { PgTestConnectionOptions } from '@pgpmjs/types';
|
|
2
|
-
import { PgConfig } from 'pg-env';
|
|
3
|
-
import { SeedAdapter } from './seed/types';
|
|
4
|
-
export declare class DbAdmin {
|
|
5
|
-
private config;
|
|
6
|
-
private verbose;
|
|
7
|
-
private roleConfig?;
|
|
8
|
-
constructor(config: PgConfig, verbose?: boolean, roleConfig?: PgTestConnectionOptions);
|
|
9
|
-
private getEnv;
|
|
10
|
-
private run;
|
|
11
|
-
private safeDropDb;
|
|
12
|
-
drop(dbName?: string): void;
|
|
13
|
-
dropTemplate(dbName: string): void;
|
|
14
|
-
create(dbName?: string): void;
|
|
15
|
-
createFromTemplate(template: string, dbName?: string): void;
|
|
16
|
-
installExtensions(extensions: string[] | string, dbName?: string): void;
|
|
17
|
-
connectionString(dbName?: string): string;
|
|
18
|
-
createTemplateFromBase(base: string, template: string): void;
|
|
19
|
-
cleanupTemplate(template: string): void;
|
|
20
|
-
grantRole(role: string, user: string, dbName?: string): Promise<void>;
|
|
21
|
-
grantConnect(role: string, dbName?: string): Promise<void>;
|
|
22
|
-
createUserRole(user: string, password: string, dbName: string, useLocksForRoles?: boolean): Promise<void>;
|
|
23
|
-
loadSql(file: string, dbName: string): void;
|
|
24
|
-
streamSql(sql: string, dbName: string): Promise<void>;
|
|
25
|
-
createSeededTemplate(templateName: string, adapter: SeedAdapter): Promise<void>;
|
|
26
|
-
}
|
package/admin.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DbAdmin = void 0;
|
|
4
|
-
const core_1 = require("@pgpmjs/core");
|
|
5
|
-
const logger_1 = require("@pgpmjs/logger");
|
|
6
|
-
const child_process_1 = require("child_process");
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
|
-
const pg_env_1 = require("pg-env");
|
|
9
|
-
const roles_1 = require("./roles");
|
|
10
|
-
const stream_1 = require("./stream");
|
|
11
|
-
const log = new logger_1.Logger('db-admin');
|
|
12
|
-
class DbAdmin {
|
|
13
|
-
config;
|
|
14
|
-
verbose;
|
|
15
|
-
roleConfig;
|
|
16
|
-
constructor(config, verbose = false, roleConfig) {
|
|
17
|
-
this.config = config;
|
|
18
|
-
this.verbose = verbose;
|
|
19
|
-
this.roleConfig = roleConfig;
|
|
20
|
-
this.config = (0, pg_env_1.getPgEnvOptions)(config);
|
|
21
|
-
}
|
|
22
|
-
getEnv() {
|
|
23
|
-
return {
|
|
24
|
-
PGHOST: this.config.host,
|
|
25
|
-
PGPORT: String(this.config.port),
|
|
26
|
-
PGUSER: this.config.user,
|
|
27
|
-
PGPASSWORD: this.config.password
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
run(command) {
|
|
31
|
-
try {
|
|
32
|
-
(0, child_process_1.execSync)(command, {
|
|
33
|
-
stdio: this.verbose ? 'inherit' : 'pipe',
|
|
34
|
-
env: {
|
|
35
|
-
...process.env,
|
|
36
|
-
...this.getEnv()
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
if (this.verbose)
|
|
40
|
-
log.success(`Executed: ${command}`);
|
|
41
|
-
}
|
|
42
|
-
catch (err) {
|
|
43
|
-
log.error(`Command failed: ${command}`);
|
|
44
|
-
if (this.verbose)
|
|
45
|
-
log.error(err.message);
|
|
46
|
-
throw err;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
safeDropDb(name) {
|
|
50
|
-
try {
|
|
51
|
-
this.run(`dropdb "${name}"`);
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
if (!err.message.includes('does not exist')) {
|
|
55
|
-
log.warn(`Could not drop database ${name}: ${err.message}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
drop(dbName) {
|
|
60
|
-
this.safeDropDb(dbName ?? this.config.database);
|
|
61
|
-
}
|
|
62
|
-
dropTemplate(dbName) {
|
|
63
|
-
this.run(`psql -c "UPDATE pg_database SET datistemplate='false' WHERE datname='${dbName}';"`);
|
|
64
|
-
this.drop(dbName);
|
|
65
|
-
}
|
|
66
|
-
create(dbName) {
|
|
67
|
-
const db = dbName ?? this.config.database;
|
|
68
|
-
this.run(`createdb -U ${this.config.user} -h ${this.config.host} -p ${this.config.port} "${db}"`);
|
|
69
|
-
}
|
|
70
|
-
createFromTemplate(template, dbName) {
|
|
71
|
-
const db = dbName ?? this.config.database;
|
|
72
|
-
this.run(`createdb -U ${this.config.user} -h ${this.config.host} -p ${this.config.port} -e "${db}" -T "${template}"`);
|
|
73
|
-
}
|
|
74
|
-
installExtensions(extensions, dbName) {
|
|
75
|
-
const db = dbName ?? this.config.database;
|
|
76
|
-
const extList = typeof extensions === 'string' ? extensions.split(',') : extensions;
|
|
77
|
-
for (const extension of extList) {
|
|
78
|
-
this.run(`psql --dbname "${db}" -c 'CREATE EXTENSION IF NOT EXISTS "${extension}" CASCADE;'`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
connectionString(dbName) {
|
|
82
|
-
const { user, password, host, port } = this.config;
|
|
83
|
-
const db = dbName ?? this.config.database;
|
|
84
|
-
return `postgres://${user}:${password}@${host}:${port}/${db}`;
|
|
85
|
-
}
|
|
86
|
-
createTemplateFromBase(base, template) {
|
|
87
|
-
this.run(`createdb -T "${base}" "${template}"`);
|
|
88
|
-
this.run(`psql -c "UPDATE pg_database SET datistemplate = true WHERE datname = '${template}';"`);
|
|
89
|
-
}
|
|
90
|
-
cleanupTemplate(template) {
|
|
91
|
-
try {
|
|
92
|
-
this.run(`psql -c "UPDATE pg_database SET datistemplate = false WHERE datname = '${template}'"`);
|
|
93
|
-
}
|
|
94
|
-
catch {
|
|
95
|
-
log.warn(`Skipping failed UPDATE of datistemplate for ${template}`);
|
|
96
|
-
}
|
|
97
|
-
this.safeDropDb(template);
|
|
98
|
-
}
|
|
99
|
-
async grantRole(role, user, dbName) {
|
|
100
|
-
const db = dbName ?? this.config.database;
|
|
101
|
-
const sql = (0, core_1.generateGrantRoleSQL)(role, user);
|
|
102
|
-
await this.streamSql(sql, db);
|
|
103
|
-
}
|
|
104
|
-
async grantConnect(role, dbName) {
|
|
105
|
-
const db = dbName ?? this.config.database;
|
|
106
|
-
const sql = `GRANT CONNECT ON DATABASE "${db}" TO ${role};`;
|
|
107
|
-
await this.streamSql(sql, db);
|
|
108
|
-
}
|
|
109
|
-
// ONLY granting admin role for testing purposes, normally the db connection for apps won't have admin role
|
|
110
|
-
// DO NOT USE THIS FOR PRODUCTION
|
|
111
|
-
async createUserRole(user, password, dbName, useLocksForRoles = false) {
|
|
112
|
-
const anonRole = (0, roles_1.getRoleName)('anonymous', this.roleConfig);
|
|
113
|
-
const authRole = (0, roles_1.getRoleName)('authenticated', this.roleConfig);
|
|
114
|
-
const adminRole = (0, roles_1.getRoleName)('administrator', this.roleConfig);
|
|
115
|
-
const sql = (0, core_1.generateCreateUserWithGrantsSQL)(user, password, [anonRole, authRole, adminRole], useLocksForRoles);
|
|
116
|
-
await this.streamSql(sql, dbName);
|
|
117
|
-
}
|
|
118
|
-
loadSql(file, dbName) {
|
|
119
|
-
if (!(0, fs_1.existsSync)(file)) {
|
|
120
|
-
throw new Error(`Missing SQL file: ${file}`);
|
|
121
|
-
}
|
|
122
|
-
this.run(`psql -f ${file} ${dbName}`);
|
|
123
|
-
}
|
|
124
|
-
async streamSql(sql, dbName) {
|
|
125
|
-
await (0, stream_1.streamSql)({
|
|
126
|
-
...this.config,
|
|
127
|
-
database: dbName
|
|
128
|
-
}, sql);
|
|
129
|
-
}
|
|
130
|
-
async createSeededTemplate(templateName, adapter) {
|
|
131
|
-
const seedDb = this.config.database;
|
|
132
|
-
this.create(seedDb);
|
|
133
|
-
await adapter.seed({
|
|
134
|
-
admin: this,
|
|
135
|
-
config: this.config,
|
|
136
|
-
pg: null, // placeholder for PgTestClient
|
|
137
|
-
connect: null // placeholder for connection factory
|
|
138
|
-
});
|
|
139
|
-
this.cleanupTemplate(templateName);
|
|
140
|
-
this.createTemplateFromBase(seedDb, templateName);
|
|
141
|
-
this.drop(seedDb);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
exports.DbAdmin = DbAdmin;
|
package/connect.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { PgTestConnectionOptions } from '@pgpmjs/types';
|
|
2
|
-
import { PgConfig } from 'pg-env';
|
|
3
|
-
import { DbAdmin } from './admin';
|
|
4
|
-
import { PgTestConnector } from './manager';
|
|
5
|
-
import { SeedAdapter } from './seed/types';
|
|
6
|
-
import { PgTestClient } from './test-client';
|
|
7
|
-
export declare const getPgRootAdmin: (config: PgConfig, connOpts?: PgTestConnectionOptions) => DbAdmin;
|
|
8
|
-
export interface GetConnectionOpts {
|
|
9
|
-
pg?: Partial<PgConfig>;
|
|
10
|
-
db?: Partial<PgTestConnectionOptions>;
|
|
11
|
-
}
|
|
12
|
-
export interface GetConnectionResult {
|
|
13
|
-
pg: PgTestClient;
|
|
14
|
-
db: PgTestClient;
|
|
15
|
-
admin: DbAdmin;
|
|
16
|
-
teardown: () => Promise<void>;
|
|
17
|
-
manager: PgTestConnector;
|
|
18
|
-
}
|
|
19
|
-
export declare const getConnections: (cn?: GetConnectionOpts, seedAdapters?: SeedAdapter[]) => Promise<GetConnectionResult>;
|
package/connect.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getConnections = exports.getPgRootAdmin = void 0;
|
|
4
|
-
const env_1 = require("@pgpmjs/env");
|
|
5
|
-
const crypto_1 = require("crypto");
|
|
6
|
-
const pg_cache_1 = require("pg-cache");
|
|
7
|
-
const pg_env_1 = require("pg-env");
|
|
8
|
-
const admin_1 = require("./admin");
|
|
9
|
-
const manager_1 = require("./manager");
|
|
10
|
-
const roles_1 = require("./roles");
|
|
11
|
-
const seed_1 = require("./seed");
|
|
12
|
-
let manager;
|
|
13
|
-
const getPgRootAdmin = (config, connOpts = {}) => {
|
|
14
|
-
const opts = (0, pg_env_1.getPgEnvOptions)({
|
|
15
|
-
user: config.user,
|
|
16
|
-
password: config.password,
|
|
17
|
-
host: config.host,
|
|
18
|
-
port: config.port,
|
|
19
|
-
database: connOpts.rootDb
|
|
20
|
-
});
|
|
21
|
-
const admin = new admin_1.DbAdmin(opts, false, connOpts);
|
|
22
|
-
return admin;
|
|
23
|
-
};
|
|
24
|
-
exports.getPgRootAdmin = getPgRootAdmin;
|
|
25
|
-
const getConnOopts = (cn = {}) => {
|
|
26
|
-
const connect = (0, env_1.getConnEnvOptions)(cn.db);
|
|
27
|
-
const config = (0, pg_env_1.getPgEnvOptions)({
|
|
28
|
-
database: `${connect.prefix}${(0, crypto_1.randomUUID)()}`,
|
|
29
|
-
...cn.pg
|
|
30
|
-
});
|
|
31
|
-
return {
|
|
32
|
-
pg: config,
|
|
33
|
-
db: connect
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
const getConnections = async (cn = {}, seedAdapters = [seed_1.seed.pgpm()]) => {
|
|
37
|
-
cn = getConnOopts(cn);
|
|
38
|
-
const config = cn.pg;
|
|
39
|
-
const connOpts = cn.db;
|
|
40
|
-
const root = (0, exports.getPgRootAdmin)(config, connOpts);
|
|
41
|
-
await root.createUserRole(connOpts.connections.app.user, connOpts.connections.app.password, connOpts.rootDb);
|
|
42
|
-
const admin = new admin_1.DbAdmin(config, false, connOpts);
|
|
43
|
-
if (process.env.TEST_DB) {
|
|
44
|
-
config.database = process.env.TEST_DB;
|
|
45
|
-
}
|
|
46
|
-
else if (connOpts.template) {
|
|
47
|
-
admin.createFromTemplate(connOpts.template, config.database);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
admin.create(config.database);
|
|
51
|
-
admin.installExtensions(connOpts.extensions);
|
|
52
|
-
}
|
|
53
|
-
await admin.grantConnect(connOpts.connections.app.user, config.database);
|
|
54
|
-
manager = manager_1.PgTestConnector.getInstance(config);
|
|
55
|
-
const pg = manager.getClient(config);
|
|
56
|
-
let teardownPromise = null;
|
|
57
|
-
const teardown = async () => {
|
|
58
|
-
if (teardownPromise)
|
|
59
|
-
return teardownPromise;
|
|
60
|
-
teardownPromise = (async () => {
|
|
61
|
-
manager.beginTeardown();
|
|
62
|
-
await (0, pg_cache_1.teardownPgPools)();
|
|
63
|
-
await manager.closeAll();
|
|
64
|
-
})();
|
|
65
|
-
return teardownPromise;
|
|
66
|
-
};
|
|
67
|
-
if (seedAdapters.length) {
|
|
68
|
-
try {
|
|
69
|
-
await seed_1.seed.compose(seedAdapters).seed({
|
|
70
|
-
connect: connOpts,
|
|
71
|
-
admin,
|
|
72
|
-
config: config,
|
|
73
|
-
pg: manager.getClient(config)
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
const err = error;
|
|
78
|
-
const msg = err && (err.stack || err.message) ? (err.stack || err.message) : String(err);
|
|
79
|
-
process.stderr.write(`[pgsql-test] Seed error (continuing): ${msg}\n`);
|
|
80
|
-
// continue without teardown to allow caller-managed lifecycle
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
const dbConfig = {
|
|
84
|
-
...config,
|
|
85
|
-
user: connOpts.connections.app.user,
|
|
86
|
-
password: connOpts.connections.app.password
|
|
87
|
-
};
|
|
88
|
-
const db = manager.getClient(dbConfig, {
|
|
89
|
-
auth: connOpts.auth,
|
|
90
|
-
roles: connOpts.roles
|
|
91
|
-
});
|
|
92
|
-
db.setContext({ role: (0, roles_1.getDefaultRole)(connOpts) });
|
|
93
|
-
return { pg, db, teardown, manager, admin };
|
|
94
|
-
};
|
|
95
|
-
exports.getConnections = getConnections;
|
package/context-utils.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { PgTestClientContext } from '@pgpmjs/types';
|
|
2
|
-
/**
|
|
3
|
-
* Generate SQL statements to set PostgreSQL session context variables
|
|
4
|
-
* Uses SET LOCAL ROLE for the 'role' key and set_config() for other variables
|
|
5
|
-
* @param context - Context settings to apply
|
|
6
|
-
* @returns SQL string with SET LOCAL ROLE and set_config() statements
|
|
7
|
-
*/
|
|
8
|
-
export declare function generateContextStatements(context: PgTestClientContext): string;
|
package/context-utils.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateContextStatements = generateContextStatements;
|
|
4
|
-
/**
|
|
5
|
-
* Generate SQL statements to set PostgreSQL session context variables
|
|
6
|
-
* Uses SET LOCAL ROLE for the 'role' key and set_config() for other variables
|
|
7
|
-
* @param context - Context settings to apply
|
|
8
|
-
* @returns SQL string with SET LOCAL ROLE and set_config() statements
|
|
9
|
-
*/
|
|
10
|
-
function generateContextStatements(context) {
|
|
11
|
-
return Object.entries(context)
|
|
12
|
-
.map(([key, val]) => {
|
|
13
|
-
if (key === 'role') {
|
|
14
|
-
if (val === null || val === undefined) {
|
|
15
|
-
return 'SET LOCAL ROLE NONE;';
|
|
16
|
-
}
|
|
17
|
-
const escapedRole = val.replace(/"/g, '""');
|
|
18
|
-
return `SET LOCAL ROLE "${escapedRole}";`;
|
|
19
|
-
}
|
|
20
|
-
// Use set_config for other context variables
|
|
21
|
-
if (val === null || val === undefined) {
|
|
22
|
-
return `SELECT set_config('${key}', NULL, true);`;
|
|
23
|
-
}
|
|
24
|
-
const escapedVal = val.replace(/'/g, "''");
|
|
25
|
-
return `SELECT set_config('${key}', '${escapedVal}', true);`;
|
|
26
|
-
})
|
|
27
|
-
.join('\n');
|
|
28
|
-
}
|