@fabrika/auth-core 0.9.0-dev.5 → 0.9.0-dev.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/auth-core",
3
- "version": "0.9.0-dev.5",
3
+ "version": "0.9.0-dev.6",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
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
 
@@ -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. */