@voxpelli/pg-utils 1.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/LICENSE +21 -0
- package/README.md +96 -0
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -0
- package/index.js +2 -0
- package/lib/csv-folder-to-db.d.ts +4 -0
- package/lib/csv-folder-to-db.d.ts.map +1 -0
- package/lib/csv-folder-to-db.js +90 -0
- package/lib/test-helpers.d.ts +17 -0
- package/lib/test-helpers.d.ts.map +1 -0
- package/lib/test-helpers.js +134 -0
- package/lib/utils.d.ts +8 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +45 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Pelle Wessman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @voxpelli/pg-utils
|
|
2
|
+
|
|
3
|
+
My personal database utils / helpers for Postgres
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@voxpelli/pg-utils)
|
|
6
|
+
[](https://www.npmjs.com/package/@voxpelli/pg-utils)
|
|
7
|
+
[](https://github.com/neostandard/neostandard)
|
|
8
|
+
[](https://github.com/voxpelli/badges-cjs-esm)
|
|
9
|
+
[](https://github.com/voxpelli/types-in-js)
|
|
10
|
+
[](https://mastodon.social/@voxpelli)
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
import {
|
|
16
|
+
csvFromFolderToDb,
|
|
17
|
+
PgTestHelpers,
|
|
18
|
+
} from '@voxpelli/pg-utils';
|
|
19
|
+
|
|
20
|
+
import pg from 'pg';
|
|
21
|
+
|
|
22
|
+
const pgHelpers = new PgTestHelpers({
|
|
23
|
+
connectionString: 'postgres://user:pass@localhost/example',
|
|
24
|
+
fixtureFolder: new URL('./fixtures', ),
|
|
25
|
+
pg,
|
|
26
|
+
umzug: new Umzug({
|
|
27
|
+
// ...
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## PgTestHelpers
|
|
33
|
+
|
|
34
|
+
Class that creates a helpers instance
|
|
35
|
+
|
|
36
|
+
### Syntax
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
new PgTestHelpers({
|
|
40
|
+
connectionString: 'postgres://user:pass@localhost/example',
|
|
41
|
+
fixtureFolder: new URL('./fixtures', ),
|
|
42
|
+
umzugInstall: new Umzug({
|
|
43
|
+
// ...
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Arguments
|
|
49
|
+
|
|
50
|
+
* `options` – _[`PgTestHelpersOptions`](#pgtesthelpersoptions)_
|
|
51
|
+
|
|
52
|
+
### PgTestHelpersOptions
|
|
53
|
+
|
|
54
|
+
* `connectionString` – _`string`_ – a connection string for the postgres database
|
|
55
|
+
* `fixtureFolder` – _`[string]`_ – _optional_ – the path to a folder of `.csv`-file fixtures named by their respective table
|
|
56
|
+
* `tablesWithDependencies` – _`[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
|
|
57
|
+
* `umzugInstall` – _`Umzug`_ – an umzug instance that can be used to initialize tables
|
|
58
|
+
|
|
59
|
+
### Methods
|
|
60
|
+
|
|
61
|
+
* `initTables() => Promise<void>` – sets up all of the tables
|
|
62
|
+
* `insertFixtures() => Promise<void>` – inserts all the fixtures data into the tables (only usable if `fixtureFolder` has been set)
|
|
63
|
+
* `removeTables() => Promise<void>` – removes all of the tables (starting with `tablesWithDependencies`)
|
|
64
|
+
|
|
65
|
+
## csvFromFolderToDb()
|
|
66
|
+
|
|
67
|
+
Imports data into tables from a folder of CSV files. All files will be imported and they should named by their table names + `.csv`.
|
|
68
|
+
|
|
69
|
+
### Syntax
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
csvFromFolderToDb(pool, path, [tablesWithDependencies]) => Promise<void>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Arguments
|
|
76
|
+
|
|
77
|
+
* `pool` – _`string | pg.Pool`_ – a postgres pool to use for the queries or a connection string that will be used to create one
|
|
78
|
+
* `path` – _`string | URL`_ – the path to the folder that contains the CSV:s named by their table names
|
|
79
|
+
* `tablesWithDependencies` – _`[string[]]`_ – _optional_ – names of tables that depend on other tables. The first name in this list will have its fixtures inserted last
|
|
80
|
+
|
|
81
|
+
### Returns
|
|
82
|
+
|
|
83
|
+
`Promise` that resolves on completion
|
|
84
|
+
|
|
85
|
+
<!-- ## Used by
|
|
86
|
+
|
|
87
|
+
* [`example`](https://example.com/) – used by this one to do X and Y
|
|
88
|
+
|
|
89
|
+
## Similar modules
|
|
90
|
+
|
|
91
|
+
* [`example`](https://example.com/) – is similar in this way
|
|
92
|
+
|
|
93
|
+
## See also
|
|
94
|
+
|
|
95
|
+
* [Announcement blog post](#)
|
|
96
|
+
* [Announcement tweet](#) -->
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":""}
|
package/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function csvFromFolderToDb(connection: ConnectionConfig | Pool, path: string | URL, tablesWithDependencies?: string[] | undefined): Promise<void>;
|
|
2
|
+
import type { ConnectionConfig } from './utils.js';
|
|
3
|
+
import type { Pool } from 'pg';
|
|
4
|
+
//# sourceMappingURL=csv-folder-to-db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv-folder-to-db.d.ts","sourceRoot":"","sources":["csv-folder-to-db.js"],"names":[],"mappings":"AAmEA,8CALW,gBAAgB,GAAG,IAAI,QACvB,MAAM,GAAG,GAAG,2BACZ,MAAM,EAAE,GAAG,SAAS,GAClB,OAAO,CAAC,IAAI,CAAC,CAwBzB;sCA7EqC,YAAY;0BAFxB,IAAI"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { opendir } from 'node:fs/promises';
|
|
3
|
+
import pathModule from 'node:path';
|
|
4
|
+
import { pipeline as promisedPipeline } from 'node:stream/promises';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
import { from as copyFrom } from 'pg-copy-streams';
|
|
8
|
+
|
|
9
|
+
import { createPgPool } from './utils.js';
|
|
10
|
+
|
|
11
|
+
/** @import { Pool } from 'pg' */
|
|
12
|
+
|
|
13
|
+
/** @import { ConnectionConfig } from './utils.js' */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string | URL} path
|
|
17
|
+
* @param {string} [extension]
|
|
18
|
+
* @returns {AsyncIterable<string>}
|
|
19
|
+
*/
|
|
20
|
+
async function * filesInFolder (path, extension) {
|
|
21
|
+
const dir = await opendir(path);
|
|
22
|
+
const stringPath = typeof path === 'string' ? path : fileURLToPath(path);
|
|
23
|
+
|
|
24
|
+
for await (const dirent of dir) {
|
|
25
|
+
if (!dirent.isFile()) continue;
|
|
26
|
+
if (extension && pathModule.extname(dirent.name) !== extension) continue;
|
|
27
|
+
yield pathModule.join(stringPath, dirent.name);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string | URL} path
|
|
33
|
+
* @param {string[]} tablesWithDependencies
|
|
34
|
+
* @returns {Promise<string[]>}
|
|
35
|
+
*/
|
|
36
|
+
async function getFilesOrderedByDependencies (path, tablesWithDependencies) {
|
|
37
|
+
/** @type {Map<string, string>} */
|
|
38
|
+
const fileMap = new Map();
|
|
39
|
+
|
|
40
|
+
for await (const file of filesInFolder(path, '.csv')) {
|
|
41
|
+
fileMap.set(pathModule.basename(file, '.csv'), file);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @type {string[]} */
|
|
45
|
+
const files = [];
|
|
46
|
+
|
|
47
|
+
for (const table of tablesWithDependencies) {
|
|
48
|
+
const file = fileMap.get(table);
|
|
49
|
+
|
|
50
|
+
if (file) {
|
|
51
|
+
files.unshift(file);
|
|
52
|
+
fileMap.delete(table);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return [
|
|
57
|
+
...fileMap.values(),
|
|
58
|
+
...files,
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {ConnectionConfig | Pool} connection
|
|
64
|
+
* @param {string | URL} path
|
|
65
|
+
* @param {string[] | undefined} [tablesWithDependencies]
|
|
66
|
+
* @returns {Promise<void>}
|
|
67
|
+
*/
|
|
68
|
+
export async function csvFromFolderToDb (connection, path, tablesWithDependencies = []) {
|
|
69
|
+
const files = await getFilesOrderedByDependencies(path, tablesWithDependencies);
|
|
70
|
+
|
|
71
|
+
const pool = (typeof connection === 'object' && 'connect' in connection) ? connection : createPgPool(connection);
|
|
72
|
+
|
|
73
|
+
const client = await pool.connect();
|
|
74
|
+
|
|
75
|
+
for (const file of files) {
|
|
76
|
+
const name = pathModule.basename(file, '.csv');
|
|
77
|
+
const dbCopy = client.query(copyFrom(`COPY ${name} FROM STDIN DELIMITER ',' CSV HEADER`));
|
|
78
|
+
|
|
79
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
80
|
+
const csvContent = createReadStream(file);
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
await promisedPipeline(csvContent, dbCopy);
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
throw new Error(`Failed inserting data into "${name}"`, { cause });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
client.release();
|
|
90
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class PgTestHelpers {
|
|
2
|
+
constructor(options: PgTestHelpersOptions);
|
|
3
|
+
queryPromise: Pool["query"];
|
|
4
|
+
initTables(): Promise<void>;
|
|
5
|
+
insertFixtures(): Promise<void>;
|
|
6
|
+
removeTables(): Promise<void>;
|
|
7
|
+
#private;
|
|
8
|
+
}
|
|
9
|
+
export type PgTestHelpersOptions = {
|
|
10
|
+
connectionConfig: ConnectionConfig;
|
|
11
|
+
fixtureFolder?: string | URL;
|
|
12
|
+
schema: string | URL | import("umzug").Umzug;
|
|
13
|
+
tablesWithDependencies?: Array<string[] | string>;
|
|
14
|
+
};
|
|
15
|
+
import type { Pool } from 'pg';
|
|
16
|
+
import type { ConnectionConfig } from './utils.js';
|
|
17
|
+
//# sourceMappingURL=test-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["test-helpers.js"],"names":[],"mappings":"AAsBA;IAaE,qBADY,oBAAoB,EAiC/B;IAnCD,cADW,IAAI,CAAC,OAAO,CAAC,CACX;IAoEb,cADc,OAAO,CAAC,IAAI,CAAC,CAiB1B;IAGD,kBADc,OAAO,CAAC,IAAI,CAAC,CAM1B;IAGD,gBADc,OAAO,CAAC,IAAI,CAAC,CAM1B;;CACF;;sBArHa,gBAAgB;oBAChB,MAAM,GAAG,GAAG;YACZ,MAAM,GAAG,GAAG,GAAG,OAAO,OAAO,EAAE,KAAK;6BACpC,KAAK,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC;;0BATZ,IAAI;sCAEQ,YAAY"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createUmzeptionPgContext,
|
|
5
|
+
pgInstallSchemaFromString,
|
|
6
|
+
} from 'umzeption';
|
|
7
|
+
|
|
8
|
+
import { csvFromFolderToDb } from './csv-folder-to-db.js';
|
|
9
|
+
import { createPgPool, isStringArray, TypeNeverError } from './utils.js';
|
|
10
|
+
|
|
11
|
+
/** @import { Pool } from 'pg' */
|
|
12
|
+
|
|
13
|
+
/** @import { ConnectionConfig } from './utils.js' */
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef PgTestHelpersOptions
|
|
17
|
+
* @property {ConnectionConfig} connectionConfig
|
|
18
|
+
* @property {string | URL} [fixtureFolder]
|
|
19
|
+
* @property {string | URL | import('umzug').Umzug} schema
|
|
20
|
+
* @property {Array<string[] | string>} [tablesWithDependencies]
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export class PgTestHelpers {
|
|
24
|
+
/** @type {string | URL | undefined} */
|
|
25
|
+
#fixtureFolder;
|
|
26
|
+
/** @type {Pool} */
|
|
27
|
+
#pool;
|
|
28
|
+
/** @type {PgTestHelpersOptions['schema']} */
|
|
29
|
+
#schema;
|
|
30
|
+
/** @type {Array<string[] | string> | undefined} */
|
|
31
|
+
#tablesWithDependencies;
|
|
32
|
+
/** @type {Pool['query']} */
|
|
33
|
+
queryPromise;
|
|
34
|
+
|
|
35
|
+
/** @param {PgTestHelpersOptions} options */
|
|
36
|
+
constructor (options) {
|
|
37
|
+
if (!options || typeof options !== 'object') {
|
|
38
|
+
throw new TypeNeverError(options, 'Expected an options object');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const {
|
|
42
|
+
connectionConfig,
|
|
43
|
+
fixtureFolder,
|
|
44
|
+
schema,
|
|
45
|
+
tablesWithDependencies,
|
|
46
|
+
} = options;
|
|
47
|
+
|
|
48
|
+
if (typeof connectionConfig !== 'string' && typeof connectionConfig !== 'object') {
|
|
49
|
+
throw new TypeNeverError(connectionConfig, 'Invalid connectionConfig, expected a string or an object');
|
|
50
|
+
}
|
|
51
|
+
if (fixtureFolder && typeof fixtureFolder !== 'string' && !(fixtureFolder instanceof URL)) {
|
|
52
|
+
throw new TypeNeverError(fixtureFolder, 'Invalid fixtureFolder, expected a string');
|
|
53
|
+
}
|
|
54
|
+
if (typeof schema !== 'string' && typeof schema !== 'object') {
|
|
55
|
+
throw new TypeNeverError(schema, 'Invalid schema, expected a string or an object');
|
|
56
|
+
}
|
|
57
|
+
if (tablesWithDependencies && !Array.isArray(tablesWithDependencies)) {
|
|
58
|
+
throw new TypeNeverError(tablesWithDependencies, 'Invalid tablesWithDependencies, expected an array');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const pool = createPgPool(connectionConfig);
|
|
62
|
+
|
|
63
|
+
this.#fixtureFolder = fixtureFolder;
|
|
64
|
+
this.#tablesWithDependencies = tablesWithDependencies;
|
|
65
|
+
this.#pool = pool;
|
|
66
|
+
this.#schema = schema;
|
|
67
|
+
this.queryPromise = pool.query.bind(pool);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @returns {Promise<string[]>} */
|
|
71
|
+
async #getTableNames () {
|
|
72
|
+
const { rows } = await this.queryPromise('SELECT tablename FROM pg_tables WHERE schemaname = \'public\'');
|
|
73
|
+
return rows.map(row => row.tablename);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Array<string[] | string>} tables
|
|
78
|
+
* @returns {Promise<void>}
|
|
79
|
+
*/
|
|
80
|
+
async #removeTablesByName (tables) {
|
|
81
|
+
if (isStringArray(tables)) {
|
|
82
|
+
await Promise.all(
|
|
83
|
+
tables.map(name => this.queryPromise('DROP TABLE IF EXISTS ' + name + ' CASCADE'))
|
|
84
|
+
).catch(cause => {
|
|
85
|
+
throw new Error(`Failed to drop tables: ${tables}`, { cause });
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
for (const name of tables) {
|
|
89
|
+
await (
|
|
90
|
+
Array.isArray(name)
|
|
91
|
+
? this.#removeTablesByName(name)
|
|
92
|
+
: this.queryPromise('DROP TABLE IF EXISTS ' + name + ' CASCADE').catch(cause => {
|
|
93
|
+
throw new Error(`Failed to drop table: ${name}`, { cause });
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @returns {Promise<void>} */
|
|
101
|
+
async initTables () {
|
|
102
|
+
try {
|
|
103
|
+
if (typeof this.#schema === 'object' && 'up' in this.#schema) {
|
|
104
|
+
await this.#schema.up();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const schema = this.#schema instanceof URL
|
|
109
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
110
|
+
? await readFile(this.#schema, 'utf8')
|
|
111
|
+
: this.#schema;
|
|
112
|
+
|
|
113
|
+
return pgInstallSchemaFromString(createUmzeptionPgContext(this.#pool), schema);
|
|
114
|
+
} catch (cause) {
|
|
115
|
+
throw new Error('Failed to create tables', { cause });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @returns {Promise<void>} */
|
|
120
|
+
async insertFixtures () {
|
|
121
|
+
if (!this.#fixtureFolder) {
|
|
122
|
+
throw new Error('No fixture folder defined');
|
|
123
|
+
}
|
|
124
|
+
return csvFromFolderToDb(this.#pool, this.#fixtureFolder, this.#tablesWithDependencies?.flat());
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @returns {Promise<void>} */
|
|
128
|
+
async removeTables () {
|
|
129
|
+
if (this.#tablesWithDependencies) {
|
|
130
|
+
await this.#removeTablesByName(this.#tablesWithDependencies);
|
|
131
|
+
}
|
|
132
|
+
await this.#removeTablesByName(await this.#getTableNames());
|
|
133
|
+
}
|
|
134
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function createPgPool(connectionString: ConnectionConfig): import("pg").Pool;
|
|
2
|
+
export function isStringArray(value: unknown): value is string[];
|
|
3
|
+
export class TypeNeverError extends TypeError {
|
|
4
|
+
constructor(value: never, message: string, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
export type ConnectionConfigObject = Pick<import("pg").ClientConfig, "host" | "port" | "user" | "password" | "stream">;
|
|
7
|
+
export type ConnectionConfig = string | ConnectionConfigObject;
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["utils.js"],"names":[],"mappings":"AASA,+CAHW,gBAAgB,GACd,OAAO,IAAI,EAAE,IAAI,CAO7B;AA2BD,qCAHW,OAAO,GACL,KAAK,IAAI,MAAM,EAAE,CAK7B;AA5BD;IAME,mBAJW,KAAK,WACL,MAAM,YACN,YAAY,EAItB;CACF;qCAvBa,IAAI,CAAC,OAAO,IAAI,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,QAAQ,CAAC;+BACjF,MAAM,GAAG,sBAAsB"}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
|
|
3
|
+
/** @typedef {Pick<import('pg').ClientConfig, 'host' | 'port' | 'user' | 'password' | 'stream'>} ConnectionConfigObject */
|
|
4
|
+
/** @typedef {string | ConnectionConfigObject} ConnectionConfig */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {ConnectionConfig} connectionString
|
|
8
|
+
* @returns {import('pg').Pool}
|
|
9
|
+
*/
|
|
10
|
+
export function createPgPool (connectionString) {
|
|
11
|
+
return new pg.Pool({
|
|
12
|
+
...(typeof connectionString === 'object' ? connectionString : { connectionString }),
|
|
13
|
+
allowExitOnIdle: true,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class TypeNeverError extends TypeError {
|
|
18
|
+
/**
|
|
19
|
+
* @param {never} value
|
|
20
|
+
* @param {string} message
|
|
21
|
+
* @param {ErrorOptions} [options]
|
|
22
|
+
*/
|
|
23
|
+
constructor (value, message, options) {
|
|
24
|
+
super(`${message}. Got: ${typeof value}`, options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Array.isArray() on its own give type any[]
|
|
30
|
+
*
|
|
31
|
+
* @param {unknown} value
|
|
32
|
+
* @returns {value is unknown[]}
|
|
33
|
+
*/
|
|
34
|
+
function typesafeIsArray (value) {
|
|
35
|
+
return Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {unknown} value
|
|
40
|
+
* @returns {value is string[]}
|
|
41
|
+
*/
|
|
42
|
+
export function isStringArray (value) {
|
|
43
|
+
if (!typesafeIsArray(value)) return false;
|
|
44
|
+
return value.every(item => typeof item === 'string');
|
|
45
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voxpelli/pg-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": " My personal database utils / helpers for Postgres",
|
|
5
|
+
"homepage": "http://github.com/voxpelli/pg-utils",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git://github.com/voxpelli/pg-utils.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "Pelle Wessman <pelle@kodfabrik.se> (http://kodfabrik.se/)",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^20.9.0 || >=22.0.0"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": "./index.js",
|
|
18
|
+
"types": "index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"index.d.ts",
|
|
22
|
+
"index.d.ts.map",
|
|
23
|
+
"lib/**/*.js",
|
|
24
|
+
"lib/**/*.d.ts",
|
|
25
|
+
"lib/**/*.d.ts.map"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build:0": "run-s clean",
|
|
29
|
+
"build:1-declaration": "tsc -p declaration.tsconfig.json",
|
|
30
|
+
"build": "run-s build:*",
|
|
31
|
+
"check:installed-check": "installed-check",
|
|
32
|
+
"check:knip": "knip",
|
|
33
|
+
"check:lint": "eslint",
|
|
34
|
+
"check:tsc": "tsc",
|
|
35
|
+
"check:type-coverage": "type-coverage --detail --strict --at-least 95 --ignore-files 'test/*'",
|
|
36
|
+
"check": "run-s clean && run-p check:*",
|
|
37
|
+
"clean:declarations-top": "rm -rf $(find . -maxdepth 1 -type f -name '*.d.ts*' ! -name 'index.d.ts')",
|
|
38
|
+
"clean:declarations-lib": "rm -rf $(find lib -type f -name '*.d.ts*' ! -name '*-types.d.ts')",
|
|
39
|
+
"clean": "run-p clean:*",
|
|
40
|
+
"prepare": "husky",
|
|
41
|
+
"prepublishOnly": "run-s build",
|
|
42
|
+
"test:mocha": "c8 --reporter=lcov --reporter=text mocha 'test/**/*.spec.js'",
|
|
43
|
+
"test-ci": "run-s test:*",
|
|
44
|
+
"test": "run-s check test:*"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/chai": "^4.3.20",
|
|
48
|
+
"@types/chai-as-promised": "^7.1.8",
|
|
49
|
+
"@types/mocha": "^10.0.10",
|
|
50
|
+
"@types/node": "^20.17.9",
|
|
51
|
+
"@types/pg-copy-streams": "^1.2.5",
|
|
52
|
+
"@voxpelli/eslint-config": "^22.2.0",
|
|
53
|
+
"@voxpelli/tsconfig": "^15.1.0",
|
|
54
|
+
"c8": "^10.1.2",
|
|
55
|
+
"chai": "^4.5.0",
|
|
56
|
+
"chai-as-promised": "^7.1.2",
|
|
57
|
+
"dotenv": "^16.4.6",
|
|
58
|
+
"eslint": "^9.16.0",
|
|
59
|
+
"husky": "^9.1.7",
|
|
60
|
+
"installed-check": "^9.3.0",
|
|
61
|
+
"knip": "^5.38.4",
|
|
62
|
+
"mocha": "^10.8.2",
|
|
63
|
+
"npm-run-all2": "^7.0.1",
|
|
64
|
+
"type-coverage": "^2.29.7",
|
|
65
|
+
"typescript": "~5.7.2"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@types/pg": "^8.11.10",
|
|
69
|
+
"pg": "^8.13.1",
|
|
70
|
+
"pg-copy-streams": "^6.0.6",
|
|
71
|
+
"umzeption": "^0.4.1",
|
|
72
|
+
"umzug": "^3.8.2"
|
|
73
|
+
}
|
|
74
|
+
}
|