@procedures/sqlite 0.2.3 → 0.3.0

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 (59) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +126 -4
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +6 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/module.d.ts +2 -1
  9. package/dist/module.d.ts.map +1 -1
  10. package/dist/module.js +93 -9
  11. package/dist/module.js.map +1 -1
  12. package/dist/procedures/db/backup.d.ts +19 -0
  13. package/dist/procedures/db/backup.d.ts.map +1 -0
  14. package/dist/procedures/db/backup.js +61 -0
  15. package/dist/procedures/db/backup.js.map +1 -0
  16. package/dist/procedures/db/index.d.ts +6 -0
  17. package/dist/procedures/db/index.d.ts.map +1 -0
  18. package/dist/procedures/db/index.js +6 -0
  19. package/dist/procedures/db/index.js.map +1 -0
  20. package/dist/procedures/db/migrate.d.ts +24 -0
  21. package/dist/procedures/db/migrate.d.ts.map +1 -0
  22. package/dist/procedures/db/migrate.js +110 -0
  23. package/dist/procedures/db/migrate.js.map +1 -0
  24. package/dist/procedures/db/schema.d.ts +13 -0
  25. package/dist/procedures/db/schema.d.ts.map +1 -0
  26. package/dist/procedures/db/schema.js +33 -0
  27. package/dist/procedures/db/schema.js.map +1 -0
  28. package/dist/procedures/db/shared.d.ts +25 -0
  29. package/dist/procedures/db/shared.d.ts.map +1 -0
  30. package/dist/procedures/db/shared.js +105 -0
  31. package/dist/procedures/db/shared.js.map +1 -0
  32. package/dist/procedures/db/tables.d.ts +12 -0
  33. package/dist/procedures/db/tables.d.ts.map +1 -0
  34. package/dist/procedures/db/tables.js +22 -0
  35. package/dist/procedures/db/tables.js.map +1 -0
  36. package/dist/procedures/db/transaction.d.ts +14 -0
  37. package/dist/procedures/db/transaction.d.ts.map +1 -0
  38. package/dist/procedures/db/transaction.js +44 -0
  39. package/dist/procedures/db/transaction.js.map +1 -0
  40. package/dist/storage.d.ts +123 -0
  41. package/dist/storage.d.ts.map +1 -0
  42. package/dist/storage.js +525 -0
  43. package/dist/storage.js.map +1 -0
  44. package/dist/types.d.ts +131 -0
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/types.js +109 -0
  47. package/dist/types.js.map +1 -1
  48. package/package.json +9 -3
  49. package/src/index.ts +26 -0
  50. package/src/module.ts +120 -9
  51. package/src/procedures/db/backup.ts +62 -0
  52. package/src/procedures/db/index.ts +5 -0
  53. package/src/procedures/db/migrate.ts +121 -0
  54. package/src/procedures/db/schema.ts +71 -0
  55. package/src/procedures/db/shared.ts +109 -0
  56. package/src/procedures/db/tables.ts +27 -0
  57. package/src/procedures/db/transaction.ts +43 -0
  58. package/src/storage.ts +591 -0
  59. package/src/types.ts +268 -0
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Type definitions for client-sqlite procedures
3
3
  */
4
+ import { z } from "zod";
4
5
  /**
5
6
  * Default database path for CLI logs
6
7
  */
@@ -88,4 +89,134 @@ export interface LogsQueryOutput {
88
89
  /** Total count (if limit was applied) */
89
90
  total?: number | undefined;
90
91
  }
