@art-ws/db-context 2.0.41 → 2.0.42

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 (2) hide show
  1. package/README.md +113 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ @art-ws/db-context
2
+ ==================
3
+
4
+ Lightweight PostgreSQL database context utilities for TypeScript/Node ESM. Provides a small abstraction over `pg` with pooled connections, typed query helpers, and simple model helpers (select/insert/update/upsert/delete) without hiding SQL.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ ```bash
10
+ pnpm add @art-ws/db-context
11
+ ```
12
+
13
+ What you get
14
+ ------------
15
+
16
+ - `DbContextBase`: thin orchestrator that hands out models and queries using a shared client factory.
17
+ - `DbModel<T>`: convenience CRUD helpers for a table with paging/order-by and upsert support; still SQL-first.
18
+ - `PgPoolsBase`: cached `pg` connection pools with idle eviction.
19
+ - `PgDbClient` / `PgDbQuery`: promise-like query wrapper with `.single()`, `.first()`, `.last()`, `.exec()` helpers.
20
+ - Type parsers: installs numeric/date/array parsers for `pg` built-ins.
21
+
22
+ Quick start
23
+ -----------
24
+
25
+ ```ts
26
+ import { MemoryCacheBase } from "@art-ws/common"
27
+ import { DbContextBase, DbModel } from "@art-ws/db-context/db-context"
28
+ import { PgPoolsBase } from "@art-ws/db-context/pg"
29
+ import { PgDbClient } from "@art-ws/db-context/pg-db-client"
30
+ import { PgDbQuery } from "@art-ws/db-context/pg-db-client"
31
+
32
+ // 1) Configure pooled connections
33
+ const pools = new PgPoolsBase(new MemoryCacheBase({}), /* timeToIdle ms */ 60_000)
34
+ const dbClientFactory = async () =>
35
+ new PgDbClient(() => pools.acquire({
36
+ PGHOST: "localhost",
37
+ PGDATABASE: "app",
38
+ PGUSER: "postgres",
39
+ PGPASSWORD: "postgres",
40
+ PGPORT: 5432,
41
+ }))
42
+
43
+ // 2) Wire DbContext with a query factory
44
+ const dbContext = new DbContextBase<{ users: UsersModel }>(
45
+ dbClientFactory,
46
+ PgDbQuery.FACTORY
47
+ )
48
+
49
+ // 3) Define a model
50
+ type User = { id: number; email: string; name: string }
51
+ class UsersModel extends DbModel<User> {
52
+ constructor(ctx: DbContextBase<any>) {
53
+ super(ctx, {
54
+ table: "users",
55
+ columns: [
56
+ { name: "id", isPrimaryKey: true },
57
+ { name: "email" },
58
+ { name: "name" },
59
+ ],
60
+ })
61
+ }
62
+ }
63
+
64
+ // 4) Register the model factory
65
+ dbContext["users"] = new UsersModel(dbContext) as any
66
+
67
+ // 5) Run queries
68
+ const all = await dbContext.getDbModel<UsersModel>("users").findAll({ limit: 50 })
69
+ const inserted = await dbContext.getDbModel<UsersModel>("users").insert({ email: "a@b.co", name: "Ann" }).single()
70
+ ```
71
+
72
+ Core pieces
73
+ -----------
74
+
75
+ - `DbContextBase` keeps a shared `DbClient` (lazy) and a map of factories for models.
76
+ - `DbModel<T>` offers `findAll`, `findById`, `findBy`, `insert`, `update`, `upsert`, `del`, and `select` helpers. All return `DbQuery<T>` so you can chain `.single()`, `.first()`, `.exec()`, or await as a Promise for rows.
77
+ - `PgPoolsBase` caches `pg` pools by connection key; `timeToIdle` controls eviction and calls `pool.end()` when evicted.
78
+ - `PgDbClient` wraps a `PoolClient`, supports `dispose()` (releases client).
79
+ - `PgDbQuery` is the default query implementation for PostgreSQL.
80
+
81
+ Notes & tips
82
+ ------------
83
+
84
+ - The library is SQL-first; write SQL strings directly. Helpers only reduce boilerplate.
85
+ - Uses ESM (`"type": "module"`) and NodeNext-friendly exports. Import with explicit `.js` paths if your TS config requires it.
86
+ - Type parsers for numeric/date/array types are installed on import of `pg-db-client`.
87
+ - `DbQuery` implements `PromiseLike`, so `await query` gives rows; `.single()` throws if row count != 1.
88
+ - `timeToIdle` should roughly match your DB idle timeout to avoid leaked pools.
89
+
90
+ Minimal API reference
91
+ ---------------------
92
+
93
+ - `DbContextBase(dbClientFactory, dbQueryFactory)`
94
+ - `getDbModel<T>(name: string): T`
95
+ - `getDbQuery<T>({ name, sql, params }): DbQuery<T>`
96
+ - `dispose()` releases the underlying client
97
+ - `DbModel<T>(dbContext, meta)`
98
+ - `findAll(opts?)`, `findById(id)`, `findBy(values, opts?)`, `insert(values)`, `update(values, where?)`, `upsert(values, { unique })`, `del(values)`
99
+ - `PgPoolsBase(cache, timeToIdleMs)`
100
+ - `acquire(options: PgConnectionOptions): Promise<PoolClient>`
101
+ - `PgDbClient(pgClientFactory)`
102
+ - `getUnderlying()`, `dispose()`, `cancel()`
103
+ - `PgDbQuery.FACTORY(args)` builds a `DbQuery` for `pg`
104
+
105
+ Testing
106
+ -------
107
+
108
+ Tests are disabled in this package (`pnpm test` is a no-op). You can still write vitest suites in your consumer project.
109
+
110
+ License
111
+ -------
112
+
113
+ UNLICENSED (see package.json).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/db-context",
3
- "version": "2.0.41",
3
+ "version": "2.0.42",
4
4
  "description": "db-context",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -21,7 +21,7 @@
21
21
  "eslint": "^9.36.0",
22
22
  "typescript": "^5.9.2",
23
23
  "@art-ws/config-ts": "2.0.9",
24
- "@art-ws/config-eslint": "2.0.6"
24
+ "@art-ws/config-eslint": "2.0.7"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "tsc",