@otwdesign/db-core 1.3.2 → 1.4.1

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.
Files changed (72) hide show
  1. package/dist/backup/api.js +67 -9
  2. package/dist/backup/api.js.map +1 -1
  3. package/dist/backup/catalog.d.ts +9 -2
  4. package/dist/backup/catalog.js +42 -18
  5. package/dist/backup/catalog.js.map +1 -1
  6. package/dist/backup/dump.d.ts +6 -2
  7. package/dist/backup/dump.js +188 -20
  8. package/dist/backup/dump.js.map +1 -1
  9. package/dist/backup/restore.d.ts +9 -1
  10. package/dist/backup/restore.js +131 -11
  11. package/dist/backup/restore.js.map +1 -1
  12. package/dist/backup/split.d.ts +7 -2
  13. package/dist/backup/split.js +38 -6
  14. package/dist/backup/split.js.map +1 -1
  15. package/dist/backup/staging.d.ts +6 -1
  16. package/dist/backup/staging.js +38 -10
  17. package/dist/backup/staging.js.map +1 -1
  18. package/dist/drivers/mysql.d.ts +6 -0
  19. package/dist/drivers/mysql.js +142 -18
  20. package/dist/drivers/mysql.js.map +1 -1
  21. package/dist/drivers/pglite.d.ts +18 -3
  22. package/dist/drivers/pglite.js +357 -55
  23. package/dist/drivers/pglite.js.map +1 -1
  24. package/dist/drivers/postgres-catalog.d.ts +16 -1
  25. package/dist/drivers/postgres-catalog.js +34 -0
  26. package/dist/drivers/postgres-catalog.js.map +1 -1
  27. package/dist/drivers/postgres.d.ts +10 -0
  28. package/dist/drivers/postgres.js +203 -29
  29. package/dist/drivers/postgres.js.map +1 -1
  30. package/dist/drivers/read-only.d.ts +4 -0
  31. package/dist/drivers/read-only.js +156 -0
  32. package/dist/drivers/read-only.js.map +1 -0
  33. package/dist/drivers/sql.d.ts +51 -4
  34. package/dist/drivers/sql.js +112 -35
  35. package/dist/drivers/sql.js.map +1 -1
  36. package/dist/drivers/sqlite.d.ts +33 -1
  37. package/dist/drivers/sqlite.js +678 -107
  38. package/dist/drivers/sqlite.js.map +1 -1
  39. package/dist/export.d.ts +4 -5
  40. package/dist/export.js +21 -3
  41. package/dist/export.js.map +1 -1
  42. package/dist/format.js +7 -1
  43. package/dist/format.js.map +1 -1
  44. package/dist/handler.js +37 -15
  45. package/dist/handler.js.map +1 -1
  46. package/dist/index.d.ts +2 -2
  47. package/dist/index.js +1 -1
  48. package/dist/index.js.map +1 -1
  49. package/dist/types.d.ts +37 -3
  50. package/dist/words.d.ts +19 -0
  51. package/dist/words.js +76 -0
  52. package/dist/words.js.map +1 -0
  53. package/package.json +8 -7
  54. package/src/backup/api.ts +82 -14
  55. package/src/backup/catalog.ts +43 -18
  56. package/src/backup/dump.ts +208 -21
  57. package/src/backup/restore.ts +152 -12
  58. package/src/backup/split.ts +38 -6
  59. package/src/backup/staging.ts +51 -10
  60. package/src/drivers/mysql.ts +171 -21
  61. package/src/drivers/pglite.ts +419 -56
  62. package/src/drivers/postgres-catalog.ts +51 -1
  63. package/src/drivers/postgres.ts +241 -33
  64. package/src/drivers/read-only.ts +174 -0
  65. package/src/drivers/sql.ts +170 -37
  66. package/src/drivers/sqlite.ts +750 -104
  67. package/src/export.ts +26 -6
  68. package/src/format.ts +9 -1
  69. package/src/handler.ts +55 -19
  70. package/src/index.ts +7 -2
  71. package/src/types.ts +38 -1
  72. package/src/words.ts +67 -0