92
+ export interface DbTablesInput {
93
+ /** Also the views (default true) */
94
+ views?: boolean | undefined;
95
+ dbPath?: string | undefined;
96
+ }
97
+ export interface DbTableInfo {
98
+ name: string;
99
+ type: "table" | "view";
100
+ /** The SQL that made the table or the view */
101
+ sql: string | null;
102
+ }
103
+ export interface DbTablesOutput {
104
+ tables: DbTableInfo[];
105
+ }
106
+ export declare const DbTablesInputSchema: z.ZodType<DbTablesInput>;
107
+ export declare const DbTablesOutputSchema: z.ZodType<DbTablesOutput>;
108
+ export interface DbSchemaInput {
109
+ /** The table or the view */
110
+ table: string;
111
+ dbPath?: string | undefined;
112
+ }
113
+ export interface DbColumnInfo {
114
+ name: string;
115
+ /** The declared type, or "" */
116
+ type: string;
117
+ notNull: boolean;
118
+ /** The SQL of the default value, or null */
119
+ defaultValue: string | null;
120
+ /** The position of the column in the primary key, from 1, or 0 */
121
+ primaryKey: number;
122
+ }
123
+ export interface DbIndexInfo {
124
+ name: string;
125
+ unique: boolean;
126
+ /** "c": CREATE INDEX, "u": a UNIQUE constraint, "pk": the PRIMARY KEY */
127
+ origin: string;
128
+ partial: boolean;
129
+ columns: string[];
130
+ }
131
+ export interface DbForeignKeyInfo {
132
+ /** The column of this table */
133
+ from: string;
134
+ /** The table that the key refers to */
135
+ table: string;
136
+ /** The column of that table, or null for its primary key */
137
+ to: string | null;
138
+ onUpdate: string;
139
+ onDelete: string;
140
+ }
141
+ export interface DbSchemaOutput {
142
+ table: string;
143
+ type: "table" | "view";
144
+ sql: string | null;
145
+ columns: DbColumnInfo[];
146
+ indexes: DbIndexInfo[];
147
+ foreignKeys: DbForeignKeyInfo[];
148
+ }
149
+ export declare const DbSchemaInputSchema: z.ZodType<DbSchemaInput>;
150
+ export declare const DbSchemaOutputSchema: z.ZodType<DbSchemaOutput>;
151
+ export interface DbStatement {
152
+ /** One SQL statement, or several without params */
153
+ sql: string;
154
+ /** The values of the ? placeholders */
155
+ params?: unknown[] | undefined;
156
+ }
157
+ export interface DbTransactionInput {
158
+ statements: DbStatement[];
159
+ dbPath?: string | undefined;
160
+ }
161
+ export interface DbStatementResult {
162
+ /** The rows that the statements changed */
163
+ changes: number;
164
+ /** The rows of a statement that gives rows (SELECT, or RETURNING) */
165
+ rows?: Record<string, unknown>[] | undefined;
166
+ }
167
+ export interface DbTransactionOutput {
168
+ /** One result for each statement of the input */
169
+ results: DbStatementResult[];
170
+ /** The rows that the transaction changed */
171
+ changes: number;
172
+ }
173
+ export declare const DbTransactionInputSchema: z.ZodType<DbTransactionInput>;
174
+ export declare const DbTransactionOutputSchema: z.ZodType<DbTransactionOutput>;
175
+ export interface DbMigration {
176
+ /** The number of the migration: the migrations run in the order of their numbers */
177
+ version: number;
178
+ /** A name for people */
179
+ name?: string | undefined;
180
+ /** The SQL of the migration (several statements are allowed) */
181
+ sql: string;
182
+ }
183
+ export interface DbMigrateInput {
184
+ /** A folder of files named <number>_<name>.sql (not with migrations) */
185
+ folder?: string | undefined;
186
+ /** The migrations (not with folder) */
187
+ migrations?: DbMigration[] | undefined;
188
+ /** The table of the applied migrations (default schema_migrations) */
189
+ table?: string | undefined;
190
+ dbPath?: string | undefined;
191
+ }
192
+ export interface DbMigrateOutput {
193
+ /** The migrations that this call applied, in order */
194
+ applied: Array<{
195
+ version: number;
196
+ name: string;
197
+ }>;
198
+ /** The number of migrations that the database had already */
199
+ skipped: number;
200
+ /** The highest applied version, or 0 */
201
+ version: number;
202
+ }
203
+ /** The name of a table that the SQL of db.migrate names: letters, digits and underscores. */
204
+ export declare const MIGRATION_TABLE_PATTERN: RegExp;
205
+ export declare const DbMigrateInputSchema: z.ZodType<DbMigrateInput>;
206
+ export declare const DbMigrateOutputSchema: z.ZodType<DbMigrateOutput>;
207
+ export interface DbBackupInput {
208
+ /** The new file. The procedure does not replace a file. */
209
+ to: string;
210
+ /** Also compact the copy (VACUUM): it leaves out the free pages */
211
+ vacuum?: boolean | undefined;
212
+ dbPath?: string | undefined;
213
+ }
214
+ export interface DbBackupOutput {
215
+ /** The path of the copy */
216
+ path: string;
217
+ /** The size of the copy, in bytes */
218
+ bytes: number;
219
+ }
220
+ export declare const DbBackupInputSchema: z.ZodType<DbBackupInput>;
221
+ export declare const DbBackupOutputSchema: z.ZodType<DbBackupOutput>;
91
222
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,MAAiD,CAAC;AAMhF,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAErE,eAAO,MAAM,UAAU,EAAE,SAAS,QAAQ,EA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B,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,KAAK,EAAE,MAAM,CAAC;CACf;AAED,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAIvD,CAAC;AAEH,eAAO,MAAM,oBAAoB,EAAE,CAAC,CAAC,OAAO,CAAC,cAAc,CAGzD,CAAC"}
package/dist/types.js CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import { homedir } from "node:os";
5
5
  import { join } from "node:path";
