pg-codegen 5.13.1 → 5.14.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.
@@ -18,15 +18,32 @@ export interface TableSpec<App> {
18
18
  /** camelCase fields backed by json/jsonb columns, serialized on writes. */
19
19
  jsonFields?: readonly string[];
20
20
  }
21
- /** Select shape: `{ id: true, name: true }` — same as the GraphQL ORM. */
21
+ /**
22
+ * Select shape — same as the GraphQL ORM, stated either way round:
23
+ *
24
+ * - `{ id: true, name: true }` reads those fields and nothing else;
25
+ * - `{ databaseId: false }` reads every field except that one, for the field a
26
+ * record has in general but this binding's table does not (a scope's copy of
27
+ * a table without the scope key column) or that a caller must not carry (a
28
+ * secret's id, row bookkeeping).
29
+ *
30
+ * Naming a field `true` is a projection and wins outright: the excluded ones
31
+ * are simply not in it.
32
+ */
22
33
  export type SelectShape<App> = {
23
34
  [K in keyof App]?: boolean;
24
35
  };
25
36
  type TrueKeys<App, S> = {
26
37
  [K in keyof S]-?: S[K] extends true ? K & keyof App : never;
27
38
  }[keyof S];
28
- /** Rows narrow to the selected fields; no `select` returns the full record. */
29
- export type SelectResult<App, S extends SelectShape<App> | undefined> = S extends SelectShape<App> ? [TrueKeys<App, S>] extends [never] ? App : Pick<App, TrueKeys<App, S>> : App;
39
+ type FalseKeys<App, S> = {
40
+ [K in keyof S]-?: S[K] extends false ? K & keyof App : never;
41
+ }[keyof S];
42
+ /**
43
+ * Rows narrow to what the select states: the picked fields, else the record
44
+ * minus the excluded ones. No `select` returns the full record.
45
+ */
46
+ export type SelectResult<App, S extends SelectShape<App> | undefined> = S extends SelectShape<App> ? [TrueKeys<App, S>] extends [never] ? [FalseKeys<App, S>] extends [never] ? App : Omit<App, FalseKeys<App, S>> : Pick<App, TrueKeys<App, S>> : App;
30
47
  /** Per-field operators from the shared query-spec grammar. */
