@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.
- package/CHANGELOG.md +74 -0
- package/README.md +147 -17
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/module.d.ts +13 -2
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +135 -18
- package/dist/module.js.map +1 -1
- package/dist/procedures/db/backup.d.ts +19 -0
- package/dist/procedures/db/backup.d.ts.map +1 -0
- package/dist/procedures/db/backup.js +61 -0
- package/dist/procedures/db/backup.js.map +1 -0
- package/dist/procedures/db/index.d.ts +6 -0
- package/dist/procedures/db/index.d.ts.map +1 -0
- package/dist/procedures/db/index.js +6 -0
- package/dist/procedures/db/index.js.map +1 -0
- package/dist/procedures/db/migrate.d.ts +24 -0
- package/dist/procedures/db/migrate.d.ts.map +1 -0
- package/dist/procedures/db/migrate.js +110 -0
- package/dist/procedures/db/migrate.js.map +1 -0
- package/dist/procedures/db/schema.d.ts +13 -0
- package/dist/procedures/db/schema.d.ts.map +1 -0
- package/dist/procedures/db/schema.js +33 -0
- package/dist/procedures/db/schema.js.map +1 -0
- package/dist/procedures/db/shared.d.ts +25 -0
- package/dist/procedures/db/shared.d.ts.map +1 -0
- package/dist/procedures/db/shared.js +105 -0
- package/dist/procedures/db/shared.js.map +1 -0
- package/dist/procedures/db/tables.d.ts +12 -0
- package/dist/procedures/db/tables.d.ts.map +1 -0
- package/dist/procedures/db/tables.js +22 -0
- package/dist/procedures/db/tables.js.map +1 -0
- package/dist/procedures/db/transaction.d.ts +14 -0
- package/dist/procedures/db/transaction.d.ts.map +1 -0
- package/dist/procedures/db/transaction.js +44 -0
- package/dist/procedures/db/transaction.js.map +1 -0
- package/dist/storage.d.ts +123 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +525 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +109 -0
- package/dist/types.js.map +1 -1
- package/package.json +9 -3
- package/src/index.ts +28 -2
- package/src/module.ts +167 -19
- package/src/procedures/db/backup.ts +62 -0
- package/src/procedures/db/index.ts +5 -0
- package/src/procedures/db/migrate.ts +121 -0
- package/src/procedures/db/schema.ts +71 -0
- package/src/procedures/db/shared.ts +109 -0
- package/src/procedures/db/tables.ts +27 -0
- package/src/procedures/db/transaction.ts +43 -0
- package/src/storage.ts +591 -0
- package/src/types.ts +270 -2
package/src/types.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { join } from "node:path";
|
|
7
|
+
import { z } from "zod";
|
|
7
8
|
|
|
8
9
|
// =============================================================================
|
|
9
10
|
// Configuration
|
|
@@ -29,7 +30,7 @@ export const LOG_LEVELS: readonly LogLevel[] = [
|
|
|
29
30
|
] as const;
|
|
30
31
|
|
|
31
32
|
// =============================================================================
|
|
32
|
-
//
|
|
33
|
+
// sqlite.query types
|
|
33
34
|
// =============================================================================
|
|
34
35
|
|
|
35
36
|
export interface DbQueryInput {
|
|
@@ -49,7 +50,7 @@ export interface DbQueryOutput<T = Record<string, unknown>> {
|
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
// =============================================================================
|
|
52
|
-
//
|
|
53
|
+
// sqlite.execute types
|
|
53
54
|
// =============================================================================
|
|
54
55
|
|
|
55
56
|
export interface DbExecuteInput {
|
|
@@ -133,3 +134,270 @@ export interface LogsQueryOutput {
|
|
|
133
134
|
/** Total count (if limit was applied) */
|
|
134
135
|
total?: number | undefined;
|
|
135
136
|
}
|
|
137
|
+
|
|
138
|
+
// =============================================================================
|
|
139
|
+
// sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate and sqlite.backup (roadmap R44)
|
|
140
|
+
// =============================================================================
|
|
141
|
+
|
|
142
|
+
/** The `dbPath` field of the R44 procedures. Its description does not show the home folder. */
|
|
143
|
+
const dbPath = z.string().min(1).optional().describe("The SQLite database file (default: the CLI log database, ~/logs/cli/cli.db)");
|
|
144
|
+
|
|
145
|
+
export interface DbTablesInput {
|
|
146
|
+
/** Also the views (default true) */
|
|
147
|
+
views?: boolean | undefined;
|
|
148
|
+
dbPath?: string | undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface DbTableInfo {
|
|
152
|
+
name: string;
|
|
153
|
+
type: "table" | "view";
|
|
154
|
+
/** The SQL that made the table or the view */
|
|
155
|
+
sql: string | null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface DbTablesOutput {
|
|
159
|
+
tables: DbTableInfo[];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export const DbTablesInputSchema: z.ZodType<DbTablesInput> = z.object({
|
|
163
|
+
views: z.boolean().optional().describe("Also give the views (default true)"),
|
|
164
|
+
dbPath,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
export const DbTablesOutputSchema: z.ZodType<DbTablesOutput> = z.looseObject({
|
|
168
|
+
tables: z
|
|
169
|
+
.array(
|
|
170
|
+
z.looseObject({
|
|
171
|
+
name: z.string().describe("The name of the table or the view"),
|
|
172
|
+
type: z.enum(["table", "view"]).describe("table or view"),
|
|
173
|
+
sql: z.string().nullable().describe("The SQL that made the table or the view"),
|
|
174
|
+
})
|
|
175
|
+
)
|
|
176
|
+
.describe("The tables (and the views), in the order of their names, without the internal sqlite_ tables"),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
export interface DbSchemaInput {
|
|
180
|
+
/** The table or the view */
|
|
181
|
+
table: string;
|
|
182
|
+
dbPath?: string | undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface DbColumnInfo {
|
|
186
|
+
name: string;
|
|
187
|
+
/** The declared type, or "" */
|
|
188
|
+
type: string;
|
|
189
|
+
notNull: boolean;
|
|
190
|
+
/** The SQL of the default value, or null */
|
|
191
|
+
defaultValue: string | null;
|
|
192
|
+
/** The position of the column in the primary key, from 1, or 0 */
|
|
193
|
+
primaryKey: number;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface DbIndexInfo {
|
|
197
|
+
name: string;
|
|
198
|
+
unique: boolean;
|
|
199
|
+
/** "c": CREATE INDEX, "u": a UNIQUE constraint, "pk": the PRIMARY KEY */
|
|
200
|
+
origin: string;
|
|
201
|
+
partial: boolean;
|
|
202
|
+
columns: string[];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface DbForeignKeyInfo {
|
|
206
|
+
/** The column of this table */
|
|
207
|
+
from: string;
|
|
208
|
+
/** The table that the key refers to */
|
|
209
|
+
table: string;
|
|
210
|
+
/** The column of that table, or null for its primary key */
|
|
211
|
+
to: string | null;
|
|
212
|
+
onUpdate: string;
|
|
213
|
+
onDelete: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface DbSchemaOutput {
|
|
217
|
+
table: string;
|
|
218
|
+
type: "table" | "view";
|
|
219
|
+
sql: string | null;
|
|
220
|
+
columns: DbColumnInfo[];
|
|
221
|
+
indexes: DbIndexInfo[];
|
|
222
|
+
foreignKeys: DbForeignKeyInfo[];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const DbSchemaInputSchema: z.ZodType<DbSchemaInput> = z.object({
|
|
226
|
+
table: z.string().min(1).describe("The table or the view"),
|
|
227
|
+
dbPath,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
export const DbSchemaOutputSchema: z.ZodType<DbSchemaOutput> = z.looseObject({
|
|
231
|
+
table: z.string().describe("The table or the view"),
|
|
232
|
+
type: z.enum(["table", "view"]).describe("table or view"),
|
|
233
|
+
sql: z.string().nullable().describe("The SQL that made the table or the view"),
|
|
234
|
+
columns: z
|
|
235
|
+
.array(
|
|
236
|
+
z.looseObject({
|
|
237
|
+
name: z.string().describe("The column"),
|
|
238
|
+
type: z.string().describe('The declared type, or ""'),
|
|
239
|
+
notNull: z.boolean().describe("True for NOT NULL"),
|
|
240
|
+
defaultValue: z.string().nullable().describe("The SQL of the default value, or null"),
|
|
241
|
+
primaryKey: z.number().describe("The position of the column in the primary key, from 1, or 0"),
|
|
242
|
+
})
|
|
243
|
+
)
|
|
244
|
+
.describe("The columns, in their order"),
|
|
245
|
+
indexes: z
|
|
246
|
+
.array(
|
|
247
|
+
z.looseObject({
|
|
248
|
+
name: z.string().describe("The index"),
|
|
249
|
+
unique: z.boolean().describe("True for a unique index"),
|
|
250
|
+
origin: z.string().describe('"c": CREATE INDEX, "u": a UNIQUE constraint, "pk": the PRIMARY KEY'),
|
|
251
|
+
partial: z.boolean().describe("True for an index with a WHERE clause"),
|
|
252
|
+
columns: z.array(z.string()).describe("The columns of the index, in order"),
|
|
253
|
+
})
|
|
254
|
+
)
|
|
255
|
+
.describe("The indexes of the table"),
|
|
256
|
+
foreignKeys: z
|
|
257
|
+
.array(
|
|
258
|
+
z.looseObject({
|
|
259
|
+
from: z.string().describe("The column of this table"),
|
|
260
|
+
table: z.string().describe("The table that the key refers to"),
|
|
261
|
+
to: z.string().nullable().describe("The column of that table, or null for its primary key"),
|
|
262
|
+
onUpdate: z.string().describe("The action ON UPDATE"),
|
|
263
|
+
onDelete: z.string().describe("The action ON DELETE"),
|
|
264
|
+
})
|
|
265
|
+
)
|
|
266
|
+
.describe("The foreign keys of the table"),
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
export interface DbStatement {
|
|
270
|
+
/** One SQL statement, or several without params */
|
|
271
|
+
sql: string;
|
|
272
|
+
/** The values of the ? placeholders */
|
|
273
|
+
params?: unknown[] | undefined;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface DbTransactionInput {
|
|
277
|
+
statements: DbStatement[];
|
|
278
|
+
dbPath?: string | undefined;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface DbStatementResult {
|
|
282
|
+
/** The rows that the statements changed */
|
|
283
|
+
changes: number;
|
|
284
|
+
/** The rows of a statement that gives rows (SELECT, or RETURNING) */
|
|
285
|
+
rows?: Record<string, unknown>[] | undefined;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export interface DbTransactionOutput {
|
|
289
|
+
/** One result for each statement of the input */
|
|
290
|
+
results: DbStatementResult[];
|
|
291
|
+
/** The rows that the transaction changed */
|
|
292
|
+
changes: number;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export const DbTransactionInputSchema: z.ZodType<DbTransactionInput> = z.object({
|
|
296
|
+
statements: z
|
|
297
|
+
.array(
|
|
298
|
+
z.object({
|
|
299
|
+
sql: z.string().min(1).describe("One SQL statement, or several without params. BEGIN, COMMIT and ROLLBACK are refused."),
|
|
300
|
+
params: z.array(z.unknown()).optional().describe("The values of the ? placeholders, in order"),
|
|
301
|
+
})
|
|
302
|
+
)
|
|
303
|
+
.min(1)
|
|
304
|
+
.describe("The statements, in order: they all take effect, or none does"),
|
|
305
|
+
dbPath,
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
export const DbTransactionOutputSchema: z.ZodType<DbTransactionOutput> = z.looseObject({
|
|
309
|
+
results: z
|
|
310
|
+
.array(
|
|
311
|
+
z.looseObject({
|
|
312
|
+
changes: z.number().describe("The rows that the statement changed"),
|
|
313
|
+
rows: z.array(z.record(z.string(), z.unknown())).optional().describe("The rows of a statement that gives rows (SELECT, or RETURNING)"),
|
|
314
|
+
})
|
|
315
|
+
)
|
|
316
|
+
.describe("One result for each statement of the input"),
|
|
317
|
+
changes: z.number().describe("The rows that the transaction changed"),
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
export interface DbMigration {
|
|
321
|
+
/** The number of the migration: the migrations run in the order of their numbers */
|
|
322
|
+
version: number;
|
|
323
|
+
/** A name for people */
|
|
324
|
+
name?: string | undefined;
|
|
325
|
+
/** The SQL of the migration (several statements are allowed) */
|
|
326
|
+
sql: string;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export interface DbMigrateInput {
|
|
330
|
+
/** A folder of files named <number>_<name>.sql (not with migrations) */
|
|
331
|
+
folder?: string | undefined;
|
|
332
|
+
/** The migrations (not with folder) */
|
|
333
|
+
migrations?: DbMigration[] | undefined;
|
|
334
|
+
/** The table of the applied migrations (default schema_migrations) */
|
|
335
|
+
table?: string | undefined;
|
|
336
|
+
dbPath?: string | undefined;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export interface DbMigrateOutput {
|
|
340
|
+
/** The migrations that this call applied, in order */
|
|
341
|
+
applied: Array<{ version: number; name: string }>;
|
|
342
|
+
/** The number of migrations that the database had already */
|
|
343
|
+
skipped: number;
|
|
344
|
+
/** The highest applied version, or 0 */
|
|
345
|
+
version: number;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The name of a table that the SQL of sqlite.migrate names: letters, digits and underscores. */
|
|
349
|
+
export const MIGRATION_TABLE_PATTERN: RegExp = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
350
|
+
|
|
351
|
+
export const DbMigrateInputSchema: z.ZodType<DbMigrateInput> = z.object({
|
|
352
|
+
folder: z.string().min(1).optional().describe("A folder of files named <number>_<name>.sql, for example 001_create_users.sql (not with migrations)"),
|
|
353
|
+
migrations: z
|
|
354
|
+
.array(
|
|
355
|
+
z.object({
|
|
356
|
+
version: z.number().int().min(1).describe("The number of the migration: the migrations run in the order of their numbers"),
|
|
357
|
+
name: z.string().optional().describe("A name for people"),
|
|
358
|
+
sql: z.string().min(1).describe("The SQL of the migration (several statements are allowed)"),
|
|
359
|
+
})
|
|
360
|
+
)
|
|
361
|
+
.optional()
|
|
362
|
+
.describe("The migrations (not with folder)"),
|
|
363
|
+
table: z
|
|
364
|
+
.string()
|
|
365
|
+
.regex(MIGRATION_TABLE_PATTERN, "letters, digits and underscores, and not a digit first")
|
|
366
|
+
.optional()
|
|
367
|
+
.describe("The table of the applied migrations (default schema_migrations)"),
|
|
368
|
+
dbPath,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
export const DbMigrateOutputSchema: z.ZodType<DbMigrateOutput> = z.looseObject({
|
|
372
|
+
applied: z
|
|
373
|
+
.array(z.looseObject({ version: z.number().describe("The number"), name: z.string().describe("The name") }))
|
|
374
|
+
.describe("The migrations that this call applied, in order"),
|
|
375
|
+
skipped: z.number().describe("The number of migrations that the database had already"),
|
|
376
|
+
version: z.number().describe("The highest applied version, or 0"),
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
export interface DbBackupInput {
|
|
380
|
+
/** The new file. The procedure does not replace a file. */
|
|
381
|
+
to: string;
|
|
382
|
+
/** Also compact the copy (VACUUM): it leaves out the free pages */
|
|
383
|
+
vacuum?: boolean | undefined;
|
|
384
|
+
dbPath?: string | undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface DbBackupOutput {
|
|
388
|
+
/** The path of the copy */
|
|
389
|
+
path: string;
|
|
390
|
+
/** The size of the copy, in bytes */
|
|
391
|
+
bytes: number;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export const DbBackupInputSchema: z.ZodType<DbBackupInput> = z.object({
|
|
395
|
+
to: z.string().min(1).describe("The new file. The procedure does not replace a file."),
|
|
396
|
+
vacuum: z.boolean().optional().describe("Also compact the copy (VACUUM): it leaves out the free pages"),
|
|
397
|
+
dbPath,
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
export const DbBackupOutputSchema: z.ZodType<DbBackupOutput> = z.looseObject({
|
|
401
|
+
path: z.string().describe("The path of the copy"),
|
|
402
|
+
bytes: z.number().describe("The size of the copy, in bytes"),
|
|
403
|
+
});
|