pg-codegen 5.15.0 → 5.16.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 +14 -0
- package/emit/templates/client.d.ts +23 -1
- package/emit/templates/client.js +6 -0
- package/emit/templates/client.ts +19 -4
- package/esm/emit/templates/client.js +6 -0
- package/esm/emit/templates/client.ts +19 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -75,6 +75,20 @@ await db.posts.findMany({ where: { or: [{ authorId }, { editorId: authorId }], n
|
|
|
75
75
|
|
|
76
76
|
Writes are encoded from the generated column metadata (json/jsonb columns stringified, `Date` → ISO), and every returned row comes back through the generated per-column decoders, so a value that does not match its column's type raises `CoerceError` instead of flowing on as a lie.
|
|
77
77
|
|
|
78
|
+
A write value may also be an expression, so a write that reads the column it sets stays one statement instead of a read followed by a write:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { add, col } from '@constructive-io/query-builder';
|
|
82
|
+
|
|
83
|
+
await db.resources.update({
|
|
84
|
+
where: { id },
|
|
85
|
+
data: { status: 'failed', lastError: message, errorCount: add(col('error_count'), 1) }
|
|
86
|
+
});
|
|
87
|
+
// UPDATE … SET status = $1, last_error = $2, error_count = error_count + 1 WHERE id = $3
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
An expression is deparsed in place — never bound as a value, never serialized as one.
|
|
91
|
+
|
|
78
92
|
### Selections type the result
|
|
79
93
|
|
|
80
94
|
`select` is the only projection, stated either way round, and the return type follows it with nothing to annotate:
|
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prisma-like table client over @constructive-io/query-builder.
|
|
3
|
+
*
|
|
4
|
+
* This is the RUNTIME code that gets copied to generated output. Generated
|
|
5
|
+
* per-schema `db` factories bind one TableClient per table, wiring the
|
|
6
|
+
* generated column metadata and field decoders in so call sites only see
|
|
7
|
+
* camelCase fields, `where` filters in the shared query-spec grammar, and
|
|
8
|
+
* decoded application values.
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This file is read at codegen time and written to output.
|
|
11
|
+
* Any changes here will affect all generated clients.
|
|
12
|
+
*/
|
|
13
|
+
import type { Expr } from '@constructive-io/query-builder';
|
|
1
14
|
/** Anything with pg's `query(text, values)` shape: Pool, PoolClient, Client. */
|
|
2
15
|
export interface Queryable {
|
|
3
16
|
query(text: string, values?: unknown[]): Promise<{
|
|
@@ -86,8 +99,17 @@ export type Where<App> = {
|
|
|
86
99
|
* Write data keyed by camelCase field names. Extra string keys are physical
|
|
87
100
|
* column names — runtime-configured scope key columns are stamped the same
|
|
88
101
|
* way any other value is.
|
|
102
|
+
*
|
|
103
|
+
* A value may also be a query-builder expression, so a write that reads the
|
|
104
|
+
* column it sets stays one statement:
|
|
105
|
+
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* data: { lastError: message, errorCount: add(col('error_count'), 1) }
|
|
108
|
+
* ```
|
|
89
109
|
*/
|
|
90
|
-
export type Data<App> =
|
|
110
|
+
export type Data<App> = {
|
|
111
|
+
[K in keyof App]?: App[K] | Expr;
|
|
112
|
+
} & {
|
|
91
113
|
[column: string]: unknown;
|
|
92
114
|
};
|
|
93
115
|
export type OrderDirection = 'ASC' | 'DESC';
|
package/emit/templates/client.js
CHANGED
|
@@ -224,6 +224,12 @@ class TableClient {
|
|
|
224
224
|
for (const [field, value] of Object.entries(data)) {
|
|
225
225
|
if (value === undefined)
|
|
226
226
|
continue;
|
|
227
|
+
// An expression is SQL the caller wrote: it is deparsed in place, never
|
|
228
|
+
// bound as a value, and never serialized as one either.
|
|
229
|
+
if (typeof value === 'function') {
|
|
230
|
+
encoded[this.column(field)] = value;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
227
233
|
if (value !== null && this.spec.jsonFields?.includes(field)) {
|
|
228
234
|
encoded[this.column(field)] = JSON.stringify(value);
|
|
229
235
|
continue;
|
package/emit/templates/client.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* NOTE: This file is read at codegen time and written to output.
|
|
11
11
|
* Any changes here will affect all generated clients.
|
|
12
12
|
*/
|
|
13
|
-
import type { Filter, SqlValue } from '@constructive-io/query-builder';
|
|
13
|
+
import type { Expr, Filter, Operand, SqlValue } from '@constructive-io/query-builder';
|
|
14
14
|
import { fn, lit, QueryBuilder } from '@constructive-io/query-builder';
|
|
15
15
|
|
|
16
16
|
/** Anything with pg's `query(text, values)` shape: Pool, PoolClient, Client. */
|
|
@@ -135,8 +135,17 @@ export type Where<App> = {
|
|
|
135
135
|
* Write data keyed by camelCase field names. Extra string keys are physical
|
|
136
136
|
* column names — runtime-configured scope key columns are stamped the same
|
|
137
137
|
* way any other value is.
|
|
138
|
+
*
|
|
139
|
+
* A value may also be a query-builder expression, so a write that reads the
|
|
140
|
+
* column it sets stays one statement:
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* data: { lastError: message, errorCount: add(col('error_count'), 1) }
|
|
144
|
+
* ```
|
|
138
145
|
*/
|
|
139
|
-
export type Data<App> =
|
|
146
|
+
export type Data<App> = { [K in keyof App]?: App[K] | Expr } & {
|
|
147
|
+
[column: string]: unknown;
|
|
148
|
+
};
|
|
140
149
|
|
|
141
150
|
export type OrderDirection = 'ASC' | 'DESC';
|
|
142
151
|
|
|
@@ -387,10 +396,16 @@ export class TableClient<App> {
|
|
|
387
396
|
}
|
|
388
397
|
}
|
|
389
398
|
|
|
390
|
-
private encodeData(data: Data<App>): Record<string,
|
|
391
|
-
const encoded: Record<string,
|
|
399
|
+
private encodeData(data: Data<App>): Record<string, Operand> {
|
|
400
|
+
const encoded: Record<string, Operand> = {};
|
|
392
401
|
for (const [field, value] of Object.entries(data)) {
|
|
393
402
|
if (value === undefined) continue;
|
|
403
|
+
// An expression is SQL the caller wrote: it is deparsed in place, never
|
|
404
|
+
// bound as a value, and never serialized as one either.
|
|
405
|
+
if (typeof value === 'function') {
|
|
406
|
+
encoded[this.column(field)] = value as Expr;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
394
409
|
if (value !== null && this.spec.jsonFields?.includes(field)) {
|
|
395
410
|
encoded[this.column(field)] = JSON.stringify(value);
|
|
396
411
|
continue;
|
|
@@ -220,6 +220,12 @@ export class TableClient {
|
|
|
220
220
|
for (const [field, value] of Object.entries(data)) {
|
|
221
221
|
if (value === undefined)
|
|
222
222
|
continue;
|
|
223
|
+
// An expression is SQL the caller wrote: it is deparsed in place, never
|
|
224
|
+
// bound as a value, and never serialized as one either.
|
|
225
|
+
if (typeof value === 'function') {
|
|
226
|
+
encoded[this.column(field)] = value;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
223
229
|
if (value !== null && this.spec.jsonFields?.includes(field)) {
|
|
224
230
|
encoded[this.column(field)] = JSON.stringify(value);
|
|
225
231
|
continue;
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* NOTE: This file is read at codegen time and written to output.
|
|
11
11
|
* Any changes here will affect all generated clients.
|
|
12
12
|
*/
|
|
13
|
-
import type { Filter, SqlValue } from '@constructive-io/query-builder';
|
|
13
|
+
import type { Expr, Filter, Operand, SqlValue } from '@constructive-io/query-builder';
|
|
14
14
|
import { fn, lit, QueryBuilder } from '@constructive-io/query-builder';
|
|
15
15
|
|
|
16
16
|
/** Anything with pg's `query(text, values)` shape: Pool, PoolClient, Client. */
|
|
@@ -135,8 +135,17 @@ export type Where<App> = {
|
|
|
135
135
|
* Write data keyed by camelCase field names. Extra string keys are physical
|
|
136
136
|
* column names — runtime-configured scope key columns are stamped the same
|
|
137
137
|
* way any other value is.
|
|
138
|
+
*
|
|
139
|
+
* A value may also be a query-builder expression, so a write that reads the
|
|
140
|
+
* column it sets stays one statement:
|
|
141
|
+
*
|
|
142
|
+
* ```ts
|
|
143
|
+
* data: { lastError: message, errorCount: add(col('error_count'), 1) }
|
|
144
|
+
* ```
|
|
138
145
|
*/
|
|
139
|
-
export type Data<App> =
|
|
146
|
+
export type Data<App> = { [K in keyof App]?: App[K] | Expr } & {
|
|
147
|
+
[column: string]: unknown;
|
|
148
|
+
};
|
|
140
149
|
|
|
141
150
|
export type OrderDirection = 'ASC' | 'DESC';
|
|
142
151
|
|
|
@@ -387,10 +396,16 @@ export class TableClient<App> {
|
|
|
387
396
|
}
|
|
388
397
|
}
|
|
389
398
|
|
|
390
|
-
private encodeData(data: Data<App>): Record<string,
|
|
391
|
-
const encoded: Record<string,
|
|
399
|
+
private encodeData(data: Data<App>): Record<string, Operand> {
|
|
400
|
+
const encoded: Record<string, Operand> = {};
|
|
392
401
|
for (const [field, value] of Object.entries(data)) {
|
|
393
402
|
if (value === undefined) continue;
|
|
403
|
+
// An expression is SQL the caller wrote: it is deparsed in place, never
|
|
404
|
+
// bound as a value, and never serialized as one either.
|
|
405
|
+
if (typeof value === 'function') {
|
|
406
|
+
encoded[this.column(field)] = value as Expr;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
394
409
|
if (value !== null && this.spec.jsonFields?.includes(field)) {
|
|
395
410
|
encoded[this.column(field)] = JSON.stringify(value);
|
|
396
411
|
continue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pg-codegen",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.16.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Type-safe TypeScript codegen from live PostgreSQL schemas: row types, coerce-backed decoders, serializers, table metadata, and enum unions",
|
|
6
6
|
"main": "index.js",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"makage": "^0.3.0",
|
|
59
59
|
"pgsql-test": "^5.11.0"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "51a32e8527c226772c2219b721c3cb6af8e66979"
|
|
62
62
|
}
|