@n8io/baseline-postgres 2.0.0 → 3.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/README.md +3 -2
- package/dist/index.d.ts +38 -4
- package/dist/index.js +34 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,9 +8,10 @@ pnpm add @n8io/baseline-postgres
|
|
|
8
8
|
|
|
9
9
|
## Exports
|
|
10
10
|
|
|
11
|
-
- `postgresPoolCreate({ databaseUrl,
|
|
11
|
+
- `postgresPoolCreate({ databaseUrl, max?, connectTimeoutMs?, vendor? })`: the process's one pool (10 connections, 3 s connect timeout). `vendor` takes pg's own pool settings, such as `ssl` or `idleTimeoutMillis`, and passes them through. The named options win: `vendor` throws on the keys they set, on `host`, `port`, `database`, `user` and `password` (pg lets the URL win, so put them in `databaseUrl`), and on `ssl` when the URL carries an `ssl*` parameter. It is outside the contract: nothing in it is tested here, and moving off pg deletes it.
|
|
12
|
+
- `PostgresPool`, `PostgresClient`: the pool and a checked-out connection. Name these in your own signatures, not `pg`'s types. They are pg's own pool and client, because Drizzle's `drizzle({ client })` needs the real ones.
|
|
12
13
|
- `postgresTransactionRun(pgPool, work, { timeoutMs? })`: runs `work(pgClient)` in one transaction; COMMIT on `ok`, ROLLBACK on `err` or a throw. It ends after `timeoutMs` (default 5 s).
|
|
13
|
-
- `postgresMigrate({ databaseUrl, migrationsFolder })`: applies pending Drizzle migrations under an advisory lock, so concurrent starts apply each one once.
|
|
14
|
+
- `postgresMigrate({ databaseUrl, migrationsFolder, vendor? })`: applies pending Drizzle migrations under an advisory lock, so concurrent starts apply each one once. `vendor` takes pg's settings for its one connection, under the same rules as the pool's.
|
|
14
15
|
- `postgresIdColumnCreate(prefix)`: a Drizzle `uuid` column that reads and writes `Id<prefix>`.
|
|
15
16
|
- `postgresUniqueViolationIs(error, constraint)`: true when a unique violation on that constraint is anywhere in the `cause` chain.
|
|
16
17
|
|
package/dist/index.d.ts
CHANGED
|
@@ -8,15 +8,49 @@ type PostgresIdColumnBuilder<P extends string> = PgCustomColumnBuilder<{
|
|
|
8
8
|
driverParam: string;
|
|
9
9
|
}>;
|
|
10
10
|
export declare const postgresIdColumnCreate: <P extends string>(prefix: P) => ((name?: string) => PostgresIdColumnBuilder<P>);
|
|
11
|
+
declare const postgresVendorOwnedKeys: {
|
|
12
|
+
readonly connectionString: "databaseUrl";
|
|
13
|
+
readonly connectionTimeoutMillis: "connectTimeoutMs";
|
|
14
|
+
readonly database: "databaseUrl";
|
|
15
|
+
readonly host: "databaseUrl";
|
|
16
|
+
readonly password: "databaseUrl";
|
|
17
|
+
readonly port: "databaseUrl";
|
|
18
|
+
readonly user: "databaseUrl";
|
|
19
|
+
};
|
|
20
|
+
type PostgresVendorOwnedKey = keyof typeof postgresVendorOwnedKeys;
|
|
21
|
+
/**
|
|
22
|
+
* pg's own client settings for the migration's one connection, passed through as they are.
|
|
23
|
+
* Outside this Package's contract, like `PostgresPoolVendorOptions`.
|
|
24
|
+
*/
|
|
25
|
+
type PostgresMigrateVendorOptions = Omit<pg.ClientConfig, PostgresVendorOwnedKey>;
|
|
11
26
|
export declare const postgresMigrate: (options: {
|
|
12
27
|
databaseUrl: string;
|
|
13
28
|
migrationsFolder: string;
|
|
29
|
+
/** Settings only pg has, such as `ssl`: the same ones the pool takes. */
|
|
30
|
+
vendor?: PostgresMigrateVendorOptions;
|
|
14
31
|
}) => Promise<void>;
|
|
15
|
-
|
|
32
|
+
/** The process's Postgres connection pool. Close it with `end()`. */
|
|
33
|
+
type PostgresPool = pg.Pool;
|
|
34
|
+
/** One connection checked out of the pool: what `postgresTransactionRun` hands to its work. */
|
|
35
|
+
type PostgresClient = pg.PoolClient;
|
|
36
|
+
/**
|
|
37
|
+
* pg's own pool settings, passed through as they are. Outside this Package's contract: nothing
|
|
38
|
+
* here is tested, and moving off pg deletes it. The keys the named options and the URL set are
|
|
39
|
+
* refused.
|
|
40
|
+
*/
|
|
41
|
+
type PostgresPoolVendorOptions = Omit<pg.PoolConfig, PostgresVendorOwnedKey | "max">;
|
|
42
|
+
type PostgresPoolOptions = {
|
|
16
43
|
databaseUrl: string;
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
/** How long a connect may take. Default 3 s. */
|
|
45
|
+
connectTimeoutMs?: number;
|
|
46
|
+
/** The most connections the pool opens. Default 10. */
|
|
47
|
+
max?: number;
|
|
48
|
+
/** Settings only pg has, such as `ssl` or `idleTimeoutMillis`. */
|
|
49
|
+
vendor?: PostgresPoolVendorOptions;
|
|
50
|
+
};
|
|
51
|
+
export declare const postgresPoolCreate: ({ connectTimeoutMs, databaseUrl, max, vendor }: PostgresPoolOptions) => PostgresPool;
|
|
52
|
+
export declare const postgresTransactionRun: <T, E>(pgPool: PostgresPool, work: (pgClient: PostgresClient) => Promise<Result<T, E>>, options?: {
|
|
19
53
|
timeoutMs?: number;
|
|
20
54
|
}) => Promise<Result<T, E>>;
|
|
21
55
|
export declare const postgresUniqueViolationIs: (error: unknown, constraint: string) => boolean;
|
|
22
|
-
export {};
|
|
56
|
+
export type { PostgresClient, PostgresMigrateVendorOptions, PostgresPool, PostgresPoolOptions, PostgresPoolVendorOptions };
|
package/dist/index.js
CHANGED
|
@@ -24,11 +24,39 @@ const postgresUrlAssert = (databaseUrl) => {
|
|
|
24
24
|
const found = scheme ? `"${scheme}://"` : "no scheme";
|
|
25
25
|
throw new TypeError(`Invalid database URL: found ${found}; use postgres:// or postgresql://`);
|
|
26
26
|
};
|
|
27
|
+
// src/postgres-vendor-assert.ts
|
|
28
|
+
const postgresVendorOwnedKeys = {
|
|
29
|
+
connectionString: "databaseUrl",
|
|
30
|
+
connectionTimeoutMillis: "connectTimeoutMs",
|
|
31
|
+
database: "databaseUrl",
|
|
32
|
+
host: "databaseUrl",
|
|
33
|
+
password: "databaseUrl",
|
|
34
|
+
port: "databaseUrl",
|
|
35
|
+
user: "databaseUrl"
|
|
36
|
+
};
|
|
37
|
+
const postgresUrlSslParams = [
|
|
38
|
+
"ssl",
|
|
39
|
+
"sslcert",
|
|
40
|
+
"sslkey",
|
|
41
|
+
"sslmode",
|
|
42
|
+
"sslrootcert"
|
|
43
|
+
];
|
|
44
|
+
const postgresVendorAssert = (vendor, databaseUrl, owned = {}) => {
|
|
45
|
+
for (const [key, option] of Object.entries({
|
|
46
|
+
...postgresVendorOwnedKeys,
|
|
47
|
+
...owned
|
|
48
|
+
})) if (key in vendor) throw new TypeError(`vendor.${key} is not allowed: set it with ${option} instead`);
|
|
49
|
+
const urlParams = new URL(databaseUrl).searchParams;
|
|
50
|
+
const sslParam = postgresUrlSslParams.find((name) => urlParams.has(name));
|
|
51
|
+
if ("ssl" in vendor && sslParam !== void 0) throw new TypeError(`vendor.ssl is not allowed with ${sslParam} in databaseUrl: pg lets the URL win; set TLS in one place`);
|
|
52
|
+
};
|
|
27
53
|
// src/postgres-migrate.ts
|
|
28
54
|
const postgresMigrateLockKey = createHash("sha256").update("baseline:migrate").digest().readBigInt64BE(0).toString();
|
|
29
55
|
const postgresMigrate = async (options) => {
|
|
30
56
|
postgresUrlAssert(options.databaseUrl);
|
|
57
|
+
postgresVendorAssert(options.vendor ?? {}, options.databaseUrl);
|
|
31
58
|
const pgClient = new pg.Client({
|
|
59
|
+
...options.vendor,
|
|
32
60
|
connectionString: options.databaseUrl,
|
|
33
61
|
connectionTimeoutMillis: 3e3
|
|
34
62
|
});
|
|
@@ -43,15 +71,15 @@ const postgresMigrate = async (options) => {
|
|
|
43
71
|
}
|
|
44
72
|
};
|
|
45
73
|
// src/postgres-pool.ts
|
|
46
|
-
const postgresPoolCreate = (
|
|
47
|
-
const { databaseUrl, ...overrides } = options;
|
|
74
|
+
const postgresPoolCreate = ({ connectTimeoutMs = 3e3, databaseUrl, max = 10, vendor = {} }) => {
|
|
48
75
|
postgresUrlAssert(databaseUrl);
|
|
76
|
+
postgresVendorAssert(vendor, databaseUrl, { max: "max" });
|
|
49
77
|
const pgPool = new pg.Pool({
|
|
50
|
-
connectionString: databaseUrl,
|
|
51
|
-
connectionTimeoutMillis: 3e3,
|
|
52
78
|
keepAlive: true,
|
|
53
|
-
|
|
54
|
-
|
|
79
|
+
...vendor,
|
|
80
|
+
connectionString: databaseUrl,
|
|
81
|
+
connectionTimeoutMillis: connectTimeoutMs,
|
|
82
|
+
max
|
|
55
83
|
});
|
|
56
84
|
pgPool.on("error", (error) => {
|
|
57
85
|
logger.error({ err: error }, "Postgres idle client error");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n8io/baseline-postgres",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Postgres pool, Result-aware transactions, migrations and prefixed-id columns on pg and Drizzle.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@n8io/baseline-id": "^2.0.0",
|
|
31
|
-
"@n8io/baseline-logger": "^
|
|
31
|
+
"@n8io/baseline-logger": "^3.0.0",
|
|
32
32
|
"@n8io/baseline-result": "^1.1.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|