@n8io/baseline-postgres 4.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 CHANGED
@@ -10,7 +10,7 @@ pnpm add @n8io/baseline-postgres
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
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).
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(pgPool, async (pgClient) => {
38
- const rows = await drizzle({ client: pgClient })
39
- .insert(fooTable)
40
- .values(foo)
41
- .returning();
42
- return rows[0] ? ok(rows[0]) : err({ type: "FooNotCreated" as const });
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
@@ -52,8 +52,10 @@ type PostgresPoolOptions = {
52
52
  vendor?: PostgresPoolVendorOptions;
53
53
  };
54
54
  export declare const postgresPoolCreate: ({ connectTimeoutMs, databaseUrl, max, vendor }: PostgresPoolOptions) => PostgresPool;
55
- export declare const postgresTransactionRun: <T, E>(pgPool: PostgresPool, work: (pgClient: PostgresClient) => Promise<Result<T, E>>, options?: {
56
- timeoutMs?: number;
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>>;
57
59
  }) => Promise<Result<T, E>>;
58
60
  export declare const postgresUniqueViolationIs: (error: unknown, constraint: string) => boolean;
59
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 = (vendor, databaseUrl, owned = {}) => {
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(options.vendor ?? {}, options.databaseUrl);
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(vendor, databaseUrl, { max: "max" });
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,
@@ -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, work, options = {}) => {
96
- const timeoutMs = options.timeoutMs ?? 5e3;
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": "4.0.0",
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",