@voxpelli/pg-utils 2.2.0 → 2.3.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/README.md +7 -2
- package/lib/test-helpers.d.ts +1 -0
- package/lib/test-helpers.d.ts.map +1 -1
- package/lib/test-helpers.js +21 -4
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +1 -0
- package/package.json +14 -11
package/README.md
CHANGED
|
@@ -20,7 +20,8 @@ import {
|
|
|
20
20
|
const pgHelpers = new PgTestHelpers({
|
|
21
21
|
connectionString: 'postgres://user:pass@localhost/example',
|
|
22
22
|
fixtureFolder: new URL('./fixtures', import.meta.url),
|
|
23
|
-
|
|
23
|
+
ignoreTables: ['xyz'],
|
|
24
|
+
schema: new URL('./create-tables.sql', import.meta.url),
|
|
24
25
|
tablesWithDependencies: [
|
|
25
26
|
'abc',
|
|
26
27
|
['foo', 'bar'],
|
|
@@ -38,7 +39,10 @@ Class that creates a helpers instance
|
|
|
38
39
|
new PgTestHelpers({
|
|
39
40
|
connectionString: 'postgres://user:pass@localhost/example',
|
|
40
41
|
fixtureFolder: new URL('./fixtures', import.meta.url),
|
|
41
|
-
|
|
42
|
+
ignoreTables: [
|
|
43
|
+
// ...
|
|
44
|
+
],
|
|
45
|
+
schema: new URL('./create-tables.sql', import.meta.url),
|
|
42
46
|
tablesWithDependencies: [
|
|
43
47
|
// ...
|
|
44
48
|
]
|
|
@@ -53,6 +57,7 @@ new PgTestHelpers({
|
|
|
53
57
|
|
|
54
58
|
* `connectionString` – _`string | _ – a connection string for the postgres database
|
|
55
59
|
* `fixtureFolder` – _`[string | URL]`_ – _optional_ – the path to a folder of `.csv`-file fixtures named by their respective table
|
|
60
|
+
* `ignoreTables` – _`[string[]]`_ – _optional_ – names of tables to ignore when dropping
|
|
56
61
|
* `schema` – _`string | URL | Umzug`_ – an umzug instance that can be used to initialize tables or the schema itself or a `URL` to a text file containing the schema
|
|
57
62
|
* `tablesWithDependencies` – _`[Array<string[] | string>]`_ – _optional_ – names of tables that depend on other tables. If some of these tables depend on each other, then use nested arrays to ensure that within the same array no two tables depend on each other
|
|
58
63
|
|
package/lib/test-helpers.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export class PgTestHelpers {
|
|
|
10
10
|
export type PgTestHelpersOptions = {
|
|
11
11
|
connectionString: string;
|
|
12
12
|
fixtureFolder?: string | URL;
|
|
13
|
+
ignoreTables?: string[];
|
|
13
14
|
schema: string | URL | ((pool: Pool) => import("umzug").Umzug<import("umzeption").UmzeptionContext<"pg", import("umzeption").FastifyPostgresStyleDb>>);
|
|
14
15
|
tablesWithDependencies?: Array<string[] | string>;
|
|
15
16
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["test-helpers.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["test-helpers.js"],"names":[],"mappings":"AAqBA;IAeE,qBADY,oBAAoB,EAsC/B;IAxCD,cADW,IAAI,CAAC,OAAO,CAAC,CACX;IA0Eb,cADc,OAAO,CAAC,IAAI,CAAC,CAiB1B;IAGD,kBADc,OAAO,CAAC,IAAI,CAAC,CAM1B;IAGD,gBADc,OAAO,CAAC,IAAI,CAAC,CAc1B;IAGD,OADc,OAAO,CAAC,IAAI,CAAC,CAG1B;;CACF;;sBA3Ia,MAAM;oBACN,MAAM,GAAG,GAAG;mBACZ,MAAM,EAAE;YACR,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,OAAO,EAAE,KAAK,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,IAAI,EAAE,OAAO,WAAW,EAAE,sBAAsB,CAAC,CAAC,CAAC;6BAC9I,KAAK,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC;;0BARZ,IAAI"}
|
package/lib/test-helpers.js
CHANGED
|
@@ -14,6 +14,7 @@ import { createPgPool, isStringArray, TypeNeverError } from './utils.js';
|
|
|
14
14
|
* @typedef PgTestHelpersOptions
|
|
15
15
|
* @property {string} connectionString
|
|
16
16
|
* @property {string | URL} [fixtureFolder]
|
|
17
|
+
* @property {string[]} [ignoreTables]
|
|
17
18
|
* @property {string | URL | ((pool: Pool) => import('umzug').Umzug<import('umzeption').UmzeptionContext<'pg', import('umzeption').FastifyPostgresStyleDb>>)} schema
|
|
18
19
|
* @property {Array<string[] | string>} [tablesWithDependencies]
|
|
19
20
|
*/
|
|
@@ -21,6 +22,8 @@ import { createPgPool, isStringArray, TypeNeverError } from './utils.js';
|
|
|
21
22
|
export class PgTestHelpers {
|
|
22
23
|
/** @type {string | URL | undefined} */
|
|
23
24
|
#fixtureFolder;
|
|
25
|
+
/** @type {string[] | undefined} */
|
|
26
|
+
#ignoreTables;
|
|
24
27
|
/** @type {Pool} */
|
|
25
28
|
#pool;
|
|
26
29
|
/** @type {PgTestHelpersOptions['schema']} */
|
|
@@ -39,6 +42,7 @@ export class PgTestHelpers {
|
|
|
39
42
|
const {
|
|
40
43
|
connectionString,
|
|
41
44
|
fixtureFolder,
|
|
45
|
+
ignoreTables,
|
|
42
46
|
schema,
|
|
43
47
|
tablesWithDependencies,
|
|
44
48
|
} = options;
|
|
@@ -49,6 +53,9 @@ export class PgTestHelpers {
|
|
|
49
53
|
if (fixtureFolder && typeof fixtureFolder !== 'string' && !(fixtureFolder instanceof URL)) {
|
|
50
54
|
throw new TypeNeverError(fixtureFolder, 'Invalid fixtureFolder, expected a string');
|
|
51
55
|
}
|
|
56
|
+
if (ignoreTables && !Array.isArray(ignoreTables)) {
|
|
57
|
+
throw new TypeNeverError(ignoreTables, 'Invalid ignoreTables, expected an array');
|
|
58
|
+
}
|
|
52
59
|
if (typeof schema !== 'string' && typeof schema !== 'object' && typeof schema !== 'function') {
|
|
53
60
|
throw new TypeNeverError(schema, 'Invalid schema, expected a string, object or function');
|
|
54
61
|
}
|
|
@@ -59,6 +66,7 @@ export class PgTestHelpers {
|
|
|
59
66
|
const pool = createPgPool(connectionString);
|
|
60
67
|
|
|
61
68
|
this.#fixtureFolder = fixtureFolder;
|
|
69
|
+
this.#ignoreTables = ignoreTables;
|
|
62
70
|
this.#tablesWithDependencies = tablesWithDependencies;
|
|
63
71
|
this.#pool = pool;
|
|
64
72
|
this.#schema = schema;
|
|
@@ -73,10 +81,11 @@ export class PgTestHelpers {
|
|
|
73
81
|
|
|
74
82
|
/**
|
|
75
83
|
* @param {Array<string[] | string>} tables
|
|
84
|
+
* @param {boolean} [allowParallelRemoval]
|
|
76
85
|
* @returns {Promise<void>}
|
|
77
86
|
*/
|
|
78
|
-
async #removeTablesByName (tables) {
|
|
79
|
-
if (isStringArray(tables)) {
|
|
87
|
+
async #removeTablesByName (tables, allowParallelRemoval = false) {
|
|
88
|
+
if (allowParallelRemoval && isStringArray(tables)) {
|
|
80
89
|
await Promise.all(
|
|
81
90
|
tables.map(name => this.queryPromise('DROP TABLE IF EXISTS ' + name + ' CASCADE'))
|
|
82
91
|
).catch(cause => {
|
|
@@ -86,7 +95,7 @@ export class PgTestHelpers {
|
|
|
86
95
|
for (const name of tables) {
|
|
87
96
|
await (
|
|
88
97
|
Array.isArray(name)
|
|
89
|
-
? this.#removeTablesByName(name)
|
|
98
|
+
? this.#removeTablesByName(name, true)
|
|
90
99
|
: this.queryPromise('DROP TABLE IF EXISTS ' + name + ' CASCADE').catch(cause => {
|
|
91
100
|
throw new Error(`Failed to drop table: ${name}`, { cause });
|
|
92
101
|
})
|
|
@@ -127,7 +136,15 @@ export class PgTestHelpers {
|
|
|
127
136
|
if (this.#tablesWithDependencies) {
|
|
128
137
|
await this.#removeTablesByName(this.#tablesWithDependencies);
|
|
129
138
|
}
|
|
130
|
-
|
|
139
|
+
|
|
140
|
+
let tableNames = await this.#getTableNames();
|
|
141
|
+
const ignoreTables = this.#ignoreTables;
|
|
142
|
+
|
|
143
|
+
if (ignoreTables) {
|
|
144
|
+
tableNames = tableNames.filter(name => !ignoreTables.includes(name));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await this.#removeTablesByName(tableNames);
|
|
131
148
|
}
|
|
132
149
|
|
|
133
150
|
/** @returns {Promise<void>} */
|
package/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["utils.js"],"names":[],"mappings":"AAMA,+CAHW,MAAM,GACJ,OAAO,IAAI,EAAE,IAAI,CAO7B;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["utils.js"],"names":[],"mappings":"AAMA,+CAHW,MAAM,GACJ,OAAO,IAAI,EAAE,IAAI,CAO7B;AA4BD,qCAHW,OAAO,GACL,KAAK,IAAI,MAAM,EAAE,CAK7B;AA5BD;IAME,mBAJW,KAAK,WACL,MAAM,YACN,YAAY,EAItB;CACF"}
|
package/lib/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voxpelli/pg-utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": " My personal database utils / helpers for Postgres",
|
|
5
5
|
"homepage": "http://github.com/voxpelli/pg-utils",
|
|
6
6
|
"repository": {
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"clean:declarations-top": "rm -rf $(find . -maxdepth 1 -type f -name '*.d.ts*' ! -name 'index.d.ts')",
|
|
38
38
|
"clean:declarations-lib": "rm -rf $(find lib -type f -name '*.d.ts*' ! -name '*-types.d.ts')",
|
|
39
39
|
"clean": "run-p clean:*",
|
|
40
|
-
"
|
|
40
|
+
"husky-enable": "husky",
|
|
41
|
+
"husky-disable": "git config --unset core.hooksPath",
|
|
41
42
|
"prepublishOnly": "run-s build",
|
|
42
43
|
"test:mocha": "c8 --reporter=lcov --reporter=text mocha 'test/**/*.spec.js'",
|
|
43
44
|
"test-ci": "run-s test:*",
|
|
@@ -47,28 +48,30 @@
|
|
|
47
48
|
"@types/chai": "^4.3.20",
|
|
48
49
|
"@types/chai-as-promised": "^7.1.8",
|
|
49
50
|
"@types/mocha": "^10.0.10",
|
|
50
|
-
"@types/node": "^20.
|
|
51
|
+
"@types/node": "^20.19.30",
|
|
51
52
|
"@types/pg-copy-streams": "^1.2.5",
|
|
53
|
+
"@types/sinon": "^21.0.0",
|
|
52
54
|
"@voxpelli/eslint-config": "^23.0.0",
|
|
53
|
-
"@voxpelli/tsconfig": "^
|
|
55
|
+
"@voxpelli/tsconfig": "^16.1.0",
|
|
54
56
|
"c8": "^10.1.3",
|
|
55
57
|
"chai": "^4.5.0",
|
|
56
58
|
"chai-as-promised": "^7.1.2",
|
|
57
|
-
"dotenv": "^
|
|
58
|
-
"eslint": "^9.
|
|
59
|
+
"dotenv": "^17.2.3",
|
|
60
|
+
"eslint": "^9.39.2",
|
|
59
61
|
"husky": "^9.1.7",
|
|
60
62
|
"installed-check": "^9.3.0",
|
|
61
|
-
"knip": "^5.
|
|
62
|
-
"mocha": "^11.
|
|
63
|
-
"npm-run-all2": "^
|
|
63
|
+
"knip": "^5.82.1",
|
|
64
|
+
"mocha": "^11.7.5",
|
|
65
|
+
"npm-run-all2": "^8.0.4",
|
|
66
|
+
"sinon": "^21.0.1",
|
|
64
67
|
"type-coverage": "^2.29.7",
|
|
65
|
-
"typescript": "~5.
|
|
68
|
+
"typescript": "~5.9.3",
|
|
66
69
|
"validate-conventional-commit": "^1.0.4"
|
|
67
70
|
},
|
|
68
71
|
"dependencies": {
|
|
69
72
|
"@types/pg": "^8.11.10",
|
|
70
73
|
"pg": "^8.13.1",
|
|
71
|
-
"pg-copy-streams": "^
|
|
74
|
+
"pg-copy-streams": "^7.0.0",
|
|
72
75
|
"umzeption": "^0.4.1",
|
|
73
76
|
"umzug": "^3.8.2"
|
|
74
77
|
}
|