@n8io/baseline-postgres 1.2.0 → 2.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
@@ -9,33 +9,31 @@ pnpm add @n8io/baseline-postgres
9
9
  ## Exports
10
10
 
11
11
  - `postgresPoolCreate({ databaseUrl, ...pg.PoolConfig })`: the process's one pool (10 connections, 3 s connect timeout).
12
- - `postgresTransaction(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
- - `postgresPortsGuard(ports, isOpen)`: wraps a transaction's ports so a call after the transaction ended throws.
12
+ - `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
13
  - `postgresMigrate({ databaseUrl, migrationsFolder })`: applies pending Drizzle migrations under an advisory lock, so concurrent starts apply each one once.
15
- - `postgresIdColumn(prefix)`: a Drizzle `uuid` column that reads and writes `Id<prefix>`.
16
- - `postgresCursorEncode(cursor)`, `postgresCursorDecode(text, prefix)`, `PostgresCursor`: a list page's position as base64url; a cursor that does not decode reads as none.
17
- - `postgresUniqueViolation(error, constraint)`: true when a unique violation on that constraint is anywhere in the `cause` chain.
14
+ - `postgresIdColumnCreate(prefix)`: a Drizzle `uuid` column that reads and writes `Id<prefix>`.
15
+ - `postgresUniqueViolationIs(error, constraint)`: true when a unique violation on that constraint is anywhere in the `cause` chain.
18
16
 
19
17
  ## Example
20
18
 