6
+ import { z } from "zod";
6
7
  // =============================================================================
7
8
  // Configuration
8
9
  // =============================================================================
@@ -17,4 +18,112 @@ export const LOG_LEVELS = [
17
18
  "warn",
18
19
  "error",
19
20
  ];
21
+ // =============================================================================
22
+ // db.tables, db.schema, db.transaction, db.migrate and db.backup (roadmap R44)
23
+ // =============================================================================
24
+ /** The `dbPath` field of the R44 procedures. Its description does not show the home folder. */
25
+ const dbPath = z.string().min(1).optional().describe("The SQLite database file (default: the CLI log database, ~/logs/cli/cli.db)");
26
+ export const DbTablesInputSchema = z.object({
27
+ views: z.boolean().optional().describe("Also give the views (default true)"),
28
+ dbPath,
29
+ });
30
+ export const DbTablesOutputSchema = z.looseObject({
31
+ tables: z
32
+ .array(z.looseObject({
33
+ name: z.string().describe("The name of the table or the view"),
34
+ type: z.enum(["table", "view"]).describe("table or view"),
35
+ sql: z.string().nullable().describe("The SQL that made the table or the view"),
36
+ }))
37
+ .describe("The tables (and the views), in the order of their names, without the internal sqlite_ tables"),
38
+ });
39
+ export const DbSchemaInputSchema = z.object({
40
+ table: z.string().min(1).describe("The table or the view"),
41
+ dbPath,
42
+ });
43
+ export const DbSchemaOutputSchema = z.looseObject({
44
+ table: z.string().describe("The table or the view"),
45
+ type: z.enum(["table", "view"]).describe("table or view"),
46
+ sql: z.string().nullable().describe("The SQL that made the table or the view"),
47
+ columns: z
48
+ .array(z.looseObject({
49
+ name: z.string().describe("The column"),
50
+ type: z.string().describe('The declared type, or ""'),
51
+ notNull: z.boolean().describe("True for NOT NULL"),
52
+ defaultValue: z.string().nullable().describe("The SQL of the default value, or null"),
53
+ primaryKey: z.number().describe("The position of the column in the primary key, from 1, or 0"),
54
+ }))
55
+ .describe("The columns, in their order"),
56
+ indexes: z
57
+ .array(z.looseObject({
58
+ name: z.string().describe("The index"),
59
+ unique: z.boolean().describe("True for a unique index"),
60
+ origin: z.string().describe('"c": CREATE INDEX, "u": a UNIQUE constraint, "pk": the PRIMARY KEY'),
61
+ partial: z.boolean().describe("True for an index with a WHERE clause"),
62
+ columns: z.array(z.string()).describe("The columns of the index, in order"),
63
+ }))
64
+ .describe("The indexes of the table"),
65
+ foreignKeys: z
66
+ .array(z.looseObject({
67
+ from: z.string().describe("The column of this table"),
68
+ table: z.string().describe("The table that the key refers to"),
69
+ to: z.string().nullable().describe("The column of that table, or null for its primary key"),
70
+ onUpdate: z.string().describe("The action ON UPDATE"),
71
+ onDelete: z.string().describe("The action ON DELETE"),
72
+ }))
73
+ .describe("The foreign keys of the table"),
74
+ });
75
+ export const DbTransactionInputSchema = z.object({
76
+ statements: z
77
+ .array(z.object({
78
+ sql: z.string().min(1).describe("One SQL statement, or several without params. BEGIN, COMMIT and ROLLBACK are refused."),
79
+ params: z.array(z.unknown()).optional().describe("The values of the ? placeholders, in order"),
80
+ }))
81
+ .min(1)
82
+ .describe("The statements, in order: they all take effect, or none does"),
83
+ dbPath,
84
+ });
85
+ export const DbTransactionOutputSchema = z.looseObject({
86
+ results: z
87
+ .array(z.looseObject({
88
+ changes: z.number().describe("The rows that the statement changed"),
89
+ rows: z.array(z.record(z.string(), z.unknown())).optional().describe("The rows of a statement that gives rows (SELECT, or RETURNING)"),
90
+ }))
91
+ .describe("One result for each statement of the input"),
92
+ changes: z.number().describe("The rows that the transaction changed"),
93
+ });
94
+ /** The name of a table that the SQL of db.migrate names: letters, digits and underscores. */
95
+ export const MIGRATION_TABLE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
96
+ export const DbMigrateInputSchema = z.object({
97
+ folder: z.string().min(1).optional().describe("A folder of files named <number>_<name>.sql, for example 001_create_users.sql (not with migrations)"),
98
+ migrations: z
99
+ .array(z.object({
100
+ version: z.number().int().min(1).describe("The number of the migration: the migrations run in the order of their numbers"),
101
+ name: z.string().optional().describe("A name for people"),
102
+ sql: z.string().min(1).describe("The SQL of the migration (several statements are allowed)"),
103
+ }))
104
+ .optional()
105
+ .describe("The migrations (not with folder)"),
106
+ table: z
107
+ .string()
108
+ .regex(MIGRATION_TABLE_PATTERN, "letters, digits and underscores, and not a digit first")
109
+ .optional()
110
+ .describe("The table of the applied migrations (default schema_migrations)"),
111
+ dbPath,
112
+ });
113
+ export const DbMigrateOutputSchema = z.looseObject({
114
+ applied: z
115
+ .array(z.looseObject({ version: z.number().describe("The number"), name: z.string().describe("The name") }))
116
+ .describe("The migrations that this call applied, in order"),
117
+ skipped: z.number().describe("The number of migrations that the database had already"),
118
+ version: z.number().describe("The highest applied version, or 0"),
119
+ });
120
+ export const DbBackupInputSchema = z.object({
121
+ to: z.string().min(1).describe("The new file. The procedure does not replace a file."),
122
+ vacuum: z.boolean().optional().describe("Also compact the copy (VACUUM): it leaves out the free pages"),
123
+ dbPath,
124
+ });
125
+ export const DbBackupOutputSchema = z.looseObject({
126
+ path: z.string().describe("The path of the copy"),
127
+ bytes: z.number().describe("The size of the copy, in bytes"),
128
+ });
20
129
  //# sourceMappingURL=types.js.map
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,gFAAgF;AAChF,gBAAgB;AAChB,gFAAgF;AAEhF;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAW,IAAI,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE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sDAAsD,CAAC;IACtF,MAAM,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,8DAA8D,CAAC;IACvG,MAAM;CACP,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,oBAAoB,GAA8B,CAAC,CAAC,WAAW,CAAC;IAC3E,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,sBAAsB,CAAC;IACjD,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,gCAAgC,CAAC;CAC7D,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@procedures/sqlite",
4
- "version": "0.2.3",
5
- "description": "SQLite procedures with sql.js and no native build: db.query, db.execute and structured logs (logs.store, logs.query), safe for concurrent writes.",
4
+ "version": "0.3.0",
5
+ "description": "SQLite procedures with sql.js and no native build: queries, tables and schemas, transactions, migrations, backups and structured logs, safe for concurrent writes, and SqliteStorage, a durable collection storage with a change log.",
6
6
  "keywords": [
7
7
  "sqlite",
8
8
  "sql.js",
@@ -36,6 +36,10 @@
36
36
  "./register": {
37
37
  "types": "./dist/register.d.ts",
38
38
  "import": "./dist/register.js"
39
+ },
40
+ "./storage": {
41
+ "types": "./dist/storage.d.ts",
42
+ "import": "./dist/storage.js"
39
43
  }
40
44
  },
