@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.21

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 (80) hide show
  1. package/package.json +35 -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 +229 -40
  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 +6 -4
  14. package/src/file-columns.ts +56 -11
  15. package/src/framework-tables.ts +28 -8
  16. package/src/framework.ts +14 -5
  17. package/src/guard.ts +373 -15
  18. package/src/index.ts +13 -1
  19. package/src/job-leases.ts +212 -55
  20. package/src/job-wakes.ts +450 -0
  21. package/src/json.ts +124 -0
  22. package/src/links.ts +8 -1
  23. package/src/migrations-sqlite.ts +163 -0
  24. package/src/migrations.ts +25 -0
  25. package/src/nodes.ts +84 -14
  26. package/src/orm-types.ts +124 -45
  27. package/src/orm.ts +283 -133
  28. package/src/policy.ts +1851 -119
  29. package/src/postgres.ts +26 -0
  30. package/src/registry.ts +52 -3
  31. package/src/sql.ts +247 -476
  32. package/src/sqlite-bun.ts +149 -0
  33. package/src/sqlite-migrate.ts +242 -0
  34. package/src/sqlite.ts +269 -0
  35. package/src/store.ts +2290 -239
  36. package/src/stored-files.ts +188 -52
  37. package/src/tables.ts +164 -3
  38. package/src/temporal.ts +122 -9
  39. package/src/testing-shared.ts +1348 -0
  40. package/src/testing-sqlite.ts +685 -0
  41. package/src/testing.ts +310 -494
  42. package/src/vocabulary-tables.ts +124 -18
  43. package/types/src/application-rows.d.ts +58 -0
  44. package/types/src/authority.d.ts +147 -0
  45. package/types/src/builtins.d.ts +2 -0
  46. package/types/src/capability.d.ts +55 -2
  47. package/types/src/codegen-sqlite.d.ts +13 -0
  48. package/types/src/codegen.d.ts +79 -5
  49. package/types/src/condition-nodes.d.ts +9 -0
  50. package/types/src/conditions.d.ts +151 -0
  51. package/types/src/database.d.ts +81 -20
  52. package/types/src/description.d.ts +26 -8
  53. package/types/src/errors.d.ts +5 -4
  54. package/types/src/file-columns.d.ts +6 -4
  55. package/types/src/framework.d.ts +11 -5
  56. package/types/src/guard.d.ts +24 -0
  57. package/types/src/index.d.ts +4 -3
  58. package/types/src/job-leases.d.ts +34 -5
  59. package/types/src/job-wakes.d.ts +101 -0
  60. package/types/src/json.d.ts +18 -0
  61. package/types/src/migrations-sqlite.d.ts +4 -0
  62. package/types/src/migrations.d.ts +1 -0
  63. package/types/src/nodes.d.ts +17 -2
  64. package/types/src/orm-types.d.ts +91 -42
  65. package/types/src/orm.d.ts +10 -0
  66. package/types/src/policy.d.ts +332 -6
  67. package/types/src/postgres.d.ts +12 -0
  68. package/types/src/registry.d.ts +37 -3
  69. package/types/src/sql.d.ts +29 -21
  70. package/types/src/sqlite-bun.d.ts +21 -0
  71. package/types/src/sqlite-migrate.d.ts +28 -0
  72. package/types/src/sqlite.d.ts +24 -0
  73. package/types/src/store.d.ts +143 -34
  74. package/types/src/stored-files.d.ts +6 -2
  75. package/types/src/tables.d.ts +17 -1
  76. package/types/src/temporal.d.ts +24 -0
  77. package/types/src/testing-shared.d.ts +290 -0
  78. package/types/src/testing-sqlite.d.ts +37 -0
  79. package/types/src/testing.d.ts +2 -93
  80. package/types/src/vocabulary-tables.d.ts +141 -17
