@n8io/baseline-postgres 3.0.0 → 5.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 +11 -8
- package/dist/index.d.ts +9 -4
- package/dist/index.js +13 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@ pnpm add @n8io/baseline-postgres
|
|
|
9
9
|
## Exports
|
|
10
10
|
|
|
11
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.
|
|
13
|
-
- `postgresTransactionRun(pgPool, work,
|
|
12
|
+
- `PostgresPool`, `PostgresClient`: the pool and a checked-out connection. `await using` calls the pool's `end()` when its block ends. 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.
|
|
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).
|
|
14
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.
|
|
15
15
|
- `postgresIdColumnCreate(prefix)`: a Drizzle `uuid` column that reads and writes `Id<prefix>`.
|
|
16
16
|
- `postgresUniqueViolationIs(error, constraint)`: true when a unique violation on that constraint is anywhere in the `cause` chain.
|
|
@@ -34,12 +34,15 @@ export const fooTable = pgTable("foo", {
|
|
|
34
34
|
|
|
35
35
|
const pgPool = postgresPoolCreate({ databaseUrl: ENV.DATABASE_URL });
|
|
36
36
|
|
|
37
|
-
const result = await postgresTransactionRun(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
const result = await postgresTransactionRun({
|
|
38
|
+
pgPool,
|
|
39
|
+
work: async (pgClient) => {
|
|
40
|
+
const rows = await drizzle({ client: pgClient })
|
|
41
|
+
.insert(fooTable)
|
|
42
|
+
.values(foo)
|
|
43
|
+
.returning();
|
|
44
|
+
return rows[0] ? ok(rows[0]) : err({ type: "FooNotCreated" as const });
|
|
45
|
+
},
|
|
43
46
|
});
|
|
44
47
|
```
|
|
45
48
|
|
package/dist/index.d.ts
CHANGED
|
@@ -29,8 +29,11 @@ export declare const postgresMigrate: (options: {
|
|
|
29
29
|
/** Settings only pg has, such as `ssl`: the same ones the pool takes. */
|
|
30
30
|
vendor?: PostgresMigrateVendorOptions;
|
|
31
31
|
}) => Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The process's Postgres connection pool. Close it with `end()`; `await using` calls `end()` when
|
|
34
|
+
* its block ends.
|
|
35
|
+
*/
|
|
36
|
+
type PostgresPool = pg.Pool & AsyncDisposable;
|
|
34
37
|
/** One connection checked out of the pool: what `postgresTransactionRun` hands to its work. */
|
|
35
38
|
type PostgresClient = pg.PoolClient;
|
|
36
39
|
/**
|
|
@@ -49,8 +52,10 @@ type PostgresPoolOptions = {
|
|
|
49
52
|
vendor?: PostgresPoolVendorOptions;
|
|
50
53
|
};
|
|
51
54
|
export declare const postgresPoolCreate: ({ connectTimeoutMs, databaseUrl, max, vendor }: PostgresPoolOptions) => PostgresPool;
|
|
52
|
-
export declare const postgresTransactionRun: <T, E>(pgPool:
|
|
53
|
-
|
|
55
|
+
export declare const postgresTransactionRun: <T, E>({ pgPool, timeoutMs: timeoutMsGiven, work }: {
|
|
56
|
+
pgPool: PostgresPool;
|
|
57
|
+
timeoutMs?: number | null;
|
|
58
|
+
work: (pgClient: PostgresClient) => Promise<Result<T, E>>;
|
|
54
59
|
}) => Promise<Result<T, E>>;
|
|
55
60
|
export declare const postgresUniqueViolationIs: (error: unknown, constraint: string) => boolean;
|
|
56
61
|
export type { PostgresClient, PostgresMigrateVendorOptions, PostgresPool, PostgresPoolOptions, PostgresPoolVendorOptions };
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ const postgresUrlSslParams = [
|
|
|
41
41
|
"sslmode",
|
|
42
42
|
"sslrootcert"
|
|
43
43
|
];
|
|
44
|
-
const postgresVendorAssert = (
|
|
44
|
+
const postgresVendorAssert = ({ databaseUrl, owned = {}, vendor }) => {
|
|
45
45
|
for (const [key, option] of Object.entries({
|
|
46
46
|
...postgresVendorOwnedKeys,
|
|
47
47
|
...owned
|
|
@@ -54,7 +54,10 @@ const postgresVendorAssert = (vendor, databaseUrl, owned = {}) => {
|
|
|
54
54
|
const postgresMigrateLockKey = createHash("sha256").update("baseline:migrate").digest().readBigInt64BE(0).toString();
|
|
55
55
|
const postgresMigrate = async (options) => {
|
|
56
56
|
postgresUrlAssert(options.databaseUrl);
|
|
57
|
-
postgresVendorAssert(
|
|
57
|
+
postgresVendorAssert({
|
|
58
|
+
vendor: options.vendor ?? {},
|
|
59
|
+
databaseUrl: options.databaseUrl
|
|
60
|
+
});
|
|
58
61
|
const pgClient = new pg.Client({
|
|
59
62
|
...options.vendor,
|
|
60
63
|
connectionString: options.databaseUrl,
|
|
@@ -73,7 +76,11 @@ const postgresMigrate = async (options) => {
|
|
|
73
76
|
// src/postgres-pool.ts
|
|
74
77
|
const postgresPoolCreate = ({ connectTimeoutMs = 3e3, databaseUrl, max = 10, vendor = {} }) => {
|
|
75
78
|
postgresUrlAssert(databaseUrl);
|
|
76
|
-
postgresVendorAssert(
|
|
79
|
+
postgresVendorAssert({
|
|
80
|
+
vendor,
|
|
81
|
+
databaseUrl,
|
|
82
|
+
owned: { max: "max" }
|
|
83
|
+
});
|
|
77
84
|
const pgPool = new pg.Pool({
|
|
78
85
|
keepAlive: true,
|
|
79
86
|
...vendor,
|
|
@@ -84,7 +91,7 @@ const postgresPoolCreate = ({ connectTimeoutMs = 3e3, databaseUrl, max = 10, ven
|
|
|
84
91
|
pgPool.on("error", (error) => {
|
|
85
92
|
logger.error({ err: error }, "Postgres idle client error");
|
|
86
93
|
});
|
|
87
|
-
return pgPool;
|
|
94
|
+
return Object.assign(pgPool, { [Symbol.asyncDispose]: () => pgPool.end() });
|
|
88
95
|
};
|
|
89
96
|
// src/postgres-transaction-run.ts
|
|
90
97
|
const clientGuard = (pgClient, isOpen) => new Proxy(pgClient, { get(target, property) {
|
|
@@ -92,8 +99,8 @@ const clientGuard = (pgClient, isOpen) => new Proxy(pgClient, { get(target, prop
|
|
|
92
99
|
const value = Reflect.get(target, property, target);
|
|
93
100
|
return typeof value === "function" ? value.bind(target) : value;
|
|
94
101
|
} });
|
|
95
|
-
const postgresTransactionRun = async (pgPool,
|
|
96
|
-
const timeoutMs =
|
|
102
|
+
const postgresTransactionRun = async ({ pgPool, timeoutMs: timeoutMsGiven, work }) => {
|
|
103
|
+
const timeoutMs = timeoutMsGiven ?? 5e3;
|
|
97
104
|
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0) throw new TypeError("timeoutMs must be a positive integer");
|
|
98
105
|
const pgClient = await pgPool.connect();
|
|
99
106
|
let broken = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n8io/baseline-postgres",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.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",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"registry": "https://registry.npmjs.org"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@n8io/baseline-id": "^
|
|
31
|
-
"@n8io/baseline-logger": "^3.
|
|
30
|
+
"@n8io/baseline-id": "^3.0.0",
|
|
31
|
+
"@n8io/baseline-logger": "^3.1.0",
|
|
32
32
|
"@n8io/baseline-result": "^1.1.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|