21
19
  ```ts
22
20
  import {
23
- postgresIdColumn,
21
+ postgresIdColumnCreate,
24
22
  postgresPoolCreate,
25
- postgresTransaction,
23
+ postgresTransactionRun,
26
24
  } from "@n8io/baseline-postgres";
27
25
  import { err, ok } from "@n8io/baseline-result";
28
26
  import { drizzle } from "drizzle-orm/node-postgres";
29
27
  import { pgTable, text } from "drizzle-orm/pg-core";
30
28
 
31
29
  export const fooTable = pgTable("foo", {
32
- id: postgresIdColumn("foo")().primaryKey(),
30
+ id: postgresIdColumnCreate("foo")().primaryKey(),
33
31
  name: text().notNull(),
34
32
  });
35
33
 
36
34
  const pgPool = postgresPoolCreate({ databaseUrl: ENV.DATABASE_URL });
37
35
 
38
- const result = await postgresTransaction(pgPool, async (pgClient) => {
36
+ const result = await postgresTransactionRun(pgPool, async (pgClient) => {
39
37
  const rows = await drizzle({ client: pgClient })
40
38
  .insert(fooTable)
41
39
  .values(foo)
package/dist/index.d.ts CHANGED
@@ -2,18 +2,12 @@ import { Id } from "@n8io/baseline-id";
2
2
  import { PgCustomColumnBuilder } from "drizzle-orm/pg-core";
3
3
  import pg from "pg";
4
4
  import { Result } from "@n8io/baseline-result";
5
- type PostgresCursor<P extends string> = {
6
- k: string;
7
- id: Id<P>;
8
- };
9
- export declare const postgresCursorEncode: <P extends string>(cursor: PostgresCursor<P>) => string;
10
- export declare const postgresCursorDecode: <P extends string>(cursor: string, prefix: P) => PostgresCursor<P> | undefined;
11
5
  type PostgresIdColumnBuilder<P extends string> = PgCustomColumnBuilder<{
12
6
  data: Id<P>;
13
7
  dataType: "custom";
14
8
  driverParam: string;
15
9
  }>;
16
- export declare const postgresIdColumn: <P extends string>(prefix: P) => ((name?: string) => PostgresIdColumnBuilder<P>);
10
+ export declare const postgresIdColumnCreate: <P extends string>(prefix: P) => ((name?: string) => PostgresIdColumnBuilder<P>);
17
11
  export declare const postgresMigrate: (options: {
18
12
  databaseUrl: string;
19
13
  migrationsFolder: string;
@@ -21,9 +15,8 @@ export declare const postgresMigrate: (options: {
21
15
  export declare const postgresPoolCreate: (options: {
22
16
  databaseUrl: string;
23
17
  } & Partial<pg.PoolConfig>) => pg.Pool;
24
- export declare const postgresPortsGuard: <T extends object>(ports: T, isOpen: () => boolean) => T;
25
- export declare const postgresTransaction: <T, E>(pgPool: pg.Pool, work: (pgClient: pg.PoolClient) => Promise<Result<T, E>>, options?: {
18
+ export declare const postgresTransactionRun: <T, E>(pgPool: pg.Pool, work: (pgClient: pg.PoolClient) => Promise<Result<T, E>>, options?: {
26
19
  timeoutMs?: number;
27
20
  }) => Promise<Result<T, E>>;
28
- export declare const postgresUniqueViolation: (error: unknown, constraint: string) => boolean;
29
- export type { PostgresCursor };
21
+ export declare const postgresUniqueViolationIs: (error: unknown, constraint: string) => boolean;
22
+ export {};
package/dist/index.js CHANGED
@@ -5,23 +5,8 @@ import { drizzle } from "drizzle-orm/node-postgres";
5
5
  import { migrate } from "drizzle-orm/node-postgres/migrator";
6
6
  import pg from "pg";
7
7
  import { logger } from "@n8io/baseline-logger";
8
- // src/postgres-cursor.ts
9
- const postgresCursorEncode = (cursor) => Buffer.from(JSON.stringify(cursor)).toString("base64url");
10
- const postgresCursorDecode = (cursor, prefix) => {
11
- try {
12
- const parsed = JSON.parse(Buffer.from(cursor, "base64url").toString());
13
- if (typeof parsed !== "object" || parsed === null) return void 0;
14
- const { id, k } = parsed;
15
- return typeof k === "string" && typeof id === "string" && id.startsWith(`${prefix}_`) ? {
16
- id,
17
- k
18
- } : void 0;
19
- } catch {
20
- return;
21
- }
22
- };
23
8
  // src/postgres-id-column.ts
24
- const postgresIdColumn = (prefix) => {
9
+ const postgresIdColumnCreate = (prefix) => {
25
10
  const column = customType({
26
11
  dataType: () => "uuid",
27
12
  fromDriver: (value) => idFromUuid(prefix, value),
@@ -57,7 +42,7 @@ const postgresMigrate = async (options) => {
57
42
  await pgClient.end();
58
43
  }
59
44
  };
60
- // src/postgres-pool-create.ts
45
+ // src/postgres-pool.ts
61
46
  const postgresPoolCreate = (options) => {
62
47
  const { databaseUrl, ...overrides } = options;
63
48
  postgresUrlAssert(databaseUrl);
@@ -73,18 +58,13 @@ const postgresPoolCreate = (options) => {
73
58
  });
74
59
  return pgPool;
75
60
  };
76
- // src/postgres-ports-guard.ts
77
- const postgresPortsGuard = (ports, isOpen) => Object.fromEntries(Object.entries(ports).map(([key, value]) => [key, typeof value === "function" ? async (...args) => {
78
- if (!isOpen()) throw new Error("Transaction ports used after the transaction ended; keep every call inside the callback");
79
- return value(...args);
80
- } : value]));
81
- // src/postgres-transaction.ts
61
+ // src/postgres-transaction-run.ts
82
62
  const clientGuard = (pgClient, isOpen) => new Proxy(pgClient, { get(target, property) {
83
63
  if (!isOpen()) throw new Error("Postgres client used after its transaction ended; keep every query inside the callback");
84
64
  const value = Reflect.get(target, property, target);
85
65
  return typeof value === "function" ? value.bind(target) : value;
86
66
  } });
87
- const postgresTransaction = async (pgPool, work, options = {}) => {
67
+ const postgresTransactionRun = async (pgPool, work, options = {}) => {
88
68
  const timeoutMs = options.timeoutMs ?? 5e3;
89
69
  if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0) throw new TypeError("timeoutMs must be a positive integer");
90
70
  const pgClient = await pgPool.connect();
@@ -119,8 +99,8 @@ const postgresTransaction = async (pgPool, work, options = {}) => {
119
99
  pgClient.release(broken || void 0);
120
100
  }
121
101
  };
122
- // src/postgres-unique-violation.ts
123
- const postgresUniqueViolation = (error, constraint) => {
102
+ // src/postgres-unique-violation-is.ts
103
+ const postgresUniqueViolationIs = (error, constraint) => {
124
104
  const seen = /* @__PURE__ */ new Set();
125
105
  let current = error;
126
106
  while (typeof current === "object" && current !== null && !seen.has(current)) {
@@ -130,4 +110,4 @@ const postgresUniqueViolation = (error, constraint) => {
130
110
  }
131
111
  return false;
132
112
  };
133
- export { postgresCursorDecode, postgresCursorEncode, postgresIdColumn, postgresMigrate, postgresPoolCreate, postgresPortsGuard, postgresTransaction, postgresUniqueViolation };
113
+ export { postgresIdColumnCreate, postgresMigrate, postgresPoolCreate, postgresTransactionRun, postgresUniqueViolationIs };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n8io/baseline-postgres",
3
- "version": "1.2.0",
3
+ "version": "2.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,9 +27,9 @@
27
27
  "registry": "https://registry.npmjs.org"
28
28
  },
29
29
  "dependencies": {
30
- "@n8io/baseline-id": "^1.1.0",
30
+ "@n8io/baseline-id": "^2.0.0",
31
31
  "@n8io/baseline-logger": "^2.1.0",
32
- "@n8io/baseline-result": "^1.1.0"
32
+ "@n8io/baseline-result": "^1.1.1"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@types/pg": "^8",
@@ -49,6 +49,7 @@
49
49
  "scripts": {
50
50
  "build": "tsdown",
51
51
  "check": "tsc --noEmit && pnpm pack --out dist/package.tgz && publint dist/package.tgz --strict && attw dist/package.tgz --profile esm-only",
52
- "test": "vitest run"
52
+ "test": "vitest run",
53
+ "test:unit": "vitest run --project unit --passWithNoTests"
53
54
  }
54
55
  }