@@ -0,0 +1,156 @@
1
+ import { DbAdminError } from "../errors.js";
2
+ import { leadingWord } from "../words.js";
3
+ /*
4
+ * What read access runs of someone's own SQL. A read-only transaction keeps a statement from
5
+ * changing rows; the server's files, its programs, its settings and its other connections aren't
6
+ * rows, and a login that may touch them (a superuser, root, and PGlite, which has no other kind)
7
+ * still can through a connection marked read-only: COPY … TO PROGRAM runs a command on the
8
+ * database's server.
9
+ *
10
+ * So read access takes the statements that read, by their first word, and no others: what isn't
11
+ * on the list isn't run, however it's written. Within a statement that reads, a function can
12
+ * still reach past the database, and no list has every function: the ones that are known are
13
+ * refused by name, wherever the name appears, in a string or a comment too, since a function
14
+ * that runs SQL it's given could be handed the name in pieces. That refuses a statement that
15
+ * only mentions one, which read access can live with. It's a guard, not a wall: a function the
16
+ * database's owner wrote can do anything. A read-only connection is safe with a login that may
17
+ * only read.
18
+ */
19
+ function refuse(what) {
20
+ throw new DbAdminError(`This connection is read-only: ${what}`, 403, 'not-allowed');
21
+ }
22
+ const READS = 'Read access runs SELECT, WITH, VALUES, TABLE, SHOW and EXPLAIN';
23
+ /** The statements that read, on PostgreSQL. */
24
+ const POSTGRES_READS = new Set(['SELECT', 'WITH', 'VALUES', 'TABLE', 'SHOW', 'EXPLAIN']);
25
+ /**
26
+ * PostgreSQL's functions that read or write the server's files, act on the server or on its
27
+ * other connections, open another connection, hold a lock for the session, or run SQL they're
28
+ * given as text (through which any of the others could be named in pieces).
29
+ */
30
+ const POSTGRES_NAMES = new RegExp(`\\b(${[
31
+ // The server's files, read or written.
32
+ 'lo_export',
33
+ 'lo_import',
34
+ 'pg_read_\\w*file\\w*',
35
+ 'pg_ls_[a-z_]*dir\\w*',
36
+ 'pg_stat_file',
37
+ 'pg_file_\\w+',
38
+ 'pg_hba_file_rules',
39
+ 'pg_ident_file_mappings',
40
+ 'pg_show_all_file_settings',
41
+ 'pg_current_logfile',
42
+ // The server itself, and its other connections.
43
+ 'pg_terminate_backend',
44
+ 'pg_cancel_backend',
45
+ 'pg_log_backend_memory_contexts',
46
+ 'pg_log_standby_snapshot',
47
+ 'pg_reload_conf',
48
+ 'pg_rotate_logfile\\w*',
49
+ 'pg_switch_wal',
50
+ 'pg_promote',
51
+ 'pg_create_restore_point',
52
+ 'pg_backup_\\w+',
53
+ 'pg_start_backup',
54
+ 'pg_stop_backup',
55
+ 'pg_wal_replay_\\w+',
56
+ 'pg_stat_\\w*reset\\w*',
57
+ 'pg_create_\\w*replication_slot',
58
+ 'pg_copy_\\w*replication_slot',
59
+ 'pg_drop_replication_slot',
60
+ 'pg_replication_slot_advance',
61
+ 'pg_sync_replication_slots',
62
+ 'pg_replication_origin_(?:create|drop|advance|session_setup|session_reset|xact_setup|xact_reset)',
63
+ 'pg_logical_\\w+',
64
+ // The session, which the next query on this connection shares.
65
+ 'pg_advisory_\\w+',
66
+ 'pg_try_advisory_\\w+',
67
+ 'set_config',
68
+ 'setseed',
69
+ // Another connection, or SQL given as text, through which any of the above could be named
70
+ // in pieces: built in, and from the extensions most often installed.
71
+ 'dblink\\w*',
72
+ 'query_to_xml\\w*',
73
+ 'cursor_to_xml\\w*',
74
+ 'ts_stat',
75
+ 'ts_rewrite',
76
+ 'crosstab\\w*',
77
+ 'connectby',
78
+ 'xpath_table',
79
+ ].join('|')})\\b`, 'i');
80
+ /** PostgreSQL, and PGlite, which is one. */
81
+ export function refuseBeyondReadingPostgres(sql) {
82
+ const word = leadingWord(sql);
83
+ if (!POSTGRES_READS.has(word)) {
84
+ refuse(word
85
+ ? `${READS}, not ${word}. COPY, DO and LOAD among the others reach the server itself, which no transaction holds back.`
86
+ : `${READS}.`);
87
+ }
88
+ const name = POSTGRES_NAMES.exec(sql)?.[1];
89
+ if (name) {
90
+ refuse(`${name.toLowerCase()} reaches past the database’s rows: to the server’s files, the server itself, its other connections, or SQL of its own. A statement that only mentions it is refused as well.`);
91
+ }
92
+ // A name spelled in escapes is a name this can't read.
93
+ if (/(?<![A-Za-z0-9_$])U&\s*["']/i.test(sql)) {
94
+ refuse('names and strings written with U& escapes aren’t taken here.');
95
+ }
96
+ }
97
+ /** The statements that read, on MySQL and MariaDB. */
98
+ const MYSQL_READS = new Set([
99
+ 'SELECT',
100
+ 'WITH',
101
+ 'VALUES',
102
+ 'TABLE',
103
+ 'SHOW',
104
+ 'EXPLAIN',
105
+ 'DESCRIBE',
106
+ 'DESC',
107
+ ]);
108
+ const MYSQL_NAMES = /\b(asynchronous_connection_failover_\w+|group_replication_\w+|innodb_set_open_files_limit|innodb_redo_log_\w+|sys_exec|sys_eval)\b/i;
109
+ /** MySQL and MariaDB. */
110
+ export function refuseBeyondReadingMysql(sql) {
111
+ // What's inside /*! … */ and /*M! … */ runs as SQL: a statement within a comment.
112
+ if (/\/\*M?!/.test(sql))
113
+ refuse('comments that run as SQL (/*! … */) aren’t taken here.');
114
+ const word = mysqlFirstWord(sql);
115
+ if (!MYSQL_READS.has(word)) {
116
+ refuse(word
117
+ ? `${READS} (DESCRIBE too), not ${word}. KILL, SET GLOBAL and FLUSH among the others act on the server itself, which no transaction holds back.`
118
+ : `${READS}.`);
119
+ }
120
+ // Keywords and a function no SELECT can spell any other way: refused wherever they appear.
121
+ const beyond = /\b(OUTFILE|DUMPFILE|LOAD_FILE)\b/i.exec(sql)?.[1];
122
+ if (beyond) {
123
+ refuse(`${beyond.toUpperCase()} reads or writes a file on the database’s server. A statement that only mentions it is refused as well.`);
124
+ }
125
+ // Functions that change the server, which a SELECT may call and no transaction undoes.
126
+ const acts = MYSQL_NAMES.exec(sql)?.[1];
127
+ if (acts) {
128
+ refuse(`${acts.toLowerCase()} changes the server itself. A statement that only mentions it is refused as well.`);
129
+ }
130
+ }
131
+ /** The first word past spaces, comments (-- , # and blocks) and brackets, in capitals. */
132
+ function mysqlFirstWord(text) {
133
+ let at = 0;
134
+ for (;;) {
135
+ while (/[\s(;]/.test(text[at] ?? ''))
136
+ at += 1;
137
+ // `--` starts a comment before a space, a control character, or the end: MySQL's own rule.
138
+ if (text.startsWith('#', at) ||
139
+ (text.startsWith('--', at) && !(text.charCodeAt(at + 2) > 32))) {
140
+ const end = text.indexOf('\n', at);
141
+ if (end === -1)
142
+ return '';
143
+ at = end + 1;
144
+ }
145
+ else if (text.startsWith('/*', at)) {
146
+ const end = text.indexOf('*/', at + 2);
147
+ if (end === -1)
148
+ return '';
149
+ at = end + 2;
150
+ }
151
+ else
152
+ break;
153
+ }
154
+ return (/^[A-Za-z_]+/.exec(text.slice(at, at + 64))?.[0] ?? '').toUpperCase();
155
+ }
156
+ //# sourceMappingURL=read-only.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-only.js","sourceRoot":"","sources":["../../src/drivers/read-only.ts"],"names":[],"mappings":"AAAA,OA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AG,CAAC,CAAC,CAAA;YACtC,IAAI,GAAG,KAAK,CAAC,CAAC;gBAAE,OAAO,EAAE,CAAA;YACzB,EAAE,GAAG,GAAG,GAAG,CAAC,CAAA;QACd,CAAC;;YAAM,MAAK;IACd,CAAC;IACD,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;AAC/E,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { DbAdminError } from '../errors.ts';
1
2
  import type { Change, Filter } from '../types.ts';
2
3
  /** How one database spells what the shared builders write. */
3
4
  export interface Dialect {
@@ -27,13 +28,38 @@ export interface Dialect {
27
28
  inList(expression: string, values: readonly string[], parameter: (value: unknown) => string): string;
28
29
  /** An insert of nothing but defaults: `DEFAULT VALUES`, or MySQL's `() VALUES ()`. */
29
30
  insertDefaults: string;
31
+ /**
32
+ * Bytes as the grid shows them, as a literal that is those bytes; null when the text isn't in
33
+ * that form. Absent where the shown form is text the database reads as bytes itself.
34
+ */
35
+ bytes?(shown: string): string | null;
36
+ /**
37
+ * What ends a SELECT that holds its rows until the transaction ends: ` FOR UPDATE`, or nothing
38
+ * where a write transaction already holds the whole database (SQLite).
39
+ */
40
+ forUpdate: string;
30
41
  }
31
- /** Parameters as a builder adds them, with their placeholders. */
42
+ /** How long an export may wait between two batches for its reader, unless a driver is told otherwise. */
43
+ export declare const EXPORT_IDLE_MS = 300000;
44
+ /** Milliseconds as people read them: `10 s`, `0.3 s`. */
45
+ export declare function seconds(ms: number): string;
46
+ /**
47
+ * What an export says when it's ended because nobody read it. A browser that stays away mid-file
48
+ * would otherwise hold what the export holds (a connection, a cursor's rows, a lock on the file)
49
+ * for as long as it stays away.
50
+ */
51
+ export declare function stalledExport(idleMs: number): DbAdminError;
52
+ /**
53
+ * Parameters as a builder adds them, with their placeholders. `bytes` names the columns that hold
54
+ * bytes: a value for one of them, in the form the grid shows bytes in, goes into the SQL as the
55
+ * bytes it stands for, not as those letters.
56
+ */
32
57
  export declare class Parameters {
33
58
  private readonly dialect;
59
+ private readonly bytes;
34
60
  readonly values: unknown[];
35
- constructor(dialect: Dialect);
36
- add: (value: unknown) => string;
61
+ constructor(dialect: Dialect, bytes?: ReadonlySet<string>);
62
+ add: (value: unknown, column?: string) => string;
37
63
  }
38
64
  /**
39
65
  * The WHERE clause for the filters, every value a parameter. The database reads a parameter as
@@ -53,8 +79,29 @@ export interface ChangeStatement {
53
79
  shown: string;
54
80
  /** Updates and deletes must change exactly one row. */
55
81
  oneRow: boolean;
82
+ /**
83
+ * For an update that says what its cells held when they were edited: the SELECT that reads
84
+ * them now and holds the row until the transaction ends, and what they held, in its order.
85
+ */
86
+ check?: {
87
+ text: string;
88
+ values: (string | null)[];
89
+ columns: string[];
90
+ original: (string | null)[];
91
+ };
56
92
  }
57
- export declare function changeStatement(dialect: Dialect, target: string, change: Change): ChangeStatement;
93
+ /**
94
+ * A change as the statement that makes it. `bytes` names the table's columns that hold bytes,
95
+ * where the database doesn't read their shown form itself.
96
+ */
97
+ export declare function changeStatement(dialect: Dialect, target: string, change: Change, bytes?: ReadonlySet<string>): ChangeStatement;
98
+ /**
99
+ * Refuses an update whose cells no longer hold what they did when the person edited them.
100
+ * `current` is the row the statement's check read, as the grid would show it, or nothing when the
101
+ * row is gone. The check reads inside the update's transaction and holds the row, so what it sees
102
+ * is what the update would overwrite.
103
+ */
104
+ export declare function refuseChanged(dialect: Dialect, statement: ChangeStatement, current: readonly (string | null)[] | undefined): void;
58
105
  /** A whole number for SQL text, which only ever takes numbers checked here. */
59
106
  export declare function whole(value: number): number;
60
107
  /** A name in double quotes, as PostgreSQL and SQLite take them. */
@@ -1,12 +1,39 @@
1
1
  import { DbAdminError } from "../errors.js";
2
- /** Parameters as a builder adds them, with their placeholders. */
2
+ /** How long an export may wait between two batches for its reader, unless a driver is told otherwise. */
3
+ export const EXPORT_IDLE_MS = 300_000;
4
+ /** Milliseconds as people read them: `10 s`, `0.3 s`. */
5
+ export function seconds(ms) {
6
+ return `${(ms / 1000).toLocaleString('en', { maximumFractionDigits: 1 })} s`;
7
+ }
8
+ /**
9
+ * What an export says when it's ended because nobody read it. A browser that stays away mid-file
10
+ * would otherwise hold what the export holds (a connection, a cursor's rows, a lock on the file)
11
+ * for as long as it stays away.
12
+ */
13
+ export function stalledExport(idleMs) {
14
+ return new DbAdminError(`The export waited ${seconds(idleMs)} for its rows to be read, and was stopped so its connection is free for others.`, 408, 'export-stalled');
15
+ }
16
+ /** No column holds bytes, or the database reads their shown form itself. */
17
+ const NO_BYTES = new Set();
18
+ /**
19
+ * Parameters as a builder adds them, with their placeholders. `bytes` names the columns that hold
20
+ * bytes: a value for one of them, in the form the grid shows bytes in, goes into the SQL as the
21
+ * bytes it stands for, not as those letters.
22
+ */
3
23
  export class Parameters {
4
24
  dialect;
25
+ bytes;
5
26
  values = [];
6
- constructor(dialect) {
27
+ constructor(dialect, bytes = NO_BYTES) {
7
28
  this.dialect = dialect;
29
+ this.bytes = bytes;
8
30
  }
9
- add = (value) => {
31
+ add = (value, column) => {
32
+ if (column !== undefined && typeof value === 'string' && this.bytes.has(column)) {
33
+ const literal = this.dialect.bytes?.(value);
34
+ if (literal != null)
35
+ return literal;
36
+ }
10
37
  if (this.dialect.inline)
11
38
  return this.dialect.inline(value);
12
39
  this.values.push(value);
@@ -28,22 +55,24 @@ const SEARCHES = {
28
55
  export function where(dialect, filters, parameters) {
29
56
  const conditions = filters.map(({ column, operator, value }) => {
30
57
  const name = dialect.identifier(column);
58
+ // Compared with the column itself, so as the column's kind: bytes where it holds bytes.
59
+ const add = (compared) => parameters.add(compared, column);
31
60
  if (COMPARISONS.has(operator))
32
- return `${name} ${operator} ${parameters.add(value)}`;
61
+ return `${name} ${operator} ${add(value)}`;
33
62
  switch (operator) {
34
63
  case 'is null':
35
64
  return `${name} IS NULL`;
36
65
  case 'is not null':
37
66
  return `${name} IS NOT NULL`;
38
67
  case 'in':
39
- return dialect.inList(name, value, parameters.add);
68
+ return dialect.inList(name, value, add);
40
69
  case 'not in':
41
- return `NOT (${dialect.inList(name, value, parameters.add)})`;
70
+ return `NOT (${dialect.inList(name, value, add)})`;
42
71
  case 'between':
43
72
  case 'not between': {
44
73
  const [low, high] = value;
45
74
  const not = operator === 'not between' ? 'NOT ' : '';
46
- return `${name} ${not}BETWEEN ${parameters.add(low)} AND ${parameters.add(high)}`;
75
+ return `${name} ${not}BETWEEN ${add(low)} AND ${add(high)}`;
47
76
  }
48
77
  case 'like':
49
78
  return `${dialect.text(name)} LIKE ${parameters.add(value)}`;
@@ -72,43 +101,91 @@ function escapeLike(value, escape) {
72
101
  .map((c) => (c === '%' || c === '_' || c === escape ? `${escape}${c}` : c))
73
102
  .join('');
74
103
  }
75
- export function changeStatement(dialect, target, change) {
104
+ /**
105
+ * SQL with its values: `build` writes the text, calling `value` where each one goes. They become
106
+ * parameters to run, or go into the text where the driver sends none, and literals to show.
107
+ */
108
+ function written(dialect, bytes, build) {
76
109
  const values = [];
77
- const shownValues = [];
78
- const runValues = [];
110
+ const slots = [];
79
111
  // Placeholders are numbered here and swapped afterwards: `?` alone can't be told apart.
80
- const parameter = (value) => {
81
- values.push(value);
82
- shownValues.push(dialect.literal(value));
83
- runValues.push(dialect.inline ? dialect.inline(value) : dialect.placeholder(values.length));
84
- return `\uE000${values.length}\uE000`;
112
+ const text = build((column, value) => {
113
+ const literal = value !== null && bytes.has(column) ? (dialect.bytes?.(value) ?? null) : null;
114
+ if (literal !== null)
115
+ slots.push({ run: literal, shown: literal });
116
+ else if (dialect.inline) {
117
+ slots.push({ run: dialect.inline(value), shown: dialect.literal(value) });
118
+ }
119
+ else {
120
+ values.push(value);
121
+ slots.push({ run: dialect.placeholder(values.length), shown: dialect.literal(value) });
122
+ }
123
+ return `\uE000${slots.length}\uE000`;
124
+ });
125
+ // A private-use character: no SQL a driver builds contains one.
126
+ const mark = /\uE000(\d+)\uE000/g;
127
+ return {
128
+ text: text.replace(mark, (_, n) => slots[Number(n) - 1].run),
129
+ values,
130
+ shown: text.replace(mark, (_, n) => slots[Number(n) - 1].shown),
85
131
  };
86
- const assignments = (record) => Object.entries(record).map(([column, value]) => `${dialect.identifier(column)} = ${parameter(value)}`);
87
- let text;
132
+ }
133
+ /**
134
+ * A change as the statement that makes it. `bytes` names the table's columns that hold bytes,
135
+ * where the database doesn't read their shown form itself.
136
+ */
137
+ export function changeStatement(dialect, target, change, bytes = NO_BYTES) {
88
138
  if (change.type === 'insert') {
89
139
  const columns = Object.keys(change.values);
90
- text = columns.length
91
- ? `INSERT INTO ${target} (${columns.map(dialect.identifier).join(', ')}) VALUES (${columns.map((c) => parameter(change.values[c])).join(', ')})`
92
- : `INSERT INTO ${target} ${dialect.insertDefaults}`;
140
+ const statement = written(dialect, bytes, (value) => columns.length
141
+ ? `INSERT INTO ${target} (${columns.map(dialect.identifier).join(', ')}) VALUES (${columns.map((c) => value(c, change.values[c])).join(', ')})`
142
+ : `INSERT INTO ${target} ${dialect.insertDefaults}`);
143
+ return { ...statement, oneRow: false };
93
144
  }
94
- else {
95
- // SET before WHERE, in the order the SQL has them: `?` placeholders bind by position.
96
- const set = change.type === 'update' ? assignments(change.values).join(', ') : '';
97
- const key = assignments(change.key).join(' AND ');
98
- text =
99
- change.type === 'update'
100
- ? `UPDATE ${target} SET ${set} WHERE ${key}`
101
- : `DELETE FROM ${target} WHERE ${key}`;
102
- }
103
- // A private-use character: no SQL a driver builds contains one.
104
- const mark = /\uE000(\d+)\uE000/g;
145
+ const assignments = (value) => (record) => Object.entries(record).map(([column, v]) => `${dialect.identifier(column)} = ${value(column, v)}`);
146
+ // SET before WHERE, in the order the SQL has them: `?` placeholders bind by position.
147
+ const statement = written(dialect, bytes, (value) => {
148
+ const set = change.type === 'update' ? assignments(value)(change.values).join(', ') : '';
149
+ const key = assignments(value)(change.key).join(' AND ');
150
+ return change.type === 'update'
151
+ ? `UPDATE ${target} SET ${set} WHERE ${key}`
152
+ : `DELETE FROM ${target} WHERE ${key}`;
153
+ });
154
+ const original = change.type === 'update' ? change.original : undefined;
155
+ if (!original || !Object.keys(original).length)
156
+ return { ...statement, oneRow: true };
157
+ const columns = Object.keys(original);
158
+ const { text, values } = written(dialect, bytes, (value) => `SELECT ${columns.map(dialect.identifier).join(', ')} FROM ${target} WHERE ${assignments(value)(change.key).join(' AND ')}${dialect.forUpdate}`);
105
159
  return {
106
- text: text.replace(mark, (_, n) => runValues[Number(n) - 1]),
107
- values: dialect.inline ? [] : values,
108
- shown: text.replace(mark, (_, n) => shownValues[Number(n) - 1]),
109
- oneRow: change.type !== 'insert',
160
+ ...statement,
161
+ oneRow: true,
162
+ check: { text, values, columns, original: columns.map((c) => original[c] ?? null) },
110
163
  };
111
164
  }
165
+ /**
166
+ * Refuses an update whose cells no longer hold what they did when the person edited them.
167
+ * `current` is the row the statement's check read, as the grid would show it, or nothing when the
168
+ * row is gone. The check reads inside the update's transaction and holds the row, so what it sees
169
+ * is what the update would overwrite.
170
+ */
171
+ export function refuseChanged(dialect, statement, current) {
172
+ const check = statement.check;
173
+ if (!check)
174
+ return;
175
+ if (!current) {
176
+ throw new DbAdminError(`Nothing was saved: its row is gone or its key changed.\n${statement.shown}`, 409, 'conflict');
177
+ }
178
+ const show = (value) => {
179
+ const literal = dialect.literal(value);
180
+ return literal.length > 200 ? `${literal.slice(0, 200)}…` : literal;
181
+ };
182
+ check.columns.forEach((column, i) => {
183
+ const now = current[i] ?? null;
184
+ if (now === check.original[i])
185
+ return;
186
+ throw new DbAdminError(`Nothing was saved: ${column} was changed by someone else meanwhile, from ${show(check.original[i])} to ${show(now)}.\n${statement.shown}`, 409, 'conflict');
187
+ });
188
+ }
112
189
  /** A whole number for SQL text, which only ever takes numbers checked here. */
113
190
  export function whole(value) {
114
191
  if (!Number.isSafeInteger(value) || value < 0) {
@@ -1 +1 @@
1
- {"version":3,"file":"sql.js","sourceRoot":"","sources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I,CAAA;QAC9B,IAAI,GAAG,KAAK,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;YAAE,OAAM;QACrC,MAAM,IAAI,YAAY,CACpB,sBAAsB,MAAM,gDAAgD,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAE,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,SAAS,CAAC,KAAK,EAAE,EAC3I,GAAG,EACH,UAAU,CACX,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,YAAY,CAAC,GAAG,KAAK,wBAAwB,EAAE,GAAG,EAAE,aAAa,CAAC,CAAA;IAC9E,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,WAAW,CAAC,UAAkB;IAC5C,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/E,MAAM,IAAI,YAAY,CAAC,IAAI,UAAU,iBAAiB,EAAE,GAAG,EAAE,aAAa,CAAC,CAAA;IAC7E,CAAC;IACD,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAA;AAChD,CAAC"}
@@ -1,6 +1,30 @@
1
1
  import type { Driver } from '../types.ts';
2
2
  /** A value as a SQLite literal, for statements shown to people and SQL exports. */
3
3
  export declare function sqliteLiteral(value: string | null): string;
4
+ /**
5
+ * Bytes as the grid shows them, `X'1F'`, as the literal that is those bytes; null for anything
6
+ * else. SQLite never finds a blob equal to text, so a blob key compared with those letters
7
+ * matches no row.
8
+ */
9
+ export declare function sqliteBytes(shown: string): string | null;
10
+ /**
11
+ * The one of those a statement would set, by its tokens: past comments, EXPLAIN [QUERY PLAN] (a
12
+ * PRAGMA acts when it's prepared, explained or not) and a schema's name, however the name is
13
+ * quoted. Null for any other statement, and for a PRAGMA that only reads. Self-contained.
14
+ */
15
+ export declare function processWidePragma(sql: string): string | null;
16
+ /**
17
+ * In a process: for someone's own SQL. A statement can stay inside SQLite for as long as it
18
+ * likes (a recursive query with no end), where nothing stops a thread: stopping the thread stops
19
+ * its JavaScript, and there is none running. A process can be killed, and what it held on the
20
+ * file goes with it.
21
+ *
22
+ * The statement runs on a thread of that process, so its first thread is never inside SQLite:
23
+ * it always hears that the server has gone (stopped, crashed, killed), and ends the process,
24
+ * statement and all. Left to one thread, a statement with no end would outlive the server that
25
+ * asked for it, and keep a processor for itself. Exported for its test.
26
+ */
27
+ export declare const SQLITE_PROCESS_WORKER: string;
4
28
  /** The file a `sqlite:` or `file:` address names. */
5
29
  export declare function sqlitePath(url: string): string;
6
30
  /**
@@ -8,4 +32,12 @@ export declare function sqlitePath(url: string): string;
8
32
  * time on a worker thread: a long one never blocks the host, and stopping the worker stops it.
9
33
  * Read access opens the file read-only.
10
34
  */
11
- export declare function sqliteDriver(url: string): Driver;
35
+ export declare function sqliteDriver(url: string, options?: SqliteOptions): Driver;
36
+ /** How the SQLite driver behaves, where the defaults don't suit. */
37
+ export interface SqliteOptions {
38
+ /**
39
+ * How long an export may sit between two batches, its rows unread, before it's ended and lets
40
+ * go of the file. Defaults to 5 minutes.
41
+ */
42
+ exportIdleMs?: number;
43
+ }