@fabrika/auth-core 0.9.0-dev.5 → 0.9.0-dev.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/ops.ts +84 -7
- package/types/src/ops.d.ts +36 -4
package/package.json
CHANGED
package/src/ops.ts
CHANGED
|
@@ -402,6 +402,71 @@ export function readOpsDumpValue(type: unknown, value: unknown): OpsDumpValue {
|
|
|
402
402
|
throw new Error('a projected dump value does not match its type')
|
|
403
403
|
}
|
|
404
404
|
|
|
405
|
+
/**
|
|
406
|
+
* The most result columns, and the most parameters, one statement of workerd's SQLite takes; a table holds at most as
|
|
407
|
+
* many columns.
|
|
408
|
+
*/
|
|
409
|
+
export const OPS_SQLITE_MAX_COLUMNS = 100
|
|
410
|
+
|
|
411
|
+
/** The most columns of a table one statement of a page read projects: two result columns each, beside the rowid twice. */
|
|
412
|
+
export const OPS_DUMP_READ_COLUMNS = (OPS_SQLITE_MAX_COLUMNS - 2) / 2
|
|
413
|
+
|
|
414
|
+
/** One row of a keyset page: its rowid as decimal text, then the rowid and every column as the dump writes them. */
|
|
415
|
+
export interface OpsDumpPageRow {
|
|
416
|
+
readonly rowid: string
|
|
417
|
+
readonly values: readonly OpsDumpValue[]
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Runs one statement of a page read, with the parameters of the read's condition, and answers its rows. */
|
|
421
|
+
export type OpsDumpPageQuery = (sql: string) => readonly Readonly<Record<string, unknown>>[]
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* One keyset page of `table`: the first `limit` rows by rowid that `condition` (`WHERE …`, or empty) admits, read the
|
|
425
|
+
* way the producer and the importer's verification both read them. Each column reads as two result columns, so a table
|
|
426
|
+
* wider than `OPS_DUMP_READ_COLUMNS` reads in several statements over the same rows, which the caller runs in one
|
|
427
|
+
* synchronous turn, so no write falls between them. The common table expression names every result column, which an
|
|
428
|
+
* expression alone does not.
|
|
429
|
+
*/
|
|
430
|
+
export function readOpsDumpPage(query: OpsDumpPageQuery, table: OpsDumpTablePlan, condition: string, limit: number): OpsDumpPageRow[] {
|
|
431
|
+
if (!Number.isSafeInteger(limit) || limit < 1) throw new Error('a dump page reads at least one row')
|
|
432
|
+
const keyset = quoteIdentifier(table.keyset)
|
|
433
|
+
const reads: { readonly start: number; readonly count: number; readonly rows: readonly Readonly<Record<string, unknown>>[] }[] = []
|
|
434
|
+
for (let start = 0; start === 0 || start < table.columns.length; start += OPS_DUMP_READ_COLUMNS) {
|
|
435
|
+
const columns = table.columns.slice(start, start + OPS_DUMP_READ_COLUMNS)
|
|
436
|
+
const names = columns.flatMap((_, offset) => [`t${start + offset}`, `v${start + offset}`])
|
|
437
|
+
const projections = [keyset, `CAST(${keyset} AS TEXT)`, ...columns.map(opsDumpColumnProjection)]
|
|
438
|
+
const rows = query(
|
|
439
|
+
`WITH page(${['k', 'r', ...names].join(', ')}) AS (SELECT ${projections.join(', ')} FROM main.${quoteIdentifier(table.name)} ${condition}
|
|
440
|
+
ORDER BY ${keyset} LIMIT ${limit}) SELECT ${['r', ...names].join(', ')} FROM page ORDER BY k`,
|
|
441
|
+
)
|
|
442
|
+
reads.push({ start, count: columns.length, rows })
|
|
443
|
+
}
|
|
444
|
+
const first = reads[0]?.rows ?? []
|
|
445
|
+
if (reads.some(({ rows }) => rows.length !== first.length)) throw new Error('the reads of one dump page disagree on its rows')
|
|
446
|
+
return first.map((row, index) => {
|
|
447
|
+
const rowid = readOpsDumpValue('integer', row['r'])
|
|
448
|
+
if (rowid === null || rowid.type !== 'integer') throw new Error('a rowid reads as an integer')
|
|
449
|
+
const values: OpsDumpValue[] = [rowid]
|
|
450
|
+
for (const { start, count, rows } of reads) {
|
|
451
|
+
const read = rows[index]
|
|
452
|
+
if (read?.['r'] !== rowid.value) throw new Error('the reads of one dump page disagree on its rows')
|
|
453
|
+
for (let column = start; column < start + count; column++) {
|
|
454
|
+
values.push(readOpsDumpValue(read[`t${column}`], read[`v${column}`]))
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return { rowid: rowid.value, values }
|
|
458
|
+
})
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The SQL that names a dump's rowid inline, so a row of a table at `OPS_SQLITE_MAX_COLUMNS` binds no more parameters
|
|
463
|
+
* than it has columns. It is the checked decimal text in a string literal, cast as `opsDumpValueBinding` casts it.
|
|
464
|
+
*/
|
|
465
|
+
export function opsDumpRowidSql(rowid: OpsDumpValue): string {
|
|
466
|
+
if (rowid === null || rowid.type !== 'integer' || !isInt64Text(rowid.value)) throw new Error('a dump rowid must be an integer')
|
|
467
|
+
return `CAST('${rowid.value}' AS INTEGER)`
|
|
468
|
+
}
|
|
469
|
+
|
|
405
470
|
/** What a statement binds for one value of a dump. */
|
|
406
471
|
export type OpsDumpParam = string | number | Uint8Array | null
|
|
407
472
|
|
|
@@ -521,8 +586,12 @@ export interface OpsDumpSchemaTable {
|
|
|
521
586
|
readonly name: string
|
|
522
587
|
readonly virtual: boolean
|
|
523
588
|
readonly withoutRowid: boolean
|
|
524
|
-
/**
|
|
525
|
-
|
|
589
|
+
/**
|
|
590
|
+
* Every name a `CHECK` constraint of the table's SQL reads, ASCII-lowercase and unquoted (keywords included), so a
|
|
591
|
+
* column it does not list cannot change what a `CHECK` answers. Empty for a table without one; `null` from a reader
|
|
592
|
+
* that cannot tell, which counts as naming every column.
|
|
593
|
+
*/
|
|
594
|
+
readonly checkNames: readonly string[] | null
|
|
526
595
|
readonly columns: readonly OpsDumpSchemaColumn[]
|
|
527
596
|
readonly indexes: readonly OpsDumpSchemaIndex[]
|
|
528
597
|
readonly foreignKeys: readonly OpsDumpSchemaForeignKey[]
|
|
@@ -553,8 +622,9 @@ export interface OpsDumpPlan {
|
|
|
553
622
|
* The one rule both the producer, at export `begin`, and the importer, at import `begin`, apply to a schema: the plan
|
|
554
623
|
* a dump is written and loaded by, or an `unsupported_schema` refusal naming the table. A foreign key whose parent lies
|
|
555
624
|
* in its own table's cycle (the table itself included) is loaded `NULL` and linked once every row is in, so each of its
|
|
556
|
-
* columns must take a temporary `NULL`: nullable, outside the primary key, referenced by no foreign key,
|
|
557
|
-
*
|
|
625
|
+
* columns must take a temporary `NULL`: nullable, outside the primary key, referenced by no foreign key, named by no
|
|
626
|
+
* `CHECK`, in a table without a `NOT NULL` generated column, a generated column a `CHECK` names, or a partial, expression
|
|
627
|
+
* or generated-column unique index. A `WITHOUT ROWID` or
|
|
558
628
|
* virtual table, a table without a name that reads its rowid, a foreign key to a table outside the dump or without the
|
|
559
629
|
* named key, and a foreign key checked only at commit are refused.
|
|
560
630
|
*/
|
|
@@ -1585,9 +1655,16 @@ function loadOrder(tables: readonly DescribedTable[], cycle: ReadonlyMap<string,
|
|
|
1585
1655
|
|
|
1586
1656
|
/** Whether every deferred column of `table` can hold `NULL` from its part until the link pass, with nothing to violate. */
|
|
1587
1657
|
function takesTemporaryNull(table: OpsDumpSchemaTable, deferred: readonly OpsDumpSchemaColumn[], referenced: ReadonlySet<string>): boolean {
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1658
|
+
// A `CHECK` answers the same for a `NULL` in a column it does not read; a generated column may read a deferred one.
|
|
1659
|
+
const checkReads = (column: OpsDumpSchemaColumn): boolean => table.checkNames === null || table.checkNames.includes(asciiLower(column.name))
|
|
1660
|
+
if (table.columns.some((column) => column.generated && (column.notNull || checkReads(column)))) return false
|
|
1661
|
+
// A generated column that reads a deferred one may take one value for many rows while that column is `NULL`.
|
|
1662
|
+
const generated = new Set(table.columns.filter((column) => column.generated).map((column) => asciiLower(column.name)))
|
|
1663
|
+
const indexedGenerated = (index: OpsDumpSchemaIndex): boolean => index.columns.some((name) => name !== null && generated.has(asciiLower(name)))
|
|
1664
|
+
if (table.indexes.some((index) => index.unique && (index.partial || index.columns.includes(null) || indexedGenerated(index)))) return false
|
|
1665
|
+
return deferred.every((column) =>
|
|
1666
|
+
!column.notNull && column.primaryKey === 0 && !referenced.has(columnKey(table.name, column.name)) && !checkReads(column)
|
|
1667
|
+
)
|
|
1591
1668
|
}
|
|
1592
1669
|
|
|
1593
1670
|
function columnKey(table: string, column: string): string {
|
package/types/src/ops.d.ts
CHANGED
|
@@ -235,6 +235,33 @@ export type OpsDumpValue = null | {
|
|
|
235
235
|
export declare function opsDumpColumnProjection(column: string): string;
|
|
236
236
|
/** The value `opsDumpColumnProjection` read. Throws, naming no value, on a pair it would not produce. */
|
|
237
237
|
export declare function readOpsDumpValue(type: unknown, value: unknown): OpsDumpValue;
|
|
238
|
+
/**
|
|
239
|
+
* The most result columns, and the most parameters, one statement of workerd's SQLite takes; a table holds at most as
|
|
240
|
+
* many columns.
|
|
241
|
+
*/
|
|
242
|
+
export declare const OPS_SQLITE_MAX_COLUMNS = 100;
|
|
243
|
+
/** The most columns of a table one statement of a page read projects: two result columns each, beside the rowid twice. */
|
|
244
|
+
export declare const OPS_DUMP_READ_COLUMNS: number;
|
|
245
|
+
/** One row of a keyset page: its rowid as decimal text, then the rowid and every column as the dump writes them. */
|
|
246
|
+
export interface OpsDumpPageRow {
|
|
247
|
+
readonly rowid: string;
|
|
248
|
+
readonly values: readonly OpsDumpValue[];
|
|
249
|
+
}
|
|
250
|
+
/** Runs one statement of a page read, with the parameters of the read's condition, and answers its rows. */
|
|
251
|
+
export type OpsDumpPageQuery = (sql: string) => readonly Readonly<Record<string, unknown>>[];
|
|
252
|
+
/**
|
|
253
|
+
* One keyset page of `table`: the first `limit` rows by rowid that `condition` (`WHERE …`, or empty) admits, read the
|
|
254
|
+
* way the producer and the importer's verification both read them. Each column reads as two result columns, so a table
|
|
255
|
+
* wider than `OPS_DUMP_READ_COLUMNS` reads in several statements over the same rows, which the caller runs in one
|
|
256
|
+
* synchronous turn, so no write falls between them. The common table expression names every result column, which an
|
|
257
|
+
* expression alone does not.
|
|
258
|
+
*/
|
|
259
|
+
export declare function readOpsDumpPage(query: OpsDumpPageQuery, table: OpsDumpTablePlan, condition: string, limit: number): OpsDumpPageRow[];
|
|
260
|
+
/**
|
|
261
|
+
* The SQL that names a dump's rowid inline, so a row of a table at `OPS_SQLITE_MAX_COLUMNS` binds no more parameters
|
|
262
|
+
* than it has columns. It is the checked decimal text in a string literal, cast as `opsDumpValueBinding` casts it.
|
|
263
|
+
*/
|
|
264
|
+
export declare function opsDumpRowidSql(rowid: OpsDumpValue): string;
|
|
238
265
|
/** What a statement binds for one value of a dump. */
|
|
239
266
|
export type OpsDumpParam = string | number | Uint8Array | null;
|
|
240
267
|
/** The SQL expression that writes a value back and the parameter its `?` binds. */
|
|
@@ -309,8 +336,12 @@ export interface OpsDumpSchemaTable {
|
|
|
309
336
|
readonly name: string;
|
|
310
337
|
readonly virtual: boolean;
|
|
311
338
|
readonly withoutRowid: boolean;
|
|
312
|
-
/**
|
|
313
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Every name a `CHECK` constraint of the table's SQL reads, ASCII-lowercase and unquoted (keywords included), so a
|
|
341
|
+
* column it does not list cannot change what a `CHECK` answers. Empty for a table without one; `null` from a reader
|
|
342
|
+
* that cannot tell, which counts as naming every column.
|
|
343
|
+
*/
|
|
344
|
+
readonly checkNames: readonly string[] | null;
|
|
314
345
|
readonly columns: readonly OpsDumpSchemaColumn[];
|
|
315
346
|
readonly indexes: readonly OpsDumpSchemaIndex[];
|
|
316
347
|
readonly foreignKeys: readonly OpsDumpSchemaForeignKey[];
|
|
@@ -337,8 +368,9 @@ export interface OpsDumpPlan {
|
|
|
337
368
|
* The one rule both the producer, at export `begin`, and the importer, at import `begin`, apply to a schema: the plan
|
|
338
369
|
* a dump is written and loaded by, or an `unsupported_schema` refusal naming the table. A foreign key whose parent lies
|
|
339
370
|
* in its own table's cycle (the table itself included) is loaded `NULL` and linked once every row is in, so each of its
|
|
340
|
-
* columns must take a temporary `NULL`: nullable, outside the primary key, referenced by no foreign key,
|
|
341
|
-
*
|
|
371
|
+
* columns must take a temporary `NULL`: nullable, outside the primary key, referenced by no foreign key, named by no
|
|
372
|
+
* `CHECK`, in a table without a `NOT NULL` generated column, a generated column a `CHECK` names, or a partial, expression
|
|
373
|
+
* or generated-column unique index. A `WITHOUT ROWID` or
|
|
342
374
|
* virtual table, a table without a name that reads its rowid, a foreign key to a table outside the dump or without the
|
|
343
375
|
* named key, and a foreign key checked only at commit are refused.
|
|
344
376
|
*/
|