@voxpelli/pg-utils 2.1.0 → 2.3.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/README.md +7 -2
- package/lib/test-helpers.d.ts +2 -1
- package/lib/test-helpers.d.ts.map +1 -1
- package/lib/test-helpers.js +21 -5
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +1 -0
- package/package.json +8 -8
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,7 +10,8 @@ export class PgTestHelpers {
|
|
|
10
10
|
export type PgTestHelpersOptions = {
|
|
11
11
|
connectionString: string;
|
|
12
12
|
fixtureFolder?: string | URL;
|
|
13
|
-
|
|
13
|
+
ignoreTables?: string[];
|
|
14
|
+
schema: string | URL | ((pool: Pool) => import("umzug").Umzug<import("umzeption").UmzeptionContext<"pg", import("umzeption").FastifyPostgresStyleDb>>);
|
|
14
15
|
tablesWithDependencies?: Array<string[] | string>;
|
|
15
16
|
};
|
|
16
17
|
import type { Pool } from 'pg';
|
|
@@ -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;IAyEb,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;;sBA1Ia,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,13 +14,16 @@ 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
|
|
17
|
+
* @property {string[]} [ignoreTables]
|
|
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
|
*/
|
|
20
21
|
|
|
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;
|
|
@@ -88,8 +96,8 @@ export class PgTestHelpers {
|
|
|
88
96
|
Array.isArray(name)
|
|
89
97
|
? this.#removeTablesByName(name)
|
|
90
98
|
: this.queryPromise('DROP TABLE IF EXISTS ' + name + ' CASCADE').catch(cause => {
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
throw new Error(`Failed to drop table: ${name}`, { cause });
|
|
100
|
+
})
|
|
93
101
|
);
|
|
94
102
|
}
|
|
95
103
|
}
|
|
@@ -99,7 +107,7 @@ export class PgTestHelpers {
|
|
|
99
107
|
async initTables () {
|
|
100
108
|
try {
|
|
101
109
|
if (typeof this.#schema === 'function') {
|
|
102
|
-
await this.#schema().up();
|
|
110
|
+
await this.#schema(this.#pool).up();
|
|
103
111
|
return;
|
|
104
112
|
}
|
|
105
113
|
|
|
@@ -127,7 +135,15 @@ export class PgTestHelpers {
|
|
|
127
135
|
if (this.#tablesWithDependencies) {
|
|
128
136
|
await this.#removeTablesByName(this.#tablesWithDependencies);
|
|
129
137
|
}
|
|
130
|
-
|
|
138
|
+
|
|
139
|
+
let tableNames = await this.#getTableNames();
|
|
140
|
+
const ignoreTables = this.#ignoreTables;
|
|
141
|
+
|
|
142
|
+
if (ignoreTables) {
|
|
143
|
+
tableNames = tableNames.filter(name => !ignoreTables.includes(name));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
await this.#removeTablesByName(tableNames);
|
|
131
147
|
}
|
|
132
148
|
|
|
133
149
|
/** @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.0",
|
|
4
4
|
"description": " My personal database utils / helpers for Postgres",
|
|
5
5
|
"homepage": "http://github.com/voxpelli/pg-utils",
|
|
6
6
|
"repository": {
|
|
@@ -47,22 +47,22 @@
|
|
|
47
47
|
"@types/chai": "^4.3.20",
|
|
48
48
|
"@types/chai-as-promised": "^7.1.8",
|
|
49
49
|
"@types/mocha": "^10.0.10",
|
|
50
|
-
"@types/node": "^20.17.
|
|
50
|
+
"@types/node": "^20.17.31",
|
|
51
51
|
"@types/pg-copy-streams": "^1.2.5",
|
|
52
|
-
"@voxpelli/eslint-config": "^
|
|
53
|
-
"@voxpelli/tsconfig": "^15.1.
|
|
52
|
+
"@voxpelli/eslint-config": "^23.0.0",
|
|
53
|
+
"@voxpelli/tsconfig": "^15.1.2",
|
|
54
54
|
"c8": "^10.1.3",
|
|
55
55
|
"chai": "^4.5.0",
|
|
56
56
|
"chai-as-promised": "^7.1.2",
|
|
57
|
-
"dotenv": "^16.
|
|
58
|
-
"eslint": "^9.
|
|
57
|
+
"dotenv": "^16.5.0",
|
|
58
|
+
"eslint": "^9.25.1",
|
|
59
59
|
"husky": "^9.1.7",
|
|
60
60
|
"installed-check": "^9.3.0",
|
|
61
|
-
"knip": "^5.
|
|
61
|
+
"knip": "^5.50.5",
|
|
62
62
|
"mocha": "^11.1.0",
|
|
63
63
|
"npm-run-all2": "^7.0.2",
|
|
64
64
|
"type-coverage": "^2.29.7",
|
|
65
|
-
"typescript": "~5.
|
|
65
|
+
"typescript": "~5.8.3",
|
|
66
66
|
"validate-conventional-commit": "^1.0.4"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|