@n8io/baseline-postgres 1.1.0 → 1.2.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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/package.json +7 -6
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nate Clark
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,54 @@
1
+ # @n8io/baseline-postgres
2
+
3
+ Postgres on [pg](https://node-postgres.com) and [Drizzle](https://orm.drizzle.team): one pool, Result-aware transactions, migrations under a lock, and columns that store prefixed ids as `uuid`.
4
+
5
+ ```sh
6
+ pnpm add @n8io/baseline-postgres
7
+ ```
8
+
9
+ ## Exports
10
+
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.
14
+ - `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.
18
+
19
+ ## Example
20
+
21
+ ```ts
22
+ import {
23
+ postgresIdColumn,
24
+ postgresPoolCreate,
25
+ postgresTransaction,
26
+ } from "@n8io/baseline-postgres";
27
+ import { err, ok } from "@n8io/baseline-result";
28
+ import { drizzle } from "drizzle-orm/node-postgres";
29
+ import { pgTable, text } from "drizzle-orm/pg-core";
30
+
31
+ export const fooTable = pgTable("foo", {
32
+ id: postgresIdColumn("foo")().primaryKey(),
33
+ name: text().notNull(),
34
+ });
35
+
36
+ const pgPool = postgresPoolCreate({ databaseUrl: ENV.DATABASE_URL });
37
+
38
+ const result = await postgresTransaction(pgPool, async (pgClient) => {
39
+ const rows = await drizzle({ client: pgClient })
40
+ .insert(fooTable)
41
+ .values(foo)
42
+ .returning();
43
+ return rows[0] ? ok(rows[0]) : err({ type: "FooNotCreated" as const });
44
+ });
45
+ ```
46
+
47
+ ## Needs
48
+
49
+ - Peers: `pg`, `drizzle-orm` (`1.0.0-rc.5`, pinned until 1.0 is out) and, for types, `@types/pg`.
50
+ - Env, by the template's convention: `DATABASE_URL` (`postgres://` or `postgresql://`). Transactions use `transaction_timeout`, so Postgres 17 or newer.
51
+
52
+ ## Tests
53
+
54
+ `pnpm test` needs Docker: it starts a Postgres container.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@n8io/baseline-postgres",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Postgres pool, Result-aware transactions, migrations and prefixed-id columns on pg and Drizzle.",
5
5
  "type": "module",
6
- "license": "UNLICENSED",
6
+ "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/n8io/baseline.git",
@@ -13,7 +13,8 @@
13
13
  "node": ">=26"
14
14
  },
15
15
  "files": [
16
- "dist"
16
+ "dist",
17
+ "README.md"
17
18
  ],
18
19
  "exports": {
19
20
  ".": {
@@ -26,9 +27,9 @@
26
27
  "registry": "https://registry.npmjs.org"
27
28
  },
28
29
  "dependencies": {
29
- "@n8io/baseline-id": "^1.0.0",
30
- "@n8io/baseline-logger": "^2.0.0",
31
- "@n8io/baseline-result": "^1.0.0"
30
+ "@n8io/baseline-id": "^1.1.0",
31
+ "@n8io/baseline-logger": "^2.1.0",
32
+ "@n8io/baseline-result": "^1.1.0"
32
33
  },
33
34
  "peerDependencies": {
34
35
  "@types/pg": "^8",