@fabrika/db 0.9.0-dev.4 → 0.9.0-dev.41
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 +39 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +230 -41
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +10 -6
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +15 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +17 -1
- package/src/job-leases.ts +228 -55
- package/src/job-wakes.ts +600 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/list-cursor.ts +82 -0
- package/src/migrations-sqlite.ts +210 -0
- package/src/migrations.ts +73 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +206 -71
- package/src/orm.ts +425 -170
- package/src/policy.ts +1979 -141
- package/src/postgres.ts +26 -0
- package/src/registry.ts +55 -3
- package/src/request-receipts.ts +107 -0
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2292 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +172 -6
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1351 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +159 -27
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +9 -6
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +12 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +5 -4
- package/types/src/job-leases.d.ts +40 -5
- package/types/src/job-wakes.d.ts +124 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/list-cursor.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +4 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +163 -71
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +351 -23
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +40 -3
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/sql.d.ts +29 -21
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +18 -2
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +169 -22
|
@@ -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
|
+
}
|