@rawsql-ts/sql-contract-zod 0.1.0 → 0.1.3

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
@@ -1,387 +1,405 @@
1
- # @rawsql-ts/sql-contract-zod
2
-
3
- ## Overview
4
-
5
- @rawsql-ts/sql-contract-zod maps SQL results to DTOs and validates them with Zod.
6
-
7
- Read (R) looks like this:
8
-
9
- ```ts
10
- // Create a reader (defaults to the appLike preset so snake_case -> camelCase works out of the box).
11
- const reader = createReader(executor)
12
-
13
- const customer = await reader.zod(CustomerSchema).one(
14
- 'SELECT customer_id, customer_name FROM customers WHERE customer_id = $1',
15
- [42],
16
- )
17
- ```
18
-
19
- Create (C) / Update (U) / Delete (D) look like this:
20
-
21
- ```ts
22
- // Create a writer for executing simple INSERT / UPDATE / DELETE statements.
23
- const writer = createWriter(executor)
24
-
25
- await writer.insert('customers', { name: 'alice', status: 'active' })
26
- await writer.update('customers', { status: 'active' }, { id: 42 })
27
- await writer.remove('customers', { id: 17 })
28
- ```
29
-
30
- ---
31
-
32
- ## Getting Started
33
-
34
- ### Installation
35
-
36
- ```sh
37
- pnpm add @rawsql-ts/sql-contract-zod zod
38
- ```
39
-
40
- @rawsql-ts/sql-contract-zod depends on `@rawsql-ts/sql-contract` for the mapper helper primitives and adds runtime validation through Zod.
41
-
42
- ### Minimal CRUD sample
43
-
44
- ```ts
45
- import { Pool } from 'pg'
46
- import { createWriter } from '@rawsql-ts/sql-contract/writer'
47
- import { createReader, type QueryParams } from '@rawsql-ts/sql-contract/mapper'
48
- import { zNumberFromString, zDateFromString } from '@rawsql-ts/sql-contract-zod'
49
- import { z } from 'zod'
50
-
51
- const CustomerSchema = z.object({
52
- // appLike-style conventions map snake_case columns to camelCase keys.
53
- customerId: z.number(),
54
- customerName: z.string(),
55
- customerStatus: z.string(),
56
- balance: zNumberFromString,
57
- joinedAt: zDateFromString,
58
- })
59
-
60
- async function main() {
61
- // Define these once for the whole application.
62
- const pool = new Pool({ connectionString: process.env.DATABASE_URL })
63
-
64
- const executor = async (sql: string, params: QueryParams) => {
65
- const result = await pool.query(sql, params as unknown[])
66
- return result.rows
67
- }
68
-
69
- // Create a reader with common conventions (customizable via presets).
70
- const reader = createReader(executor)
71
-
72
- // Create a writer for executing simple INSERT / UPDATE / DELETE statements.
73
- const writer = createWriter(executor)
74
-
75
- const sql = `
76
- SELECT
77
- customer_id,
78
- customer_name,
79
- customer_status,
80
- balance,
81
- joined_date
82
- FROM customers
83
- WHERE customer_id = $1
84
- `
85
-
86
- const customer = await reader.zod(CustomerSchema).one(sql, [42])
87
-
88
- await writer.insert('customers', { name: 'alice', status: 'pending' })
89
- await writer.update('customers', { status: 'active' }, { id: 42 })
90
- await writer.remove('customers', { id: 17 })
91
-
92
- await pool.end()
93
- void customer
94
- }
95
-
96
- void main()
97
- ```
98
-
99
- This snippet shows a typical setup: define an executor, create a reader and writer, validate DTOs with Zod, and execute simple INSERT/UPDATE/DELETE statements.
100
-
101
- ---
102
-
103
- ## Features
104
-
105
- * Runtime dependency on Zod for validation helpers (no other runtime packages are required)
106
- * Zero DBMS dependency
107
- (tested with PostgreSQL, MySQL, SQL Server, and SQLite)
108
- * Zero database client dependency
109
- (works with any client that executes SQL and returns rows)
110
- * Zero framework and ORM dependency
111
- (fits into any application architecture that uses raw SQL)
112
- * No schema models or metadata required
113
- (tables, columns, and relationships are defined only in SQL)
114
- * Result mapping helpers that operate on any SQL returning rows
115
- (including SELECT queries and CUD statements with RETURNING or aggregate results)
116
- * Simple builders for common INSERT, UPDATE, and DELETE cases, without query inference
117
- * Zod-aware reader (`reader.zod`) that validates mapped DTOs with preset-based conventions or an explicit RowMapping
118
- * Optional coercion helpers (`zNumberFromString`, `zBigIntFromString`, `zDateFromString`)
119
- * Readers that accept the same `RowMapping` instances your mapper already uses
120
- * `createReader(executor)` applies `mapperPresets.appLike()` by default, so snake_case columns work without extra setup
121
- * Params normalization: omitting params is treated the same as passing `[]`
122
- * Throws the original `ZodError`, keeping logging and monitoring layers intact
123
-
124
- ---
125
-
126
- ## Philosophy
127
-
128
- ### SQL as Specification
129
-
130
- How data is retrieved, which fields are selected, how values are transformed, and which conditions are applied are all domain requirements.
131
-
132
- SQL is the most precise and unambiguous language for expressing these requirements.
133
- Natural language is not.
134
-
135
- ### SQL as Fast Feedback
136
-
137
- Effective software development depends on rapid iteration across design, implementation, and verification.
138
-
139
- Because SQL is executable and directly verifiable, it provides fast and reliable feedback.
140
- DSLs do not.
141
-
142
- ---
143
-
144
- ## Direction
145
-
146
- ### Mechanical Work in Read Queries
147
-
148
- When working with raw SQL, certain mechanical tasks are unavoidable.
149
- Mapping returned rows into application-level DTOs is repetitive, straightforward, and not part of the domain logic.
150
-
151
- This library focuses on reducing that burden by providing explicit result mapping,
152
- followed by optional DTO validation using Zod after mapping completes.
153
-
154
- ### Mechanical Work in Write Queries
155
-
156
- Write operations such as INSERT, UPDATE, and DELETE usually carry less domain significance than SELECT queries.
157
- They tend to be short, predictable, and mechanically structured.
158
-
159
- This library provides minimal builder helpers for common write cases only,
160
- and intentionally goes no further than that.
161
-
162
- ---
163
-
164
- ## Executor: DBMS / Driver Integration
165
-
166
- `sql-contract-zod` is designed as a reusable, DBMS-agnostic library.
167
- To integrate it with a specific database or driver, you define a small executor function.
168
-
169
- An executor receives a SQL string and parameters, executes them using your DB driver,
170
- and returns the resulting rows as `Row[]`.
171
-
172
- ```ts
173
- const executor = async (sql: string, params: QueryParams) => {
174
- const result = await pool.query(sql, params as unknown[])
175
- return result.rows
176
- }
177
- ```
178
-
179
- This function is the single integration point between `sql-contract-zod` and the DBMS.
180
- All database- and driver-specific concerns-connection pooling, transactions,
181
- retries, and error handling-belong entirely inside the executor.
182
-
183
- ### Parameters
184
-
185
- The `params` argument uses the exported `QueryParams` type.
186
- It supports both positional arrays and named records.
187
-
188
- Reader methods always supply a params array (defaulting to `[]`),
189
- so executors may safely treat `params` as `unknown[]` without defensive checks.
190
-
191
- ---
192
-
193
- ## Mapper: Query Result Mapping (R)
194
-
195
- The mapper is responsible for projecting query results (`Row[]`) into DTOs.
196
-
197
- In a typical application, a mapper is created once and reused across queries.
198
- It defines application-wide projection behavior,
199
- while individual queries decide the DTO shape.
200
-
201
- The mapper operates purely on returned rows and never inspects SQL,
202
- parameters, or execution behavior.
203
- To keep mapping predictable, it does not guess column semantics or relationships.
204
- It focuses on structural projection and column name normalization only.
205
- Value coercion is intentionally handled by Zod.
206
-
207
- ```ts
208
- import { createReader, mapperPresets } from '@rawsql-ts/sql-contract/mapper'
209
-
210
- const reader = createReader(executor)
211
- const readerWithOverrides = createReader(executor, mapperPresets.safe())
212
- ```
213
-
214
- ---
215
-
216
- ### Query result cardinality: `one` and `list`
217
-
218
- Reader methods distinguish between queries that return a single row
219
- and those that return multiple rows.
220
-
221
- ```ts
222
- const row = await reader.zod(Schema).one(sql, params)
223
- // row is DTO
224
-
225
- const rows = await reader.zod(Schema).list(sql, params)
226
- // rows is DTO[]
227
- ```
228
-
229
- `one(...)` enforces an exact cardinality contract:
230
-
231
- - **0 rows** -> throws an error
232
- - **1 row** -> returns a DTO
233
- - **n rows (n >= 2)** -> throws an error
234
-
235
- `list(...)` performs no cardinality checks:
236
-
237
- - **0 rows** -> returns an empty array `[]`
238
- - **1 row** -> returns an array with one DTO
239
- - **n rows (n >= 2)** -> returns an array of `n` DTOs
240
-
241
- When using `reader.zod(...)`, Zod validation is performed first,
242
- and the cardinality check is applied afterward.
243
-
244
- ---
245
-
246
- ### Zod helpers overview
247
-
248
- | Helper | Description |
249
- | ------ | ----------- |
250
- | `reader.zod` | Validates mapped DTOs with Zod before returning them |
251
- | `parseRows` / `parseRow` | Validate DTOs obtained from other sources |
252
- | `zNumberFromString` | Accepts numbers or numeric strings |
253
- | `zBigIntFromString` | Accepts bigints or strings |
254
- | `zDateFromString` | Accepts `Date` objects or ISO strings |
255
-
256
- All helpers propagate the original `ZodError`.
257
-
258
- ---
259
-
260
- ### Duck-typed mapping (no model definitions)
261
-
262
- For lightweight or localized use cases,
263
- the reader supports duck-typed projections without defining schemas.
264
-
265
- ```ts
266
- const rows = await reader.list<{ customerId: number }>(
267
- 'select customer_id from customers limit 1',
268
- )
269
- ```
270
-
271
- You can also omit the DTO type:
272
-
273
- ```ts
274
- const rows = await reader.list(
275
- 'select customer_id from customers limit 1',
276
- )
277
- ```
278
-
279
- ---
280
-
281
- ### Single DTO with Zod (recommended)
282
-
283
- ```ts
284
- import { z } from 'zod'
285
-
286
- const CustomerSchema = z.object({
287
- customerId: z.number(),
288
- customerName: z.string(),
289
- })
290
-
291
- const sql = `
292
- select
293
- customer_id,
294
- customer_name
295
- from customers
296
- where customer_id = $1
297
- `
298
-
299
- const row = await reader.zod(CustomerSchema).one(sql, [42])
300
- ```
301
-
302
- ---
303
-
304
- ### Mapping to multiple models (joined rows)
305
-
306
- You can map joined result sets into multiple related DTOs.
307
- Relations are explicitly defined and never inferred.
308
-
309
- ```ts
310
- const customerMapping = rowMapping({
311
- name: 'customer',
312
- key: 'customerId',
313
- })
314
-
315
- const orderMapping = rowMapping({
316
- name: 'order',
317
- key: 'orderId',
318
- }).belongsTo('customer', customerMapping, 'customerId')
319
- ```
320
-
321
- ```ts
322
- const rows = await reader.zod(OrderSchema, orderMapping).list(sql, [42])
323
- ```
324
-
325
- In multi-model mappings, related DTOs are **interned by their key**.
326
- If multiple rows reference the same key, the same related instance is reused.
327
-
328
- ```ts
329
- rows[0].customer === rows[1].customer // true (same customerId)
330
- ```
331
-
332
- Mutating related DTOs in place will affect all rows that reference them.
333
-
334
- ---
335
-
336
- ## Writer: emitting simple C / U / D statements
337
-
338
- The writer helpers provide a small, opinionated DSL for common INSERT,
339
- UPDATE, and DELETE statements.
340
-
341
- At its core, the writer constructs `{ sql, params }`.
342
- When bound to an executor, it also executes those statements.
343
-
344
- ### Executing statements
345
-
346
- ```ts
347
- await writer.insert('projects', { name: 'Apollo', owner_id: 7 })
348
- await writer.update('projects', { name: 'Apollo' }, { project_id: 1 })
349
- await writer.remove('projects', { project_id: 1 })
350
- ```
351
-
352
- ### Building statements without execution
353
-
354
- ```ts
355
- const stmt = writer.build.insert('projects', { name: 'Apollo', owner_id: 7 })
356
- ```
357
-
358
- ---
359
-
360
- ## CUD with RETURNING
361
-
362
- When write statements return rows,
363
- consume the result with a reader to map and validate DTOs.
364
-
365
- ```ts
366
- const stmt = writer.build.insert(
367
- 'users',
368
- { user_name: 'alice' },
369
- { returning: ['user_id', 'user_name'] },
370
- )
371
-
372
- const row = await reader.zod(CreatedUserSchema).one(stmt.sql, stmt.params)
373
- ```
374
-
375
- ---
376
-
377
- ## DBMS and driver differences
378
-
379
- All DBMS- and driver-specific concerns live in the executor.
380
- Both writer and mapper remain independent of these differences.
381
-
382
- ---
383
-
384
- ## Influences / Related Ideas
385
-
386
- sql-contract-zod is inspired by minimal mapping libraries such as Dapper,
387
- favoring explicit SQL and thin, predictable mapping layers.
1
+ # @rawsql-ts/sql-contract-zod
2
+
3
+ ## Overview
4
+
5
+ @rawsql-ts/sql-contract-zod maps SQL results to DTOs and validates them with Zod.
6
+
7
+ Read (R) looks like this:
8
+
9
+ ```ts
10
+ // Create a reader (defaults to the appLike preset so snake_case -> camelCase works out of the box).
11
+ const reader = createReader(executor)
12
+
13
+ const customer = await reader.zod(CustomerSchema).one(
14
+ 'SELECT customer_id, customer_name FROM customers WHERE customer_id = $1',
15
+ [42],
16
+ )
17
+ ```
18
+
19
+ Create (C) / Update (U) / Delete (D) look like this:
20
+
21
+ ```ts
22
+ // Create a writer for executing simple INSERT / UPDATE / DELETE statements.
23
+ const writer = createWriter(executor)
24
+
25
+ await writer.insert('customers', { name: 'alice', status: 'active' })
26
+ await writer.update('customers', { status: 'active' }, { id: 42 })
27
+ await writer.remove('customers', { id: 17 })
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Getting Started
33
+
34
+ ### Installation
35
+
36
+ ```sh
37
+ pnpm add @rawsql-ts/sql-contract-zod zod
38
+ ```
39
+
40
+ @rawsql-ts/sql-contract-zod depends on `@rawsql-ts/sql-contract` for the mapper helper primitives and adds runtime validation through Zod.
41
+
42
+ The built-in Zod helpers rely on the core package's `decimalStringToNumberUnsafe` and `bigintStringToBigInt` utilities so the same string-to-number and string-to-bigint coercion policies are shared.
43
+
44
+ ### Minimal CRUD sample
45
+
46
+ ```ts
47
+ import { Pool } from 'pg'
48
+ import { createWriter } from '@rawsql-ts/sql-contract/writer'
49
+ import { createReader, type QueryParams } from '@rawsql-ts/sql-contract/mapper'
50
+ import { zNumberFromString, zDateFromString } from '@rawsql-ts/sql-contract-zod'
51
+ import { z } from 'zod'
52
+
53
+ const CustomerSchema = z.object({
54
+ // appLike-style conventions map snake_case columns to camelCase keys.
55
+ customerId: z.number(),
56
+ customerName: z.string(),
57
+ customerStatus: z.string(),
58
+ balance: zNumberFromString,
59
+ joinedAt: zDateFromString,
60
+ })
61
+
62
+ async function main() {
63
+ // Define these once for the whole application.
64
+ const pool = new Pool({ connectionString: process.env.DATABASE_URL })
65
+
66
+ const executor = async (sql: string, params: QueryParams) => {
67
+ const result = await pool.query(sql, params as unknown[])
68
+ return result.rows
69
+ }
70
+
71
+ // Create a reader with common conventions (customizable via presets).
72
+ const reader = createReader(executor)
73
+
74
+ // Create a writer for executing simple INSERT / UPDATE / DELETE statements.
75
+ const writer = createWriter(executor)
76
+
77
+ const sql = `
78
+ SELECT
79
+ customer_id,
80
+ customer_name,
81
+ customer_status,
82
+ balance,
83
+ joined_date
84
+ FROM customers
85
+ WHERE customer_id = $1
86
+ `
87
+
88
+ const customer = await reader.zod(CustomerSchema).one(sql, [42])
89
+
90
+ await writer.insert('customers', { name: 'alice', status: 'pending' })
91
+ await writer.update('customers', { status: 'active' }, { id: 42 })
92
+ await writer.remove('customers', { id: 17 })
93
+
94
+ await pool.end()
95
+ void customer
96
+ }
97
+
98
+ void main()
99
+ ```
100
+
101
+ This snippet shows a typical setup: define an executor, create a reader and writer, validate DTOs with Zod, and execute simple INSERT/UPDATE/DELETE statements.
102
+
103
+ ---
104
+
105
+ ## Features
106
+
107
+ * Runtime dependency on Zod for validation helpers (no other runtime packages are required)
108
+ * Zero DBMS dependency
109
+ (tested with PostgreSQL, MySQL, SQL Server, and SQLite)
110
+ * Zero database client dependency
111
+ (works with any client that executes SQL and returns rows)
112
+ * Zero framework and ORM dependency
113
+ (fits into any application architecture that uses raw SQL)
114
+ * No schema models or metadata required
115
+ (tables, columns, and relationships are defined only in SQL)
116
+ * Result mapping helpers that operate on any SQL returning rows
117
+ (including SELECT queries and CUD statements with RETURNING or aggregate results)
118
+ * Simple builders for common INSERT, UPDATE, and DELETE cases, without query inference
119
+ * Zod-aware reader (`reader.zod`) that validates mapped DTOs with preset-based conventions or an explicit RowMapping
120
+ * Scalar helper (`reader.scalar`) that validates single-column results before returning them
121
+ * Optional coercion helpers (`zNumberFromString`, `zBigIntFromString`, `zDateFromString`)
122
+ * Readers that accept the same `RowMapping` instances your mapper already uses
123
+ * `createReader(executor)` applies `mapperPresets.appLike()` by default, so snake_case columns work without extra setup
124
+ * Params normalization: omitting params is treated the same as passing `[]`
125
+ * Throws the original `ZodError`, keeping logging and monitoring layers intact
126
+
127
+ ---
128
+
129
+ ## Philosophy
130
+
131
+ ### SQL as Specification
132
+
133
+ How data is retrieved, which fields are selected, how values are transformed, and which conditions are applied are all domain requirements.
134
+
135
+ SQL is the most precise and unambiguous language for expressing these requirements.
136
+ Natural language is not.
137
+
138
+ ### SQL as Fast Feedback
139
+
140
+ Effective software development depends on rapid iteration across design, implementation, and verification.
141
+
142
+ Because SQL is executable and directly verifiable, it provides fast and reliable feedback.
143
+ DSLs do not.
144
+
145
+ ---
146
+
147
+ ## Direction
148
+
149
+ ### Mechanical Work in Read Queries
150
+
151
+ When working with raw SQL, certain mechanical tasks are unavoidable.
152
+ Mapping returned rows into application-level DTOs is repetitive, straightforward, and not part of the domain logic.
153
+
154
+ This library focuses on reducing that burden by providing explicit result mapping,
155
+ followed by optional DTO validation using Zod after mapping completes.
156
+
157
+ ### Mechanical Work in Write Queries
158
+
159
+ Write operations such as INSERT, UPDATE, and DELETE usually carry less domain significance than SELECT queries.
160
+ They tend to be short, predictable, and mechanically structured.
161
+
162
+ This library provides minimal builder helpers for common write cases only,
163
+ and intentionally goes no further than that.
164
+
165
+ ---
166
+
167
+ ## Executor: DBMS / Driver Integration
168
+
169
+ `sql-contract-zod` is designed as a reusable, DBMS-agnostic library.
170
+ To integrate it with a specific database or driver, you define a small executor function.
171
+
172
+ An executor receives a SQL string and parameters, executes them using your DB driver,
173
+ and returns the resulting rows as `Row[]`.
174
+
175
+ ```ts
176
+ const executor = async (sql: string, params: QueryParams) => {
177
+ const result = await pool.query(sql, params as unknown[])
178
+ return result.rows
179
+ }
180
+ ```
181
+
182
+ This function is the single integration point between `sql-contract-zod` and the DBMS.
183
+ All database- and driver-specific concerns-connection pooling, transactions,
184
+ retries, and error handling-belong entirely inside the executor.
185
+
186
+ ### Parameters
187
+
188
+ The `params` argument uses the exported `QueryParams` type.
189
+ It supports both positional arrays and named records.
190
+
191
+ Reader methods always supply a params array (defaulting to `[]`),
192
+ so executors may safely treat `params` as `unknown[]` without defensive checks.
193
+
194
+ ---
195
+
196
+ ## Mapper: Query Result Mapping (R)
197
+
198
+ The mapper is responsible for projecting query results (`Row[]`) into DTOs.
199
+
200
+ In a typical application, a mapper is created once and reused across queries.
201
+ It defines application-wide projection behavior,
202
+ while individual queries decide the DTO shape.
203
+
204
+ The mapper operates purely on returned rows and never inspects SQL,
205
+ parameters, or execution behavior.
206
+ To keep mapping predictable, it does not guess column semantics or relationships.
207
+ It focuses on structural projection and column name normalization only.
208
+ Value coercion is intentionally handled by Zod.
209
+
210
+ ```ts
211
+ import { createReader, mapperPresets } from '@rawsql-ts/sql-contract/mapper'
212
+
213
+ const reader = createReader(executor)
214
+ const readerWithOverrides = createReader(executor, mapperPresets.safe())
215
+ ```
216
+
217
+ ---
218
+
219
+ ### Query result cardinality: `one` and `list`
220
+
221
+ Reader methods distinguish between queries that return a single row
222
+ and those that return multiple rows.
223
+
224
+ ```ts
225
+ const row = await reader.zod(Schema).one(sql, params)
226
+ // row is DTO
227
+
228
+ const rows = await reader.zod(Schema).list(sql, params)
229
+ // rows is DTO[]
230
+ ```
231
+
232
+ `one(...)` enforces an exact cardinality contract:
233
+
234
+ - **0 rows** -> throws an error
235
+ - **1 row** -> returns a DTO
236
+ - **n rows (n >= 2)** -> throws an error
237
+
238
+ `list(...)` performs no cardinality checks:
239
+
240
+ - **0 rows** -> returns an empty array `[]`
241
+ - **1 row** -> returns an array with one DTO
242
+ - **n rows (n >= 2)** -> returns an array of `n` DTOs
243
+
244
+ When using `reader.zod(...)`, Zod validation is performed first,
245
+ and the cardinality check is applied afterward.
246
+
247
+ ---
248
+
249
+ ### Zod helpers overview
250
+
251
+ | Helper | Description |
252
+ | ------ | ----------- |
253
+ | `reader.zod` | Validates mapped DTOs with Zod before returning them |
254
+ | `reader.scalar` | Validates a single-column result with Zod before returning the scalar value |
255
+ | `parseRows` / `parseRow` | Validate DTOs obtained from other sources |
256
+ | `zNumberFromString` | Accepts numbers or numeric strings |
257
+ | `zBigIntFromString` | Accepts bigints or strings |
258
+ | `zDateFromString` | Accepts `Date` objects or ISO strings |
259
+
260
+ All helpers propagate the original `ZodError`.
261
+
262
+ ### Scalar queries
263
+
264
+ Use `reader.scalar` when a query returns exactly one row with a single column.
265
+ The helper throws if the result set contains zero or more than one row or if multiple columns are returned.
266
+ Zod validation applies directly to the scalar value, allowing reuse of helpers such as `zNumberFromString`.
267
+
268
+ ```ts
269
+ const activeCount = await reader.scalar(
270
+ zNumberFromString,
271
+ 'select count(*) from customers where status = $1',
272
+ ['active'],
273
+ )
274
+ ```
275
+
276
+ ---
277
+
278
+ ### Duck-typed mapping (no model definitions)
279
+
280
+ For lightweight or localized use cases,
281
+ the reader supports duck-typed projections without defining schemas.
282
+
283
+ ```ts
284
+ const rows = await reader.list<{ customerId: number }>(
285
+ 'select customer_id from customers limit 1',
286
+ )
287
+ ```
288
+
289
+ You can also omit the DTO type:
290
+
291
+ ```ts
292
+ const rows = await reader.list(
293
+ 'select customer_id from customers limit 1',
294
+ )
295
+ ```
296
+
297
+ ---
298
+
299
+ ### Single DTO with Zod (recommended)
300
+
301
+ ```ts
302
+ import { z } from 'zod'
303
+
304
+ const CustomerSchema = z.object({
305
+ customerId: z.number(),
306
+ customerName: z.string(),
307
+ })
308
+
309
+ const sql = `
310
+ select
311
+ customer_id,
312
+ customer_name
313
+ from customers
314
+ where customer_id = $1
315
+ `
316
+
317
+ const row = await reader.zod(CustomerSchema).one(sql, [42])
318
+ ```
319
+
320
+ ---
321
+
322
+ ### Mapping to multiple models (joined rows)
323
+
324
+ You can map joined result sets into multiple related DTOs.
325
+ Relations are explicitly defined and never inferred.
326
+
327
+ ```ts
328
+ const customerMapping = rowMapping({
329
+ name: 'customer',
330
+ key: 'customerId',
331
+ })
332
+
333
+ const orderMapping = rowMapping({
334
+ name: 'order',
335
+ key: 'orderId',
336
+ }).belongsTo('customer', customerMapping, 'customerId')
337
+ ```
338
+
339
+ ```ts
340
+ const rows = await reader.zod(OrderSchema, orderMapping).list(sql, [42])
341
+ ```
342
+
343
+ In multi-model mappings, related DTOs are **interned by their key**.
344
+ If multiple rows reference the same key, the same related instance is reused.
345
+
346
+ ```ts
347
+ rows[0].customer === rows[1].customer // true (same customerId)
348
+ ```
349
+
350
+ Mutating related DTOs in place will affect all rows that reference them.
351
+
352
+ ---
353
+
354
+ ## Writer: emitting simple C / U / D statements
355
+
356
+ The writer helpers provide a small, opinionated DSL for common INSERT,
357
+ UPDATE, and DELETE statements.
358
+
359
+ At its core, the writer constructs `{ sql, params }`.
360
+ When bound to an executor, it also executes those statements.
361
+
362
+ ### Executing statements
363
+
364
+ ```ts
365
+ await writer.insert('projects', { name: 'Apollo', owner_id: 7 })
366
+ await writer.update('projects', { name: 'Apollo' }, { project_id: 1 })
367
+ await writer.remove('projects', { project_id: 1 })
368
+ ```
369
+
370
+ ### Building statements without execution
371
+
372
+ ```ts
373
+ const stmt = writer.build.insert('projects', { name: 'Apollo', owner_id: 7 })
374
+ ```
375
+
376
+ ---
377
+
378
+ ## CUD with RETURNING
379
+
380
+ When write statements return rows,
381
+ consume the result with a reader to map and validate DTOs.
382
+
383
+ ```ts
384
+ const stmt = writer.build.insert(
385
+ 'users',
386
+ { user_name: 'alice' },
387
+ { returning: ['user_id', 'user_name'] },
388
+ )
389
+
390
+ const row = await reader.zod(CreatedUserSchema).one(stmt.sql, stmt.params)
391
+ ```
392
+
393
+ ---
394
+
395
+ ## DBMS and driver differences
396
+
397
+ All DBMS- and driver-specific concerns live in the executor.
398
+ Both writer and mapper remain independent of these differences.
399
+
400
+ ---
401
+
402
+ ## Influences / Related Ideas
403
+
404
+ sql-contract-zod is inspired by minimal mapping libraries such as Dapper,
405
+ favoring explicit SQL and thin, predictable mapping layers.