31
48
  export interface FieldOps<V> {
32
49
  equalTo?: V;
@@ -127,6 +144,14 @@ export declare class TableClient<App> {
127
144
  private column;
128
145
  private selectedFields;
129
146
  private decodeRow;
147
+ /**
148
+ * The predicate to apply, or nothing to apply: a caller that spreads a
149
+ * conditional key column (a scope that records none) states an empty filter,
150
+ * and an unqualified read is what it asked for.
151
+ */
152
+ private predicate;
153
+ /** A write says which rows: an empty filter would mean the whole table. */
154
+ private required;
130
155
  private filter;
131
156
  private fieldFilter;
132
157
  private applyOrderBy;
@@ -55,8 +55,9 @@ class TableClient {
55
55
  async findMany(args = {}) {
56
56
  const fields = this.selectedFields(args.select);
57
57
  const query = this.baseQuery().select(fields.map(field => this.column(field)));
58
- if (args.where)
59
- query.where(this.filter(args.where));
58
+ const predicate = this.predicate(args.where);
59
+ if (predicate)
60
+ query.where(predicate);
60
61
  this.applyOrderBy(query, args.orderBy);
61
62
  if (args.limit !== undefined)
62
63
  query.limit(args.limit);
@@ -78,8 +79,9 @@ class TableClient {
78
79
  }
79
80
  async count(where) {
80
81
  const query = this.baseQuery().select([]).selectExpr('count', (0, query_builder_1.fn)('count', [(0, query_builder_1.lit)(1)]));
81
- if (where)
82
- query.where(this.filter(where));
82
+ const predicate = this.predicate(where);
83
+ if (predicate)
84
+ query.where(predicate);
83
85
  const { text, values } = query.build();
84
86
  const { rows } = await this.db.query(text, values);
85
87
  const row = rows[0];
@@ -98,7 +100,7 @@ class TableClient {
98
100
  const fields = this.selectedFields(args.select);
99
101
  const query = this.baseQuery()
100
102
  .update(this.encodeData(args.data))
101
- .where(this.filter(args.where))
103
+ .where(this.required(args.where, 'update'))
102
104
  .returning(fields.map(field => this.column(field)));
103
105
  const { text, values } = query.build();
104
106
  const { rows } = await this.db.query(text, values);
@@ -114,7 +116,7 @@ class TableClient {
114
116
  const fields = this.selectedFields(args.select);
115
117
  const query = this.baseQuery()
116
118
  .delete()
117
- .where(this.filter(args.where))
119
+ .where(this.required(args.where, 'delete'))
118
120
  .returning(fields.map(field => this.column(field)));
119
121
  const { text, values } = query.build();
120
122
  const { rows } = await this.db.query(text, values);
@@ -135,7 +137,11 @@ class TableClient {
135
137
  if (!select)
136
138
  return all;
137
139
  const picked = all.filter(field => select[field] === true);
138
- return picked.length > 0 ? picked : all;
140
+ if (picked.length > 0)
141
+ return picked;
142
+ // Nothing picked: the select either excludes fields or says nothing at all,
143
+ // and both read what is left.
144
+ return all.filter(field => select[field] !== false);
139
145
  }
140
146
  decodeRow(row, fields) {
141
147
  const raw = row;
@@ -145,6 +151,25 @@ class TableClient {
145
151
  }
146
152
  return decoded;
147
153
  }
154
+ /**
155
+ * The predicate to apply, or nothing to apply: a caller that spreads a
156
+ * conditional key column (a scope that records none) states an empty filter,
157
+ * and an unqualified read is what it asked for.
158
+ */
159
+ predicate(where) {
160
+ if (!where)
161
+ return undefined;
162
+ const filter = this.filter(where);
163
+ return Object.keys(filter).length > 0 ? filter : undefined;
164
+ }
165
+ /** A write says which rows: an empty filter would mean the whole table. */
166
+ required(where, operation) {
167
+ const predicate = this.predicate(where);
168
+ if (!predicate) {
169
+ throw new Error(`${this.spec.table}.${operation}: refusing an empty where filter, which would match every row`);
170
+ }
171
+ return predicate;
172
+ }
148
173
  filter(where) {
149
174
  const filter = {};
150
175
  for (const [key, value] of Object.entries(where)) {
@@ -33,18 +33,38 @@ export interface TableSpec<App> {
33
33
  jsonFields?: readonly string[];
34
34
  }
35
35
 
36
- /** Select shape: `{ id: true, name: true }` — same as the GraphQL ORM. */
36
+ /**
37
+ * Select shape — same as the GraphQL ORM, stated either way round:
38
+ *
39
+ * - `{ id: true, name: true }` reads those fields and nothing else;
40
+ * - `{ databaseId: false }` reads every field except that one, for the field a
41
+ * record has in general but this binding's table does not (a scope's copy of
42
+ * a table without the scope key column) or that a caller must not carry (a
43
+ * secret's id, row bookkeeping).
44
+ *
45
+ * Naming a field `true` is a projection and wins outright: the excluded ones
46
+ * are simply not in it.
47
+ */
37
48
  export type SelectShape<App> = { [K in keyof App]?: boolean };
38
49
 
39
50
  type TrueKeys<App, S> = {
40
51
  [K in keyof S]-?: S[K] extends true ? K & keyof App : never;
41
52
  }[keyof S];
42
53
 
43
- /** Rows narrow to the selected fields; no `select` returns the full record. */
54
+ type FalseKeys<App, S> = {
55
+ [K in keyof S]-?: S[K] extends false ? K & keyof App : never;
56
+ }[keyof S];
57
+
58
+ /**
59
+ * Rows narrow to what the select states: the picked fields, else the record
60
+ * minus the excluded ones. No `select` returns the full record.
61
+ */
44
62
  export type SelectResult<App, S extends SelectShape<App> | undefined> =
45
63
  S extends SelectShape<App>
46
64
  ? [TrueKeys<App, S>] extends [never]
47
- ? App
65
+ ? [FalseKeys<App, S>] extends [never]
66
+ ? App
67
+ : Omit<App, FalseKeys<App, S>>
48
68
  : Pick<App, TrueKeys<App, S>>
49
69
  : App;
50
70
 
@@ -182,7 +202,8 @@ export class TableClient<App> {
182
202
  ): Promise<SelectResult<App, S>[]> {
183
203
  const fields = this.selectedFields(args.select);
184
204
  const query = this.baseQuery().select(fields.map(field => this.column(field)));
185
- if (args.where) query.where(this.filter(args.where));
205
+ const predicate = this.predicate(args.where);
206
+ if (predicate) query.where(predicate);
186
207
  this.applyOrderBy(query, args.orderBy);
187
208
  if (args.limit !== undefined) query.limit(args.limit);
188
209
  if (args.offset !== undefined) query.offset(args.offset);
@@ -208,7 +229,8 @@ export class TableClient<App> {
208
229
 
209
230
  async count(where?: Where<App>): Promise<number> {
210
231
  const query = this.baseQuery().select([]).selectExpr('count', fn('count', [lit(1)]));
211
- if (where) query.where(this.filter(where));
232
+ const predicate = this.predicate(where);
233
+ if (predicate) query.where(predicate);
212
234
  const { text, values } = query.build();
213
235
  const { rows } = await this.db.query(text, values);
214
236
  const row = rows[0] as { count: string | number };
@@ -233,7 +255,7 @@ export class TableClient<App> {
233
255
  const fields = this.selectedFields(args.select);
234
256
  const query = this.baseQuery()
235
257
  .update(this.encodeData(args.data))
236
- .where(this.filter(args.where))
258
+ .where(this.required(args.where, 'update'))
237
259
  .returning(fields.map(field => this.column(field)));
238
260
  const { text, values } = query.build();
239
261
  const { rows } = await this.db.query(text, values);
@@ -254,7 +276,7 @@ export class TableClient<App> {
254
276
  const fields = this.selectedFields(args.select);
255
277
  const query = this.baseQuery()
256
278
  .delete()
257
- .where(this.filter(args.where))
279
+ .where(this.required(args.where, 'delete'))
258
280
  .returning(fields.map(field => this.column(field)));
259
281
  const { text, values } = query.build();
260
282
  const { rows } = await this.db.query(text, values);
@@ -278,7 +300,10 @@ export class TableClient<App> {
278
300
  const all = Object.keys(this.spec.columnByField) as (keyof App & string)[];
279
301
  if (!select) return all;
280
302
  const picked = all.filter(field => select[field] === true);
281
- return picked.length > 0 ? picked : all;
303
+ if (picked.length > 0) return picked;
304
+ // Nothing picked: the select either excludes fields or says nothing at all,
305
+ // and both read what is left.
306
+ return all.filter(field => select[field] !== false);
282
307
  }
283
308
 
284
309
  private decodeRow<S extends SelectShape<App> | undefined>(
@@ -293,6 +318,28 @@ export class TableClient<App> {
293
318
  return decoded as SelectResult<App, S>;
294
319
  }
295
320
 
321
+ /**
322
+ * The predicate to apply, or nothing to apply: a caller that spreads a
323
+ * conditional key column (a scope that records none) states an empty filter,
324
+ * and an unqualified read is what it asked for.
325
+ */
326
+ private predicate(where: Where<App> | undefined): Filter | undefined {
327
+ if (!where) return undefined;
328
+ const filter = this.filter(where);
329
+ return Object.keys(filter).length > 0 ? filter : undefined;
330
+ }
331
+
332
+ /** A write says which rows: an empty filter would mean the whole table. */
333
+ private required(where: Where<App>, operation: string): Filter {
334
+ const predicate = this.predicate(where);
335
+ if (!predicate) {
336
+ throw new Error(
337
+ `${this.spec.table}.${operation}: refusing an empty where filter, which would match every row`
338
+ );
339
+ }
340
+ return predicate;
341
+ }
342
+
296
343
  private filter(where: Where<App>): Filter {
297
344
  const filter: Filter = {};
298
345
  for (const [key, value] of Object.entries(where)) {
@@ -51,8 +51,9 @@ export class TableClient {
51
51
  async findMany(args = {}) {
52
52
  const fields = this.selectedFields(args.select);
53
53
  const query = this.baseQuery().select(fields.map(field => this.column(field)));
54
- if (args.where)
55
- query.where(this.filter(args.where));
54
+ const predicate = this.predicate(args.where);
55
+ if (predicate)
56
+ query.where(predicate);
56
57
  this.applyOrderBy(query, args.orderBy);
57
58
  if (args.limit !== undefined)
58
59
  query.limit(args.limit);
@@ -74,8 +75,9 @@ export class TableClient {
74
75
  }
75
76
  async count(where) {
76
77
  const query = this.baseQuery().select([]).selectExpr('count', fn('count', [lit(1)]));
77
- if (where)
78
- query.where(this.filter(where));
78
+ const predicate = this.predicate(where);
79
+ if (predicate)
80
+ query.where(predicate);
79
81
  const { text, values } = query.build();
80
82
  const { rows } = await this.db.query(text, values);
81
83
  const row = rows[0];
@@ -94,7 +96,7 @@ export class TableClient {
94
96
  const fields = this.selectedFields(args.select);
95
97
  const query = this.baseQuery()
96
98
  .update(this.encodeData(args.data))
97
- .where(this.filter(args.where))
99
+ .where(this.required(args.where, 'update'))
98
100
  .returning(fields.map(field => this.column(field)));
99
101
  const { text, values } = query.build();
100
102
  const { rows } = await this.db.query(text, values);
@@ -110,7 +112,7 @@ export class TableClient {
110
112
  const fields = this.selectedFields(args.select);
111
113
  const query = this.baseQuery()
112
114
  .delete()
113
- .where(this.filter(args.where))
115
+ .where(this.required(args.where, 'delete'))
114
116
  .returning(fields.map(field => this.column(field)));
115
117
  const { text, values } = query.build();
116
118
  const { rows } = await this.db.query(text, values);
@@ -131,7 +133,11 @@ export class TableClient {
131
133
  if (!select)
132
134
  return all;
133
135
  const picked = all.filter(field => select[field] === true);
134
- return picked.length > 0 ? picked : all;
136
+ if (picked.length > 0)
137
+ return picked;
138
+ // Nothing picked: the select either excludes fields or says nothing at all,
139
+ // and both read what is left.
140
+ return all.filter(field => select[field] !== false);
135
141
  }
136
142
  decodeRow(row, fields) {
137
143
  const raw = row;
@@ -141,6 +147,25 @@ export class TableClient {
141
147
  }
142
148
  return decoded;
143
149
  }
150
+ /**
151
+ * The predicate to apply, or nothing to apply: a caller that spreads a
152
+ * conditional key column (a scope that records none) states an empty filter,
153
+ * and an unqualified read is what it asked for.
154
+ */
155
+ predicate(where) {
156
+ if (!where)
157
+ return undefined;
158
+ const filter = this.filter(where);
159
+ return Object.keys(filter).length > 0 ? filter : undefined;
160
+ }
161
+ /** A write says which rows: an empty filter would mean the whole table. */
162
+ required(where, operation) {
163
+ const predicate = this.predicate(where);
164
+ if (!predicate) {
165
+ throw new Error(`${this.spec.table}.${operation}: refusing an empty where filter, which would match every row`);
166
+ }
167
+ return predicate;
168
+ }
144
169
  filter(where) {
145
170
  const filter = {};
146
171
  for (const [key, value] of Object.entries(where)) {
@@ -33,18 +33,38 @@ export interface TableSpec<App> {
33
33
  jsonFields?: readonly string[];
34
34
  }
35
35
 
36
- /** Select shape: `{ id: true, name: true }` — same as the GraphQL ORM. */
36
+ /**
37
+ * Select shape — same as the GraphQL ORM, stated either way round:
38
+ *
39
+ * - `{ id: true, name: true }` reads those fields and nothing else;
40
+ * - `{ databaseId: false }` reads every field except that one, for the field a
41
+ * record has in general but this binding's table does not (a scope's copy of
42
+ * a table without the scope key column) or that a caller must not carry (a
43
+ * secret's id, row bookkeeping).
44
+ *
45
+ * Naming a field `true` is a projection and wins outright: the excluded ones
46
+ * are simply not in it.
47
+ */
37
48
  export type SelectShape<App> = { [K in keyof App]?: boolean };
38
49
 
39
50
  type TrueKeys<App, S> = {
40
51
  [K in keyof S]-?: S[K] extends true ? K & keyof App : never;
41
52
  }[keyof S];
42
53
 
43
- /** Rows narrow to the selected fields; no `select` returns the full record. */
54
+ type FalseKeys<App, S> = {
55
+ [K in keyof S]-?: S[K] extends false ? K & keyof App : never;
56
+ }[keyof S];
57
+
58
+ /**
59
+ * Rows narrow to what the select states: the picked fields, else the record
60
+ * minus the excluded ones. No `select` returns the full record.
61
+ */
44
62
  export type SelectResult<App, S extends SelectShape<App> | undefined> =
45
63
  S extends SelectShape<App>
46
64
  ? [TrueKeys<App, S>] extends [never]
47
- ? App
65
+ ? [FalseKeys<App, S>] extends [never]
66
+ ? App
67
+ : Omit<App, FalseKeys<App, S>>
48
68
  : Pick<App, TrueKeys<App, S>>
49
69
  : App;
50
70
 
@@ -182,7 +202,8 @@ export class TableClient<App> {
182
202
  ): Promise<SelectResult<App, S>[]> {
183
203
  const fields = this.selectedFields(args.select);
184
204
  const query = this.baseQuery().select(fields.map(field => this.column(field)));
185
- if (args.where) query.where(this.filter(args.where));
205
+ const predicate = this.predicate(args.where);
206
+ if (predicate) query.where(predicate);
186
207
  this.applyOrderBy(query, args.orderBy);
187
208
  if (args.limit !== undefined) query.limit(args.limit);
188
209
  if (args.offset !== undefined) query.offset(args.offset);
@@ -208,7 +229,8 @@ export class TableClient<App> {
208
229
 
209
230
  async count(where?: Where<App>): Promise<number> {
210
231
  const query = this.baseQuery().select([]).selectExpr('count', fn('count', [lit(1)]));
211
- if (where) query.where(this.filter(where));
232
+ const predicate = this.predicate(where);
233
+ if (predicate) query.where(predicate);
212
234
  const { text, values } = query.build();
213
235
  const { rows } = await this.db.query(text, values);
214
236
  const row = rows[0] as { count: string | number };
@@ -233,7 +255,7 @@ export class TableClient<App> {
233
255
  const fields = this.selectedFields(args.select);
234
256
  const query = this.baseQuery()
235
257
  .update(this.encodeData(args.data))
236
- .where(this.filter(args.where))
258
+ .where(this.required(args.where, 'update'))
237
259
  .returning(fields.map(field => this.column(field)));
238
260
  const { text, values } = query.build();
239
261
  const { rows } = await this.db.query(text, values);
@@ -254,7 +276,7 @@ export class TableClient<App> {
254
276
  const fields = this.selectedFields(args.select);
255
277
  const query = this.baseQuery()
256
278
  .delete()
257
- .where(this.filter(args.where))
279
+ .where(this.required(args.where, 'delete'))
258
280
  .returning(fields.map(field => this.column(field)));
259
281
  const { text, values } = query.build();
260
282
  const { rows } = await this.db.query(text, values);
@@ -278,7 +300,10 @@ export class TableClient<App> {
278
300
  const all = Object.keys(this.spec.columnByField) as (keyof App & string)[];
279
301
  if (!select) return all;
280
302
  const picked = all.filter(field => select[field] === true);
281
- return picked.length > 0 ? picked : all;
303
+ if (picked.length > 0) return picked;
304
+ // Nothing picked: the select either excludes fields or says nothing at all,
305
+ // and both read what is left.
306
+ return all.filter(field => select[field] !== false);
282
307
  }
283
308
 
284
309
  private decodeRow<S extends SelectShape<App> | undefined>(
@@ -293,6 +318,28 @@ export class TableClient<App> {
293
318
  return decoded as SelectResult<App, S>;
294
319
  }
295
320
 
321
+ /**
322
+ * The predicate to apply, or nothing to apply: a caller that spreads a
323
+ * conditional key column (a scope that records none) states an empty filter,
324
+ * and an unqualified read is what it asked for.
325
+ */
326
+ private predicate(where: Where<App> | undefined): Filter | undefined {
327
+ if (!where) return undefined;
328
+ const filter = this.filter(where);
329
+ return Object.keys(filter).length > 0 ? filter : undefined;
330
+ }
331
+
332
+ /** A write says which rows: an empty filter would mean the whole table. */
333
+ private required(where: Where<App>, operation: string): Filter {
334
+ const predicate = this.predicate(where);
335
+ if (!predicate) {
336
+ throw new Error(
337
+ `${this.spec.table}.${operation}: refusing an empty where filter, which would match every row`
338
+ );
339
+ }
340
+ return predicate;
341
+ }
342
+
296
343
  private filter(where: Where<App>): Filter {
297
344
  const filter: Filter = {};
298
345
  for (const [key, value] of Object.entries(where)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-codegen",
3
- "version": "5.13.1",
3
+ "version": "5.14.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": "133b685e9a4ff8af8955db9f49ec3992adb200a9"
61
+ "gitHead": "4da763d15eefc04846e27a6e8b1647f57676be28"
62
62
  }