@workbasehq/core 0.0.0 → 0.1.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.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/package.json +62 -1
- package/src/account-provisioning.ts +24 -0
- package/src/admin-forms.tsx +70 -0
- package/src/admin-security.ts +63 -0
- package/src/admin-ui.tsx +438 -0
- package/src/app.ts +115 -0
- package/src/audit-event.ts +23 -0
- package/src/audit.ts +35 -0
- package/src/authorization-api.ts +3 -0
- package/src/authorization.ts +135 -0
- package/src/authorized-execution.ts +15 -0
- package/src/bootstrap.ts +23 -0
- package/src/browser-dispatch.ts +19 -0
- package/src/calendar-display.ts +26 -0
- package/src/capabilities.ts +41 -0
- package/src/commands.ts +46 -0
- package/src/config-proposal-contract.ts +16 -0
- package/src/config-proposals.ts +95 -0
- package/src/config-shorthand.ts +200 -0
- package/src/config.ts +286 -0
- package/src/console-contract.ts +105 -0
- package/src/console-contribution.ts +7 -0
- package/src/console.ts +84 -0
- package/src/crud.ts +146 -0
- package/src/crypto.ts +33 -0
- package/src/detail-display.ts +59 -0
- package/src/detail-layout.ts +17 -0
- package/src/errors.ts +3 -0
- package/src/event-delivery.ts +76 -0
- package/src/events.ts +11 -0
- package/src/execution/atomic.ts +70 -0
- package/src/execution/compiler.ts +55 -0
- package/src/execution/context.ts +100 -0
- package/src/execution/d1.ts +46 -0
- package/src/execution/errors.ts +21 -0
- package/src/execution/index.ts +6 -0
- package/src/execution/outbox.ts +8 -0
- package/src/execution/permissions.ts +9 -0
- package/src/execution/policies.ts +156 -0
- package/src/execution/policy-receipts.ts +51 -0
- package/src/execution/types.ts +74 -0
- package/src/execution/workflow.ts +35 -0
- package/src/form-display.ts +36 -0
- package/src/idempotency.ts +10 -0
- package/src/identity-providers.ts +2 -0
- package/src/identity-settings.ts +42 -0
- package/src/identity-writes.ts +14 -0
- package/src/identity.ts +2 -0
- package/src/index.ts +32 -0
- package/src/json-csrf.ts +22 -0
- package/src/kernel/adapters/tenancy.ts +8 -0
- package/src/kernel/audit-port.ts +12 -0
- package/src/kernel/builtin-source.ts +34 -0
- package/src/kernel/composition.ts +77 -0
- package/src/kernel/identity-port.ts +36 -0
- package/src/kernel/mail-outbox-port.ts +23 -0
- package/src/kernel/migrations.ts +76 -0
- package/src/kernel/operation-name.ts +8 -0
- package/src/kernel/plugin.ts +92 -0
- package/src/kernel/predicate.ts +69 -0
- package/src/kernel/registry.ts +198 -0
- package/src/kernel/schema-check.ts +13 -0
- package/src/logging.ts +21 -0
- package/src/migrations/better-auth-schema.ts +2 -0
- package/src/migrations/email-delivery-schema.ts +6 -0
- package/src/migrations/planner.ts +77 -0
- package/src/migrations/schema.ts +85 -0
- package/src/migrations/version-contract.ts +28 -0
- package/src/migrations/versions.ts +90 -0
- package/src/ops-schema.ts +13 -0
- package/src/ops.ts +34 -0
- package/src/organization-permissions.ts +13 -0
- package/src/organization-rules.ts +25 -0
- package/src/organizations.ts +43 -0
- package/src/plugins.ts +3 -0
- package/src/policies.ts +186 -0
- package/src/public-schema.ts +65 -0
- package/src/query/compiler.ts +395 -0
- package/src/query/cursor.ts +43 -0
- package/src/query/enum-labels.json +16 -0
- package/src/query/enum-values.ts +7 -0
- package/src/query/errors.ts +10 -0
- package/src/query/executor.ts +118 -0
- package/src/query/facade.ts +34 -0
- package/src/query/labels.ts +105 -0
- package/src/query/limits.ts +8 -0
- package/src/query/observability.ts +49 -0
- package/src/query/routes.ts +60 -0
- package/src/query/schema.ts +216 -0
- package/src/query/search.ts +15 -0
- package/src/relay.ts +65 -0
- package/src/request-timing.ts +71 -0
- package/src/schema-compatibility.ts +43 -0
- package/src/scope-operations.ts +3 -0
- package/src/session-contract.ts +1 -0
- package/src/types.ts +30 -0
- package/src/worker-fetch.ts +14 -0
- package/src/workflow-routes.ts +156 -0
- package/src/workflow-runtime.ts +327 -0
- package/src/workflow-schema.ts +29 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { ApiError } from '../errors'
|
|
2
|
+
import { Q, Fn, Cond, Condition, toSQLWithBindings, type ISQLFlavor, type SelectQuery, type ISequelizable } from '@jakub.knejzlik/ts-query'
|
|
3
|
+
import { renderPredicate, type KernelRegistry } from '../kernel/registry'
|
|
4
|
+
import type { TableFilter, PredicateRelation } from '../kernel/plugin'
|
|
5
|
+
import type { Scope } from '../types'
|
|
6
|
+
import { quote } from '../migrations/schema'
|
|
7
|
+
import { QueryError } from './errors'
|
|
8
|
+
import { enumLabel, valueLabel } from './search'
|
|
9
|
+
import { fieldType } from './schema'
|
|
10
|
+
import { labelPath, readableLabel, labelAlias, sortAlias, expressionColumns, expressionPath, readableDisplay } from './labels'
|
|
11
|
+
import { QUERY_LIMITS as L, type QueryCost } from './limits'
|
|
12
|
+
import type { Filter, Order, QueryInput, Scalar, Selection } from './schema'
|
|
13
|
+
|
|
14
|
+
export interface Statement { sql: string; bindings: unknown[] }
|
|
15
|
+
const col = (alias: string, field: string) => `${alias}.${field}`
|
|
16
|
+
const value = (v: unknown) => Q.value((typeof v === 'boolean' ? Number(v) : v) as Scalar)
|
|
17
|
+
const truth = (yes: boolean) => Cond.equal(Q.raw('1'), Q.raw(yes ? '1' : '0'))
|
|
18
|
+
/** Trusted structured predicates render in the library's binding context, never as raw question marks. */
|
|
19
|
+
class ScopedCondition extends Condition {
|
|
20
|
+
constructor(private filter: TableFilter, private alias: string, private relation?: (node: PredicateRelation, parent: string, negated: boolean, flavor: ISQLFlavor) => string) { super() }
|
|
21
|
+
toSQL(flavor: ISQLFlavor) {
|
|
22
|
+
return this.filter.kind === 'where'
|
|
23
|
+
? renderPredicate(this.filter.predicate, this.alias, v => value(v).toSQL(flavor), this.relation ? (node, parent, negated) => this.relation!(node, parent, negated, flavor) : undefined).sql
|
|
24
|
+
: this.filter.kind === 'all' ? '1=1' : '0=1'
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class ContainsCondition extends Condition {
|
|
28
|
+
constructor(private column: string, private text: string, private insensitive = false) { super() }
|
|
29
|
+
toSQL(flavor: ISQLFlavor) {
|
|
30
|
+
const column = flavor.escapeColumn(this.column)
|
|
31
|
+
if (!this.insensitive) return `instr(${column}, ${value(this.text).toSQL(flavor)}) > 0`
|
|
32
|
+
// SQLite lower() folds ASCII. Fold the searched Unicode letters explicitly as
|
|
33
|
+
// bound replacements too, so Czech names match either casing on D1.
|
|
34
|
+
let folded = `lower(CAST(${column} AS TEXT))`
|
|
35
|
+
for (const letter of new Set(this.text.toLowerCase())) {
|
|
36
|
+
const upper = letter.toUpperCase()
|
|
37
|
+
if (upper !== letter && /[^\x00-\x7f]/.test(upper)) folded = `replace(${folded}, ${value(upper).toSQL(flavor)}, ${value(letter).toSQL(flavor)})`
|
|
38
|
+
}
|
|
39
|
+
return `instr(${folded}, ${value(this.text.toLowerCase()).toSQL(flavor)}) > 0`
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** D1 binds JS numbers as REAL; preserve that storage type and leave JSON text untouched. NULL is handled by the caller. */
|
|
43
|
+
class InCondition extends Condition {
|
|
44
|
+
constructor(private column: string, private values: Scalar[]) { super() }
|
|
45
|
+
toSQL(flavor: ISQLFlavor) {
|
|
46
|
+
return `${flavor.escapeColumn(this.column)} IN (SELECT CASE WHEN type IN ('integer', 'real') THEN CAST(value AS REAL) ELSE value END FROM json_each(${value(JSON.stringify(this.values.map(v => typeof v === 'boolean' ? Number(v) : v))).toSQL(flavor)}))`
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The library assigns numbered placeholders; compact equal typed parameters afterwards.
|
|
50
|
+
* Values never enter SQL text. Reusing a search term across columns costs one binding,
|
|
51
|
+
* including when policy scopes repeat the same constants in visibility probes.
|
|
52
|
+
*/
|
|
53
|
+
function compactBindings(statement: Statement): Statement {
|
|
54
|
+
const bindings: unknown[] = []; const indices = new Map<unknown, number>()
|
|
55
|
+
// Trusted label literals can contain text such as '?1'; only rewrite placeholders
|
|
56
|
+
// outside SQLite string/identifier quotes.
|
|
57
|
+
const sql = statement.sql.replace(/'(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`|\?(\d+)/g, (token, index: string | undefined) => {
|
|
58
|
+
if (!index) return token
|
|
59
|
+
const operand = statement.bindings[Number(index) - 1]
|
|
60
|
+
if (!indices.has(operand)) { bindings.push(operand); indices.set(operand, bindings.length) }
|
|
61
|
+
return `?${indices.get(operand)}`
|
|
62
|
+
})
|
|
63
|
+
return { sql, bindings }
|
|
64
|
+
}
|
|
65
|
+
const numberedSQLite: ISQLFlavor = Object.assign(Object.create(Q.flavors.sqlite), { parameterPlaceholder: (index: number) => `?${index}` })
|
|
66
|
+
/** One visibility check per readable FK in the scoped source, shared by all uses. */
|
|
67
|
+
class ForeignKeyValue extends Condition {
|
|
68
|
+
constructor(private column: string, private visible: Condition) { super() }
|
|
69
|
+
toSQL(flavor: ISQLFlavor) {
|
|
70
|
+
return `CASE WHEN ${this.visible.toSQL(flavor)} THEN ${flavor.escapeColumn(this.column)} ELSE NULL END`
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Rebind trusted expression column operands to scoped source/join aliases. */
|
|
74
|
+
class DisplayExpression extends Condition {
|
|
75
|
+
constructor(private expression: ISequelizable, private columns: Map<string, string | Condition>, private visible: Condition[]) { super() }
|
|
76
|
+
toSQL(flavor: ISQLFlavor) {
|
|
77
|
+
const qualified = Object.assign(Object.create(flavor), {
|
|
78
|
+
// Fixed schema constants must match expression-index SQL. Request and policy
|
|
79
|
+
// values still render with the original binding flavor, including FK probes.
|
|
80
|
+
escapeValue: Q.flavors.sqlite.escapeValue.bind(Q.flavors.sqlite),
|
|
81
|
+
escapeRawValue: Q.flavors.sqlite.escapeRawValue.bind(Q.flavors.sqlite),
|
|
82
|
+
bindValue: undefined,
|
|
83
|
+
escapeColumn: (column: string, legacy?: boolean) => {
|
|
84
|
+
const mapped = this.columns.get(column)
|
|
85
|
+
if (!mapped) return flavor.escapeColumn(column, legacy)
|
|
86
|
+
return typeof mapped === 'string' ? flavor.escapeColumn(mapped) : mapped.toSQL(flavor)
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
const sql = this.expression.toSQL(qualified)
|
|
90
|
+
return this.visible.length ? `CASE WHEN ${Cond.and(this.visible).toSQL(flavor)} THEN ${sql} ELSE NULL END` : sql
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function queryOrder(node: Selection): Order[] {
|
|
94
|
+
const order = node.orderBy ?? []
|
|
95
|
+
return order.some(o => o.field === 'id') ? order : [...order, { field: 'id', dir: 'asc' }]
|
|
96
|
+
}
|
|
97
|
+
export function selectedFields(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection) {
|
|
98
|
+
return node.select ?? kernel.fieldMask(entity, scope)
|
|
99
|
+
}
|
|
100
|
+
/** Includes and cursors may need unprojected columns; all are still checked against fieldMask. */
|
|
101
|
+
export function executionFields(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection, extra: string[] = []) {
|
|
102
|
+
return [...new Set([...selectedFields(kernel, scope, entity, node), ...queryOrder(node).filter(o => !o.label).map(o => o.field), ...extra,
|
|
103
|
+
...Object.keys(node.include ?? {}).map(relation => {
|
|
104
|
+
const r = kernel.config.entities[entity].relations[relation]
|
|
105
|
+
return r.many ? 'id' : r.field
|
|
106
|
+
})])]
|
|
107
|
+
}
|
|
108
|
+
/** Internal AST only. All caller input has passed validateQuery before reaching this module. */
|
|
109
|
+
function filterCondition(kernel: KernelRegistry, entity: string, alias: string, filter: Filter, nextAlias: () => string, positive = true): Condition {
|
|
110
|
+
const conditions: Condition[] = []
|
|
111
|
+
for (const [key, v] of Object.entries(filter)) {
|
|
112
|
+
if (key === 'and' || key === 'or') {
|
|
113
|
+
const children = (v as Filter[]).map(child => filterCondition(kernel, entity, alias, child, nextAlias, positive))
|
|
114
|
+
conditions.push(key === 'and' ? Cond.and(children) : Cond.or(children)); continue
|
|
115
|
+
}
|
|
116
|
+
if (key === 'not') { conditions.push(Cond.not(filterCondition(kernel, entity, alias, v as Filter, nextAlias, false))); continue }
|
|
117
|
+
if (Object.hasOwn(kernel.config.entities[entity].relations, key)) {
|
|
118
|
+
const r = kernel.config.entities[entity].relations[key]; const childAlias = nextAlias()
|
|
119
|
+
const quantifier = r.many ? Object.keys(v as object)[0] : undefined
|
|
120
|
+
const sub = Q.select().from(r.entity, childAlias).field(col(childAlias, 'id'))
|
|
121
|
+
.where(Cond.columnEqual(col(childAlias, r.many ? r.field : 'id'), col(alias, r.many ? 'id' : r.field)))
|
|
122
|
+
.where(filterCondition(kernel, r.entity, childAlias, (quantifier ? (v as Record<string, Filter>)[quantifier] : v) as Filter, nextAlias))
|
|
123
|
+
conditions.push(quantifier === 'none' ? Cond.notExists(sub) : Cond.exists(sub)); continue
|
|
124
|
+
}
|
|
125
|
+
const column = col(alias, key)
|
|
126
|
+
for (const [op, operand] of Object.entries(v as object)) {
|
|
127
|
+
// Keep the masked predicate as the visibility residual. A redundant raw seek
|
|
128
|
+
// is safe only in positive contexts: FALSE versus UNKNOWN matters under NOT.
|
|
129
|
+
if (positive && kernel.config.entities[entity].fields[key]?.type === 'ref') {
|
|
130
|
+
if (op === 'eq' && operand !== null) conditions.push(Cond.equal(col(alias, `__raw_${key}`), value(operand)))
|
|
131
|
+
if (op === 'in' && (operand as Scalar[]).length && !(operand as Scalar[]).includes(null)) conditions.push(new InCondition(col(alias, `__raw_${key}`), operand as Scalar[]))
|
|
132
|
+
}
|
|
133
|
+
const methods = { eq: Cond.equal, ne: Cond.notEqual, lt: Cond.lessThan, lte: Cond.lessThanOrEqual, gt: Cond.greaterThan, gte: Cond.greaterThanOrEqual }
|
|
134
|
+
if (op === 'isNull') conditions.push(operand ? Cond.isNull(column) : Cond.isNotNull(column))
|
|
135
|
+
else if ((op === 'eq' || op === 'ne') && operand === null) conditions.push(op === 'eq' ? Cond.isNull(column) : Cond.isNotNull(column))
|
|
136
|
+
else if (op === 'in') {
|
|
137
|
+
const list = operand as Scalar[]; const nonnull = list.filter(x => x !== null)
|
|
138
|
+
conditions.push(list.length ? Cond.or([...(nonnull.length ? [new InCondition(column, nonnull)] : []), ...(list.includes(null) ? [Cond.isNull(column)] : [])]) : truth(false))
|
|
139
|
+
} else if (op === 'contains') conditions.push(new ContainsCondition(column, operand as string))
|
|
140
|
+
else conditions.push(methods[op as keyof typeof methods](column, value(operand)))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return conditions.length ? Cond.and(conditions) : truth(true)
|
|
144
|
+
}
|
|
145
|
+
function hasRequiredForeignKeySeek(kernel: KernelRegistry, entity: string, filter: Filter): boolean {
|
|
146
|
+
return Object.entries(filter).some(([field, operand]) => {
|
|
147
|
+
if (field === 'and') return (operand as Filter[]).some(child => hasRequiredForeignKeySeek(kernel, entity, child))
|
|
148
|
+
if (kernel.config.entities[entity].fields[field]?.type !== 'ref') return false
|
|
149
|
+
const operators = operand as Record<string, unknown>
|
|
150
|
+
return (Object.hasOwn(operators, 'eq') && operators.eq !== null)
|
|
151
|
+
|| (Array.isArray(operators.in) && operators.in.length > 0 && !operators.in.includes(null))
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
/** SQLite NULLs sort first ascending, last descending. Build a lexicographic seek with the same semantics. */
|
|
155
|
+
function seek(alias: string, order: Order[], keys: Scalar[], preferForeignKeySeek = false) {
|
|
156
|
+
return Cond.or(order.map((o, index) => {
|
|
157
|
+
const column = preferForeignKeySeek ? Q.raw(`+${quote(alias)}.${quote(o.field)}`) : col(alias, o.field)
|
|
158
|
+
const key = keys[index]
|
|
159
|
+
const after = key === null ? o.dir === 'asc' ? Cond.isNotNull(column) : truth(false)
|
|
160
|
+
: o.dir === 'asc' ? Cond.greaterThan(column, value(key)) : Cond.or([Cond.lessThan(column, value(key)), Cond.isNull(column)])
|
|
161
|
+
return Cond.and([...order.slice(0, index).map((prev, i) => keys[i] === null ? Cond.isNull(col(alias, prev.field)) : Cond.equal(col(alias, prev.field), value(keys[i]))), after])
|
|
162
|
+
}))
|
|
163
|
+
}
|
|
164
|
+
export function assertStatement(statement: Statement, cost?: QueryCost) {
|
|
165
|
+
if (statement.bindings.length > L.bindings || statement.sql.length > L.sqlLength) throw new QueryError('query_too_expensive', [], cost)
|
|
166
|
+
return statement
|
|
167
|
+
}
|
|
168
|
+
export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: SelectQuery, cost?: QueryCost, requiredDisplays?: Set<string>): Statement {
|
|
169
|
+
let references = 0
|
|
170
|
+
function scopedTable(entity: string, extra?: (alias: string) => Condition, maskForeignKeys = true, displayRequired = requiredDisplays?.has(entity) ?? true): SelectQuery {
|
|
171
|
+
if (!Object.hasOwn(kernel.config.entities, entity)) throw new QueryError('query_invalid')
|
|
172
|
+
// Bounds policy recursion too, before a future policy compiler can introduce cycles.
|
|
173
|
+
if (++references > L.tableReferences) throw new QueryError('query_too_expensive', [], cost)
|
|
174
|
+
const alias = `scope_row_${references}`
|
|
175
|
+
let query = Q.select().from(entity, alias)
|
|
176
|
+
const fields = kernel.fieldMask(entity, scope)
|
|
177
|
+
const definition = kernel.config.entities[entity]
|
|
178
|
+
const foreignKeys = new Map<string, Condition>()
|
|
179
|
+
function foreignKeyVisible(field: string) {
|
|
180
|
+
let visible = foreignKeys.get(field)
|
|
181
|
+
if (!visible) {
|
|
182
|
+
const target = definition.fields[field].entity!
|
|
183
|
+
visible = kernel.fieldMask(target, scope).includes('id') && kernel.can(scope, target, 'read')
|
|
184
|
+
? Cond.exists(scopedTable(target, child => Cond.columnEqual(col(child, 'id'), col(alias, field)), false))
|
|
185
|
+
: truth(false)
|
|
186
|
+
foreignKeys.set(field, visible)
|
|
187
|
+
}
|
|
188
|
+
return visible
|
|
189
|
+
}
|
|
190
|
+
const joins = new Map<string, string>()
|
|
191
|
+
let label: Condition | ReturnType<typeof Q.raw> = Q.raw('NULL')
|
|
192
|
+
const labelVisibility: Condition[] = []
|
|
193
|
+
if (maskForeignKeys && displayRequired && definition.label && readableDisplay(kernel, scope, entity)) {
|
|
194
|
+
const columns = new Map<string, string | Condition>()
|
|
195
|
+
// A joined source computes its own label only when this expression consumes
|
|
196
|
+
// it. Passing through an account to its customer does not need account.label.
|
|
197
|
+
const displayJoins = new Set(expressionColumns(definition.label).flatMap(column => {
|
|
198
|
+
const path = expressionPath(kernel.config, entity, column)
|
|
199
|
+
return path.field === kernel.config.entities[path.entity].displayField ? [`/${path.hops.map(hop => hop.field).join('/')}`] : []
|
|
200
|
+
}))
|
|
201
|
+
for (const column of expressionColumns(definition.label)) {
|
|
202
|
+
const path = expressionPath(kernel.config, entity, column)
|
|
203
|
+
let parent = alias; let key = ''
|
|
204
|
+
for (const hop of path.hops) {
|
|
205
|
+
key += `/${hop.field}`
|
|
206
|
+
let target = joins.get(key)
|
|
207
|
+
if (!target) {
|
|
208
|
+
const table = scopedTable(hop.target, undefined, true, displayJoins.has(key))
|
|
209
|
+
target = `expression_join_${references}_${joins.size}`; joins.set(key, target)
|
|
210
|
+
query = query.leftJoin(Q.table(table, target), Cond.columnEqual(col(target, 'id'), col(parent, hop.field)))
|
|
211
|
+
}
|
|
212
|
+
parent = target
|
|
213
|
+
}
|
|
214
|
+
if (parent === alias && definition.fields[path.field]?.type === 'ref') {
|
|
215
|
+
const visible = foreignKeyVisible(path.field)
|
|
216
|
+
columns.set(column, new ForeignKeyValue(col(alias, path.field), visible))
|
|
217
|
+
labelVisibility.push(Cond.or([Cond.isNull(col(alias, path.field)), visible]))
|
|
218
|
+
} else {
|
|
219
|
+
columns.set(column, col(parent, path.field))
|
|
220
|
+
const target = kernel.config.entities[path.entity]
|
|
221
|
+
// A raw NULL is ordinary absence; a non-null raw FK masked to NULL is
|
|
222
|
+
// denied. Reuse its masked projection rather than add another EXISTS.
|
|
223
|
+
if (target.fields[path.field]?.type === 'ref') labelVisibility.push(Cond.or([
|
|
224
|
+
Cond.isNull(col(parent, `__raw_${path.field}`)), Cond.isNotNull(col(parent, path.field)),
|
|
225
|
+
]))
|
|
226
|
+
if (target.label && path.field === target.displayField) labelVisibility.push(Cond.equal(col(parent, '__display_visible'), Q.raw('1')))
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
labelVisibility.push(...[...joins.values()].map(target => Cond.isNotNull(col(target, 'id'))))
|
|
230
|
+
label = new DisplayExpression(definition.label, columns, labelVisibility)
|
|
231
|
+
}
|
|
232
|
+
if (maskForeignKeys && displayRequired && definition.label) query = query.field(readableDisplay(kernel, scope, entity) ? labelVisibility.length ? Cond.and(labelVisibility) : Q.raw('1') : Q.raw('0'), '__display_visible')
|
|
233
|
+
for (const field of fields) {
|
|
234
|
+
const definition = kernel.config.entities[entity].fields[field]
|
|
235
|
+
if (kernel.config.entities[entity].label && field === kernel.config.entities[entity].displayField) {
|
|
236
|
+
query = query.field(label, field)
|
|
237
|
+
} else if (maskForeignKeys && definition?.type === 'ref') {
|
|
238
|
+
// Target identity must itself be readable. Visibility probes expose no target
|
|
239
|
+
// columns and use the same scoped predicate/correlation as policy relations.
|
|
240
|
+
const visible = foreignKeyVisible(field)
|
|
241
|
+
query = query.field(new ForeignKeyValue(col(alias, field), visible), field)
|
|
242
|
+
.field(col(alias, field), `__raw_${field}`)
|
|
243
|
+
} else query = query.field(col(alias, field))
|
|
244
|
+
}
|
|
245
|
+
if (!fields.length) query = query.field(Q.raw('1'), '__policy_row')
|
|
246
|
+
query = query.where(new ScopedCondition(kernel.tableFilter(entity, scope), alias, relation))
|
|
247
|
+
return extra ? query.where(extra(alias)) : query
|
|
248
|
+
}
|
|
249
|
+
function relation(node: PredicateRelation, parent: string, negated: boolean, flavor: ISQLFlavor): string {
|
|
250
|
+
const sub = Q.select().from(node.entity, 'policy_target').field(Q.raw('1'))
|
|
251
|
+
// Correlation and policy-only fields stay inside the scoped source, even if those
|
|
252
|
+
// fields are unreadable. The derived table still exposes readable columns only.
|
|
253
|
+
const condition = negated ? Cond.notExists(sub) : Cond.exists(sub)
|
|
254
|
+
return condition.toSQL(flavor, { transformTable: entity => scopedTable(entity, alias => Cond.and([
|
|
255
|
+
Cond.columnEqual(col(alias, node.field), col(parent, node.parentField)),
|
|
256
|
+
new ScopedCondition({ kind: 'where', predicate: node.predicate }, alias, relation),
|
|
257
|
+
]), false) })
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
return assertStatement(compactBindings(toSQLWithBindings(ast, numberedSQLite, { transformTable: entity => scopedTable(entity) })), cost)
|
|
261
|
+
} catch (error) { if (error instanceof ApiError) throw error; throw new QueryError('query_invalid') }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Expression demand follows all user-visible uses, including unprojected filters.
|
|
265
|
+
* The table transform is entity-based; multiple occurrences share this conservative
|
|
266
|
+
* union. Expression-internal joins carry their own narrower dependency demand.
|
|
267
|
+
*/
|
|
268
|
+
function requiredDisplayEntities(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection) {
|
|
269
|
+
const required = new Set<string>()
|
|
270
|
+
const add = (source: string, fields: string[]) => {
|
|
271
|
+
if (fields.includes(kernel.config.entities[source].displayField!)) required.add(source)
|
|
272
|
+
}
|
|
273
|
+
const filter = (source: string, where: Filter) => {
|
|
274
|
+
for (const [field, operand] of Object.entries(where)) {
|
|
275
|
+
if (field === 'and' || field === 'or') { (operand as Filter[]).forEach(child => filter(source, child)); continue }
|
|
276
|
+
if (field === 'not') { filter(source, operand as Filter); continue }
|
|
277
|
+
const relation = kernel.config.entities[source].relations[field]
|
|
278
|
+
if (relation) filter(relation.entity, relation.many ? Object.values(operand as Record<string, Filter>)[0] : operand as Filter)
|
|
279
|
+
else add(source, [field])
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
add(entity, executionFields(kernel, scope, entity, node))
|
|
283
|
+
if (node.where) filter(entity, node.where)
|
|
284
|
+
return { required, add }
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function buildSelect(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection, extras: string[] = []) {
|
|
288
|
+
let aliases = 0
|
|
289
|
+
let ast = Q.select().from(entity, 'row')
|
|
290
|
+
for (const field of executionFields(kernel, scope, entity, node, [...extras, ...((node as QueryInput).search?.fields ?? []).filter(key => !labelPath(kernel.config, entity, key))])) ast = ast.field(col('row', field))
|
|
291
|
+
if (node.where) ast = ast.where(filterCondition(kernel, entity, 'row', node.where, () => `rel_${++aliases}`))
|
|
292
|
+
return ast
|
|
293
|
+
}
|
|
294
|
+
/** Server-configured aggregate over the same masked, policy-scoped source as lists.
|
|
295
|
+
* No LIMIT on the input: a stat covers all authorized matching rows in one statement.
|
|
296
|
+
*/
|
|
297
|
+
export function compileAggregate(kernel: KernelRegistry, scope: Scope, query: QueryInput,
|
|
298
|
+
aggregate: { fn: 'count' } | { fn: 'sum'; field: string }, cost?: QueryCost) {
|
|
299
|
+
const source = buildSelect(kernel, scope, query.entity, query)
|
|
300
|
+
const expression = aggregate.fn === 'count' ? Fn.count('aggregate_row.id') : Fn.sum(col('aggregate_row', aggregate.field))
|
|
301
|
+
const ast = Q.select().from(source, 'aggregate_row').field(expression, 'value').field(Fn.count('aggregate_row.id'), '__count')
|
|
302
|
+
return renderStatement(kernel, scope, ast, cost, requiredDisplayEntities(kernel, scope, query.entity, query).required)
|
|
303
|
+
}
|
|
304
|
+
/** All values come from scoped joined tables; a missing/denied target has one safe marker. */
|
|
305
|
+
class LabelObject extends Condition {
|
|
306
|
+
constructor(private entity: string, private alias: string, private display?: string) { super() }
|
|
307
|
+
toSQL(flavor: ISQLFlavor) {
|
|
308
|
+
const id = flavor.escapeColumn(col(this.alias, 'id'))
|
|
309
|
+
const display = this.display ? value(this.display).toSQL(flavor) : 'NULL'
|
|
310
|
+
const row = this.display ? `json_object('id', ${id}, ${display}, ${flavor.escapeColumn(col(this.alias, this.display))})` : `json_object('id', ${id})`
|
|
311
|
+
return `CASE WHEN ${id} IS NULL THEN json_object('status', 'unavailable') ELSE json_object('status', 'ok', 'entity', ${value(this.entity).toSQL(flavor)}, 'display', ${display}, 'row', ${row}) END`
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
export const orderField = (order: Order) => order.label ? sortAlias(order.field) : order.field
|
|
315
|
+
export function compileRoot(kernel: KernelRegistry, scope: Scope, query: QueryInput, keys?: Scalar[], cost?: QueryCost) {
|
|
316
|
+
let source = buildSelect(kernel, scope, query.entity, query)
|
|
317
|
+
const { required, add } = requiredDisplayEntities(kernel, scope, query.entity, query)
|
|
318
|
+
add(query.entity, query.search?.fields ?? [])
|
|
319
|
+
const labelKeys = [...new Set([...(query.labels ?? []), ...Object.keys(query.labelWhere ?? {}), ...(query.search?.fields ?? []).filter(key => labelPath(kernel.config, query.entity, key)), ...(query.orderBy ?? []).filter(o => o.label).map(o => o.field)])]
|
|
320
|
+
const joins = new Map<string, string>()
|
|
321
|
+
for (const key of labelKeys) {
|
|
322
|
+
const plan = readableLabel(kernel, scope, query.entity, key)
|
|
323
|
+
let expression: Condition | ReturnType<typeof Q.raw> = Q.raw("json_object('status', 'unavailable')")
|
|
324
|
+
if (plan) {
|
|
325
|
+
if (plan.display) add(plan.entity, [plan.display])
|
|
326
|
+
let parent = 'row'; let path = ''
|
|
327
|
+
for (const hop of plan.hops) {
|
|
328
|
+
path += `/${hop.field}`
|
|
329
|
+
let alias = joins.get(path)
|
|
330
|
+
if (!alias) {
|
|
331
|
+
alias = `label_join_${joins.size}`; joins.set(path, alias)
|
|
332
|
+
source = source.leftJoin(Q.table(hop.target, alias), Cond.columnEqual(col(alias, 'id'), col(parent, hop.field)))
|
|
333
|
+
}
|
|
334
|
+
parent = alias
|
|
335
|
+
}
|
|
336
|
+
expression = new LabelObject(plan.entity, parent, plan.display)
|
|
337
|
+
}
|
|
338
|
+
source = source.field(expression, labelAlias(key))
|
|
339
|
+
}
|
|
340
|
+
// Compute the key once from the authorized label, then seek/order/limit over it.
|
|
341
|
+
let labeled = Q.select().from(source, 'labels').field('labels.*')
|
|
342
|
+
for (const key of labelKeys) {
|
|
343
|
+
const display = readableLabel(kernel, scope, query.entity, key)?.display
|
|
344
|
+
labeled = labeled.field(display ? Q.raw(`json_extract(${quote('labels')}."${labelAlias(key)}", '$.row.${display}')`) : Q.raw('NULL'), sortAlias(key))
|
|
345
|
+
}
|
|
346
|
+
let ast = labelKeys.length ? Q.select().from(labeled, 'row').field('row.*') : source
|
|
347
|
+
const order = queryOrder(query).map(o => ({ ...o, field: orderField(o) }))
|
|
348
|
+
function searchCondition(key: string, text: string, locale: 'en' | 'cs' = 'en'): Condition {
|
|
349
|
+
const path = labelPath(kernel.config, query.entity, key)
|
|
350
|
+
const plan = path ? readableLabel(kernel, scope, query.entity, key) : undefined
|
|
351
|
+
if (path && !plan?.display) return truth(false)
|
|
352
|
+
const entity = plan?.entity ?? query.entity; const field = plan?.display ?? key
|
|
353
|
+
const column = col('row', path ? sortAlias(key) : key)
|
|
354
|
+
const spec = kernel.config.entities[entity].fields[field]
|
|
355
|
+
const needle = text.toLocaleLowerCase(locale)
|
|
356
|
+
if (spec?.type === 'enum') {
|
|
357
|
+
const matches = (spec.values ?? []).filter(code => enumLabel(kernel.config, entity, code, locale).toLocaleLowerCase(locale).includes(needle))
|
|
358
|
+
return matches.length ? new InCondition(column, matches) : truth(false)
|
|
359
|
+
}
|
|
360
|
+
if (fieldType(kernel.config, entity, field) === 'bool') {
|
|
361
|
+
const matches = [
|
|
362
|
+
...(valueLabel(kernel.config, entity, 'yes', locale).toLocaleLowerCase(locale).includes(needle) ? [Cond.equal(column, value(true))] : []),
|
|
363
|
+
...(valueLabel(kernel.config, entity, 'no', locale).toLocaleLowerCase(locale).includes(needle) ? [Cond.equal(column, value(false))] : []),
|
|
364
|
+
]
|
|
365
|
+
return matches.length ? Cond.or(matches) : truth(false)
|
|
366
|
+
}
|
|
367
|
+
return new ContainsCondition(column, text, true)
|
|
368
|
+
}
|
|
369
|
+
for (const [key, predicate] of Object.entries(query.labelWhere ?? {})) ast = ast.where(searchCondition(key, predicate.contains, predicate.locale))
|
|
370
|
+
if (query.search) ast = ast.where(Cond.or(query.search.fields.map(key => searchCondition(key, query.search!.text, query.search!.locale))))
|
|
371
|
+
// With ID-only ordering and LIMIT, SQLite otherwise prefers scanning the tenant's
|
|
372
|
+
// (org,id) index even over a selective raw FK predicate. Unary + preserves the
|
|
373
|
+
// value/order but leaves index selection to the FK filter; sort its candidates.
|
|
374
|
+
// Apply it to cursor comparisons too, so later pages keep the selective index.
|
|
375
|
+
// Do not steer disjunctions, negative filters or other indexed ordering shapes.
|
|
376
|
+
const preferForeignKeySeek = order.length === 1 && order[0].field === 'id' && !!query.where && hasRequiredForeignKeySeek(kernel, query.entity, query.where)
|
|
377
|
+
if (keys) ast = ast.where(seek('row', order, keys, preferForeignKeySeek))
|
|
378
|
+
for (const o of order) ast = ast.orderBy(preferForeignKeySeek ? Q.raw(`+${quote('row')}.${quote(o.field)}`) : col('row', o.field), o.dir.toUpperCase() as 'ASC' | 'DESC')
|
|
379
|
+
ast = ast.limit(query.limit + 1)
|
|
380
|
+
return { ast, ...renderStatement(kernel, scope, ast, cost, required) }
|
|
381
|
+
}
|
|
382
|
+
/** One statement per relation per level; all parent identities share one JSON binding. */
|
|
383
|
+
export function compileInclude(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection, parentField: string, ids: Scalar[], cost?: QueryCost) {
|
|
384
|
+
let child = buildSelect(kernel, scope, entity, node, [parentField])
|
|
385
|
+
child = child.where(ids.length ? new InCondition(col('row', parentField), ids) : truth(false))
|
|
386
|
+
if (kernel.config.entities[entity].fields[parentField]?.type === 'ref' && ids.length && !ids.includes(null)) child = child.where(new InCondition(col('row', `__raw_${parentField}`), ids))
|
|
387
|
+
const fields = executionFields(kernel, scope, entity, node, [parentField])
|
|
388
|
+
const sort = queryOrder(node).map(o => `${quote('row')}.${quote(o.field)} ${o.dir.toUpperCase()}`).join(', ')
|
|
389
|
+
child = child.field(Q.raw(`ROW_NUMBER() OVER (PARTITION BY ${quote('row')}.${quote(parentField)} ORDER BY ${sort})`), '__row_number')
|
|
390
|
+
let outer = Q.select().from(child, 'ranked')
|
|
391
|
+
for (const field of fields) outer = outer.field(col('ranked', field))
|
|
392
|
+
outer = outer.where(Cond.lessThanOrEqual('ranked.__row_number', value(node.limit ?? L.children)))
|
|
393
|
+
.orderBy('ranked.__row_number').orderBy(col('ranked', parentField))
|
|
394
|
+
return renderStatement(kernel, scope, outer, cost, requiredDisplayEntities(kernel, scope, entity, node).required)
|
|
395
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { equalHash, sha256, signWebhook } from '../crypto'
|
|
2
|
+
import type { InstanceConfig } from '../config'
|
|
3
|
+
import type { Scope } from '../types'
|
|
4
|
+
import { requestHash } from '../idempotency'
|
|
5
|
+
import { invalid, QueryError } from './errors'
|
|
6
|
+
import { QUERY_LIMITS as L } from './limits'
|
|
7
|
+
import { fieldType, type QueryInput, type Scalar } from './schema'
|
|
8
|
+
import { labelPath } from './labels'
|
|
9
|
+
import { queryOrder } from './compiler'
|
|
10
|
+
const encode = (text: string) => btoa(String.fromCharCode(...new TextEncoder().encode(text))).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
|
|
11
|
+
const decode = (text: string) => new TextDecoder('utf-8', { fatal: true, ignoreBOM: false }).decode(Uint8Array.from(atob(text.replaceAll('-', '+').replaceAll('_', '/')), c => c.charCodeAt(0)))
|
|
12
|
+
/** Rejected inputs get a digest too; JSON/driver text is never logged. */
|
|
13
|
+
export async function inputHash(input: unknown) {
|
|
14
|
+
let serialized = 'invalid-query'
|
|
15
|
+
try { serialized = JSON.stringify(input) ?? serialized } catch { /* cycles / excessive recursion */ }
|
|
16
|
+
return sha256(serialized)
|
|
17
|
+
}
|
|
18
|
+
export async function queryHash(config: InstanceConfig, scope: Scope, query: QueryInput) {
|
|
19
|
+
const { cursor: _cursor, ...shape } = query
|
|
20
|
+
return requestHash('query', { query: shape, revision: config.revision, organization: scope.organizationId, actor: scope.actor, role: scope.role })
|
|
21
|
+
}
|
|
22
|
+
export async function encodeCursor(secret: string, hash: string, revision: number, keys: Scalar[]) {
|
|
23
|
+
const payload = encode(JSON.stringify({ hash, revision, keys }))
|
|
24
|
+
if (payload.length + 65 > L.cursorBytes) throw new QueryError('query_too_expensive', ['cursor'])
|
|
25
|
+
return `${payload}.${await signWebhook(secret, `workbase-query-cursor-v1.${payload}`)}`
|
|
26
|
+
}
|
|
27
|
+
export async function decodeCursor(secret: string, hash: string, config: InstanceConfig, query: QueryInput): Promise<Scalar[] | undefined> {
|
|
28
|
+
if (!query.cursor) return undefined
|
|
29
|
+
try {
|
|
30
|
+
if (query.cursor.length > L.cursorBytes) invalid(['cursor'])
|
|
31
|
+
const [payload, signature, extra] = query.cursor.split('.')
|
|
32
|
+
if (extra !== undefined || !payload || !signature || !equalHash(signature, await signWebhook(secret, `workbase-query-cursor-v1.${payload}`))) invalid(['cursor'])
|
|
33
|
+
const data = JSON.parse(decode(payload))
|
|
34
|
+
const order = queryOrder(query)
|
|
35
|
+
if (data.hash !== hash || data.revision !== config.revision || !Array.isArray(data.keys) || data.keys.length !== order.length) invalid(['cursor'])
|
|
36
|
+
data.keys.forEach((key: unknown, i: number) => {
|
|
37
|
+
const path = order[i].label ? labelPath(config, query.entity, order[i].field) : undefined
|
|
38
|
+
const type = path?.display ? fieldType(config, path.entity, path.display) : fieldType(config, query.entity, order[i].field)
|
|
39
|
+
if (key !== null && (['int', 'float', 'bool'].includes(type) ? typeof key !== 'number' || !Number.isFinite(key) : typeof key !== 'string')) invalid(['cursor'])
|
|
40
|
+
})
|
|
41
|
+
return data.keys
|
|
42
|
+
} catch { return invalid(['cursor']) }
|
|
43
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"en": {
|
|
3
|
+
"value_owner": "Owner",
|
|
4
|
+
"value_dispatcher": "Member",
|
|
5
|
+
"value_viewer": "Viewer",
|
|
6
|
+
"value_administrator": "Administrator",
|
|
7
|
+
"value_unknown": "Unknown"
|
|
8
|
+
},
|
|
9
|
+
"cs": {
|
|
10
|
+
"value_owner": "Vlastník",
|
|
11
|
+
"value_dispatcher": "Člen",
|
|
12
|
+
"value_viewer": "Čtenář",
|
|
13
|
+
"value_administrator": "Správce",
|
|
14
|
+
"value_unknown": "Neznámá role"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Authoritative built-in roles, consumed by config's exhaustive runtime grant
|
|
2
|
+
* table and the release label guard. Instance roles remain configuration-owned. */
|
|
3
|
+
export const coreEnums = {
|
|
4
|
+
role: ['owner', 'dispatcher', 'viewer', 'administrator', 'unknown'],
|
|
5
|
+
} as const
|
|
6
|
+
|
|
7
|
+
export type CoreRole = typeof coreEnums.role[number]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ApiError } from '../errors'
|
|
2
|
+
import type { QueryCost } from './limits'
|
|
3
|
+
export type QueryCode = 'query_invalid' | 'field_forbidden' | 'relation_forbidden' | 'query_too_expensive' | 'entity_not_found' | 'policy_violation'
|
|
4
|
+
export type QueryPath = (string | number)[]
|
|
5
|
+
export class QueryError extends ApiError {
|
|
6
|
+
constructor(code: QueryCode, public path: QueryPath = [], public cost?: QueryCost) {
|
|
7
|
+
super(code === 'entity_not_found' ? 404 : code.endsWith('forbidden') || code === 'policy_violation' ? 403 : 400, code)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export const invalid = (path: QueryPath = []): never => { throw new QueryError('query_invalid', path) }
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ApiError } from '../errors'
|
|
2
|
+
import type { KernelRegistry } from '../kernel/registry'
|
|
3
|
+
import type { Scope } from '../types'
|
|
4
|
+
import { compileRoot, compileInclude, queryOrder, orderField, selectedFields, type Statement } from './compiler'
|
|
5
|
+
import { decodeCursor, encodeCursor, inputHash, queryHash } from './cursor'
|
|
6
|
+
import { QUERY_LIMITS as L, type QueryCost } from './limits'
|
|
7
|
+
import { labelAlias } from './labels'
|
|
8
|
+
import { QueryError, type QueryPath } from './errors'
|
|
9
|
+
import { validateQuery, type QueryInput, type Scalar, type Selection } from './schema'
|
|
10
|
+
export type Row = Record<string, any>
|
|
11
|
+
/** Shared projection for entity reads and server-configured dashboard queries. */
|
|
12
|
+
export function projectQueryRow(kernel: KernelRegistry, scope: Scope, entity: string, node: Selection, row: Row): Row {
|
|
13
|
+
return Object.fromEntries(selectedFields(kernel, scope, entity, node).map(field => [field,
|
|
14
|
+
kernel.config.entities[entity].fields[field]?.type === 'bool' && row[field] !== null ? Boolean(row[field]) : row[field]]))
|
|
15
|
+
}
|
|
16
|
+
export function projectRootRow(kernel: KernelRegistry, scope: Scope, query: QueryInput, row: Row): Row {
|
|
17
|
+
return { ...projectQueryRow(kernel, scope, query.entity, query, row), ...(query.labels ? {
|
|
18
|
+
__labels: Object.fromEntries(query.labels.map(key => [key, row[labelAlias(key)] ? JSON.parse(row[labelAlias(key)]) : null])),
|
|
19
|
+
} : {}) }
|
|
20
|
+
}
|
|
21
|
+
export interface QueryResult { data: Row[]; next_cursor: string | null }
|
|
22
|
+
export type { QueryLog } from './observability'
|
|
23
|
+
import { emptyQueryLog, recordQuery, type QueryLog } from './observability'
|
|
24
|
+
/** Count configured shape only after validation; never inspect policy trees or values. */
|
|
25
|
+
function shape(kernel: KernelRegistry, scope: Scope, query: QueryInput, cost: QueryCost) {
|
|
26
|
+
let selectCount = 0; let includeCount = 0
|
|
27
|
+
function visit(entity: string, node: Selection) {
|
|
28
|
+
selectCount += (node.select ?? kernel.fieldMask(entity, scope)).length
|
|
29
|
+
for (const [key, child] of Object.entries(node.include ?? {})) {
|
|
30
|
+
includeCount++; visit(kernel.config.entities[entity].relations[key].entity, child)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
visit(query.entity, query)
|
|
34
|
+
return { entity: query.entity, depth: cost.depth, relationCount: cost.relations, selectCount, includeCount }
|
|
35
|
+
}
|
|
36
|
+
export async function executeQuery(kernel: KernelRegistry, scope: Scope, db: D1Database, secret: string, input: unknown,
|
|
37
|
+
options: { writeLog?: (log: QueryLog) => void; legacyErrors?: boolean } = {}): Promise<QueryResult> {
|
|
38
|
+
const started = performance.now()
|
|
39
|
+
let bindingCount = 0; let bindingHighWaterMark = 0; let resultRows = 0; let rowsRead: number | null = null
|
|
40
|
+
let hash = await inputHash(input); let cost: QueryCost | undefined; let query: QueryInput | undefined; let statementCount = 0; let outcome = 'query_invalid'
|
|
41
|
+
try {
|
|
42
|
+
const validated = validateQuery(kernel, scope, input, options.legacyErrors)
|
|
43
|
+
query = validated.query; cost = validated.cost; hash = await queryHash(kernel.config, scope, query)
|
|
44
|
+
const keys = await decodeCursor(secret, hash, kernel.config, query)
|
|
45
|
+
let totalRows = 0
|
|
46
|
+
async function run(statements: Statement[]) {
|
|
47
|
+
if (!statements.length) return []
|
|
48
|
+
statementCount += statements.length
|
|
49
|
+
if (statementCount > L.statements) throw new QueryError('query_too_expensive', [], cost)
|
|
50
|
+
// The compiler owns all statements. Driver/library errors never cross the query boundary.
|
|
51
|
+
for (const statement of statements) {
|
|
52
|
+
bindingCount += statement.bindings.length; bindingHighWaterMark = Math.max(bindingHighWaterMark, statement.bindings.length)
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const results = await db.batch<Row>(statements.map(statement => db.prepare(statement.sql).bind(...statement.bindings)))
|
|
56
|
+
for (const result of results) if (typeof result.meta?.rows_read === 'number') rowsRead = (rowsRead ?? 0) + result.meta.rows_read
|
|
57
|
+
return results.map(result => result.results)
|
|
58
|
+
}
|
|
59
|
+
catch { throw new QueryError('query_invalid') }
|
|
60
|
+
}
|
|
61
|
+
function assertDenial(entity: string, rows: Row[], path: QueryPath = []) {
|
|
62
|
+
const filter = kernel.tableFilter(entity, scope)
|
|
63
|
+
if ('deny' in filter && filter.deny === 'forbidden' && !rows.length) throw new QueryError('policy_violation', path)
|
|
64
|
+
}
|
|
65
|
+
const [root] = await run([compileRoot(kernel, scope, query, keys, cost)])
|
|
66
|
+
assertDenial(query.entity, root)
|
|
67
|
+
const more = root.length > query.limit; const rows = root.slice(0, query.limit); totalRows += rows.length
|
|
68
|
+
const nextCursor = more ? await encodeCursor(secret, hash, kernel.config.revision, queryOrder(query).map(o => rows.at(-1)![orderField(o)] ?? null)) : null
|
|
69
|
+
const data = rows.map(row => projectRootRow(kernel, scope, query!, row))
|
|
70
|
+
type Job = { entity: string; node: Selection; raw: Row[]; out: Row[]; path: QueryPath }
|
|
71
|
+
let level: Job[] = [{ entity: query.entity, node: query, raw: rows, out: data, path: [] }]
|
|
72
|
+
while (level.some(job => Object.keys(job.node.include ?? {}).length)) {
|
|
73
|
+
if (level.reduce((sum, job) => sum + job.raw.length, 0) > L.parents) throw new QueryError('query_too_expensive', ['include'], cost)
|
|
74
|
+
const next: Job[] = []; let levelRows = 0
|
|
75
|
+
const pending = []
|
|
76
|
+
for (const job of level) for (const [key, node] of Object.entries(job.node.include ?? {})) {
|
|
77
|
+
const path = [...job.path, 'include', key]
|
|
78
|
+
const relation = kernel.config.entities[job.entity].relations[key]
|
|
79
|
+
const parentField = relation.many ? relation.field : 'id'
|
|
80
|
+
const ids = [...new Set(job.raw.map(row => row[relation.many ? 'id' : relation.field]).filter(id => id !== null && id !== undefined))] as Scalar[]
|
|
81
|
+
// Parent identities share one JSON binding, independent of parent count.
|
|
82
|
+
// compileInclude checks the complete policy/client/window statement budget.
|
|
83
|
+
pending.push({ job, key, node, path, relation, parentField,
|
|
84
|
+
statement: ids.length ? compileInclude(kernel, scope, relation.entity, node, parentField, ids, cost) : null })
|
|
85
|
+
}
|
|
86
|
+
// Siblings depend only on their shared parent level. Compile the whole level
|
|
87
|
+
// before executing it, preserving statement/binding limits and one D1 trip.
|
|
88
|
+
const results = await run(pending.flatMap(item => item.statement ? [item.statement] : []))
|
|
89
|
+
let resultIndex = 0
|
|
90
|
+
for (const { job, key, node, path, relation, parentField, statement } of pending) {
|
|
91
|
+
const found = statement ? results[resultIndex++] : []
|
|
92
|
+
levelRows += found.length; totalRows += found.length
|
|
93
|
+
if (levelRows > L.levelRows || totalRows > L.rows) throw new QueryError('query_too_expensive', ['include', key], cost)
|
|
94
|
+
assertDenial(relation.entity, found, path)
|
|
95
|
+
const projected = found.map(row => projectQueryRow(kernel, scope, relation.entity, node, row))
|
|
96
|
+
const groups = new Map<unknown, Row[]>()
|
|
97
|
+
found.forEach((row, i) => { const group = groups.get(row[parentField]) ?? []; group.push(projected[i]); groups.set(row[parentField], group) })
|
|
98
|
+
job.raw.forEach((row, i) => { const group = groups.get(row[relation.many ? 'id' : relation.field]) ?? []; job.out[i][key] = relation.many ? group : group[0] ?? null })
|
|
99
|
+
next.push({ entity: relation.entity, node, raw: found, out: projected, path })
|
|
100
|
+
}
|
|
101
|
+
level = next
|
|
102
|
+
}
|
|
103
|
+
resultRows = data.length
|
|
104
|
+
outcome = 'ok'; return { data, next_cursor: nextCursor }
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof QueryError && error.cost) cost = error.cost
|
|
107
|
+
outcome = error instanceof ApiError ? error.code : 'query_invalid'
|
|
108
|
+
if (error instanceof ApiError || options.legacyErrors) throw error
|
|
109
|
+
throw new QueryError('query_invalid')
|
|
110
|
+
} finally {
|
|
111
|
+
const log: QueryLog = { ...emptyQueryLog(scope, hash),
|
|
112
|
+
shape: query ? shape(kernel, scope, query, cost!) : emptyQueryLog(scope, hash).shape,
|
|
113
|
+
estimatedCost: cost ?? null, statementCount, bindingCount, bindingHighWaterMark, rowsRead,
|
|
114
|
+
resultRows, durationMs: performance.now() - started, errorCode: outcome === 'ok' ? null : outcome }
|
|
115
|
+
if (options.writeLog) options.writeLog(log)
|
|
116
|
+
else recordQuery(db, log)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { InstanceConfig } from '../config'
|
|
2
|
+
import { ApiError } from '../errors'
|
|
3
|
+
import type { Filter, QueryInput } from './schema'
|
|
4
|
+
// Only server-created translations receive the legacy operand grammar. A JSON
|
|
5
|
+
// property (or executeQuery's error-vocabulary option) cannot opt /query into it.
|
|
6
|
+
const translations = new WeakSet<object>()
|
|
7
|
+
export const isRestQuery = (input: unknown) => typeof input === 'object' && input !== null && translations.has(input)
|
|
8
|
+
/** Keep the existing REST vocabulary and cursor bytes; the execution engine is shared. */
|
|
9
|
+
export function restQuery(config: InstanceConfig, entity: string, params: Record<string, string>, id?: string): QueryInput {
|
|
10
|
+
if (!Object.hasOwn(config.entities, entity)) throw new ApiError(404, 'entity_not_found')
|
|
11
|
+
const definition = config.entities[entity]
|
|
12
|
+
const limit = id === undefined ? Number(params.limit ?? '25') : 1
|
|
13
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 90) throw new ApiError(400, 'invalid_limit')
|
|
14
|
+
const filters: Filter[] = id === undefined ? [] : [{ id: { eq: id } }]
|
|
15
|
+
if (id === undefined) {
|
|
16
|
+
if (params.cursor) filters.push({ id: { gt: params.cursor } })
|
|
17
|
+
for (const [key, input] of Object.entries(params)) {
|
|
18
|
+
if (['limit', 'cursor', 'expand'].includes(key)) continue
|
|
19
|
+
const match = /^(\w+)(?:\[(gte|lte|gt|lt)\])?$/.exec(key)
|
|
20
|
+
if (!match || !Object.hasOwn(definition.fields, match[1])) throw new ApiError(400, 'unknown_filter')
|
|
21
|
+
const field = definition.fields[match[1]]; let value: string | number | boolean = input
|
|
22
|
+
if (['int', 'float'].includes(field.type)) { value = Number(input); if (!Number.isFinite(value)) throw new ApiError(400, 'invalid_filter') }
|
|
23
|
+
if (field.type === 'bool') { if (!['true', 'false', '1', '0'].includes(input)) throw new ApiError(400, 'invalid_filter'); value = ['true', '1'].includes(input) }
|
|
24
|
+
filters.push({ [match[1]]: { [match[2] ?? 'eq']: value } })
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const expansions = params.expand?.split(',').filter(Boolean) ?? []
|
|
28
|
+
if (expansions.length > 5) throw new ApiError(400, 'too_many_expansions')
|
|
29
|
+
for (const key of expansions) if (!Object.hasOwn(definition.relations, key)) throw new ApiError(400, 'unknown_expansion')
|
|
30
|
+
const query = { entity, limit, ...(filters.length ? { where: { and: filters } } : {}),
|
|
31
|
+
...(expansions.length ? { include: Object.fromEntries(expansions.map(key => [key, {}])) } : {}) }
|
|
32
|
+
translations.add(query)
|
|
33
|
+
return query
|
|
34
|
+
}
|