@aigne/afs-sqlite 1.11.0-beta.4 → 1.11.0-beta.6
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/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs +10 -0
- package/dist/actions/built-in.cjs +408 -15
- package/dist/actions/built-in.d.cts.map +1 -1
- package/dist/actions/built-in.d.mts.map +1 -1
- package/dist/actions/built-in.mjs +408 -15
- package/dist/actions/built-in.mjs.map +1 -1
- package/dist/actions/registry.cjs +35 -3
- package/dist/actions/registry.d.cts +36 -17
- package/dist/actions/registry.d.cts.map +1 -1
- package/dist/actions/registry.d.mts +36 -17
- package/dist/actions/registry.d.mts.map +1 -1
- package/dist/actions/registry.mjs +35 -3
- package/dist/actions/registry.mjs.map +1 -1
- package/dist/actions/types.d.cts +24 -5
- package/dist/actions/types.d.cts.map +1 -1
- package/dist/actions/types.d.mts +24 -5
- package/dist/actions/types.d.mts.map +1 -1
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts.map +1 -1
- package/dist/index.cjs +2 -3
- package/dist/index.d.cts +4 -3
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +3 -2
- package/dist/node/builder.cjs +67 -87
- package/dist/node/builder.d.cts +11 -15
- package/dist/node/builder.d.cts.map +1 -1
- package/dist/node/builder.d.mts +11 -15
- package/dist/node/builder.d.mts.map +1 -1
- package/dist/node/builder.mjs +65 -85
- package/dist/node/builder.mjs.map +1 -1
- package/dist/operations/crud.cjs +24 -68
- package/dist/operations/crud.d.cts +23 -38
- package/dist/operations/crud.d.cts.map +1 -1
- package/dist/operations/crud.d.mts +23 -38
- package/dist/operations/crud.d.mts.map +1 -1
- package/dist/operations/crud.mjs +25 -69
- package/dist/operations/crud.mjs.map +1 -1
- package/dist/operations/query-builder.d.cts.map +1 -1
- package/dist/operations/query-builder.d.mts.map +1 -1
- package/dist/operations/search.cjs +4 -10
- package/dist/operations/search.d.cts +19 -23
- package/dist/operations/search.d.cts.map +1 -1
- package/dist/operations/search.d.mts +19 -23
- package/dist/operations/search.d.mts.map +1 -1
- package/dist/operations/search.mjs +4 -10
- package/dist/operations/search.mjs.map +1 -1
- package/dist/router/path-router.cjs +7 -15
- package/dist/router/path-router.d.cts +4 -7
- package/dist/router/path-router.d.cts.map +1 -1
- package/dist/router/path-router.d.mts +4 -7
- package/dist/router/path-router.d.mts.map +1 -1
- package/dist/router/path-router.mjs +7 -15
- package/dist/router/path-router.mjs.map +1 -1
- package/dist/router/types.d.cts.map +1 -1
- package/dist/router/types.d.mts.map +1 -1
- package/dist/schema/introspector.d.cts +19 -19
- package/dist/schema/introspector.d.cts.map +1 -1
- package/dist/schema/introspector.d.mts +19 -19
- package/dist/schema/introspector.d.mts.map +1 -1
- package/dist/schema/service.cjs +86 -0
- package/dist/schema/service.d.cts +52 -0
- package/dist/schema/service.d.cts.map +1 -0
- package/dist/schema/service.d.mts +52 -0
- package/dist/schema/service.d.mts.map +1 -0
- package/dist/schema/service.mjs +87 -0
- package/dist/schema/service.mjs.map +1 -0
- package/dist/schema/types.d.cts.map +1 -1
- package/dist/schema/types.d.mts.map +1 -1
- package/dist/sqlite-afs.cjs +444 -114
- package/dist/sqlite-afs.d.cts +230 -54
- package/dist/sqlite-afs.d.cts.map +1 -1
- package/dist/sqlite-afs.d.mts +230 -54
- package/dist/sqlite-afs.d.mts.map +1 -1
- package/dist/sqlite-afs.mjs +446 -116
- package/dist/sqlite-afs.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/decorate.js
|
|
3
|
+
function __decorate(decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.__decorate = __decorate;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/decorate.js
|
|
2
|
+
function __decorate(decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { __decorate };
|
|
@@ -17,19 +17,6 @@ async function execRun(db, query) {
|
|
|
17
17
|
* Registers built-in actions to the registry
|
|
18
18
|
*/
|
|
19
19
|
function registerBuiltInActions(registry) {
|
|
20
|
-
registry.register({
|
|
21
|
-
name: "refresh",
|
|
22
|
-
description: "Refresh the schema cache for this module",
|
|
23
|
-
tableLevel: true,
|
|
24
|
-
rowLevel: false,
|
|
25
|
-
handler: async (ctx) => {
|
|
26
|
-
await ctx.module.refreshSchema();
|
|
27
|
-
return {
|
|
28
|
-
success: true,
|
|
29
|
-
message: "Schema refreshed successfully"
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
20
|
registry.register({
|
|
34
21
|
name: "export",
|
|
35
22
|
description: "Export table data in specified format (json, csv)",
|
|
@@ -68,12 +55,20 @@ function registerBuiltInActions(registry) {
|
|
|
68
55
|
description: "Create a copy of this row",
|
|
69
56
|
tableLevel: false,
|
|
70
57
|
rowLevel: true,
|
|
58
|
+
inputSchemaGenerator: (schemaCtx) => {
|
|
59
|
+
if (!schemaCtx.tableSchema) return {
|
|
60
|
+
type: "object",
|
|
61
|
+
properties: {},
|
|
62
|
+
additionalProperties: false
|
|
63
|
+
};
|
|
64
|
+
return generateDuplicateSchema(schemaCtx.tableSchema);
|
|
65
|
+
},
|
|
71
66
|
handler: async (ctx) => {
|
|
72
67
|
if (!ctx.row) return {
|
|
73
68
|
success: false,
|
|
74
69
|
message: "Row data not available"
|
|
75
70
|
};
|
|
76
|
-
const schema = ctx.
|
|
71
|
+
const schema = await ctx.schemaService.getSchema(ctx.table);
|
|
77
72
|
if (!schema) return {
|
|
78
73
|
success: false,
|
|
79
74
|
message: `Table '${ctx.table}' not found`
|
|
@@ -97,12 +92,20 @@ function registerBuiltInActions(registry) {
|
|
|
97
92
|
description: "Validate row data against schema constraints",
|
|
98
93
|
tableLevel: false,
|
|
99
94
|
rowLevel: true,
|
|
95
|
+
inputSchemaGenerator: (schemaCtx) => {
|
|
96
|
+
if (!schemaCtx.tableSchema) return {
|
|
97
|
+
type: "object",
|
|
98
|
+
properties: {},
|
|
99
|
+
additionalProperties: false
|
|
100
|
+
};
|
|
101
|
+
return generateValidateSchema(schemaCtx.tableSchema);
|
|
102
|
+
},
|
|
100
103
|
handler: async (ctx) => {
|
|
101
104
|
if (!ctx.row) return {
|
|
102
105
|
success: false,
|
|
103
106
|
message: "Row data not available"
|
|
104
107
|
};
|
|
105
|
-
const schema = ctx.
|
|
108
|
+
const schema = await ctx.schemaService.getSchema(ctx.table);
|
|
106
109
|
if (!schema) return {
|
|
107
110
|
success: false,
|
|
108
111
|
message: `Table '${ctx.table}' not found`
|
|
@@ -125,6 +128,306 @@ function registerBuiltInActions(registry) {
|
|
|
125
128
|
};
|
|
126
129
|
}
|
|
127
130
|
});
|
|
131
|
+
registry.register({
|
|
132
|
+
name: "create-table",
|
|
133
|
+
description: "Create a new table in the database",
|
|
134
|
+
rootLevel: true,
|
|
135
|
+
tableLevel: false,
|
|
136
|
+
rowLevel: false,
|
|
137
|
+
inputSchema: {
|
|
138
|
+
type: "object",
|
|
139
|
+
description: "Create a new SQLite table with specified columns",
|
|
140
|
+
properties: {
|
|
141
|
+
name: {
|
|
142
|
+
type: "string",
|
|
143
|
+
description: "Table name (alphanumeric and underscores, must start with letter)",
|
|
144
|
+
pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
|
|
145
|
+
minLength: 1,
|
|
146
|
+
maxLength: 128
|
|
147
|
+
},
|
|
148
|
+
columns: {
|
|
149
|
+
type: "array",
|
|
150
|
+
description: "Column definitions (at least one required)",
|
|
151
|
+
minItems: 1,
|
|
152
|
+
items: {
|
|
153
|
+
type: "object",
|
|
154
|
+
description: "Column definition",
|
|
155
|
+
properties: {
|
|
156
|
+
name: {
|
|
157
|
+
type: "string",
|
|
158
|
+
description: "Column name (alphanumeric and underscores)",
|
|
159
|
+
pattern: "^[a-zA-Z_][a-zA-Z0-9_]*$",
|
|
160
|
+
minLength: 1,
|
|
161
|
+
maxLength: 128
|
|
162
|
+
},
|
|
163
|
+
type: {
|
|
164
|
+
type: "string",
|
|
165
|
+
description: "SQLite column type",
|
|
166
|
+
enum: [
|
|
167
|
+
"INTEGER",
|
|
168
|
+
"TEXT",
|
|
169
|
+
"REAL",
|
|
170
|
+
"BLOB",
|
|
171
|
+
"NUMERIC",
|
|
172
|
+
"INT",
|
|
173
|
+
"TINYINT",
|
|
174
|
+
"SMALLINT",
|
|
175
|
+
"MEDIUMINT",
|
|
176
|
+
"BIGINT",
|
|
177
|
+
"UNSIGNED BIG INT",
|
|
178
|
+
"INT2",
|
|
179
|
+
"INT8",
|
|
180
|
+
"CHARACTER",
|
|
181
|
+
"VARCHAR",
|
|
182
|
+
"VARYING CHARACTER",
|
|
183
|
+
"NCHAR",
|
|
184
|
+
"NATIVE CHARACTER",
|
|
185
|
+
"NVARCHAR",
|
|
186
|
+
"CLOB",
|
|
187
|
+
"DOUBLE",
|
|
188
|
+
"DOUBLE PRECISION",
|
|
189
|
+
"FLOAT",
|
|
190
|
+
"DECIMAL",
|
|
191
|
+
"BOOLEAN",
|
|
192
|
+
"DATE",
|
|
193
|
+
"DATETIME",
|
|
194
|
+
"TIMESTAMP"
|
|
195
|
+
],
|
|
196
|
+
"x-affinity-mapping": {
|
|
197
|
+
INTEGER: [
|
|
198
|
+
"INTEGER",
|
|
199
|
+
"INT",
|
|
200
|
+
"TINYINT",
|
|
201
|
+
"SMALLINT",
|
|
202
|
+
"MEDIUMINT",
|
|
203
|
+
"BIGINT",
|
|
204
|
+
"UNSIGNED BIG INT",
|
|
205
|
+
"INT2",
|
|
206
|
+
"INT8"
|
|
207
|
+
],
|
|
208
|
+
TEXT: [
|
|
209
|
+
"TEXT",
|
|
210
|
+
"CHARACTER",
|
|
211
|
+
"VARCHAR",
|
|
212
|
+
"VARYING CHARACTER",
|
|
213
|
+
"NCHAR",
|
|
214
|
+
"NATIVE CHARACTER",
|
|
215
|
+
"NVARCHAR",
|
|
216
|
+
"CLOB"
|
|
217
|
+
],
|
|
218
|
+
REAL: [
|
|
219
|
+
"REAL",
|
|
220
|
+
"DOUBLE",
|
|
221
|
+
"DOUBLE PRECISION",
|
|
222
|
+
"FLOAT"
|
|
223
|
+
],
|
|
224
|
+
NUMERIC: [
|
|
225
|
+
"NUMERIC",
|
|
226
|
+
"DECIMAL",
|
|
227
|
+
"BOOLEAN",
|
|
228
|
+
"DATE",
|
|
229
|
+
"DATETIME",
|
|
230
|
+
"TIMESTAMP"
|
|
231
|
+
],
|
|
232
|
+
BLOB: ["BLOB"]
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
nullable: {
|
|
236
|
+
type: "boolean",
|
|
237
|
+
description: "Whether the column allows NULL values (default: true)",
|
|
238
|
+
default: true
|
|
239
|
+
},
|
|
240
|
+
primaryKey: {
|
|
241
|
+
type: "boolean",
|
|
242
|
+
description: "Whether this column is the primary key (default: false). INTEGER PRIMARY KEY will auto-increment.",
|
|
243
|
+
default: false
|
|
244
|
+
},
|
|
245
|
+
unique: {
|
|
246
|
+
type: "boolean",
|
|
247
|
+
description: "Whether this column must have unique values (default: false)",
|
|
248
|
+
default: false
|
|
249
|
+
},
|
|
250
|
+
defaultValue: {
|
|
251
|
+
oneOf: [
|
|
252
|
+
{ type: "string" },
|
|
253
|
+
{ type: "number" },
|
|
254
|
+
{ type: "boolean" },
|
|
255
|
+
{ type: "null" }
|
|
256
|
+
],
|
|
257
|
+
description: "Default value for the column. Use string 'CURRENT_TIMESTAMP' for auto timestamp."
|
|
258
|
+
},
|
|
259
|
+
references: {
|
|
260
|
+
type: "object",
|
|
261
|
+
description: "Foreign key reference",
|
|
262
|
+
properties: {
|
|
263
|
+
table: {
|
|
264
|
+
type: "string",
|
|
265
|
+
description: "Referenced table name"
|
|
266
|
+
},
|
|
267
|
+
column: {
|
|
268
|
+
type: "string",
|
|
269
|
+
description: "Referenced column name"
|
|
270
|
+
},
|
|
271
|
+
onDelete: {
|
|
272
|
+
type: "string",
|
|
273
|
+
enum: [
|
|
274
|
+
"CASCADE",
|
|
275
|
+
"SET NULL",
|
|
276
|
+
"SET DEFAULT",
|
|
277
|
+
"RESTRICT",
|
|
278
|
+
"NO ACTION"
|
|
279
|
+
],
|
|
280
|
+
description: "Action on delete (default: NO ACTION)"
|
|
281
|
+
},
|
|
282
|
+
onUpdate: {
|
|
283
|
+
type: "string",
|
|
284
|
+
enum: [
|
|
285
|
+
"CASCADE",
|
|
286
|
+
"SET NULL",
|
|
287
|
+
"SET DEFAULT",
|
|
288
|
+
"RESTRICT",
|
|
289
|
+
"NO ACTION"
|
|
290
|
+
],
|
|
291
|
+
description: "Action on update (default: NO ACTION)"
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
required: ["table", "column"]
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
required: ["name", "type"],
|
|
298
|
+
additionalProperties: false
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
ifNotExists: {
|
|
302
|
+
type: "boolean",
|
|
303
|
+
description: "If true, do not throw error if table already exists (default: false)",
|
|
304
|
+
default: false
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
required: ["name", "columns"],
|
|
308
|
+
additionalProperties: false
|
|
309
|
+
},
|
|
310
|
+
handler: async (ctx, params) => {
|
|
311
|
+
const tableName = params.name;
|
|
312
|
+
const columns = params.columns;
|
|
313
|
+
const ifNotExists = params.ifNotExists;
|
|
314
|
+
if (!tableName) throw new Error("Table name is required");
|
|
315
|
+
if (!/^[a-zA-Z][a-zA-Z0-9_]*$/.test(tableName)) throw new Error("Table name must start with a letter and contain only alphanumeric characters and underscores");
|
|
316
|
+
if (!columns || columns.length === 0) throw new Error("At least one column is required");
|
|
317
|
+
for (const col of columns) if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(col.name)) throw new Error(`Invalid column name: ${col.name}. Must start with a letter or underscore and contain only alphanumeric characters and underscores`);
|
|
318
|
+
const validTypes = [
|
|
319
|
+
"INTEGER",
|
|
320
|
+
"TEXT",
|
|
321
|
+
"REAL",
|
|
322
|
+
"BLOB",
|
|
323
|
+
"NUMERIC",
|
|
324
|
+
"INT",
|
|
325
|
+
"TINYINT",
|
|
326
|
+
"SMALLINT",
|
|
327
|
+
"MEDIUMINT",
|
|
328
|
+
"BIGINT",
|
|
329
|
+
"UNSIGNED BIG INT",
|
|
330
|
+
"INT2",
|
|
331
|
+
"INT8",
|
|
332
|
+
"CHARACTER",
|
|
333
|
+
"VARCHAR",
|
|
334
|
+
"VARYING CHARACTER",
|
|
335
|
+
"NCHAR",
|
|
336
|
+
"NATIVE CHARACTER",
|
|
337
|
+
"NVARCHAR",
|
|
338
|
+
"CLOB",
|
|
339
|
+
"DOUBLE",
|
|
340
|
+
"DOUBLE PRECISION",
|
|
341
|
+
"FLOAT",
|
|
342
|
+
"DECIMAL",
|
|
343
|
+
"BOOLEAN",
|
|
344
|
+
"DATE",
|
|
345
|
+
"DATETIME",
|
|
346
|
+
"TIMESTAMP"
|
|
347
|
+
];
|
|
348
|
+
for (const col of columns) {
|
|
349
|
+
const upperType = col.type.toUpperCase();
|
|
350
|
+
if (!validTypes.includes(upperType)) throw new Error(`Invalid column type: ${col.type}. Valid types are: ${validTypes.join(", ")}`);
|
|
351
|
+
}
|
|
352
|
+
if (!ifNotExists) {
|
|
353
|
+
if (await ctx.schemaService.hasTable(tableName)) throw new Error(`Table '${tableName}' already exists`);
|
|
354
|
+
}
|
|
355
|
+
const columnDefs = [];
|
|
356
|
+
const foreignKeys = [];
|
|
357
|
+
for (const col of columns) {
|
|
358
|
+
const parts = [`"${col.name}"`, col.type.toUpperCase()];
|
|
359
|
+
if (col.primaryKey) {
|
|
360
|
+
parts.push("PRIMARY KEY");
|
|
361
|
+
if (col.type.toUpperCase() === "INTEGER" || col.type.toUpperCase() === "INT") parts.push("AUTOINCREMENT");
|
|
362
|
+
}
|
|
363
|
+
if (col.nullable === false && !col.primaryKey) parts.push("NOT NULL");
|
|
364
|
+
if (col.unique && !col.primaryKey) parts.push("UNIQUE");
|
|
365
|
+
if (col.defaultValue !== void 0) if (col.defaultValue === "CURRENT_TIMESTAMP" || col.defaultValue === "CURRENT_DATE" || col.defaultValue === "CURRENT_TIME") parts.push(`DEFAULT ${col.defaultValue}`);
|
|
366
|
+
else parts.push(`DEFAULT ${formatValueForSQL(col.defaultValue)}`);
|
|
367
|
+
columnDefs.push(parts.join(" "));
|
|
368
|
+
if (col.references) {
|
|
369
|
+
let fkDef = `FOREIGN KEY ("${col.name}") REFERENCES "${col.references.table}"("${col.references.column}")`;
|
|
370
|
+
if (col.references.onDelete) fkDef += ` ON DELETE ${col.references.onDelete}`;
|
|
371
|
+
if (col.references.onUpdate) fkDef += ` ON UPDATE ${col.references.onUpdate}`;
|
|
372
|
+
foreignKeys.push(fkDef);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const allDefs = [...columnDefs, ...foreignKeys];
|
|
376
|
+
const createSQL = `CREATE TABLE ${ifNotExists ? "IF NOT EXISTS " : ""}"${tableName}" (${allDefs.join(", ")})`;
|
|
377
|
+
await execRun(ctx.db, createSQL);
|
|
378
|
+
return {
|
|
379
|
+
success: true,
|
|
380
|
+
data: {
|
|
381
|
+
tableName,
|
|
382
|
+
columnCount: columns.length,
|
|
383
|
+
sql: createSQL
|
|
384
|
+
},
|
|
385
|
+
message: `Table '${tableName}' created successfully`
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
registry.register({
|
|
390
|
+
name: "insert",
|
|
391
|
+
description: "Insert a new row into the table",
|
|
392
|
+
rootLevel: false,
|
|
393
|
+
tableLevel: true,
|
|
394
|
+
rowLevel: false,
|
|
395
|
+
inputSchemaGenerator: (schemaCtx) => {
|
|
396
|
+
if (!schemaCtx.tableSchema) return {
|
|
397
|
+
type: "object",
|
|
398
|
+
properties: { data: {
|
|
399
|
+
type: "object",
|
|
400
|
+
description: "Row data to insert (column names as keys)",
|
|
401
|
+
additionalProperties: true
|
|
402
|
+
} },
|
|
403
|
+
required: ["data"]
|
|
404
|
+
};
|
|
405
|
+
return generateInsertSchema(schemaCtx.tableSchema);
|
|
406
|
+
},
|
|
407
|
+
handler: async (ctx, params) => {
|
|
408
|
+
const data = params.data;
|
|
409
|
+
if (!data || Object.keys(data).length === 0) throw new Error("Insert data is required");
|
|
410
|
+
if (!await ctx.schemaService.getSchema(ctx.table)) throw new Error(`Table '${ctx.table}' not found`);
|
|
411
|
+
const columns = Object.keys(data);
|
|
412
|
+
const values = columns.map((col) => formatValueForSQL(data[col]));
|
|
413
|
+
const insertSQL = `INSERT INTO "${ctx.table}" (${columns.map((c) => `"${c}"`).join(", ")}) VALUES (${values.join(", ")})`;
|
|
414
|
+
try {
|
|
415
|
+
await execRun(ctx.db, insertSQL);
|
|
416
|
+
} catch (error) {
|
|
417
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
418
|
+
throw new Error(`Insert failed: ${message}`);
|
|
419
|
+
}
|
|
420
|
+
const newId = (await execAll(ctx.db, "SELECT last_insert_rowid() as id"))[0]?.id;
|
|
421
|
+
return {
|
|
422
|
+
success: true,
|
|
423
|
+
data: {
|
|
424
|
+
id: newId,
|
|
425
|
+
...data
|
|
426
|
+
},
|
|
427
|
+
message: `Row inserted successfully with id ${newId}`
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
});
|
|
128
431
|
}
|
|
129
432
|
/**
|
|
130
433
|
* Formats a value for SQL insertion
|
|
@@ -137,6 +440,96 @@ function formatValueForSQL(value) {
|
|
|
137
440
|
if (typeof value === "object") return `'${JSON.stringify(value).replace(/'/g, "''")}'`;
|
|
138
441
|
return `'${String(value).replace(/'/g, "''")}'`;
|
|
139
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Maps SQLite column type to JSON Schema type
|
|
445
|
+
*/
|
|
446
|
+
function sqliteTypeToJsonSchema(sqliteType) {
|
|
447
|
+
const upperType = sqliteType.toUpperCase();
|
|
448
|
+
if (upperType.includes("INT")) return { type: "integer" };
|
|
449
|
+
if (upperType.includes("REAL") || upperType.includes("FLOAT") || upperType.includes("DOUBLE")) return { type: "number" };
|
|
450
|
+
if (upperType.includes("BOOL")) return {
|
|
451
|
+
type: "boolean",
|
|
452
|
+
description: "Stored as INTEGER (0/1)"
|
|
453
|
+
};
|
|
454
|
+
if (upperType.includes("DATE") || upperType.includes("TIME")) return {
|
|
455
|
+
type: "string",
|
|
456
|
+
format: "date-time"
|
|
457
|
+
};
|
|
458
|
+
if (upperType.includes("BLOB")) return {
|
|
459
|
+
type: "string",
|
|
460
|
+
format: "binary",
|
|
461
|
+
description: "Base64 encoded binary data"
|
|
462
|
+
};
|
|
463
|
+
return { type: "string" };
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Generates JSON Schema for insert action based on table schema
|
|
467
|
+
*/
|
|
468
|
+
function generateInsertSchema(tableSchema) {
|
|
469
|
+
const properties = {};
|
|
470
|
+
const required = [];
|
|
471
|
+
for (const col of tableSchema.columns) {
|
|
472
|
+
if (col.pk > 0 && col.type.toUpperCase().includes("INTEGER")) continue;
|
|
473
|
+
const propSchema = { ...sqliteTypeToJsonSchema(col.type) };
|
|
474
|
+
if (col.dfltValue !== null) {
|
|
475
|
+
propSchema.default = col.dfltValue;
|
|
476
|
+
propSchema.description = `${propSchema.description ?? ""}${propSchema.description ? ". " : ""}Default: ${col.dfltValue}`.trim();
|
|
477
|
+
}
|
|
478
|
+
if (col.notnull && col.dfltValue === null && col.pk === 0) required.push(col.name);
|
|
479
|
+
properties[col.name] = propSchema;
|
|
480
|
+
}
|
|
481
|
+
return {
|
|
482
|
+
type: "object",
|
|
483
|
+
description: `Insert a new row into "${tableSchema.name}" table`,
|
|
484
|
+
properties: { data: {
|
|
485
|
+
type: "object",
|
|
486
|
+
description: "Row data to insert",
|
|
487
|
+
properties,
|
|
488
|
+
required: required.length > 0 ? required : void 0,
|
|
489
|
+
additionalProperties: false
|
|
490
|
+
} },
|
|
491
|
+
required: ["data"]
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Generates JSON Schema for validate action based on table schema
|
|
496
|
+
*/
|
|
497
|
+
function generateValidateSchema(tableSchema) {
|
|
498
|
+
const columnInfo = tableSchema.columns.map((col) => ({
|
|
499
|
+
name: col.name,
|
|
500
|
+
type: col.type,
|
|
501
|
+
nullable: !col.notnull,
|
|
502
|
+
primaryKey: col.pk > 0
|
|
503
|
+
}));
|
|
504
|
+
const foreignKeyInfo = tableSchema.foreignKeys.map((fk) => ({
|
|
505
|
+
column: fk.from,
|
|
506
|
+
references: `${fk.table}.${fk.to}`
|
|
507
|
+
}));
|
|
508
|
+
return {
|
|
509
|
+
type: "object",
|
|
510
|
+
description: `Validate row data against "${tableSchema.name}" table constraints`,
|
|
511
|
+
properties: {},
|
|
512
|
+
additionalProperties: false,
|
|
513
|
+
"x-table-schema": {
|
|
514
|
+
columns: columnInfo,
|
|
515
|
+
foreignKeys: foreignKeyInfo
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Generates JSON Schema for duplicate action based on table schema
|
|
521
|
+
*/
|
|
522
|
+
function generateDuplicateSchema(tableSchema) {
|
|
523
|
+
const pkColumn = tableSchema.primaryKey[0] ?? "rowid";
|
|
524
|
+
const copiedColumns = tableSchema.columns.filter((col) => col.name !== pkColumn).map((col) => col.name);
|
|
525
|
+
return {
|
|
526
|
+
type: "object",
|
|
527
|
+
description: `Create a copy of the row (excluding primary key "${pkColumn}")`,
|
|
528
|
+
properties: {},
|
|
529
|
+
additionalProperties: false,
|
|
530
|
+
"x-copied-columns": copiedColumns
|
|
531
|
+
};
|
|
532
|
+
}
|
|
140
533
|
|
|
141
534
|
//#endregion
|
|
142
535
|
exports.registerBuiltInActions = registerBuiltInActions;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"built-in.d.cts","names":[],"sources":["../../src/actions/built-in.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"built-in.d.cts","names":[],"sources":["../../src/actions/built-in.ts"],"mappings":";;;;;AAuBA;iBAAgB,sBAAA,CAAuB,QAAA,EAAU,eAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"built-in.d.mts","names":[],"sources":["../../src/actions/built-in.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"built-in.d.mts","names":[],"sources":["../../src/actions/built-in.ts"],"mappings":";;;;;AAuBA;iBAAgB,sBAAA,CAAuB,QAAA,EAAU,eAAA"}
|