@workbasehq/core 0.2.6 → 0.2.7
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 +1 -1
- package/src/config.ts +1 -1
- package/src/query/compiler.ts +45 -9
- package/src/query/expressions.ts +2 -2
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -18,7 +18,7 @@ export { Q, Cond }
|
|
|
18
18
|
/** SQLite date formatting with column operands; values use the library wire encoding. */
|
|
19
19
|
export const Fn = { ...LibraryFn, strftime: (format: string, date: string) => Q.expr(`FN(${JSON.stringify(['STRFTIME', Q.value(format).serialize(), Q.expr(date).serialize()])})`) }
|
|
20
20
|
|
|
21
|
-
export const CORE_VERSION = '0.2.
|
|
21
|
+
export const CORE_VERSION = '0.2.7'
|
|
22
22
|
const navigationIconSchema = z.enum(['LayoutDashboard', 'Users', 'MapPin', 'Coins', 'Wallet', 'ArrowLeftRight', 'Settings', 'Building2', 'Activity', 'Workflow', 'Database', 'Folder'])
|
|
23
23
|
const identifier = z.string().regex(/^[a-z][a-z0-9_]*$/)
|
|
24
24
|
export const serializedExpressionSchema = z.union([z.string().min(1).max(32000), z.record(z.string(), z.unknown()).transform(value => JSON.stringify(value))])
|
package/src/query/compiler.ts
CHANGED
|
@@ -207,6 +207,9 @@ export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: Selec
|
|
|
207
207
|
return visible
|
|
208
208
|
}
|
|
209
209
|
const computed = new Map<string, Condition>()
|
|
210
|
+
const computedOperands = new Map<string, Condition>()
|
|
211
|
+
const computedVisibility = new Map<string, Condition>()
|
|
212
|
+
const emptyComputedReferences = new Map<string, Condition>()
|
|
210
213
|
const computing = new Set<string>()
|
|
211
214
|
const presentation = new Map<string, Condition>()
|
|
212
215
|
function computedValue(field: string): Condition {
|
|
@@ -241,17 +244,20 @@ export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: Selec
|
|
|
241
244
|
parent = target
|
|
242
245
|
}
|
|
243
246
|
if (parent === alias && definition.fields[dependency.field]?.computed) {
|
|
244
|
-
|
|
245
|
-
columns.set(dependency.column,
|
|
246
|
-
visible.push(
|
|
247
|
+
computedValue(dependency.field)
|
|
248
|
+
columns.set(dependency.column, computedOperands.get(dependency.field)!)
|
|
249
|
+
visible.push(computedVisibility.get(dependency.field) ?? truth(false))
|
|
247
250
|
} else if (parent === alias && definition.fields[dependency.field]?.type === 'ref') {
|
|
248
251
|
const allowed = foreignKeyVisible(dependency.field)
|
|
249
|
-
columns.set(dependency.column,
|
|
252
|
+
columns.set(dependency.column, col(alias, dependency.field))
|
|
250
253
|
visible.push(Cond.or([Cond.isNull(col(alias, dependency.field)), allowed]))
|
|
251
254
|
} else {
|
|
252
255
|
columns.set(dependency.column, col(parent, dependency.field))
|
|
253
256
|
if (kernel.config.entities[dependency.entity].label && dependency.field === kernel.config.entities[dependency.entity].displayField) visible.push(Cond.equal(col(parent, '__display_visible'), Q.raw('1')))
|
|
254
|
-
if (kernel.config.entities[dependency.entity].fields[dependency.field]?.computed) visible.push(Cond.
|
|
257
|
+
if (kernel.config.entities[dependency.entity].fields[dependency.field]?.computed) visible.push(Cond.equal(col(parent, `__computed_visible_${dependency.field}`), Q.raw('1')))
|
|
258
|
+
else if (kernel.config.entities[dependency.entity].fields[dependency.field]?.type === 'ref') visible.push(Cond.or([
|
|
259
|
+
Cond.isNull(col(parent, `__raw_${dependency.field}`)), Cond.isNotNull(col(parent, dependency.field)),
|
|
260
|
+
]))
|
|
255
261
|
}
|
|
256
262
|
}
|
|
257
263
|
const source = (target: string) => scopedTable(target, undefined, true, true, info.tables.get(target) ?? new Set())
|
|
@@ -265,11 +271,22 @@ export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: Selec
|
|
|
265
271
|
}
|
|
266
272
|
presentation.set(field, new DisplayPartsExpression(info.parts!, columns, nested, source))
|
|
267
273
|
}
|
|
274
|
+
// Keep the operand and its availability separate internally. Reusing an
|
|
275
|
+
// already-masked operand loses NULL provenance and recursively duplicates
|
|
276
|
+
// visibility probes for every layer of a composed expression.
|
|
277
|
+
const operand = new ComputedExpression(info, columns, [], source)
|
|
278
|
+
computedOperands.set(field, operand)
|
|
268
279
|
let result: Condition = new ComputedExpression(info, columns, visible, source)
|
|
280
|
+
const dependenciesVisible = visible.length ? Cond.and(visible) : truth(true)
|
|
281
|
+
computedVisibility.set(field, dependenciesVisible)
|
|
269
282
|
if (definition.fields[field].type === 'ref') {
|
|
270
|
-
|
|
283
|
+
// A masked NULL is not absence. Carry both states through computed
|
|
284
|
+
// dependencies, including nullable scalar intermediates and joined rows.
|
|
285
|
+
const empty = Cond.and([dependenciesVisible, Cond.isNull(operand)])
|
|
286
|
+
emptyComputedReferences.set(field, empty)
|
|
271
287
|
const allowed = Cond.exists(scopedTable(definition.fields[field].entity!, child => Cond.equal(col(child, 'id'), operand), false))
|
|
272
|
-
|
|
288
|
+
computedVisibility.set(field, Cond.and([dependenciesVisible, Cond.or([Cond.isNull(operand), allowed])]))
|
|
289
|
+
result = new ForeignKeyValue(operand, Cond.and([dependenciesVisible, allowed]))
|
|
273
290
|
}
|
|
274
291
|
computed.set(field, result); computing.delete(field); return result
|
|
275
292
|
}
|
|
@@ -311,7 +328,8 @@ export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: Selec
|
|
|
311
328
|
const target = kernel.config.entities[path.entity]
|
|
312
329
|
// A raw NULL is ordinary absence; a non-null raw FK masked to NULL is
|
|
313
330
|
// denied. Reuse its masked projection rather than add another EXISTS.
|
|
314
|
-
if (target.fields[path.field]?.
|
|
331
|
+
if (target.fields[path.field]?.computed) labelVisibility.push(Cond.equal(col(parent, `__computed_visible_${path.field}`), Q.raw('1')))
|
|
332
|
+
else if (target.fields[path.field]?.type === 'ref') labelVisibility.push(Cond.or([
|
|
315
333
|
Cond.isNull(col(parent, `__raw_${path.field}`)), Cond.isNotNull(col(parent, path.field)),
|
|
316
334
|
]))
|
|
317
335
|
if (target.label && path.field === target.displayField) labelVisibility.push(Cond.equal(col(parent, '__display_visible'), Q.raw('1')))
|
|
@@ -326,6 +344,10 @@ export function renderStatement(kernel: KernelRegistry, scope: Scope, ast: Selec
|
|
|
326
344
|
if (definition?.computed) {
|
|
327
345
|
const projected = !computedDemand || computedDemand.has(field) ? computedValue(field) : Q.raw('NULL')
|
|
328
346
|
query = query.field(projected, field)
|
|
347
|
+
if (maskForeignKeys) {
|
|
348
|
+
query = query.field(computedVisibility.get(field) ?? truth(false), `__computed_visible_${field}`)
|
|
349
|
+
if (definition.type === 'ref') query = query.field(emptyComputedReferences.get(field) ?? truth(false), `__empty_${field}`)
|
|
350
|
+
}
|
|
329
351
|
if (maskForeignKeys && (!computedDemand || computedDemand.has(field)) && needsDisplayParts(kernel.config, entity, field)) {
|
|
330
352
|
query = query.field(presentation.get(field) ?? Q.raw('NULL'), partsAlias(field))
|
|
331
353
|
}
|
|
@@ -410,6 +432,13 @@ class LabelObject extends Condition {
|
|
|
410
432
|
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`
|
|
411
433
|
}
|
|
412
434
|
}
|
|
435
|
+
/** Only an absent FK on an authorized source row is empty. A failed join is not. */
|
|
436
|
+
class NullableLabel extends Condition {
|
|
437
|
+
constructor(private empty: Condition, private label: ISequelizable) { super() }
|
|
438
|
+
toSQL(flavor: ISQLFlavor) {
|
|
439
|
+
return `CASE WHEN ${this.empty.toSQL(flavor)} THEN json_object('status', 'empty') ELSE ${this.label.toSQL(flavor)} END`
|
|
440
|
+
}
|
|
441
|
+
}
|
|
413
442
|
export const orderField = (order: Order) => order.label ? sortAlias(order.field) : order.field
|
|
414
443
|
export function compileRoot(kernel: KernelRegistry, scope: Scope, query: QueryInput, keys?: Scalar[], cost?: QueryCost) {
|
|
415
444
|
let source = buildSelect(kernel, scope, query.entity, query)
|
|
@@ -417,13 +446,20 @@ export function compileRoot(kernel: KernelRegistry, scope: Scope, query: QueryIn
|
|
|
417
446
|
add(query.entity, query.search?.fields ?? [])
|
|
418
447
|
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)])]
|
|
419
448
|
const joins = new Map<string, string>()
|
|
449
|
+
const emptyReference = (parent: string, hop: { source: string; field: string }) =>
|
|
450
|
+
kernel.config.entities[hop.source].fields[hop.field].computed
|
|
451
|
+
? readableComputed(kernel, scope, hop.source, hop.field) ? Cond.equal(col(parent, `__empty_${hop.field}`), Q.raw('1')) : truth(false)
|
|
452
|
+
: Cond.isNull(col(parent, `__raw_${hop.field}`))
|
|
420
453
|
for (const key of labelKeys) {
|
|
421
454
|
const plan = readableLabel(kernel, scope, query.entity, key)
|
|
455
|
+
const first = labelPath(kernel.config, query.entity, key)!.hops[0]
|
|
456
|
+
const empty: Condition[] = [emptyReference('row', first)]
|
|
422
457
|
let expression: Condition | ReturnType<typeof Q.raw> = Q.raw("json_object('status', 'unavailable')")
|
|
423
458
|
if (plan) {
|
|
424
459
|
if (plan.display) add(plan.entity, [plan.display])
|
|
425
460
|
let parent = 'row'; let path = ''
|
|
426
461
|
for (const hop of plan.hops) {
|
|
462
|
+
if (parent !== 'row') empty.push(Cond.and([Cond.isNotNull(col(parent, 'id')), emptyReference(parent, hop)]))
|
|
427
463
|
path += `/${hop.field}`
|
|
428
464
|
let alias = joins.get(path)
|
|
429
465
|
if (!alias) {
|
|
@@ -434,7 +470,7 @@ export function compileRoot(kernel: KernelRegistry, scope: Scope, query: QueryIn
|
|
|
434
470
|
}
|
|
435
471
|
expression = new LabelObject(plan.entity, parent, plan.display, !!plan.display && needsDisplayParts(kernel.config, plan.entity, plan.display))
|
|
436
472
|
}
|
|
437
|
-
source = source.field(expression, labelAlias(key))
|
|
473
|
+
source = source.field(new NullableLabel(Cond.or(empty), expression), labelAlias(key))
|
|
438
474
|
}
|
|
439
475
|
// Compute the key once from the authorized label, then seek/order/limit over it.
|
|
440
476
|
let labeled = Q.select().from(source, 'labels').field('labels.*')
|
package/src/query/expressions.ts
CHANGED
|
@@ -69,7 +69,7 @@ export function expressionInfo(config: Config, entity: string, field: string, wi
|
|
|
69
69
|
clause?: 'SELECT' | 'WHERE' | 'JOIN ON' | 'ORDER BY'; selectAggregate?: boolean;
|
|
70
70
|
aggregate?: string; aggregates?: { found: boolean }; outerAliases?: Set<string>; outerReferences?: Set<string>;
|
|
71
71
|
aggregateReferences?: { aliases: Set<string>; references: Set<string> }[] }
|
|
72
|
-
const root: Context = { aliases: new Map([['self', entity]]), aliasContexts: new Map(), current: entity, query: false }
|
|
72
|
+
const root: Context = { aliases: new Map([['self', entity]]), aliasContexts: new Map(), current: entity, query: false, rawRefs: config.entities[entity].fields[field]?.type === 'ref' }
|
|
73
73
|
const reference = (column: string, context: Context): Kind => {
|
|
74
74
|
if (column.includes('*') || !/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$/.test(column)) fail(`Unknown field: ${column}`)
|
|
75
75
|
const parts = column.split('.'); let source = context.current ?? entity; let qualifier: string | undefined
|
|
@@ -89,7 +89,7 @@ export function expressionInfo(config: Config, entity: string, field: string, wi
|
|
|
89
89
|
let name = parts[0]
|
|
90
90
|
const spec = config.entities[source] && Object.hasOwn(config.entities[source].fields, name) ? config.entities[source].fields[name] : undefined
|
|
91
91
|
if (!spec && !Object.hasOwn(base, name)) fail(`Unknown schema field: ${source}.${name}`, 'Choose a field returned by inspect_schema.')
|
|
92
|
-
//
|
|
92
|
+
// Descriptive expressions consume the display value; computed refs consume IDs.
|
|
93
93
|
if (spec?.type === 'ref' && !context.rawRefs) {
|
|
94
94
|
if (qualifier && qualifier !== 'self') fail('Ref paths inside subqueries require an explicit schema join')
|
|
95
95
|
hops.push({ source, field: name, target: spec!.entity! }); source = spec!.entity!
|