41
45
  "files": [
@@ -60,12 +64,14 @@
60
64
  "procedures": "./dist/register.js"
61
65
  },
62
66
  "dependencies": {
63
- "@procedures/client": "0.2.3",
67
+ "@procedures/client": "0.3.0",
64
68
  "@mark1russell7/docker-sqlite": "^0.1.0",
65
69
  "zod": "^4.6.5"
66
70
  },
67
71
  "devDependencies": {
68
72
  "@mark1russell7/cue": "^1.0.0",
73
+ "@procedures/collections": "0.3.0",
74
+ "@procedures/store": "0.3.0",
69
75
  "@types/node": "^22.0.0",
70
76
  "typescript": "^7.0.2",
71
77
  "vitest": "^5.0.3"
package/src/index.ts CHANGED
@@ -49,16 +49,42 @@ export type {
49
49
  LogsQueryOutput,
50
50
  LogEntry,
51
51
  LogLevel,
52
+ DbTablesInput,
53
+ DbTablesOutput,
54
+ DbTableInfo,
55
+ DbSchemaInput,
56
+ DbSchemaOutput,
57
+ DbColumnInfo,
58
+ DbIndexInfo,
59
+ DbForeignKeyInfo,
60
+ DbStatement,
61
+ DbStatementResult,
62
+ DbTransactionInput,
63
+ DbTransactionOutput,
64
+ DbMigration,
65
+ DbMigrateInput,
66
+ DbMigrateOutput,
67
+ DbBackupInput,
68
+ DbBackupOutput,
52
69
  } from "./types.js";
53
70
 
54
71
  export { DEFAULT_DB_PATH, LOG_LEVELS } from "./types.js";
55
72
 
73
+ // The db.* procedures of roadmap R44, for direct use
74
+ export { dbTables, dbSchema, dbTransaction, dbMigrate, dbBackup } from "./procedures/db/index.js";
75
+
56
76
  // =============================================================================
57
77
  // Registration
58
78
  // =============================================================================
59
79
 
60
80
  export { registerSqliteProcedures } from "./module.js";
61
81
 
82
+ // =============================================================================
83
+ // The collection storage (roadmap R45). The entry @procedures/sqlite/storage has it alone.
84
+ // =============================================================================
85
+
86
+ export { SqliteStorage, checkTableName, MEMORY_PATH, type SqliteStorageOptions } from "./storage.js";
87
+
62
88
  // =============================================================================
63
89
  // Re-export docker-sqlite utilities for direct use
64
90
  // =============================================================================
package/src/module.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * Procedure Registration for SQLite
5
5
  *
6
- * Registers database and logging procedures with the client system.
6
+ * Registers database and logging procedures with the client system: db.query, db.execute,
7
+ * db.tables, db.schema, db.transaction, db.migrate, db.backup, logs.store and logs.query.
7
8
  * This file is referenced by package.json's client.procedures field.
8
9
  */
9
10
 
@@ -15,6 +16,7 @@ import {
15
16
  execute,
16
17
  lastInsertRowId,
17
18
  runMigrations,
19
+ tableExists,
18
20
  type Migration,
19
21
  } from "@mark1russell7/docker-sqlite";
20
22
  import {
@@ -31,6 +33,29 @@ import {
31
33
  type LogEntry,
32
34
  type LogLevel,
33
35
  } from "./types.js";
36
+ import { dbTables, dbSchema, dbTransaction, dbMigrate, dbBackup } from "./procedures/db/index.js";
37
+ import {
38
+ DbTablesInputSchema,
39
+ DbTablesOutputSchema,
40
+ DbSchemaInputSchema,
41
+ DbSchemaOutputSchema,
42
+ DbTransactionInputSchema,
43
+ DbTransactionOutputSchema,
44
+ DbMigrateInputSchema,
45
+ DbMigrateOutputSchema,
46
+ DbBackupInputSchema,
47
+ DbBackupOutputSchema,
48
+ type DbTablesInput,
49
+ type DbTablesOutput,
50
+ type DbSchemaInput,
51
+ type DbSchemaOutput,
52
+ type DbTransactionInput,
53
+ type DbTransactionOutput,
54
+ type DbMigrateInput,
55
+ type DbMigrateOutput,
56
+ type DbBackupInput,
57
+ type DbBackupOutput,
58
+ } from "./types.js";
34
59
 
35
60
  // =============================================================================
36
61
  // Schemas
@@ -173,15 +198,17 @@ const dbQueryProcedure = createProcedure()
173
198
  .input(dbQueryInputSchema)
174
199
  .output(dbQueryOutputSchema)
175
200
  .meta({
176
- description: "Execute a SQL SELECT query",
177
- // The SQL is not limited to SELECT: the first statement runs, and the file keeps its changes
178
- effects: { reads: ["fs"], writes: ["fs"] },
201
+ description: "Execute a SQL SELECT query (read only: a statement that changes data fails)",
202
+ // PRAGMA query_only refuses each statement that changes the database, so the procedure only
203
+ // reads (CAP-1). Before, any statement ran, and the file kept its changes.
204
+ effects: { reads: ["fs"], writes: [] },
179
205
  })
180
206
  .handler(async (input: DbQueryInput): Promise<DbQueryOutput> => {
181
207
  const dbPath = input.dbPath ?? DEFAULT_DB_PATH;
182
208
  const params = input.params ?? [];
183
209
 
184
210
  return withConnection({ dbPath }, (db) => {
211
+ db.run("PRAGMA query_only = 1");
185
212
  const result = query(db, input.sql, params);
186
213
  return {
187
214
  columns: result.columns,
@@ -287,16 +314,18 @@ const logsQueryProcedure = createProcedure()
287
314
  .output(logsQueryOutputSchema)
288
315
  .meta({
289
316
  description: "Query log entries from the database",
290
- // It makes the database file and the logs table when they are missing
291
- effects: { reads: ["fs"], writes: ["fs"], destructive: false, idempotent: true },
317
+ // A missing file or table gives no entries, and the query makes neither (CAP-3). Before, the
318
+ // first query made the database file and its table.
319
+ effects: { reads: ["fs"], writes: [] },
292
320
  })
293
321
  .handler(async (input: LogsQueryInput): Promise<LogsQueryOutput> => {
294
322
  const dbPath = input.dbPath ?? DEFAULT_DB_PATH;
295
323
 
296
- // Ensure table exists
297
- await ensureLogsTable(dbPath);
298
-
299
324
  return withConnection({ dbPath }, (db) => {
325
+ if (!tableExists(db, "logs")) {
326
+ return { logs: [] };
327
+ }
328
+ db.run("PRAGMA query_only = 1");
300
329
  const conditions: string[] = [];
301
330
  const params: unknown[] = [];
302
331
 
@@ -382,6 +411,83 @@ const logsQueryProcedure = createProcedure()
382
411
  })
383
412
  .build();
384
413
 
414
+ // =============================================================================
415
+ // db.tables, db.schema, db.transaction, db.migrate and db.backup (roadmap R44)
416
+ // =============================================================================
417
+
418
+ const dbTablesProcedure = createProcedure()
419
+ .path(["db", "tables"])
420
+ .input(zodAdapter<DbTablesInput>(DbTablesInputSchema))
421
+ .output(zodAdapter<DbTablesOutput>(DbTablesOutputSchema))
422
+ .meta({
423
+ description: "The tables and views of a database (read only: a missing file is an error)",
424
+ effects: { reads: ["fs"], writes: [] },
425
+ args: [],
426
+ shorts: {},
427
+ output: "json",
428
+ })
429
+ .handler(async (input: DbTablesInput): Promise<DbTablesOutput> => dbTables(input))
430
+ .build();
431
+
432
+ const dbSchemaProcedure = createProcedure()
433
+ .path(["db", "schema"])
434
+ .input(zodAdapter<DbSchemaInput>(DbSchemaInputSchema))
435
+ .output(zodAdapter<DbSchemaOutput>(DbSchemaOutputSchema))
436
+ .meta({
437
+ description: "The columns, indexes and foreign keys of a table (read only)",
438
+ effects: { reads: ["fs"], writes: [] },
439
+ args: ["table"],
440
+ shorts: {},
441
+ output: "json",
442
+ })
443
+ .handler(async (input: DbSchemaInput): Promise<DbSchemaOutput> => dbSchema(input))
444
+ .build();
445
+
446
+ const dbTransactionProcedure = createProcedure()
447
+ .path(["db", "transaction"])
448
+ .input(zodAdapter<DbTransactionInput>(DbTransactionInputSchema))
449
+ .output(zodAdapter<DbTransactionOutput>(DbTransactionOutputSchema))
450
+ .meta({
451
+ description: "Run statements in one transaction: they all take effect, or none does",
452
+ // The statements of the caller can change or delete any row
453
+ effects: { reads: ["fs"], writes: ["fs"] },
454
+ args: [],
455
+ shorts: {},
456
+ output: "json",
457
+ })
458
+ .handler(async (input: DbTransactionInput): Promise<DbTransactionOutput> => dbTransaction(input))
459
+ .build();
460
+
461
+ const dbMigrateProcedure = createProcedure()
462
+ .path(["db", "migrate"])
463
+ .input(zodAdapter<DbMigrateInput>(DbMigrateInputSchema))
464
+ .output(zodAdapter<DbMigrateOutput>(DbMigrateOutputSchema))
465
+ .meta({
466
+ description: "Apply the numbered migrations (a folder or a list) that a database does not have, in one transaction",
467
+ // A migration can drop a table: destructive. A second call applies nothing.
468
+ effects: { reads: ["fs"], writes: ["fs"], idempotent: true },
469
+ args: [],
470
+ shorts: {},
471
+ output: "json",
472
+ })
473
+ .handler(async (input: DbMigrateInput): Promise<DbMigrateOutput> => dbMigrate(input))
474
+ .build();
475
+
476
+ const dbBackupProcedure = createProcedure()
477
+ .path(["db", "backup"])
478
+ .input(zodAdapter<DbBackupInput>(DbBackupInputSchema))
479
+ .output(zodAdapter<DbBackupOutput>(DbBackupOutputSchema))
480
+ .meta({
481
+ description: "Copy a database to a new file (it does not replace a file)",
482
+ // It makes a new file, and never replaces one. A second call finds the file and fails.
483
+ effects: { reads: ["fs"], writes: ["fs"], destructive: false, idempotent: false },
484
+ args: ["to"],
485
+ shorts: {},
486
+ output: "json",
487
+ })
488
+ .handler(async (input: DbBackupInput): Promise<DbBackupOutput> => dbBackup(input))
489
+ .build();
490
+
385
491
  // =============================================================================
386
492
  // Registration
387
493
  // =============================================================================
@@ -395,6 +501,11 @@ export const sqliteModule: ProcedureModule = defineModule("@procedures/sqlite",
395
501
  dbExecuteProcedure,
396
502
  logsStoreProcedure,
397
503
  logsQueryProcedure,
504
+ dbTablesProcedure,
505
+ dbSchemaProcedure,
506
+ dbTransactionProcedure,
507
+ dbMigrateProcedure,
508
+ dbBackupProcedure,
398
509
  ]);
399
510
 
400
511
  export default sqliteModule;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * db.backup procedure (roadmap R44)
3
+ *
4
+ * A copy of a database in a new file. The package runs SQLite in WebAssembly (sql.js): the
5
+ * database is in the memory of the module, and `VACUUM INTO` and the backup API write into the
6
+ * file system of the module, not to the disk. So the procedure takes the image of the database
7
+ * (`export()`, the serialized database) in the file queue of the database, where no other call
8
+ * of this process changes it. It writes the image to a file next to the target, checks it with
9
+ * `PRAGMA integrity_check`, and renames it.
10
+ *
11
+ * - The procedure does not replace a file, and it does not write the source.
12
+ * - With `vacuum: true`, it compacts the copy (VACUUM on the copy): the result of `VACUUM INTO`.
13
+ */
14
+
15
+ import { existsSync } from "node:fs";
16
+ import { rename, stat, unlink, writeFile } from "node:fs/promises";
17
+ import { resolve } from "node:path";
18
+ import { query, withConnection } from "@mark1russell7/docker-sqlite";
19
+ import type { DbBackupInput, DbBackupOutput } from "../../types.js";
20
+ import { databasePath, requireDatabaseFile } from "./shared.js";
21
+
22
+ /** The key of a file path: Windows file names do not depend on case. */
23
+ function fileKey(path: string): string {
24
+ const absolute = resolve(path);
25
+ return process.platform === "win32" ? absolute.toLowerCase() : absolute;
26
+ }
27
+
28
+ /**
29
+ * Copy a database to a new file
30
+ */
31
+ export async function dbBackup(input: DbBackupInput): Promise<DbBackupOutput> {
32
+ const source = databasePath(input.dbPath);
33
+ requireDatabaseFile("db.backup", source);
34
+ const target = resolve(input.to);
35
+ if (source !== ":memory:" && fileKey(source) === fileKey(target)) {
36
+ throw new Error("db.backup: the copy cannot be the database file itself");
37
+ }
38
+ if (existsSync(target)) {
39
+ const error = new Error(`db.backup: the file exists already: ${target}. The procedure does not replace a file.`) as NodeJS.ErrnoException;
40
+ error.code = "EEXIST";
41
+ throw error;
42
+ }
43
+
44
+ const image = await withConnection({ dbPath: source }, (db) => db.export());
45
+ const temporary = `${target}.${process.pid}.${Date.now()}.tmp`;
46
+ try {
47
+ await writeFile(temporary, image);
48
+ if (input.vacuum) {
49
+ // The VACUUM changes the copy only: withConnection saves it to the temporary file
50
+ await withConnection({ dbPath: temporary }, (db) => db.run("VACUUM"));
51
+ }
52
+ const check = await withConnection({ dbPath: temporary }, (db) => query<{ integrity_check: string }>(db, "PRAGMA integrity_check").rows);
53
+ const verdict = check.map((row) => row.integrity_check).join("; ");
54
+ if (verdict !== "ok") throw new Error(`db.backup: the copy is not valid: ${verdict}`);
55
+ if (existsSync(target)) throw new Error(`db.backup: the file exists already: ${target}. The procedure does not replace a file.`);
56
+ await rename(temporary, target);
57
+ } catch (error) {
58
+ await unlink(temporary).catch(() => undefined);
59
+ throw error;
60
+ }
61
+ return { path: target, bytes: (await stat(target)).size };
62
+ }
@@ -0,0 +1,5 @@
1
+ export { dbTables } from "./tables.js";
2
+ export { dbSchema } from "./schema.js";
3
+ export { dbTransaction } from "./transaction.js";
4
+ export { dbMigrate } from "./migrate.js";
5
+ export { dbBackup } from "./backup.js";