@fabrika/db 0.9.0-dev.4 → 0.9.0-dev.40

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.
Files changed (84) hide show
  1. package/package.json +39 -4
  2. package/src/admissions.ts +78 -12
  3. package/src/application-rows.ts +108 -0
  4. package/src/authority.ts +560 -0
  5. package/src/builtins.ts +21 -0
  6. package/src/capability.ts +56 -2
  7. package/src/codegen-sqlite.ts +194 -0
  8. package/src/codegen.ts +230 -41
  9. package/src/condition-nodes.ts +119 -0
  10. package/src/conditions.ts +683 -0
  11. package/src/database.ts +227 -61
  12. package/src/description.ts +37 -8
  13. package/src/errors.ts +10 -6
  14. package/src/file-columns.ts +56 -11
  15. package/src/framework-tables.ts +28 -8
  16. package/src/framework.ts +15 -5
  17. package/src/guard.ts +373 -15
  18. package/src/index.ts +17 -1
  19. package/src/job-leases.ts +228 -55
  20. package/src/job-wakes.ts +600 -0
  21. package/src/json.ts +124 -0
  22. package/src/links.ts +8 -1
  23. package/src/list-cursor.ts +82 -0
  24. package/src/migrations-sqlite.ts +210 -0
  25. package/src/migrations.ts +73 -0
  26. package/src/nodes.ts +84 -14
  27. package/src/orm-types.ts +206 -71
  28. package/src/orm.ts +425 -170
  29. package/src/policy.ts +1979 -141
  30. package/src/postgres.ts +26 -0
  31. package/src/registry.ts +55 -3
  32. package/src/request-receipts.ts +107 -0
  33. package/src/sql.ts +247 -476
  34. package/src/sqlite-bun.ts +149 -0
  35. package/src/sqlite-migrate.ts +242 -0
  36. package/src/sqlite.ts +269 -0
  37. package/src/store.ts +2292 -239
  38. package/src/stored-files.ts +188 -52
  39. package/src/tables.ts +172 -6
  40. package/src/temporal.ts +122 -9
  41. package/src/testing-shared.ts +1351 -0
  42. package/src/testing-sqlite.ts +685 -0
  43. package/src/testing.ts +310 -494
  44. package/src/vocabulary-tables.ts +159 -27
  45. package/types/src/application-rows.d.ts +58 -0
  46. package/types/src/authority.d.ts +147 -0
  47. package/types/src/builtins.d.ts +2 -0
  48. package/types/src/capability.d.ts +55 -2
  49. package/types/src/codegen-sqlite.d.ts +13 -0
  50. package/types/src/codegen.d.ts +79 -5
  51. package/types/src/condition-nodes.d.ts +9 -0
  52. package/types/src/conditions.d.ts +151 -0
  53. package/types/src/database.d.ts +81 -20
  54. package/types/src/description.d.ts +26 -8
  55. package/types/src/errors.d.ts +9 -6
  56. package/types/src/file-columns.d.ts +6 -4
  57. package/types/src/framework.d.ts +12 -5
  58. package/types/src/guard.d.ts +24 -0
  59. package/types/src/index.d.ts +5 -4
  60. package/types/src/job-leases.d.ts +40 -5
  61. package/types/src/job-wakes.d.ts +124 -0
  62. package/types/src/json.d.ts +18 -0
  63. package/types/src/list-cursor.d.ts +13 -0
  64. package/types/src/migrations-sqlite.d.ts +4 -0
  65. package/types/src/migrations.d.ts +4 -0
  66. package/types/src/nodes.d.ts +17 -2
  67. package/types/src/orm-types.d.ts +163 -71
  68. package/types/src/orm.d.ts +21 -0
  69. package/types/src/policy.d.ts +351 -23
  70. package/types/src/postgres.d.ts +12 -0
  71. package/types/src/registry.d.ts +40 -3
  72. package/types/src/request-receipts.d.ts +22 -0
  73. package/types/src/sql.d.ts +29 -21
  74. package/types/src/sqlite-bun.d.ts +21 -0
  75. package/types/src/sqlite-migrate.d.ts +28 -0
  76. package/types/src/sqlite.d.ts +24 -0
  77. package/types/src/store.d.ts +143 -34
  78. package/types/src/stored-files.d.ts +6 -2
  79. package/types/src/tables.d.ts +18 -2
  80. package/types/src/temporal.d.ts +24 -0
  81. package/types/src/testing-shared.d.ts +290 -0
  82. package/types/src/testing-sqlite.d.ts +37 -0
  83. package/types/src/testing.d.ts +2 -93
  84. package/types/src/vocabulary-tables.d.ts +169 -22
package/src/nodes.ts CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  WhereNode,
39
39
  } from 'kysely'