@@ -0,0 +1,119 @@
1
+ // The predicate of a structured row condition, and the one subquery shape a scope predicate holds. Apart from the
2
+ // parse in `conditions.ts`, which `@fabrika/db/tables` loads in a browser without Kysely.
3
+
4
+ import {
5
+ BinaryOperationNode,
6
+ ColumnNode,
7
+ type OperationNode,
8
+ OperatorNode,
9
+ ParensNode,
10
+ QueryNode,
11
+ ReferenceNode,
12
+ SelectionNode,
13
+ SelectQueryNode,
14
+ TableNode,
15
+ UnaryOperationNode,
16
+ ValueListNode,
17
+ ValueNode,
18
+ } from 'kysely'
19
+ import { isArray } from './builtins'
20
+ import { type ConditionHop, type ConditionLiteral, type ConditionModel, NOW, type NowOperand, scalar } from './conditions'
21
+ import { allOf, anyOf, column } from './nodes'
22
+
23
+ function comparison(operator: 'eq' | 'notEq' | 'lt' | 'lte' | 'gt' | 'gte'): OperatorNode {
24
+ switch (operator) {
25
+ case 'eq':
26
+ return OperatorNode.create('=')
27
+ case 'notEq':
28
+ return OperatorNode.create('<>')
29
+ case 'lt':
30
+ return OperatorNode.create('<')
31
+ case 'lte':
32
+ return OperatorNode.create('<=')
33
+ case 'gt':
34
+ return OperatorNode.create('>')
35
+ case 'gte':
36
+ return OperatorNode.create('>=')
37
+ }
38
+ }
39
+
40
+ /** The predicate of `condition` over `table`'s rows; `now` is the statement's clock, bound as a value. */
41
+ export function conditionNode(table: string, condition: ConditionModel, now: Date): OperationNode {
42
+ switch (condition.kind) {
43
+ case 'all':
44
+ return ParensNode.create(allOf(nodesOf(table, condition.terms, now)))
45
+ case 'any':
46
+ return anyOf(nodesOf(table, condition.terms, now))
47
+ case 'not':
48
+ return UnaryOperationNode.create(OperatorNode.create('not'), ParensNode.create(conditionNode(table, condition.term, now)))
49
+ case 'some':
50
+ return alongPath(table, condition.hops, 0, (target) => {
51
+ // The rows of the other table whose key names this row: a null key names none, so `not in` never turns unknown.
52
+ const named = BinaryOperationNode.create(
53
+ column(condition.table, condition.column),
54
+ OperatorNode.create('is not'),
55
+ ValueNode.createImmediate(null),
56
+ )
57
+ const rows = subquery(condition.table, condition.column, allOf([named, ParensNode.create(conditionNode(condition.table, condition.term, now))]))
58
+ const some = BinaryOperationNode.create(column(target, condition.key), OperatorNode.create('in'), rows)
59
+ return condition.negated ? UnaryOperationNode.create(OperatorNode.create('not'), ParensNode.create(some)) : some
60
+ })
61
+ case 'test':
62
+ return alongPath(table, condition.hops, 0, (target) => testNode(target, condition, now))
63
+ }
64
+ }
65
+
66
+ function nodesOf(table: string, terms: readonly ConditionModel[], now: Date): OperationNode[] {
67
+ const nodes: OperationNode[] = []
68
+ for (let index = 0; index < terms.length; index++) {
69
+ const term = terms[index]
70
+ if (term !== undefined) nodes[nodes.length] = conditionNode(table, term, now)
71
+ }
72
+ return nodes
73
+ }
74
+
75
+ /** `table.fk in (select target.key from target where …)` per hop, then `leaf` over the row the path reaches. */
76
+ function alongPath(table: string, hops: readonly ConditionHop[], position: number, leaf: (table: string) => OperationNode): OperationNode {
77
+ const hop = hops[position]
78
+ if (hop === undefined) {
79
+ return leaf(table)
80
+ }
81
+ const inner = alongPath(hop.table, hops, position + 1, leaf)
82
+ return BinaryOperationNode.create(column(table, hop.column), OperatorNode.create('in'), subquery(hop.table, hop.key, inner))
83
+ }
84
+
85
+ /** A column's own test. */
86
+ function testNode(table: string, test: Extract<ConditionModel, { kind: 'test' }>, now: Date): OperationNode {
87
+ const left = column(table, test.column)
88
+ const value = test.value
89
+ switch (test.operator) {
90
+ case 'isNull':
91
+ return BinaryOperationNode.create(left, OperatorNode.create(value === true ? 'is' : 'is not'), ValueNode.createImmediate(null))
92
+ case 'in':
93
+ case 'notIn':
94
+ return BinaryOperationNode.create(left, OperatorNode.create(test.operator === 'in' ? 'in' : 'not in'), ValueListNode.create(literals(value)))
95
+ default:
96
+ return BinaryOperationNode.create(left, comparison(test.operator), ValueNode.create(value === NOW ? now : scalar(value)))
97
+ }
98
+ }
99
+
100
+ function literals(value: ConditionLiteral | readonly ConditionLiteral[] | boolean | NowOperand): ValueNode[] {
101
+ const nodes: ValueNode[] = []
102
+ if (isArray(value)) {
103
+ for (let index = 0; index < value.length; index++) {
104
+ nodes[nodes.length] = ValueNode.create(value[index])
105
+ }
106
+ }
107
+ return nodes
108
+ }
109
+
110
+ /**
111
+ * `select table.key from table where <where>`: the one subquery shape the guard admits in a scope predicate. Without
112
+ * `where`, every row's key: only a child view's parent subquery takes that form.
113
+ */
114
+ export function subquery(table: string, key: string, where?: OperationNode): OperationNode {
115
+ const select = SelectQueryNode.cloneWithSelections(SelectQueryNode.createFrom([TableNode.create(table)]), [
116
+ SelectionNode.create(ReferenceNode.create(ColumnNode.create(key), TableNode.create(table))),
117
+ ])
118
+ return where === undefined ? select : QueryNode.cloneWithWhere(select, where)
119
+ }