@procedures/sqlite 0.2.3 → 0.4.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 +74 -0
  2. package/README.md +147 -17
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +5 -3
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +8 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/module.d.ts +13 -2
  9. package/dist/module.d.ts.map +1 -1
  10. package/dist/module.js +135 -18
  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 +28 -2
  50. package/src/module.ts +167 -19
  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 +270 -2
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 sqlite.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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C3B,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
+ // sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate and sqlite.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 sqlite.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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C,CAAC,QAAQ,CAAC,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.4.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.4.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.4.0",
74
+ "@procedures/store": "0.4.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
@@ -10,8 +10,8 @@
10
10
  *
11
11
  * const client = new Client(...);
12
12
  *
13
- * // Execute a query
14
- * const result = await client.call(["db", "query"], {
13
+ * // Execute a query (sqlite.query: the old name db.query is a deprecated alias)
14
+ * const result = await client.call(["sqlite", "query"], {
15
15
  * sql: "SELECT * FROM logs WHERE level = ?",
16
16
  * params: ["error"]
17
17
  * });
@@ -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 sqlite.* 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,11 +3,14 @@
3
3
  *
4
4
  * Procedure Registration for SQLite
5
5
  *
6
- * Registers database and logging procedures with the client system.
6
+ * The module has the database and logging procedures: sqlite.query, sqlite.execute,
7
+ * sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate, sqlite.backup, logs.store
8
+ * and logs.query. The old names db.* stay for one release as deprecated aliases (refer to
9
+ * `deprecatedDbAlias`).
7
10
  * This file is referenced by package.json's client.procedures field.
8
11
  */
9
12
 
10
- import { createProcedure, zodAdapter, defineModule, type ProcedureModule, PROCEDURE_REGISTRY, registerModules, type ProcedureRegistry } from "@procedures/client";
13
+ import { createProcedure, zodAdapter, defineModule, type AnyProcedure, type ProcedureModule, PROCEDURE_REGISTRY, registerModules, type ProcedureRegistry } from "@procedures/client";
11
14
  import { z } from "zod";
12
15
  import {
13
16
  withConnection,
@@ -15,6 +18,7 @@ import {
15
18
  execute,
16
19
  lastInsertRowId,
17
20
  runMigrations,
21
+ tableExists,
18
22
  type Migration,
19
23
  } from "@mark1russell7/docker-sqlite";
20
24
  import {
@@ -31,6 +35,29 @@ import {
31
35
  type LogEntry,
32
36
  type LogLevel,
33
37
  } from "./types.js";
38
+ import { dbTables, dbSchema, dbTransaction, dbMigrate, dbBackup } from "./procedures/db/index.js";
39
+ import {
40
+ DbTablesInputSchema,
41
+ DbTablesOutputSchema,
42
+ DbSchemaInputSchema,
43
+ DbSchemaOutputSchema,
44
+ DbTransactionInputSchema,
45
+ DbTransactionOutputSchema,
46
+ DbMigrateInputSchema,
47
+ DbMigrateOutputSchema,
48
+ DbBackupInputSchema,
49
+ DbBackupOutputSchema,
50
+ type DbTablesInput,
51
+ type DbTablesOutput,
52
+ type DbSchemaInput,
53
+ type DbSchemaOutput,
54
+ type DbTransactionInput,
55
+ type DbTransactionOutput,
56
+ type DbMigrateInput,
57
+ type DbMigrateOutput,
58
+ type DbBackupInput,
59
+ type DbBackupOutput,
60
+ } from "./types.js";
34
61
 
35
62
  // =============================================================================
36
63
  // Schemas
@@ -39,10 +66,11 @@ import {
39
66
  // Real schemas (roadmap 2.1): the input is validated before the handler, and the MCP tool list
40
67
  // and the help of mark describe each field. zodAdapter<T> checks each schema against its type.
41
68
 
69
+ // The description is the same on each computer: it does not show the home folder of this one
42
70
  const dbPathField = z
43
71
  .string()
44
72
  .optional()
45
- .describe(`The SQLite database file (default: ${DEFAULT_DB_PATH}). A query does not make a missing file.`);
73
+ .describe("The SQLite database file (default: the CLI log database, ~/logs/cli/cli.db). A query does not make a missing file.");
46
74
  const paramsField = z
47
75
  .array(z.unknown())
48
76
  .optional()
@@ -165,23 +193,25 @@ async function ensureLogsTable(dbPath: string): Promise<void> {
165
193
  }
166
194
 
167
195
  // =============================================================================
168
- // db.query Procedure
196
+ // sqlite.query Procedure
169
197
  // =============================================================================
170
198
 
171
- const dbQueryProcedure = createProcedure()
172
- .path(["db", "query"])
199
+ const sqliteQueryProcedure = createProcedure()
200
+ .path(["sqlite", "query"])
173
201
  .input(dbQueryInputSchema)
174
202
  .output(dbQueryOutputSchema)
175
203
  .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"] },
204
+ description: "Execute a SQL SELECT query (read only: a statement that changes data fails)",
205
+ // PRAGMA query_only refuses each statement that changes the database, so the procedure only
206
+ // reads (CAP-1). Before, any statement ran, and the file kept its changes.
207
+ effects: { reads: ["fs"], writes: [] },
179
208
  })
180
209
  .handler(async (input: DbQueryInput): Promise<DbQueryOutput> => {
181
210
  const dbPath = input.dbPath ?? DEFAULT_DB_PATH;
182
211
  const params = input.params ?? [];
183
212
 
184
213
  return withConnection({ dbPath }, (db) => {
214
+ db.run("PRAGMA query_only = 1");
185
215
  const result = query(db, input.sql, params);
186
216
  return {
187
217
  columns: result.columns,
@@ -192,11 +222,11 @@ const dbQueryProcedure = createProcedure()
192
222
  .build();
193
223
 
194
224
  // =============================================================================
195
- // db.execute Procedure
225
+ // sqlite.execute Procedure
196
226
  // =============================================================================
197
227
 
198
- const dbExecuteProcedure = createProcedure()
199
- .path(["db", "execute"])
228
+ const sqliteExecuteProcedure = createProcedure()
229
+ .path(["sqlite", "execute"])
200
230
  .input(dbExecuteInputSchema)
201
231
  .output(dbExecuteOutputSchema)
202
232
  .meta({
@@ -287,16 +317,18 @@ const logsQueryProcedure = createProcedure()
287
317
  .output(logsQueryOutputSchema)
288
318
  .meta({
289
319
  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 },
320
+ // A missing file or table gives no entries, and the query makes neither (CAP-3). Before, the
321
+ // first query made the database file and its table.
322
+ effects: { reads: ["fs"], writes: [] },
292
323
  })
293
324
  .handler(async (input: LogsQueryInput): Promise<LogsQueryOutput> => {
294
325
  const dbPath = input.dbPath ?? DEFAULT_DB_PATH;
295
326
 
296
- // Ensure table exists
297
- await ensureLogsTable(dbPath);
298
-
299
327
  return withConnection({ dbPath }, (db) => {
328
+ if (!tableExists(db, "logs")) {
329
+ return { logs: [] };
330
+ }
331
+ db.run("PRAGMA query_only = 1");
300
332
  const conditions: string[] = [];
301
333
  const params: unknown[] = [];
302
334
 
@@ -382,6 +414,121 @@ const logsQueryProcedure = createProcedure()
382
414
  })
383
415
  .build();
384
416
 
417
+ // =============================================================================
418
+ // sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate and sqlite.backup (roadmap R44)
419
+ // =============================================================================
420
+
421
+ const sqliteTablesProcedure = createProcedure()
422
+ .path(["sqlite", "tables"])
423
+ .input(zodAdapter<DbTablesInput>(DbTablesInputSchema))
424
+ .output(zodAdapter<DbTablesOutput>(DbTablesOutputSchema))
425
+ .meta({
426
+ description: "The tables and views of a database (read only: a missing file is an error)",
427
+ effects: { reads: ["fs"], writes: [] },
428
+ args: [],
429
+ shorts: {},
430
+ output: "json",
431
+ })
432
+ .handler(async (input: DbTablesInput): Promise<DbTablesOutput> => dbTables(input))
433
+ .build();
434
+
435
+ const sqliteSchemaProcedure = createProcedure()
436
+ .path(["sqlite", "schema"])
437
+ .input(zodAdapter<DbSchemaInput>(DbSchemaInputSchema))
438
+ .output(zodAdapter<DbSchemaOutput>(DbSchemaOutputSchema))
439
+ .meta({
440
+ description: "The columns, indexes and foreign keys of a table (read only)",
441
+ effects: { reads: ["fs"], writes: [] },
442
+ args: ["table"],
443
+ shorts: {},
444
+ output: "json",
445
+ })
446
+ .handler(async (input: DbSchemaInput): Promise<DbSchemaOutput> => dbSchema(input))
447
+ .build();
448
+
449
+ const sqliteTransactionProcedure = createProcedure()
450
+ .path(["sqlite", "transaction"])
451
+ .input(zodAdapter<DbTransactionInput>(DbTransactionInputSchema))
452
+ .output(zodAdapter<DbTransactionOutput>(DbTransactionOutputSchema))
453
+ .meta({
454
+ description: "Run statements in one transaction: they all take effect, or none does",
455
+ // The statements of the caller can change or delete any row
456
+ effects: { reads: ["fs"], writes: ["fs"] },
457
+ args: [],
458
+ shorts: {},
459
+ output: "json",
460
+ })
461
+ .handler(async (input: DbTransactionInput): Promise<DbTransactionOutput> => dbTransaction(input))
462
+ .build();
463
+
464
+ const sqliteMigrateProcedure = createProcedure()
465
+ .path(["sqlite", "migrate"])
466
+ .input(zodAdapter<DbMigrateInput>(DbMigrateInputSchema))
467
+ .output(zodAdapter<DbMigrateOutput>(DbMigrateOutputSchema))
468
+ .meta({
469
+ description: "Apply the numbered migrations (a folder or a list) that a database does not have, in one transaction",
470
+ // A migration can drop a table: destructive. A second call applies nothing.
471
+ effects: { reads: ["fs"], writes: ["fs"], idempotent: true },
472
+ args: [],
473
+ shorts: {},
474
+ output: "json",
475
+ })
476
+ .handler(async (input: DbMigrateInput): Promise<DbMigrateOutput> => dbMigrate(input))
477
+ .build();
478
+
479
+ const sqliteBackupProcedure = createProcedure()
480
+ .path(["sqlite", "backup"])
481
+ .input(zodAdapter<DbBackupInput>(DbBackupInputSchema))
482
+ .output(zodAdapter<DbBackupOutput>(DbBackupOutputSchema))
483
+ .meta({
484
+ description: "Copy a database to a new file (it does not replace a file)",
485
+ // It makes a new file, and never replaces one. A second call finds the file and fails.
486
+ effects: { reads: ["fs"], writes: ["fs"], destructive: false, idempotent: false },
487
+ args: ["to"],
488
+ shorts: {},
489
+ output: "json",
490
+ })
491
+ .handler(async (input: DbBackupInput): Promise<DbBackupOutput> => dbBackup(input))
492
+ .build();
493
+
494
+ // =============================================================================
495
+ // The deprecated db.* aliases (one release)
496
+ // =============================================================================
497
+
498
+ /** The procedures of the `sqlite` namespace. Each one has a deprecated alias in `db`. */
499
+ const sqliteProcedures: readonly AnyProcedure[] = [
500
+ sqliteQueryProcedure,
501
+ sqliteExecuteProcedure,
502
+ sqliteTablesProcedure,
503
+ sqliteSchemaProcedure,
504
+ sqliteTransactionProcedure,
505
+ sqliteMigrateProcedure,
506
+ sqliteBackupProcedure,
507
+ ];
508
+
509
+ /**
510
+ * This function gives the deprecated alias `db.<name>` of the procedure `sqlite.<name>`. The alias
511
+ * has the same schemas, handler and effects. Its description names the new procedure. The aliases
512
+ * go away in the next release. The MCP server does not expose them (`bundle-mcp`).
513
+ */
514
+ export function deprecatedDbAlias(procedure: AnyProcedure): AnyProcedure {
515
+ const name = procedure.path.slice(1).join(".");
516
+ const description = typeof procedure.metadata.description === "string" ? ` ${procedure.metadata.description}` : "";
517
+ return {
518
+ ...procedure,
519
+ path: ["db", ...procedure.path.slice(1)],
520
+ metadata: {
521
+ ...procedure.metadata,
522
+ description: `Deprecated: use sqlite.${name}.${description}`,
523
+ deprecated: true,
524
+ deprecationMessage: `Use sqlite.${name}. The alias db.${name} goes away in the next release.`,
525
+ },
526
+ };
527
+ }
528
+
529
+ /** The deprecated aliases `db.query`, `db.execute`, `db.tables`, `db.schema`, `db.transaction`, `db.migrate` and `db.backup`. */
530
+ export const dbAliasProcedures: readonly AnyProcedure[] = sqliteProcedures.map(deprecatedDbAlias);
531
+
385
532
  // =============================================================================
386
533
  // Registration
387
534
  // =============================================================================
@@ -391,10 +538,11 @@ const logsQueryProcedure = createProcedure()
391
538
  * file registers nothing. (`register.ts` registers the same procedures in the global registry.)
392
539
  */
393
540
  export const sqliteModule: ProcedureModule = defineModule("@procedures/sqlite", [
394
- dbQueryProcedure,
395
- dbExecuteProcedure,
541
+ ...sqliteProcedures,
396
542
  logsStoreProcedure,
397
543
  logsQueryProcedure,
544
+ // The old names, for one release
545
+ ...dbAliasProcedures,
398
546
  ]);
399
547
 
400
548
  export default sqliteModule;