40
40
  import { inSet, type NameSet, nameSet, namesOf } from './builtins'
41
+ import type { SqlDialect } from './database'
41
42
  import type { Compare, Condition, Contains, JoinState, Primitive, SelectState } from './description'
42
43
  import { QueryRefusedError } from './errors'
43
44
 
@@ -49,29 +50,54 @@ export function equals(table: string, name: string, value: Primitive): Operation
49
50
  return BinaryOperationNode.create(column(table, name), OperatorNode.create('='), ValueNode.create(value))
50
51
  }
51
52
 
53
+ // The scope predicates are built through these, so they read their nodes by index, never through an iterator or a method.
52
54
  export function allOf(nodes: readonly OperationNode[]): OperationNode {
53
- const [first, ...rest] = nodes
54
- if (first === undefined) {
55
+ let joined = nodes[0]
56
+ if (joined === undefined) {
55
57
  throw new QueryRefusedError('statement_shape', 'an empty conjunction')
56
58
  }
57
- return rest.reduce<OperationNode>((left, right) => AndNode.create(left, right), first)
59
+ for (let index = 1; index < nodes.length; index++) {
60
+ const node = nodes[index]
61
+ if (node !== undefined) {
62
+ joined = AndNode.create(joined, node)
63
+ }
64
+ }
65
+ return joined
58
66
  }
59
67
 
60
68
  /** One node, or the OR of several in a parenthesis: inside an AND, `a and b or c` would read as `(a and b) or c`. */
61
69
  export function anyOf(nodes: readonly OperationNode[]): OperationNode {
62
- const [first, ...rest] = nodes
63
- if (first === undefined) {
70
+ let joined = nodes[0]
71
+ if (joined === undefined) {
64
72
  throw new QueryRefusedError('statement_shape', 'an empty disjunction')
65
73
  }
66
- return rest.length === 0 ? first : ParensNode.create(rest.reduce<OperationNode>((left, right) => OrNode.create(left, right), first))
74
+ for (let index = 1; index < nodes.length; index++) {
75
+ const node = nodes[index]
76
+ if (node !== undefined) {
77
+ joined = OrNode.create(joined, node)
78
+ }
79
+ }
80
+ return nodes.length === 1 ? joined : ParensNode.create(joined)
67
81
  }
68
82
 
69
83
  export function inList(table: string, name: string, values: readonly string[]): OperationNode {
70
- return BinaryOperationNode.create(
71
- column(table, name),
72
- OperatorNode.create('in'),
73
- ValueListNode.create(values.map((value) => ValueNode.create(value))),
74
- )
84
+ return BinaryOperationNode.create(column(table, name), OperatorNode.create('in'), valueList(values))
85
+ }
86
+
87
+ /** `($1, $2, …)`: bound values, as the right operand of an `in`. */
88
+ export function valueList(values: readonly string[]): OperationNode {
89
+ return ValueListNode.create(valueNodes(values))
90
+ }
91
+
92
+ function valueNodes(values: readonly string[]): ValueNode[] {
93
+ const nodes: ValueNode[] = []
94
+ for (let index = 0; index < values.length; index++) {
95
+ const value = values[index]
96
+ if (value !== undefined) {
97
+ nodes[nodes.length] = ValueNode.create(value)
98
+ }
99
+ }
100
+ return nodes
75
101
  }
76
102
 
77
103
  /** The scope of a `Within` that is not limited to scope values. */
@@ -156,7 +182,9 @@ function joinNode(parent: string, join: JoinState): JoinNode {
156
182
  BinaryOperationNode.create(column(child, pair.childColumn), OperatorNode.create('='), column(parent, pair.parentColumn))
157
183
  ),
158
184
  )
159
- const conditions = conditionsNode(child, join.select.conditions)
185
+ const requested = conditionsNode(child, join.select.conditions)
186
+ const held = join.condition
187
+ const conditions = held === undefined ? requested : requested === undefined ? held : AndNode.create(requested, ParensNode.create(held))
160
188
  const on = conditions === undefined ? foreignKey : AndNode.create(foreignKey, ParensNode.create(conditions))
161
189
  return JoinNode.createWithOn('InnerJoin', TableNode.create(child), on)
162
190
  }
@@ -167,6 +195,15 @@ function joinedSelections(join: JoinState): SelectionNode[] {
167
195
  return columns.map((name) => SelectionNode.create(AliasNode.create(column(child, name), IdentifierNode.create(`${child}.${name}`))))
168
196
  }
169
197
 
