inferative 0.2.0 → 0.3.2

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.
@@ -1,4 +1,5 @@
1
1
  import { n as DatabaseSession, t as DatabaseDialect } from "./database-session-CZpfIBpG.mjs";
2
+ import { t as MysqlScopeSql } from "./scope-sql-CaOv1V5P.mjs";
2
3
  import { t as MysqlLoadOrder } from "./mysql-load-order-Crg3OJVM.mjs";
3
4
  import { createConnection } from "mysql2/promise";
4
5
  //#region src/backup/mysql-columns.ts
@@ -207,6 +208,8 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
207
208
  schema: "",
208
209
  table: MysqlSession.journalTable
209
210
  };
211
+ /** See {@link DatabaseSession.scopeSql}. */
212
+ scopeSql = new MysqlScopeSql();
210
213
  connection;
211
214
  /** The tables that the open snapshot locked, in the order of their names. */
212
215
  locked;
@@ -418,12 +421,13 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
418
421
  * See {@link DatabaseSession.rows}. A table with a primary key reads in pages of {@link fetchSize} rows in the
419
422
  * order of the key, each page after the last key of the one before; a table with none reads in one statement.
420
423
  */
421
- async *rows(table) {
424
+ async *rows(table, filter) {
422
425
  const facts = this.factsOf(table);
423
426
  const values = table.columns.map((column) => MysqlColumns.readExpression(column.name, column.type));
424
- const name = MysqlColumns.quote(table.name);
427
+ const name = `${MysqlColumns.quote(table.name)}${filter === void 0 ? "" : ` AS ${filter.alias}`}`;
428
+ const kept = filter === void 0 ? [] : [`(${filter.condition})`];
425
429
  if (facts.primaryKey.length === 0) {
426
- yield* await this.values(`SELECT ${values.join(", ")} FROM ${name}`);
430
+ yield* await this.values(`SELECT ${values.join(", ")} FROM ${name}${MysqlSession.whereOf(kept)}`);
427
431
  return;
428
432
  }
429
433
  const keys = facts.primaryKey.map((column) => MysqlColumns.cursorRead(column.name, column.type));
@@ -433,7 +437,7 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
433
437
  const page = `ORDER BY ${order} LIMIT ${String(MysqlSession.fetchSize)}`;
434
438
  let last;
435
439
  for (;;) {
436
- const rows = last === void 0 ? await this.values(`${select} ${page}`) : await this.values(`${select} WHERE ${after} ${page}`, last);
440
+ const rows = last === void 0 ? await this.values(`${select}${MysqlSession.whereOf(kept)} ${page}`) : await this.values(`${select}${MysqlSession.whereOf([...kept, after])} ${page}`, last);
437
441
  for (const row of rows) yield row.slice(0, values.length);
438
442
  if (rows.length < MysqlSession.fetchSize) return;
439
443
  last = rows.at(-1)?.slice(values.length);
@@ -484,19 +488,21 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
484
488
  return state.isCalled === false && /^[0-9]{1,20}$/u.test(state.lastValue) && BigInt(state.lastValue) >= 1n && BigInt(state.lastValue) <= MysqlSession.largestCounter;
485
489
  }
486
490
  /** See {@link DatabaseSession.secretKeyCounts}. */
487
- async secretKeyCounts(table) {
491
+ async secretKeyCounts(table, filter) {
488
492
  if (!await this.hasTable(table)) return;
493
+ const name = `${MysqlColumns.quote(table)}${filter === void 0 ? "" : ` AS ${filter.alias}`}`;
489
494
  return {
490
495
  schema: "",
491
- counts: (await this.values(`SELECT key_id, CAST(COUNT(*) AS CHAR) FROM ${MysqlColumns.quote(table)} GROUP BY key_id ORDER BY key_id`)).map((row) => ({
496
+ counts: (await this.values(`SELECT key_id, CAST(COUNT(*) AS CHAR) FROM ${name}${MysqlSession.whereOf(filter === void 0 ? [] : [`(${filter.condition})`])} GROUP BY key_id ORDER BY key_id`)).map((row) => ({
492
497
  keyId: MysqlSession.text(row[0]) ?? "",
493
498
  values: Number(MysqlSession.text(row[1]) ?? "0")
494
499
  }))
495
500
  };
496
501
  }
497
502
  /** See {@link DatabaseSession.secretSample}. */
498
- async secretSample(_schema, table, keyId) {
499
- const [row] = await this.values(`SELECT reference, envelope FROM ${MysqlColumns.quote(table)} WHERE key_id = ? LIMIT 1`, [keyId]);
503
+ async secretSample(_schema, table, keyId, filter) {
504
+ const name = `${MysqlColumns.quote(table)}${filter === void 0 ? "" : ` AS ${filter.alias}`}`;
505
+ const [row] = await this.values(`SELECT reference, envelope FROM ${name}${MysqlSession.whereOf([...filter === void 0 ? [] : [`(${filter.condition})`], "key_id = ?"])} LIMIT 1`, [keyId]);
500
506
  return row === void 0 ? void 0 : {
501
507
  reference: MysqlSession.text(row[0]) ?? "",
502
508
  envelope: MysqlSession.text(row[1]) ?? ""
@@ -531,7 +537,7 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
531
537
  }
532
538
  /** See {@link DatabaseSession.startLoad}. */
533
539
  async startLoad(targets, sequences) {
534
- const { MysqlLoad } = await import("./mysql-load-Cal8mOLJ.mjs");
540
+ const { MysqlLoad } = await import("./mysql-load-D5w8rWRx.mjs");
535
541
  return MysqlLoad.start(this, await this.catalog(), targets, sequences);
536
542
  }
537
543
  /** @returns The facts of a table of the last catalog read. */
@@ -648,6 +654,10 @@ var MysqlSession = class MysqlSession extends DatabaseSession {
648
654
  }
649
655
  return [...indexes.values()].map((index) => `${index.head} parts=${JSON.stringify(index.parts)}`);
650
656
  }
657
+ /** @returns The `WHERE` clause of the conditions, all of which must hold, or nothing for none. */
658
+ static whereOf(conditions) {
659
+ return conditions.length === 0 ? "" : ` WHERE ${conditions.join(" AND ")}`;
660
+ }
651
661
  /** @returns The name of each table that holds rows, the migrations journal included, in the order of their names. */
652
662
  async lockableTables() {
653
663
  return (await this.values("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_TYPE = 'BASE TABLE'")).map((row) => MysqlSession.text(row[0]) ?? "").sort(MysqlSession.byCodeUnits);
@@ -1,5 +1,5 @@
1
- import { t as PgSession } from "./pg-session-CfrvvW7D.mjs";
2
- import { n as TableRows, t as DatabaseLoad } from "./database-load-DtoThqg4.mjs";
1
+ import { t as PgSession } from "./pg-session-D3UKJzIv.mjs";
2
+ import { n as TableRows, t as DatabaseLoad } from "./database-load-m_JValnc.mjs";
3
3
  //#region src/backup/pg-load.ts
4
4
  /**
5
5
  * Writes every row of a Postgres restore in one transaction, in the order of the archive, with the foreign keys
@@ -1,4 +1,5 @@
1
1
  import { n as DatabaseSession, t as DatabaseDialect } from "./database-session-CZpfIBpG.mjs";
2
+ import { n as PgScopeSql } from "./scope-sql-CaOv1V5P.mjs";
2
3
  import pg from "pg";
3
4
  //#region ../base/server/persistence/pg/migrations/pg-connection-string.ts
4
5
  /**
@@ -97,6 +98,8 @@ var PgSession = class PgSession extends DatabaseSession {
97
98
  schema: PgSession.journalSchema,
98
99
  table: PgSession.journalTable
99
100
  };
101
+ /** See {@link DatabaseSession.scopeSql}. */
102
+ scopeSql = new PgScopeSql();
100
103
  client;
101
104
  cursorCount = 0;
102
105
  constructor(client) {
@@ -129,6 +132,10 @@ var PgSession = class PgSession extends DatabaseSession {
129
132
  static qualified(schema, name) {
130
133
  return `${PgSession.quote(schema)}.${PgSession.quote(name)}`;
131
134
  }
135
+ /** @returns The alias and the `WHERE` clause of a filter, or nothing for no filter. */
136
+ static whereOf(filter) {
137
+ return filter === void 0 ? "" : ` AS ${filter.alias} WHERE ${filter.condition}`;
138
+ }
132
139
  /**
133
140
  * Applies the migrations of a folder with drizzle's own migrator, on a connection of its own: the session's search
134
141
  * path names `pg_catalog` alone.
@@ -323,15 +330,16 @@ var PgSession = class PgSession extends DatabaseSession {
323
330
  WHERE t.typtype = 'e' AND ${PgSession.userSchema}`)).map((row) => row.line).sort();
324
331
  }
325
332
  /**
326
- * Reads every row of a table through a cursor. It needs an open transaction.
333
+ * Reads every row of a table, or the rows that a filter keeps, through a cursor. It needs an open transaction.
327
334
  *
328
335
  * @param table - The table.
336
+ * @param filter - The rows to read; all of them when absent.
329
337
  * @returns The text values of each row, in the order of the table's columns.
330
338
  */
331
- async *rows(table) {
339
+ async *rows(table, filter) {
332
340
  const cursor = `backup_rows_${String(this.cursorCount += 1)}`;
333
341
  const values = table.columns.map((column) => `${PgSession.quote(column.name)}::text`);
334
- await this.client.query(`DECLARE ${cursor} NO SCROLL CURSOR FOR SELECT ${values.join(", ")} FROM ${PgSession.qualified(table.schema, table.name)}`);
342
+ await this.client.query(`DECLARE ${cursor} NO SCROLL CURSOR FOR SELECT ${values.join(", ")} FROM ${PgSession.qualified(table.schema, table.name)}${PgSession.whereOf(filter)}`);
335
343
  try {
336
344
  for (;;) {
337
345
  const result = await this.client.query({
@@ -389,11 +397,11 @@ var PgSession = class PgSession extends DatabaseSession {
389
397
  return typeof state.isCalled === "boolean" && /^-?[0-9]{1,19}$/u.test(state.lastValue);
390
398
  }
391
399
  /** See {@link DatabaseSession.secretKeyCounts}. */
392
- async secretKeyCounts(table) {
400
+ async secretKeyCounts(table, filter) {
393
401
  const [found] = await this.query(`SELECT n.nspname AS schema FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
394
402
  WHERE c.relname = $1 AND c.relkind = 'r' ORDER BY n.nspname = 'public' DESC, n.nspname LIMIT 1`, [table]);
395
403
  if (found === void 0) return;
396
- const rows = await this.query(`SELECT key_id, count(*)::int AS value_count FROM ${PgSession.qualified(found.schema, table)}
404
+ const rows = await this.query(`SELECT key_id, count(*)::int AS value_count FROM ${PgSession.qualified(found.schema, table)}${PgSession.whereOf(filter)}
397
405
  GROUP BY key_id ORDER BY key_id`);
398
406
  return {
399
407
  schema: found.schema,
@@ -404,8 +412,9 @@ var PgSession = class PgSession extends DatabaseSession {
404
412
  };
405
413
  }
406
414
  /** See {@link DatabaseSession.secretSample}. */
407
- async secretSample(schema, table, keyId) {
408
- const [sample] = await this.query(`SELECT reference, envelope FROM ${PgSession.qualified(schema, table)} WHERE key_id = $1 LIMIT 1`, [keyId]);
415
+ async secretSample(schema, table, keyId, filter) {
416
+ const where = filter === void 0 ? " WHERE" : ` AS ${filter.alias} WHERE (${filter.condition}) AND`;
417
+ const [sample] = await this.query(`SELECT reference, envelope FROM ${PgSession.qualified(schema, table)}${where} key_id = $1 LIMIT 1`, [keyId]);
409
418
  return sample;
410
419
  }
411
420
  /** See {@link DatabaseSession.hasTable}. */
@@ -442,7 +451,7 @@ var PgSession = class PgSession extends DatabaseSession {
442
451
  }
443
452
  /** See {@link DatabaseSession.startLoad}. */
444
453
  async startLoad(_targets, sequences) {
445
- const { PgLoad } = await import("./pg-load-gKCdo5wI.mjs");
454
+ const { PgLoad } = await import("./pg-load-BFCk8dau.mjs");
446
455
  return PgLoad.start(this, sequences);
447
456
  }
448
457
  };
@@ -0,0 +1,61 @@
1
+ //#region src/backup/scope-sql.ts
2
+ /**
3
+ * The SQL of the conditions of an organization backup on Postgres. A text column compares under `COLLATE "C"`: the
4
+ * libraries mix `text` and `text COLLATE "C"` columns, and Postgres refuses to compare two implicit collations.
5
+ */
6
+ var PgScopeSql = class PgScopeSql {
7
+ /** The types that take a collation, as `format_type` names them. */
8
+ static textType = /^(?:text|character varying|character|varchar)\b/u;
9
+ /** @returns The quoted identifier. */
10
+ static quote(name) {
11
+ return `"${name.replaceAll("\"", "\"\"")}"`;
12
+ }
13
+ /** See {@link ScopeSql.table}. */
14
+ table(schema, name) {
15
+ return schema === "" ? PgScopeSql.quote(name) : `${PgScopeSql.quote(schema)}.${PgScopeSql.quote(name)}`;
16
+ }
17
+ /** See {@link ScopeSql.key}. */
18
+ key(alias, column) {
19
+ const reference = `${alias}.${PgScopeSql.quote(column.name)}`;
20
+ return PgScopeSql.textType.test(column.type) ? `${reference} COLLATE "C"` : reference;
21
+ }
22
+ /** See {@link ScopeSql.literal}. */
23
+ literal(value) {
24
+ return `'${value.replaceAll("'", "''")}'`;
25
+ }
26
+ /** See {@link ScopeSql.startsWith}. */
27
+ startsWith(alias, column, prefix) {
28
+ return `left(${this.key(alias, column)}, ${String(prefix.length)}) = ${this.literal(prefix)}`;
29
+ }
30
+ };
31
+ /**
32
+ * The SQL of the conditions of an organization backup on MySQL. A text column compares as bytes: the libraries mix
33
+ * collations, and MySQL refuses to compare two columns of different collations.
34
+ */
35
+ var MysqlScopeSql = class MysqlScopeSql {
36
+ /** The types that hold text, as the `COLUMN_TYPE` of the catalog names them. */
37
+ static textType = /^(?:var)?char\(|^(?:tiny|medium|long)?text\b/u;
38
+ /** @returns The quoted identifier. */
39
+ static quote(name) {
40
+ return `\`${name.replaceAll("`", "``")}\``;
41
+ }
42
+ /** See {@link ScopeSql.table}. */
43
+ table(_schema, name) {
44
+ return MysqlScopeSql.quote(name);
45
+ }
46
+ /** See {@link ScopeSql.key}. */
47
+ key(alias, column) {
48
+ const reference = `${alias}.${MysqlScopeSql.quote(column.name)}`;
49
+ return MysqlScopeSql.textType.test(column.type) ? `CAST(${reference} AS BINARY)` : reference;
50
+ }
51
+ /** See {@link ScopeSql.literal}. */
52
+ literal(value) {
53
+ return `'${value.replaceAll("'", "''")}'`;
54
+ }
55
+ /** See {@link ScopeSql.startsWith}. */
56
+ startsWith(alias, column, prefix) {
57
+ return `LEFT(${this.key(alias, column)}, ${String(prefix.length)}) = ${this.literal(prefix)}`;
58
+ }
59
+ };
60
+ //#endregion
61
+ export { PgScopeSql as n, MysqlScopeSql as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferative",
3
- "version": "0.2.0",
3
+ "version": "0.3.2",
4
4
  "description": "The Inferative command line: create a project, publish, audit, diagnose and import a site, back up and restore an install, connect a coding agent to the dev host, and print the secrets that a host derives from its root secret.",
5
5
  "keywords": [
6
6
  "backup",
@@ -36,23 +36,23 @@
36
36
  "@aws-sdk/client-s3": "^3.1144.0",
37
37
  "@aws-sdk/lib-storage": "^3.1144.0",
38
38
  "@aws-sdk/s3-request-presigner": "^3.1144.0",
39
- "@heximon/blob": "0.21.0",
40
- "@heximon/build": "0.21.0",
41
- "@heximon/client": "0.21.0",
42
- "@heximon/compiler": "0.21.0",
43
- "@heximon/contract": "0.21.0",
44
- "@heximon/runtime": "0.21.0",
45
- "@heximon/schema": "0.21.0",
46
- "@inferative/base": "0.2.0",
47
- "@inferative/channels": "0.2.0",
48
- "@inferative/content": "0.2.0",
49
- "@inferative/files": "0.2.0",
50
- "@inferative/pages": "0.2.0",
51
- "@inferative/sdk": "0.2.0",
52
- "@inferative/site": "0.2.0",
39
+ "@heximon/blob": "0.23.0",
40
+ "@heximon/build": "0.23.0",
41
+ "@heximon/client": "0.23.0",
42
+ "@heximon/compiler": "0.23.0",
43
+ "@heximon/contract": "0.23.0",
44
+ "@heximon/runtime": "0.23.0",
45
+ "@heximon/schema": "0.23.0",
46
+ "@inferative/base": "0.3.2",
47
+ "@inferative/channels": "0.3.2",
48
+ "@inferative/content": "0.3.2",
49
+ "@inferative/files": "0.3.2",
50
+ "@inferative/pages": "0.3.2",
51
+ "@inferative/sdk": "0.3.2",
52
+ "@inferative/site": "0.3.2",
53
53
  "age-encryption": "^0.3.1",
54
54
  "citty": "0.1.6",
55
- "create-inferative": "0.2.0",
55
+ "create-inferative": "0.3.2",
56
56
  "drizzle-orm": "^1.0.0-rc.5-169397b",
57
57
  "htmlparser2": "^12.0.0",
58
58
  "jiti": "2.7.0",