@fabrika/db 0.9.0-dev.60 → 0.9.0-dev.62
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 +4 -4
- package/src/nodes.ts +12 -8
- package/src/orm.ts +50 -13
- package/types/src/nodes.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.62",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"./src/testing.ts",
|
|
@@ -96,9 +96,9 @@
|
|
|
96
96
|
"test": "bun test"
|
|
97
97
|
},
|
|
98
98
|
"dependencies": {
|
|
99
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
100
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
101
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
99
|
+
"@fabrika/auth-core": "0.9.0-dev.62",
|
|
100
|
+
"@fabrika/platform": "0.9.0-dev.62",
|
|
101
|
+
"@fabrika/platform-node": "0.9.0-dev.62",
|
|
102
102
|
"kysely": "0.29.6",
|
|
103
103
|
"kysely-postgres-js": "5.0.1",
|
|
104
104
|
"zod": "^3.25.76"
|
package/src/nodes.ts
CHANGED
|
@@ -167,7 +167,7 @@ function rowSelection(table: string, name: string, days: NameSet): SelectionNode
|
|
|
167
167
|
return SelectionNode.create(AliasNode.create(CastNode.create(column(table, name), DataTypeNode.create('text')), IdentifierNode.create(name)))
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
-
const
|
|
170
|
+
const NO_NAMES: NameSet = nameSet([])
|
|
171
171
|
|
|
172
172
|
/** `returning key as id[, version as version]`: a mutation's acknowledgement, never row data. */
|
|
173
173
|
export function acknowledgement(table: string, key: string, version: string | undefined): ReturningNode {
|
|
@@ -197,11 +197,14 @@ function joinedSelections(join: JoinState): SelectionNode[] {
|
|
|
197
197
|
|
|
198
198
|
/**
|
|
199
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.
|
|
200
|
+
* states Postgres's order and a keyset page (`orm.ts`) reads the same rows after its cursor on both. A NOT NULL column
|
|
201
|
+
* states none: SQLite reads an order from an index only in its own null order, and such a column holds no null.
|
|
201
202
|
*/
|
|
202
|
-
function orderItem(table: string, order: SelectState['orderBy'][number], dialect: SqlDialect): OrderByItemNode {
|
|
203
|
+
function orderItem(table: string, order: SelectState['orderBy'][number], dialect: SqlDialect, notNull: NameSet): OrderByItemNode {
|
|
203
204
|
const item = OrderByItemNode.create(column(table, order.column), RawNode.createWithSql(order.direction))
|
|
204
|
-
return dialect === 'postgres'
|
|
205
|
+
return dialect === 'postgres' || inSet(notNull, order.column)
|
|
206
|
+
? item
|
|
207
|
+
: Object.freeze({ ...item, nulls: order.direction === 'asc' ? 'last' : 'first' })
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
/** `after` is a keyset page's position, ANDed to the caller's conditions inside their parenthesis. */
|
|
@@ -210,14 +213,15 @@ export function selectNode(
|
|
|
210
213
|
visible: readonly string[],
|
|
211
214
|
scope: OperationNode,
|
|
212
215
|
after?: OperationNode,
|
|
213
|
-
days: NameSet =
|
|
216
|
+
days: NameSet = NO_NAMES,
|
|
214
217
|
dialect: SqlDialect = 'postgres',
|
|
218
|
+
notNull: NameSet = NO_NAMES,
|
|
215
219
|
): SelectQueryNode {
|
|
216
220
|
const table = state.table
|
|
217
221
|
const columns = state.columns ?? visible
|
|
218
222
|
const conditions = conditionsNode(table, state.conditions)
|
|
219
223
|
const filter = after === undefined ? conditions : conditions === undefined ? after : AndNode.create(conditions, ParensNode.create(after))
|
|
220
|
-
const orderBy = state.orderBy.map((order) => orderItem(table, order, dialect))
|
|
224
|
+
const orderBy = state.orderBy.map((order) => orderItem(table, order, dialect, notNull))
|
|
221
225
|
const node: SelectQueryNode = {
|
|
222
226
|
kind: 'SelectQueryNode',
|
|
223
227
|
from: FromNode.create([TableNode.create(table)]),
|
|
@@ -248,7 +252,7 @@ export function aggregateNode(
|
|
|
248
252
|
columns: AggregateColumns,
|
|
249
253
|
conditions: readonly Condition[],
|
|
250
254
|
scope: OperationNode,
|
|
251
|
-
days: NameSet =
|
|
255
|
+
days: NameSet = NO_NAMES,
|
|
252
256
|
): SelectQueryNode {
|
|
253
257
|
const selections = [
|
|
254
258
|
...columns.groupBy.map((name) => rowSelection(table, name, days)),
|
|
@@ -434,7 +438,7 @@ export function nextClaimNode(
|
|
|
434
438
|
}
|
|
435
439
|
|
|
436
440
|
/** `select <columns> from table where scope for update`: a mutation's precondition read of its own target. */
|
|
437
|
-
export function lockedSelectNode(table: string, columns: readonly string[], scope: OperationNode, days: NameSet =
|
|
441
|
+
export function lockedSelectNode(table: string, columns: readonly string[], scope: OperationNode, days: NameSet = NO_NAMES): SelectQueryNode {
|
|
438
442
|
const node: SelectQueryNode = {
|
|
439
443
|
kind: 'SelectQueryNode',
|
|
440
444
|
from: FromNode.create([TableNode.create(table)]),
|
package/src/orm.ts
CHANGED
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
|
|
6
6
|
import { uuidv7 } from '@fabrika/platform'
|
|
7
7
|
import type { OperationNode } from 'kysely'
|
|
8
|
-
import { BinaryOperationNode, OperatorNode, ValueNode } from 'kysely'
|
|
8
|
+
import { AndNode, BinaryOperationNode, OperatorNode, ValueNode } from 'kysely'
|
|
9
9
|
import { refScope } from './authority'
|
|
10
|
-
import { freeze, hasOwn, inSet, isSafeInteger, nameSet, namesOf, ownDescriptor, ownKeys, ownNames, parseList, setOwn } from './builtins'
|
|
10
|
+
import { freeze, hasOwn, inSet, isSafeInteger, type NameSet, nameSet, namesOf, ownDescriptor, ownKeys, ownNames, parseList, setOwn } from './builtins'
|
|
11
11
|
import { type CapabilityGrant, describeGrant, type RefGrant } from './capability'
|
|
12
12
|
import type { InvocationTransaction, Session } from './database'
|
|
13
13
|
import {
|
|
@@ -1354,7 +1354,7 @@ function withoutCursor(row: Readonly<Record<string, unknown>>, order: readonly O
|
|
|
1354
1354
|
return item
|
|
1355
1355
|
}
|
|
1356
1356
|
|
|
1357
|
-
function compare(table: string, name: string, operator: '>' | '<' | '=', value: Primitive): OperationNode {
|
|
1357
|
+
function compare(table: string, name: string, operator: '>' | '>=' | '<' | '<=' | '=', value: Primitive): OperationNode {
|
|
1358
1358
|
return BinaryOperationNode.create(column(table, name), OperatorNode.create(operator), ValueNode.create(value))
|
|
1359
1359
|
}
|
|
1360
1360
|
|
|
@@ -1362,20 +1362,43 @@ function isNull(table: string, name: string, negated: boolean): OperationNode {
|
|
|
1362
1362
|
return BinaryOperationNode.create(column(table, name), OperatorNode.create(negated ? 'is not' : 'is'), ValueNode.createImmediate(null))
|
|
1363
1363
|
}
|
|
1364
1364
|
|
|
1365
|
-
/**
|
|
1366
|
-
|
|
1365
|
+
/**
|
|
1366
|
+
* Rows after `value` in one order column, where Postgres sorts nulls last ascending and first descending. A NOT NULL
|
|
1367
|
+
* column holds no null, so its terms leave the null out.
|
|
1368
|
+
*/
|
|
1369
|
+
function after(table: string, order: OrderColumn, value: CursorValue, notNull: NameSet): OperationNode | undefined {
|
|
1367
1370
|
if (order.direction === 'asc') {
|
|
1368
|
-
|
|
1371
|
+
if (value === null) {
|
|
1372
|
+
return undefined
|
|
1373
|
+
}
|
|
1374
|
+
const later = compare(table, order.column, '>', value)
|
|
1375
|
+
return inSet(notNull, order.column) ? later : anyOf([later, isNull(table, order.column, false)])
|
|
1369
1376
|
}
|
|
1370
1377
|
return value === null ? isNull(table, order.column, true) : compare(table, order.column, '<', value)
|
|
1371
1378
|
}
|
|
1372
1379
|
|
|
1373
|
-
/**
|
|
1374
|
-
function
|
|
1380
|
+
/** Rows at or after `value` in one order column, or undefined where that is every row (a null, descending). */
|
|
1381
|
+
function atOrAfter(table: string, order: OrderColumn, value: CursorValue, notNull: NameSet): OperationNode | undefined {
|
|
1382
|
+
if (order.direction === 'desc') {
|
|
1383
|
+
return value === null ? undefined : compare(table, order.column, '<=', value)
|
|
1384
|
+
}
|
|
1385
|
+
if (value === null) {
|
|
1386
|
+
return isNull(table, order.column, false)
|
|
1387
|
+
}
|
|
1388
|
+
const later = compare(table, order.column, '>=', value)
|
|
1389
|
+
return inSet(notNull, order.column) ? later : anyOf([later, isNull(table, order.column, false)])
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
/**
|
|
1393
|
+
* `(c1 after v1) or (c1 = v1 and c2 after v2) or …`: every row the order puts after the cursor's. The first column's
|
|
1394
|
+
* bound is ANDed before it, though every branch implies it: a database reads that bound as a range of an index on the
|
|
1395
|
+
* order and stops after the page, where the disjunction alone makes it fetch and sort every row before the cursor's.
|
|
1396
|
+
*/
|
|
1397
|
+
function keysetAfter(table: string, order: readonly OrderColumn[], values: readonly CursorValue[], notNull: NameSet): OperationNode {
|
|
1375
1398
|
const branches: OperationNode[] = []
|
|
1376
1399
|
for (let index = 0; index < order.length; index++) {
|
|
1377
1400
|
const term = order[index]
|
|
1378
|
-
const position = term === undefined ? undefined : after(table, term, values[index] ?? null)
|
|
1401
|
+
const position = term === undefined ? undefined : after(table, term, values[index] ?? null, notNull)
|
|
1379
1402
|
if (term === undefined || position === undefined) {
|
|
1380
1403
|
continue
|
|
1381
1404
|
}
|
|
@@ -1387,7 +1410,13 @@ function keysetAfter(table: string, order: readonly OrderColumn[], values: reado
|
|
|
1387
1410
|
}
|
|
1388
1411
|
branches[branches.length] = equal.length === 0 ? position : allOf([...equal, position])
|
|
1389
1412
|
}
|
|
1390
|
-
|
|
1413
|
+
if (branches.length === 0) {
|
|
1414
|
+
return ValueNode.createImmediate(false)
|
|
1415
|
+
}
|
|
1416
|
+
const first = order[0]
|
|
1417
|
+
const bound = first === undefined ? undefined : atOrAfter(table, first, values[0] ?? null, notNull)
|
|
1418
|
+
const rows = anyOf(branches)
|
|
1419
|
+
return bound === undefined ? rows : AndNode.create(bound, rows)
|
|
1391
1420
|
}
|
|
1392
1421
|
|
|
1393
1422
|
/** One keyset page: `limit` rows in the order asked, the tiebreak columns last, and the cursor of the next page. */
|
|
@@ -1412,13 +1441,13 @@ async function listRows(
|
|
|
1412
1441
|
const key = (): Promise<CryptoKey> => (read ??= cursorKey(session))
|
|
1413
1442
|
const position = options['after'] === undefined
|
|
1414
1443
|
? undefined
|
|
1415
|
-
: keysetAfter(table, order, await openCursor(key, cursorList(table, order), options['after'], order.length))
|
|
1444
|
+
: keysetAfter(table, order, await openCursor(key, cursorList(table, order), options['after'], order.length), context.model.notNull)
|
|
1416
1445
|
if (scope === undefined || conditions === undefined) {
|
|
1417
1446
|
return { items: [], next: null }
|
|
1418
1447
|
}
|
|
1419
1448
|
const dialect = dialectOf(context)
|
|
1420
1449
|
const node = withCursorColumns(
|
|
1421
|
-
selectNode(rowState(table, conditions, order, limit + 1), visible, scope, position, context.model.dates, dialect),
|
|
1450
|
+
selectNode(rowState(table, conditions, order, limit + 1), visible, scope, position, context.model.dates, dialect, context.model.notNull),
|
|
1422
1451
|
table,
|
|
1423
1452
|
order.map((term) => term.column),
|
|
1424
1453
|
)
|
|
@@ -1461,7 +1490,15 @@ async function allRows(
|
|
|
1461
1490
|
return []
|
|
1462
1491
|
}
|
|
1463
1492
|
const dialect = dialectOf(context)
|
|
1464
|
-
const node = selectNode(
|
|
1493
|
+
const node = selectNode(
|
|
1494
|
+
rowState(table, conditions, order, MAX_ALL_ROWS + 1),
|
|
1495
|
+
visible,
|
|
1496
|
+
scope,
|
|
1497
|
+
undefined,
|
|
1498
|
+
context.model.dates,
|
|
1499
|
+
dialect,
|
|
1500
|
+
context.model.notNull,
|
|
1501
|
+
)
|
|
1465
1502
|
const session = await run.read()
|
|
1466
1503
|
const rows = await session.run<Readonly<Record<string, unknown>>>(node, {
|
|
1467
1504
|
root: 'SelectQueryNode',
|
package/types/src/nodes.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function scopedWhere(conditions: OperationNode | undefined, scope
|
|
|
22
22
|
/** `returning key as id[, version as version]`: a mutation's acknowledgement, never row data. */
|
|
23
23
|
export declare function acknowledgement(table: string, key: string, version: string | undefined): ReturningNode;
|
|
24
24
|
/** `after` is a keyset page's position, ANDed to the caller's conditions inside their parenthesis. */
|
|
25
|
-
export declare function selectNode(state: SelectState, visible: readonly string[], scope: OperationNode, after?: OperationNode, days?: NameSet, dialect?: SqlDialect): SelectQueryNode;
|
|
25
|
+
export declare function selectNode(state: SelectState, visible: readonly string[], scope: OperationNode, after?: OperationNode, days?: NameSet, dialect?: SqlDialect, notNull?: NameSet): SelectQueryNode;
|
|
26
26
|
export interface AggregateColumns {
|
|
27
27
|
readonly groupBy: readonly string[];
|
|
28
28
|
readonly count: boolean;
|