pg-codegen 5.15.0 → 5.17.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 +31 -1
- package/emit/barrel.js +4 -0
- package/emit/record.js +9 -2
- 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/barrel.js +4 -0
- package/esm/emit/record.js +9 -2
- package/esm/emit/templates/client.js +6 -0
- package/esm/emit/templates/client.ts +19 -4
- package/package.json +7 -7
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:
|
|
@@ -92,6 +106,20 @@ await db.emailIdentities.findFirst({ select: { databaseId: false } });
|
|
|
92
106
|
|
|
93
107
|
Excluding is what a caller reaches for when a field must not be carried — a secret's id, row bookkeeping — or when this binding's table genuinely lacks it: an excluded field is absent from the type, so reading it does not compile. Naming any field `true` is a projection and wins outright; the excluded ones are simply not in it.
|
|
94
108
|
|
|
109
|
+
### Naming a filter or a write input
|
|
110
|
+
|
|
111
|
+
The client's own vocabulary is exported from the package root, keyed by a generated record, so a function that takes a filter or the values of a write says so without restating the fields:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import type { Data, Where } from './generated';
|
|
115
|
+
import type { EmailIdentities } from './generated/routing_public';
|
|
116
|
+
|
|
117
|
+
const active = (extra: Where<EmailIdentities>) => ({ isActive: true, ...extra });
|
|
118
|
+
type IdentityInput = Data<EmailIdentities>; // every field optional, camelCase
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`Where`, `Data`, `SelectShape`, `OrderBy` and `Queryable` all come from there.
|
|
122
|
+
|
|
95
123
|
### Transactions
|
|
96
124
|
|
|
97
125
|
`$with` rebinds every table client to another connection, so a transaction is the same code against a different handle:
|
|
@@ -138,7 +166,9 @@ For every table, view, materialized view and partitioned table in the schemas yo
|
|
|
138
166
|
|
|
139
167
|
Plus, per schema, a `db.ts` (the client above), an `enums.ts` — a literal union, a `readonly` value list and `as*`/`require*` checkers for every PostgreSQL enum a column references — and barrels; and one shared `client.ts` runtime at the root.
|
|
140
168
|
|
|
141
|
-
Every decoder is backed by [`@constructive-io/coerce`](../../packages/coerce): a NOT NULL column throws `CoerceError` naming the offending column, a nullable column answers `null`, and the text
|
|
169
|
+
Every decoder is backed by [`@constructive-io/coerce`](../../packages/coerce): a NOT NULL column throws `CoerceError` naming the offending column, a nullable column answers `null`, and the text form `pg` returns for `numeric` is parsed to a number. Nothing is silently coerced across types, so a malformed row cannot become a default value.
|
|
170
|
+
|
|
171
|
+
An `int8` column is a **`string`** — the canonical digits of the value, which is how `pg` returns the type, how `graphql/codegen` spells its `BigInt` scalar, and the only representation that keeps an id past 2^53 (a `number` would round `9007199254740993` to its neighbour). Its decoder accepts any of the three shapes a 64-bit value arrives in — `bigint`, integer `number`, digit string — and answers the canonical digits, so `'007'` and `7` both decode to `'7'`; a write states the string and PostgreSQL reads it as `int8`.
|
|
142
172
|
|
|
143
173
|
## 🔧 Below the client
|
|
144
174
|
|
package/emit/barrel.js
CHANGED
|
@@ -25,6 +25,10 @@ const emitSchemaIndex = (schema) => {
|
|
|
25
25
|
exports.emitSchemaIndex = emitSchemaIndex;
|
|
26
26
|
const emitRootIndex = (schemas) => {
|
|
27
27
|
const statements = schemas.map(schema => babel_1.t.exportNamedDeclaration(null, [babel_1.t.exportNamespaceSpecifier(babel_1.t.identifier((0, inflekt_1.toCamelCase)(schema.name)))], babel_1.t.stringLiteral(`./${schema.name}`)));
|
|
28
|
+
// The client's own vocabulary — `Where`, `Data`, `SelectShape`, `OrderBy` —
|
|
29
|
+
// so a consumer that names a filter or a write input states it over a
|
|
30
|
+
// generated record instead of hand-writing the field list again.
|
|
31
|
+
statements.push(babel_1.t.exportAllDeclaration(babel_1.t.stringLiteral('./client')));
|
|
28
32
|
return (0, babel_1.generateCode)(statements);
|
|
29
33
|
};
|
|
30
34
|
exports.emitRootIndex = emitRootIndex;
|
package/emit/record.js
CHANGED
|
@@ -12,7 +12,11 @@ const enums_1 = require("./enums");
|
|
|
12
12
|
const COERCERS = {
|
|
13
13
|
boolean: { require: 'requireBoolean', as: 'asBoolean', expectedArray: 'an array of booleans' },
|
|
14
14
|
integer: { require: 'requireInteger', as: 'asInteger', expectedArray: 'an array of integers' },
|
|
15
|
-
bigint: {
|
|
15
|
+
bigint: {
|
|
16
|
+
require: 'requireBigIntString',
|
|
17
|
+
as: 'asBigIntString',
|
|
18
|
+
expectedArray: 'an array of whole numbers'
|
|
19
|
+
},
|
|
16
20
|
number: { require: 'requireNumeric', as: 'asNumeric', expectedArray: 'an array of numbers' },
|
|
17
21
|
string: { require: 'requireString', as: 'asString', expectedArray: 'an array of non-empty strings' },
|
|
18
22
|
uuid: { require: 'requireUuid', as: 'asUuid', expectedArray: 'an array of UUIDs' },
|
|
@@ -24,9 +28,12 @@ const scalarTsType = (column) => {
|
|
|
24
28
|
case 'boolean':
|
|
25
29
|
return babel_1.t.tsBooleanKeyword();
|
|
26
30
|
case 'integer':
|
|
27
|
-
case 'bigint':
|
|
28
31
|
case 'number':
|
|
29
32
|
return babel_1.t.tsNumberKeyword();
|
|
33
|
+
// int8 carries values a JS number cannot hold, so it is a digit string —
|
|
34
|
+
// the same spelling graphql/codegen gives the `BigInt` scalar, and the one
|
|
35
|
+
// node-postgres already returns for the type.
|
|
36
|
+
case 'bigint':
|
|
30
37
|
case 'string':
|
|
31
38
|
case 'uuid':
|
|
32
39
|
case 'timestamp':
|
|
@@ -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;
|
package/esm/emit/barrel.js
CHANGED
|
@@ -20,5 +20,9 @@ export const emitSchemaIndex = (schema) => {
|
|
|
20
20
|
};
|
|
21
21
|
export const emitRootIndex = (schemas) => {
|
|
22
22
|
const statements = schemas.map(schema => t.exportNamedDeclaration(null, [t.exportNamespaceSpecifier(t.identifier(toCamelCase(schema.name)))], t.stringLiteral(`./${schema.name}`)));
|
|
23
|
+
// The client's own vocabulary — `Where`, `Data`, `SelectShape`, `OrderBy` —
|
|
24
|
+
// so a consumer that names a filter or a write input states it over a
|
|
25
|
+
// generated record instead of hand-writing the field list again.
|
|
26
|
+
statements.push(t.exportAllDeclaration(t.stringLiteral('./client')));
|
|
23
27
|
return generateCode(statements);
|
|
24
28
|
};
|
package/esm/emit/record.js
CHANGED
|
@@ -9,7 +9,11 @@ import { enumAsName, enumRequireName, enumTypeName } from './enums';
|
|
|
9
9
|
const COERCERS = {
|
|
10
10
|
boolean: { require: 'requireBoolean', as: 'asBoolean', expectedArray: 'an array of booleans' },
|
|
11
11
|
integer: { require: 'requireInteger', as: 'asInteger', expectedArray: 'an array of integers' },
|
|
12
|
-
bigint: {
|
|
12
|
+
bigint: {
|
|
13
|
+
require: 'requireBigIntString',
|
|
14
|
+
as: 'asBigIntString',
|
|
15
|
+
expectedArray: 'an array of whole numbers'
|
|
16
|
+
},
|
|
13
17
|
number: { require: 'requireNumeric', as: 'asNumeric', expectedArray: 'an array of numbers' },
|
|
14
18
|
string: { require: 'requireString', as: 'asString', expectedArray: 'an array of non-empty strings' },
|
|
15
19
|
uuid: { require: 'requireUuid', as: 'asUuid', expectedArray: 'an array of UUIDs' },
|
|
@@ -21,9 +25,12 @@ const scalarTsType = (column) => {
|
|
|
21
25
|
case 'boolean':
|
|
22
26
|
return t.tsBooleanKeyword();
|
|
23
27
|
case 'integer':
|
|
24
|
-
case 'bigint':
|
|
25
28
|
case 'number':
|
|
26
29
|
return t.tsNumberKeyword();
|
|
30
|
+
// int8 carries values a JS number cannot hold, so it is a digit string —
|
|
31
|
+
// the same spelling graphql/codegen gives the `BigInt` scalar, and the one
|
|
32
|
+
// node-postgres already returns for the type.
|
|
33
|
+
case 'bigint':
|
|
27
34
|
case 'string':
|
|
28
35
|
case 'uuid':
|
|
29
36
|
case 'timestamp':
|
|
@@ -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.17.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",
|
|
@@ -45,18 +45,18 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@babel/generator": "^7.29.1",
|
|
47
47
|
"@babel/types": "^7.29.0",
|
|
48
|
-
"@constructive-io/coerce": "^0.3.
|
|
49
|
-
"@constructive-io/query-builder": "^3.10.
|
|
48
|
+
"@constructive-io/coerce": "^0.3.1",
|
|
49
|
+
"@constructive-io/query-builder": "^3.10.6",
|
|
50
50
|
"inflekt": "^0.8.1",
|
|
51
|
-
"introspectron": "^5.13.
|
|
51
|
+
"introspectron": "^5.13.1",
|
|
52
52
|
"pg": "^8.21.0",
|
|
53
|
-
"pg-env": "^1.30.
|
|
53
|
+
"pg-env": "^1.30.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@types/babel__generator": "^7.6.8",
|
|
57
57
|
"@types/pg": "^8.20.4",
|
|
58
58
|
"makage": "^0.3.0",
|
|
59
|
-
"pgsql-test": "^5.11.
|
|
59
|
+
"pgsql-test": "^5.11.1"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "ce544846eafd96280fac2607f873960392ae2c89"
|
|
62
62
|
}
|