198
+ /**
199
+ * One order term. Postgres sorts nulls last ascending and first descending; SQLite the other way, so on SQLite the term
200
+ * states Postgres's order and a keyset page (`orm.ts`) reads the same rows after its cursor on both.
201
+ */
202
+ function orderItem(table: string, order: SelectState['orderBy'][number], dialect: SqlDialect): OrderByItemNode {
203
+ const item = OrderByItemNode.create(column(table, order.column), RawNode.createWithSql(order.direction))
204
+ return dialect === 'postgres' ? item : Object.freeze({ ...item, nulls: order.direction === 'asc' ? 'last' : 'first' })
205
+ }
206
+
170
207
  /** `after` is a keyset page's position, ANDed to the caller's conditions inside their parenthesis. */
171
208
  export function selectNode(
172
209
  state: SelectState,
@@ -174,12 +211,13 @@ export function selectNode(
174
211
  scope: OperationNode,
175
212
  after?: OperationNode,
176
213
  days: NameSet = NO_DAYS,
214
+ dialect: SqlDialect = 'postgres',
177
215
  ): SelectQueryNode {
178
216
  const table = state.table
179
217
  const columns = state.columns ?? visible
180
218
  const conditions = conditionsNode(table, state.conditions)
181
219
  const filter = after === undefined ? conditions : conditions === undefined ? after : AndNode.create(conditions, ParensNode.create(after))
182
- const orderBy = state.orderBy.map((order) => OrderByItemNode.create(column(table, order.column), RawNode.createWithSql(order.direction)))
220
+ const orderBy = state.orderBy.map((order) => orderItem(table, order, dialect))
183
221
  const node: SelectQueryNode = {
184
222
  kind: 'SelectQueryNode',
185
223
  from: FromNode.create([TableNode.create(table)]),
@@ -316,6 +354,18 @@ export function locateNode(table: string, columns: readonly string[], scope: Ope
316
354
  return Object.freeze(node)
317
355
  }
318
356
 
357
+ /** `select T.<name> from T where <scope> group by T.<name>`: each value of `name` the scoped rows hold, once. */
358
+ export function valuesNode(table: string, name: string, scope: OperationNode): SelectQueryNode {
359
+ const node: SelectQueryNode = {
360
+ kind: 'SelectQueryNode',
361
+ from: FromNode.create([TableNode.create(table)]),
362
+ selections: [selection(table, name)],
363
+ where: scopedWhere(undefined, scope),
364
+ groupBy: GroupByNode.create([GroupByItemNode.create(column(table, name))]),
365
+ }
366
+ return Object.freeze(node)
367
+ }
368
+
319
369
  /**
320
370
  * The same check, locked: `for share` keeps the row from moving until the transaction that checked it ends, `for
321
371
  * update` also keeps every other writer and locker of it waiting.
@@ -352,6 +402,7 @@ export function claimNode(
352
402
  due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
353
403
  scope: OperationNode,
354
404
  days: NameSet,
405
+ dialect: SqlDialect = 'postgres',
355
406
  ): SelectQueryNode {
356
407
  const table = state.table
357
408
  const waiting = allOf([
@@ -359,10 +410,29 @@ export function claimNode(
359
410
  BinaryOperationNode.create(column(table, due.due), OperatorNode.create('<='), ValueNode.create(due.now)),
360
411
  BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
361
412
  ])
362
- const node = selectNode(state, columns, scope, waiting, days)
413
+ const node = selectNode(state, columns, scope, waiting, days, dialect)
363
414
  return Object.freeze({ ...node, endModifiers: [SelectModifierNode.create('ForUpdate'), SelectModifierNode.create('SkipLocked')] })
364
415
  }
365
416
 
417
+ /**
418
+ * When the same claim next finds a row: `select <due> from table where (<conditions> and (<due> > $now and <attempts> <
419
+ * $max)) and scope order by <due> limit 1`, with `state` the claim's conditions ordered by `<due>`, limited to one.
420
+ */
421
+ export function nextClaimNode(
422
+ state: SelectState,
423
+ due: { readonly due: string; readonly attempts: string; readonly now: Date; readonly maxAttempts: number },
424
+ scope: OperationNode,
425
+ days: NameSet,
426
+ dialect: SqlDialect = 'postgres',
427
+ ): SelectQueryNode {
428
+ const table = state.table
429
+ const later = allOf([
430
+ BinaryOperationNode.create(column(table, due.due), OperatorNode.create('>'), ValueNode.create(due.now)),
431
+ BinaryOperationNode.create(column(table, due.attempts), OperatorNode.create('<'), ValueNode.create(due.maxAttempts)),
432
+ ])
433
+ return selectNode(state, [due.due], scope, later, days, dialect)
434
+ }
435
+
366
436
  /** `select <columns> from table where scope for update`: a mutation's precondition read of its own target. */
367
437
  export function lockedSelectNode(table: string, columns: readonly string[], scope: OperationNode, days: NameSet = NO_DAYS): SelectQueryNode {
368
438
  const node: SelectQueryNode = {