@fabrika/db 0.9.0-dev.50 → 0.9.0-dev.52

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/db",
3
- "version": "0.9.0-dev.50",
3
+ "version": "0.9.0-dev.52",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "./src/testing.ts",
@@ -96,9 +96,9 @@
96
96
  "test": "bun test"
97
97
  },
98
98
  "dependencies": {
99
- "@fabrika/auth-core": "0.9.0-dev.50",
100
- "@fabrika/platform": "0.9.0-dev.50",
101
- "@fabrika/platform-node": "0.9.0-dev.50",
99
+ "@fabrika/auth-core": "0.9.0-dev.52",
100
+ "@fabrika/platform": "0.9.0-dev.52",
101
+ "@fabrika/platform-node": "0.9.0-dev.52",
102
102
  "kysely": "0.29.6",
103
103
  "kysely-postgres-js": "5.0.1",
104
104
  "zod": "^3.25.76"
@@ -89,6 +89,8 @@ export interface SelectState {
89
89
  readonly limit: number | undefined
90
90
  readonly offset: number | undefined
91
91
  readonly joins: readonly JoinState[]
92
+ /** A `where` named an `in` with an empty list, so no row matches; only an aggregate's filter takes it (`finishFilter`). */
93
+ readonly matchesNone: boolean
92
94
  }
93
95
 
94
96
  type Comparable<V> = Exclude<V, null | undefined>
@@ -159,6 +161,11 @@ export class ScopedSelect<Row, Selected, Children> {
159
161
  where<C extends StringColumn<Row>>(column: C, operator: PatternOperator, pattern: string): ScopedSelect<Row, Selected, Children>
160
162
  where<C extends NullableColumn<Row>>(column: C, operator: NullOperator, value: null): ScopedSelect<Row, Selected, Children>
161
163
  where(column: string, operator: string, value: unknown): ScopedSelect<Row, Selected, Children> {
164
+ if (operator === 'in' && parseList(value, (item) => item)?.length === 0) {
165
+ // The column is still held to the shape: an empty list does not make a hidden one nameable.
166
+ comparableColumnOf(this.#shape, column, 'compares')
167
+ return this.#next({ matchesNone: true })
168
+ }
162
169
  return this.#withCondition(this.#parseCondition(column, operator, value))
163
170
  }
164
171
 
@@ -309,12 +316,22 @@ export function startSelect<Row, Children>(shape: TableShape): ScopedSelect<Row,
309
316
  limit: undefined,
310
317
  offset: undefined,
311
318
  joins: [],
319
+ matchesNone: false,
312
320
  }
313
321
  return new ScopedSelect(selectToken, state, shape)
314
322
  }
315
323
 
316
- /** The description a callback returned; it must derive from the one it received. */
324
+ /** The description a callback returned; it must derive from the one it received, and an `in` names at least one value. */
317
325
  export function finishSelect<Row, Children>(built: unknown, start: ScopedSelect<Row, Row, Children>): SelectState {
326
+ const state = finishFilter(built, start)
327
+ if (state.matchesNone) {
328
+ throw new QueryRefusedError('invalid_argument', 'in needs a non-empty plain array of values')
329
+ }
330
+ return state
331
+ }
332
+
333
+ /** `finishSelect` for an aggregate's `where`, where an `in` with an empty list matches no row (`matchesNone`). */
334
+ export function finishFilter<Row, Children>(built: unknown, start: ScopedSelect<Row, Row, Children>): SelectState {
318
335
  if (!(built instanceof ScopedSelect)) {
319
336
  throw new QueryRefusedError('invalid_argument', 'the query callback must return the description it received')
320
337
  }
package/src/orm.ts CHANGED
@@ -1315,7 +1315,7 @@ function limitOf(limit: unknown): number {
1315
1315
  }
1316
1316
 
1317
1317
  function rowState(table: string, conditions: readonly (Compare | Contains)[], order: readonly OrderColumn[], limit: number | undefined): SelectState {
1318
- return { origin: freeze({}), table, columns: undefined, conditions, orderBy: order, limit, offset: undefined, joins: [] }
1318
+ return { origin: freeze({}), table, columns: undefined, conditions, orderBy: order, limit, offset: undefined, joins: [], matchesNone: false }
1319
1319
  }
1320
1320
 
1321
1321
  /** The list a cursor belongs to, as its signature covers it: its table and its whole order, the tiebreak included. */
package/src/store.ts CHANGED
@@ -73,6 +73,7 @@ import {
73
73
  type Compare,
74
74
  type Condition,
75
75
  type Contains,
76
+ finishFilter,
76
77
  finishSelect,
77
78
  isPrimitive,
78
79
  type JoinShape,
@@ -2193,7 +2194,7 @@ function aggregateFilter<Row>(shape: TableShape, where: AggregateSpec<Row>['wher
2193
2194
  throw new QueryRefusedError('invalid_argument', 'aggregate where takes a callback')
2194
2195
  }
2195
2196
  const start = startSelect<Row, NoColumns>(shape)
2196
- const state = finishSelect(where(start), start)
2197
+ const state = finishFilter(where(start), start)
2197
2198
  if (state.columns !== undefined || state.orderBy.length > 0 || state.limit !== undefined || state.offset !== undefined || state.joins.length > 0) {
2198
2199
  throw new QueryRefusedError('invalid_argument', 'aggregate where takes where conditions only')
2199
2200
  }
@@ -2212,7 +2213,8 @@ export async function aggregateRows<Row, O>(
2212
2213
  }
2213
2214
  const columns = aggregateColumns(context.model.name, shape, spec)
2214
2215
  const filter = aggregateFilter(shape, spec.where)
2215
- if (scope === undefined) {
2216
+ // An `in` with an empty list answers what an empty `Within` does, and sends no statement.
2217
+ if (scope === undefined || filter?.matchesNone === true) {
2216
2218
  return []
2217
2219
  }
2218
2220
  const dialect = dialectOf(context)
@@ -62,6 +62,8 @@ export interface SelectState {
62
62
  readonly limit: number | undefined;
63
63
  readonly offset: number | undefined;
64
64
  readonly joins: readonly JoinState[];
65
+ /** A `where` named an `in` with an empty list, so no row matches; only an aggregate's filter takes it (`finishFilter`). */
66
+ readonly matchesNone: boolean;
65
67
  }
66
68
  type Comparable<V> = Exclude<V, null | undefined>;
67
69
  type Columns<Row> = keyof Row & string;
@@ -121,7 +123,9 @@ export declare function comparableColumnOf(shape: TableShape, name: unknown, use
121
123
  /** One `[column, operator, value]` condition over a caller's values, checked before anything reads it further. */
122
124
  export declare function parseCondition(shape: TableShape, column: unknown, operator: unknown, value: unknown): Compare | Contains;
123
125
  export declare function startSelect<Row, Children>(shape: TableShape): ScopedSelect<Row, Row, Children>;
124
- /** The description a callback returned; it must derive from the one it received. */
126
+ /** The description a callback returned; it must derive from the one it received, and an `in` names at least one value. */
125
127
  export declare function finishSelect<Row, Children>(built: unknown, start: ScopedSelect<Row, Row, Children>): SelectState;
128
+ /** `finishSelect` for an aggregate's `where`, where an `in` with an empty list matches no row (`matchesNone`). */
129
+ export declare function finishFilter<Row, Children>(built: unknown, start: ScopedSelect<Row, Row, Children>): SelectState;
126
130
  export declare function isPrimitive(value: unknown): value is Primitive;
127
131
  export {};