@aigne/afs-sqlite 1.0.1-beta

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 (76) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE.md +93 -0
  3. package/README.md +277 -0
  4. package/lib/cjs/actions/built-in.d.ts +5 -0
  5. package/lib/cjs/actions/built-in.js +165 -0
  6. package/lib/cjs/actions/registry.d.ts +49 -0
  7. package/lib/cjs/actions/registry.js +102 -0
  8. package/lib/cjs/actions/types.d.ts +51 -0
  9. package/lib/cjs/actions/types.js +2 -0
  10. package/lib/cjs/config.d.ts +89 -0
  11. package/lib/cjs/config.js +33 -0
  12. package/lib/cjs/index.d.ts +13 -0
  13. package/lib/cjs/index.js +47 -0
  14. package/lib/cjs/node/builder.d.ts +43 -0
  15. package/lib/cjs/node/builder.js +187 -0
  16. package/lib/cjs/operations/crud.d.ts +64 -0
  17. package/lib/cjs/operations/crud.js +225 -0
  18. package/lib/cjs/operations/query-builder.d.ts +37 -0
  19. package/lib/cjs/operations/query-builder.js +102 -0
  20. package/lib/cjs/operations/search.d.ts +75 -0
  21. package/lib/cjs/operations/search.js +172 -0
  22. package/lib/cjs/package.json +3 -0
  23. package/lib/cjs/router/path-router.d.ts +38 -0
  24. package/lib/cjs/router/path-router.js +90 -0
  25. package/lib/cjs/router/types.d.ts +30 -0
  26. package/lib/cjs/router/types.js +2 -0
  27. package/lib/cjs/schema/introspector.d.ts +48 -0
  28. package/lib/cjs/schema/introspector.js +186 -0
  29. package/lib/cjs/schema/types.d.ts +104 -0
  30. package/lib/cjs/schema/types.js +13 -0
  31. package/lib/cjs/sqlite-afs.d.ts +144 -0
  32. package/lib/cjs/sqlite-afs.js +337 -0
  33. package/lib/dts/actions/built-in.d.ts +5 -0
  34. package/lib/dts/actions/registry.d.ts +49 -0
  35. package/lib/dts/actions/types.d.ts +51 -0
  36. package/lib/dts/config.d.ts +89 -0
  37. package/lib/dts/index.d.ts +13 -0
  38. package/lib/dts/node/builder.d.ts +43 -0
  39. package/lib/dts/operations/crud.d.ts +64 -0
  40. package/lib/dts/operations/query-builder.d.ts +37 -0
  41. package/lib/dts/operations/search.d.ts +75 -0
  42. package/lib/dts/router/path-router.d.ts +38 -0
  43. package/lib/dts/router/types.d.ts +30 -0
  44. package/lib/dts/schema/introspector.d.ts +48 -0
  45. package/lib/dts/schema/types.d.ts +104 -0
  46. package/lib/dts/sqlite-afs.d.ts +144 -0
  47. package/lib/esm/actions/built-in.d.ts +5 -0
  48. package/lib/esm/actions/built-in.js +162 -0
  49. package/lib/esm/actions/registry.d.ts +49 -0
  50. package/lib/esm/actions/registry.js +98 -0
  51. package/lib/esm/actions/types.d.ts +51 -0
  52. package/lib/esm/actions/types.js +1 -0
  53. package/lib/esm/config.d.ts +89 -0
  54. package/lib/esm/config.js +30 -0
  55. package/lib/esm/index.d.ts +13 -0
  56. package/lib/esm/index.js +17 -0
  57. package/lib/esm/node/builder.d.ts +43 -0
  58. package/lib/esm/node/builder.js +177 -0
  59. package/lib/esm/operations/crud.d.ts +64 -0
  60. package/lib/esm/operations/crud.js +221 -0
  61. package/lib/esm/operations/query-builder.d.ts +37 -0
  62. package/lib/esm/operations/query-builder.js +92 -0
  63. package/lib/esm/operations/search.d.ts +75 -0
  64. package/lib/esm/operations/search.js +167 -0
  65. package/lib/esm/package.json +3 -0
  66. package/lib/esm/router/path-router.d.ts +38 -0
  67. package/lib/esm/router/path-router.js +83 -0
  68. package/lib/esm/router/types.d.ts +30 -0
  69. package/lib/esm/router/types.js +1 -0
  70. package/lib/esm/schema/introspector.d.ts +48 -0
  71. package/lib/esm/schema/introspector.js +182 -0
  72. package/lib/esm/schema/types.d.ts +104 -0
  73. package/lib/esm/schema/types.js +10 -0
  74. package/lib/esm/sqlite-afs.d.ts +144 -0
  75. package/lib/esm/sqlite-afs.js +333 -0
  76. package/package.json +71 -0
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ActionsRegistry = void 0;
4
+ /**
5
+ * Registry for managing action handlers
6
+ */
7
+ class ActionsRegistry {
8
+ handlers = new Map();
9
+ /**
10
+ * Registers an action handler
11
+ */
12
+ register(definition) {
13
+ this.handlers.set(definition.name, definition);
14
+ }
15
+ /**
16
+ * Registers a simple action with just name and handler
17
+ */
18
+ registerSimple(name, handler, options) {
19
+ this.register({
20
+ name,
21
+ handler,
22
+ description: options?.description,
23
+ tableLevel: options?.tableLevel ?? false,
24
+ rowLevel: options?.rowLevel ?? true,
25
+ });
26
+ }
27
+ /**
28
+ * Unregisters an action
29
+ */
30
+ unregister(name) {
31
+ return this.handlers.delete(name);
32
+ }
33
+ /**
34
+ * Checks if an action is registered
35
+ */
36
+ has(name) {
37
+ return this.handlers.has(name);
38
+ }
39
+ /**
40
+ * Gets an action definition
41
+ */
42
+ get(name) {
43
+ return this.handlers.get(name);
44
+ }
45
+ /**
46
+ * Lists all registered actions
47
+ */
48
+ list(options) {
49
+ const actions = Array.from(this.handlers.values());
50
+ if (options?.tableLevel !== undefined || options?.rowLevel !== undefined) {
51
+ return actions.filter((a) => {
52
+ if (options.tableLevel && !a.tableLevel)
53
+ return false;
54
+ if (options.rowLevel && !a.rowLevel)
55
+ return false;
56
+ return true;
57
+ });
58
+ }
59
+ return actions;
60
+ }
61
+ /**
62
+ * Lists action names
63
+ */
64
+ listNames(options) {
65
+ return this.list(options).map((a) => a.name);
66
+ }
67
+ /**
68
+ * Executes an action
69
+ */
70
+ async execute(name, ctx, params = {}) {
71
+ const definition = this.handlers.get(name);
72
+ if (!definition) {
73
+ return {
74
+ success: false,
75
+ message: `Unknown action: ${name}`,
76
+ };
77
+ }
78
+ // Validate action level
79
+ if (ctx.pk && !definition.rowLevel) {
80
+ return {
81
+ success: false,
82
+ message: `Action '${name}' is not available at row level`,
83
+ };
84
+ }
85
+ if (!ctx.pk && !definition.tableLevel) {
86
+ return {
87
+ success: false,
88
+ message: `Action '${name}' is not available at table level`,
89
+ };
90
+ }
91
+ try {
92
+ return await definition.handler(ctx, params);
93
+ }
94
+ catch (error) {
95
+ return {
96
+ success: false,
97
+ message: error instanceof Error ? error.message : String(error),
98
+ };
99
+ }
100
+ }
101
+ }
102
+ exports.ActionsRegistry = ActionsRegistry;
@@ -0,0 +1,51 @@
1
+ import type { LibSQLDatabase } from "drizzle-orm/libsql";
2
+ import type { TableSchema } from "../schema/types.js";
3
+ /**
4
+ * Context provided to action handlers
5
+ */
6
+ export interface ActionContext {
7
+ /** Database instance */
8
+ db: LibSQLDatabase;
9
+ /** All table schemas */
10
+ schemas: Map<string, TableSchema>;
11
+ /** Table this action is being executed on */
12
+ table: string;
13
+ /** Primary key of the row (if row-level action) */
14
+ pk?: string;
15
+ /** The row data (if available) */
16
+ row?: Record<string, unknown>;
17
+ /** Reference to the parent module for advanced operations */
18
+ module: {
19
+ refreshSchema(): Promise<void>;
20
+ exportTable(table: string, format: string): Promise<unknown>;
21
+ };
22
+ }
23
+ /**
24
+ * Action handler function signature
25
+ */
26
+ export type ActionHandler = (ctx: ActionContext, params: Record<string, unknown>) => Promise<ActionResult>;
27
+ /**
28
+ * Result from an action execution
29
+ */
30
+ export interface ActionResult {
31
+ success: boolean;
32
+ data?: unknown;
33
+ message?: string;
34
+ }
35
+ /**
36
+ * Action definition with metadata
37
+ */
38
+ export interface ActionDefinition {
39
+ /** Action name */
40
+ name: string;
41
+ /** Description of what the action does */
42
+ description?: string;
43
+ /** Whether this action is available at table level (vs row level) */
44
+ tableLevel?: boolean;
45
+ /** Whether this action is available at row level */
46
+ rowLevel?: boolean;
47
+ /** Input schema for the action parameters */
48
+ inputSchema?: Record<string, unknown>;
49
+ /** The handler function */
50
+ handler: ActionHandler;
51
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,89 @@
1
+ import { type AFSAccessMode } from "@aigne/afs";
2
+ import { z } from "zod";
3
+ /**
4
+ * FTS (Full-Text Search) configuration schema
5
+ */
6
+ export declare const ftsConfigSchema: z.ZodOptional<z.ZodObject<{
7
+ enabled: z.ZodDefault<z.ZodBoolean>;
8
+ tables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString, "many">>>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ enabled: boolean;
11
+ tables?: Record<string, string[]> | undefined;
12
+ }, {
13
+ enabled?: boolean | undefined;
14
+ tables?: Record<string, string[]> | undefined;
15
+ }>>;
16
+ /**
17
+ * SQLite AFS module configuration schema
18
+ */
19
+ export declare const sqliteAFSConfigSchema: z.ZodObject<{
20
+ url: z.ZodString;
21
+ name: z.ZodOptional<z.ZodString>;
22
+ description: z.ZodOptional<z.ZodString>;
23
+ accessMode: z.ZodOptional<z.ZodEnum<["readonly", "readwrite"]>>;
24
+ tables: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
25
+ excludeTables: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
26
+ fts: z.ZodOptional<z.ZodObject<{
27
+ enabled: z.ZodDefault<z.ZodBoolean>;
28
+ tables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString, "many">>>;
29
+ }, "strip", z.ZodTypeAny, {
30
+ enabled: boolean;
31
+ tables?: Record<string, string[]> | undefined;
32
+ }, {
33
+ enabled?: boolean | undefined;
34
+ tables?: Record<string, string[]> | undefined;
35
+ }>>;
36
+ wal: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
37
+ }, "strip", z.ZodTypeAny, {
38
+ url: string;
39
+ wal: boolean;
40
+ tables?: string[] | undefined;
41
+ name?: string | undefined;
42
+ description?: string | undefined;
43
+ accessMode?: "readonly" | "readwrite" | undefined;
44
+ excludeTables?: string[] | undefined;
45
+ fts?: {
46
+ enabled: boolean;
47
+ tables?: Record<string, string[]> | undefined;
48
+ } | undefined;
49
+ }, {
50
+ url: string;
51
+ tables?: string[] | undefined;
52
+ name?: string | undefined;
53
+ description?: string | undefined;
54
+ accessMode?: "readonly" | "readwrite" | undefined;
55
+ excludeTables?: string[] | undefined;
56
+ fts?: {
57
+ enabled?: boolean | undefined;
58
+ tables?: Record<string, string[]> | undefined;
59
+ } | undefined;
60
+ wal?: boolean | undefined;
61
+ }>;
62
+ /**
63
+ * SQLite AFS module configuration type
64
+ */
65
+ export type SQLiteAFSConfig = z.infer<typeof sqliteAFSConfigSchema>;
66
+ /**
67
+ * SQLite AFS module options (after parsing)
68
+ */
69
+ export interface SQLiteAFSOptions {
70
+ /** SQLite database URL */
71
+ url: string;
72
+ /** Module name */
73
+ name?: string;
74
+ /** Module description */
75
+ description?: string;
76
+ /** Access mode */
77
+ accessMode?: AFSAccessMode;
78
+ /** Tables to expose */
79
+ tables?: string[];
80
+ /** Tables to exclude */
81
+ excludeTables?: string[];
82
+ /** FTS configuration */
83
+ fts?: {
84
+ enabled?: boolean;
85
+ tables?: Record<string, string[]>;
86
+ };
87
+ /** Enable WAL mode */
88
+ wal?: boolean;
89
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sqliteAFSConfigSchema = exports.ftsConfigSchema = void 0;
4
+ const afs_1 = require("@aigne/afs");
5
+ const zod_1 = require("zod");
6
+ /**
7
+ * FTS (Full-Text Search) configuration schema
8
+ */
9
+ exports.ftsConfigSchema = zod_1.z
10
+ .object({
11
+ enabled: zod_1.z.boolean().default(true).describe("Whether FTS is enabled"),
12
+ tables: zod_1.z
13
+ .record(zod_1.z.array(zod_1.z.string()))
14
+ .optional()
15
+ .describe("Map of table name to columns to index for FTS"),
16
+ })
17
+ .optional();
18
+ /**
19
+ * SQLite AFS module configuration schema
20
+ */
21
+ exports.sqliteAFSConfigSchema = zod_1.z.object({
22
+ url: zod_1.z.string().describe("SQLite database URL (file:./path or :memory:)"),
23
+ name: zod_1.z.string().optional().describe("Module name, defaults to 'sqlite-afs'"),
24
+ description: zod_1.z.string().optional().describe("Description of this module"),
25
+ accessMode: afs_1.accessModeSchema,
26
+ tables: zod_1.z
27
+ .array(zod_1.z.string())
28
+ .optional()
29
+ .describe("Whitelist of tables to expose (if not specified, all tables are exposed)"),
30
+ excludeTables: zod_1.z.array(zod_1.z.string()).optional().describe("Tables to exclude from exposure"),
31
+ fts: exports.ftsConfigSchema,
32
+ wal: zod_1.z.boolean().optional().default(true).describe("Enable WAL mode for better concurrency"),
33
+ });
@@ -0,0 +1,13 @@
1
+ export { registerBuiltInActions } from "./actions/built-in.js";
2
+ export { ActionsRegistry } from "./actions/registry.js";
3
+ export type { ActionContext, ActionDefinition, ActionHandler, ActionResult, } from "./actions/types.js";
4
+ export { type SQLiteAFSConfig, type SQLiteAFSOptions, sqliteAFSConfigSchema } from "./config.js";
5
+ export { type BuildEntryOptions, buildActionsListEntry, buildAttributeEntry, buildAttributeListEntry, buildMetaEntry, buildRowEntry, buildSchemaEntry, buildSearchEntry, buildTableEntry, } from "./node/builder.js";
6
+ export { CRUDOperations } from "./operations/crud.js";
7
+ export { buildDelete, buildGetLastRowId, buildInsert, buildSelectAll, buildSelectByPK, buildUpdate, } from "./operations/query-builder.js";
8
+ export { createFTSConfig, type FTSConfig, FTSSearch, type FTSTableConfig, } from "./operations/search.js";
9
+ export { buildPath, createPathRouter, getVirtualPathType, isVirtualPath, matchPath, } from "./router/path-router.js";
10
+ export type { RouteAction, RouteData, RouteMatch, RouteParams } from "./router/types.js";
11
+ export { SchemaIntrospector } from "./schema/introspector.js";
12
+ export type { ColumnInfo, ForeignKeyInfo, IndexInfo, PragmaForeignKeyRow, PragmaIndexListRow, PragmaTableInfoRow, TableSchema, } from "./schema/types.js";
13
+ export { SQLiteAFS } from "./sqlite-afs.js";
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ // Main module export
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.SQLiteAFS = exports.SchemaIntrospector = exports.matchPath = exports.isVirtualPath = exports.getVirtualPathType = exports.createPathRouter = exports.buildPath = exports.FTSSearch = exports.createFTSConfig = exports.buildUpdate = exports.buildSelectByPK = exports.buildSelectAll = exports.buildInsert = exports.buildGetLastRowId = exports.buildDelete = exports.CRUDOperations = exports.buildTableEntry = exports.buildSearchEntry = exports.buildSchemaEntry = exports.buildRowEntry = exports.buildMetaEntry = exports.buildAttributeListEntry = exports.buildAttributeEntry = exports.buildActionsListEntry = exports.sqliteAFSConfigSchema = exports.ActionsRegistry = exports.registerBuiltInActions = void 0;
5
+ var built_in_js_1 = require("./actions/built-in.js");
6
+ Object.defineProperty(exports, "registerBuiltInActions", { enumerable: true, get: function () { return built_in_js_1.registerBuiltInActions; } });
7
+ // Actions
8
+ var registry_js_1 = require("./actions/registry.js");
9
+ Object.defineProperty(exports, "ActionsRegistry", { enumerable: true, get: function () { return registry_js_1.ActionsRegistry; } });
10
+ // Configuration
11
+ var config_js_1 = require("./config.js");
12
+ Object.defineProperty(exports, "sqliteAFSConfigSchema", { enumerable: true, get: function () { return config_js_1.sqliteAFSConfigSchema; } });
13
+ // Node builder
14
+ var builder_js_1 = require("./node/builder.js");
15
+ Object.defineProperty(exports, "buildActionsListEntry", { enumerable: true, get: function () { return builder_js_1.buildActionsListEntry; } });
16
+ Object.defineProperty(exports, "buildAttributeEntry", { enumerable: true, get: function () { return builder_js_1.buildAttributeEntry; } });
17
+ Object.defineProperty(exports, "buildAttributeListEntry", { enumerable: true, get: function () { return builder_js_1.buildAttributeListEntry; } });
18
+ Object.defineProperty(exports, "buildMetaEntry", { enumerable: true, get: function () { return builder_js_1.buildMetaEntry; } });
19
+ Object.defineProperty(exports, "buildRowEntry", { enumerable: true, get: function () { return builder_js_1.buildRowEntry; } });
20
+ Object.defineProperty(exports, "buildSchemaEntry", { enumerable: true, get: function () { return builder_js_1.buildSchemaEntry; } });
21
+ Object.defineProperty(exports, "buildSearchEntry", { enumerable: true, get: function () { return builder_js_1.buildSearchEntry; } });
22
+ Object.defineProperty(exports, "buildTableEntry", { enumerable: true, get: function () { return builder_js_1.buildTableEntry; } });
23
+ // Operations
24
+ var crud_js_1 = require("./operations/crud.js");
25
+ Object.defineProperty(exports, "CRUDOperations", { enumerable: true, get: function () { return crud_js_1.CRUDOperations; } });
26
+ var query_builder_js_1 = require("./operations/query-builder.js");
27
+ Object.defineProperty(exports, "buildDelete", { enumerable: true, get: function () { return query_builder_js_1.buildDelete; } });
28
+ Object.defineProperty(exports, "buildGetLastRowId", { enumerable: true, get: function () { return query_builder_js_1.buildGetLastRowId; } });
29
+ Object.defineProperty(exports, "buildInsert", { enumerable: true, get: function () { return query_builder_js_1.buildInsert; } });
30
+ Object.defineProperty(exports, "buildSelectAll", { enumerable: true, get: function () { return query_builder_js_1.buildSelectAll; } });
31
+ Object.defineProperty(exports, "buildSelectByPK", { enumerable: true, get: function () { return query_builder_js_1.buildSelectByPK; } });
32
+ Object.defineProperty(exports, "buildUpdate", { enumerable: true, get: function () { return query_builder_js_1.buildUpdate; } });
33
+ var search_js_1 = require("./operations/search.js");
34
+ Object.defineProperty(exports, "createFTSConfig", { enumerable: true, get: function () { return search_js_1.createFTSConfig; } });
35
+ Object.defineProperty(exports, "FTSSearch", { enumerable: true, get: function () { return search_js_1.FTSSearch; } });
36
+ // Router
37
+ var path_router_js_1 = require("./router/path-router.js");
38
+ Object.defineProperty(exports, "buildPath", { enumerable: true, get: function () { return path_router_js_1.buildPath; } });
39
+ Object.defineProperty(exports, "createPathRouter", { enumerable: true, get: function () { return path_router_js_1.createPathRouter; } });
40
+ Object.defineProperty(exports, "getVirtualPathType", { enumerable: true, get: function () { return path_router_js_1.getVirtualPathType; } });
41
+ Object.defineProperty(exports, "isVirtualPath", { enumerable: true, get: function () { return path_router_js_1.isVirtualPath; } });
42
+ Object.defineProperty(exports, "matchPath", { enumerable: true, get: function () { return path_router_js_1.matchPath; } });
43
+ // Schema types and introspector
44
+ var introspector_js_1 = require("./schema/introspector.js");
45
+ Object.defineProperty(exports, "SchemaIntrospector", { enumerable: true, get: function () { return introspector_js_1.SchemaIntrospector; } });
46
+ var sqlite_afs_js_1 = require("./sqlite-afs.js");
47
+ Object.defineProperty(exports, "SQLiteAFS", { enumerable: true, get: function () { return sqlite_afs_js_1.SQLiteAFS; } });
@@ -0,0 +1,43 @@
1
+ import type { AFSEntry } from "@aigne/afs";
2
+ import type { TableSchema } from "../schema/types.js";
3
+ /**
4
+ * Options for building an AFSEntry
5
+ */
6
+ export interface BuildEntryOptions {
7
+ /** Base path prefix (e.g., empty string or module mount path) */
8
+ basePath?: string;
9
+ }
10
+ /**
11
+ * Builds an AFSEntry from a database row
12
+ */
13
+ export declare function buildRowEntry(table: string, schema: TableSchema, row: Record<string, unknown>, options?: BuildEntryOptions): AFSEntry;
14
+ /**
15
+ * Builds an AFSEntry for a table listing
16
+ */
17
+ export declare function buildTableEntry(table: string, schema: TableSchema, options?: BuildEntryOptions & {
18
+ rowCount?: number;
19
+ }): AFSEntry;
20
+ /**
21
+ * Builds an AFSEntry for table schema
22
+ */
23
+ export declare function buildSchemaEntry(table: string, schema: TableSchema, options?: BuildEntryOptions): AFSEntry;
24
+ /**
25
+ * Builds an AFSEntry for an attribute (single column value)
26
+ */
27
+ export declare function buildAttributeEntry(table: string, pk: string, column: string, value: unknown, options?: BuildEntryOptions): AFSEntry;
28
+ /**
29
+ * Builds an AFSEntry listing all attributes for a row
30
+ */
31
+ export declare function buildAttributeListEntry(table: string, schema: TableSchema, pk: string, row: Record<string, unknown>, options?: BuildEntryOptions): AFSEntry[];
32
+ /**
33
+ * Builds an AFSEntry for row metadata
34
+ */
35
+ export declare function buildMetaEntry(table: string, schema: TableSchema, pk: string, row: Record<string, unknown>, options?: BuildEntryOptions): AFSEntry;
36
+ /**
37
+ * Builds AFSEntry for actions list
38
+ */
39
+ export declare function buildActionsListEntry(table: string, pk: string, actions: string[], options?: BuildEntryOptions): AFSEntry[];
40
+ /**
41
+ * Builds a search result entry with highlights
42
+ */
43
+ export declare function buildSearchEntry(table: string, schema: TableSchema, row: Record<string, unknown>, snippet?: string, options?: BuildEntryOptions): AFSEntry;
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildRowEntry = buildRowEntry;
4
+ exports.buildTableEntry = buildTableEntry;
5
+ exports.buildSchemaEntry = buildSchemaEntry;
6
+ exports.buildAttributeEntry = buildAttributeEntry;
7
+ exports.buildAttributeListEntry = buildAttributeListEntry;
8
+ exports.buildMetaEntry = buildMetaEntry;
9
+ exports.buildActionsListEntry = buildActionsListEntry;
10
+ exports.buildSearchEntry = buildSearchEntry;
11
+ /**
12
+ * Builds an AFSEntry from a database row
13
+ */
14
+ function buildRowEntry(table, schema, row, options) {
15
+ const pkColumn = schema.primaryKey[0] ?? "rowid";
16
+ const pk = String(row[pkColumn] ?? row.rowid);
17
+ const basePath = options?.basePath ?? "";
18
+ return {
19
+ id: `${table}:${pk}`,
20
+ path: `${basePath}/${table}/${pk}`,
21
+ content: row,
22
+ metadata: {
23
+ table,
24
+ primaryKey: pkColumn,
25
+ primaryKeyValue: pk,
26
+ },
27
+ createdAt: parseDate(row.created_at ?? row.createdAt),
28
+ updatedAt: parseDate(row.updated_at ?? row.updatedAt),
29
+ };
30
+ }
31
+ /**
32
+ * Builds an AFSEntry for a table listing
33
+ */
34
+ function buildTableEntry(table, schema, options) {
35
+ const basePath = options?.basePath ?? "";
36
+ return {
37
+ id: table,
38
+ path: `${basePath}/${table}`,
39
+ description: `Table: ${table} (${schema.columns.length} columns)`,
40
+ metadata: {
41
+ table,
42
+ columnCount: schema.columns.length,
43
+ primaryKey: schema.primaryKey,
44
+ childrenCount: options?.rowCount,
45
+ },
46
+ };
47
+ }
48
+ /**
49
+ * Builds an AFSEntry for table schema
50
+ */
51
+ function buildSchemaEntry(table, schema, options) {
52
+ const basePath = options?.basePath ?? "";
53
+ return {
54
+ id: `${table}:@schema`,
55
+ path: `${basePath}/${table}/@schema`,
56
+ description: `Schema for table: ${table}`,
57
+ content: {
58
+ name: schema.name,
59
+ columns: schema.columns.map((col) => ({
60
+ name: col.name,
61
+ type: col.type,
62
+ nullable: !col.notnull,
63
+ primaryKey: col.pk > 0,
64
+ defaultValue: col.dfltValue,
65
+ })),
66
+ primaryKey: schema.primaryKey,
67
+ foreignKeys: schema.foreignKeys.map((fk) => ({
68
+ column: fk.from,
69
+ references: {
70
+ table: fk.table,
71
+ column: fk.to,
72
+ },
73
+ onUpdate: fk.onUpdate,
74
+ onDelete: fk.onDelete,
75
+ })),
76
+ indexes: schema.indexes.map((idx) => ({
77
+ name: idx.name,
78
+ unique: idx.unique,
79
+ origin: idx.origin,
80
+ })),
81
+ },
82
+ metadata: {
83
+ table,
84
+ type: "schema",
85
+ },
86
+ };
87
+ }
88
+ /**
89
+ * Builds an AFSEntry for an attribute (single column value)
90
+ */
91
+ function buildAttributeEntry(table, pk, column, value, options) {
92
+ const basePath = options?.basePath ?? "";
93
+ return {
94
+ id: `${table}:${pk}:@attr:${column}`,
95
+ path: `${basePath}/${table}/${pk}/@attr/${column}`,
96
+ content: value,
97
+ metadata: {
98
+ table,
99
+ primaryKeyValue: pk,
100
+ column,
101
+ type: "attribute",
102
+ },
103
+ };
104
+ }
105
+ /**
106
+ * Builds an AFSEntry listing all attributes for a row
107
+ */
108
+ function buildAttributeListEntry(table, schema, pk, row, options) {
109
+ const basePath = options?.basePath ?? "";
110
+ return schema.columns.map((col) => ({
111
+ id: `${table}:${pk}:@attr:${col.name}`,
112
+ path: `${basePath}/${table}/${pk}/@attr/${col.name}`,
113
+ summary: col.name,
114
+ description: `${col.type}${col.notnull ? " NOT NULL" : ""}`,
115
+ content: row[col.name],
116
+ metadata: {
117
+ column: col.name,
118
+ type: col.type,
119
+ },
120
+ }));
121
+ }
122
+ /**
123
+ * Builds an AFSEntry for row metadata
124
+ */
125
+ function buildMetaEntry(table, schema, pk, row, options) {
126
+ const basePath = options?.basePath ?? "";
127
+ return {
128
+ id: `${table}:${pk}:@meta`,
129
+ path: `${basePath}/${table}/${pk}/@meta`,
130
+ content: {
131
+ table,
132
+ primaryKey: schema.primaryKey[0] ?? "rowid",
133
+ primaryKeyValue: pk,
134
+ schema: {
135
+ columns: schema.columns.map((c) => c.name),
136
+ types: Object.fromEntries(schema.columns.map((c) => [c.name, c.type])),
137
+ },
138
+ foreignKeys: schema.foreignKeys.filter((fk) => Object.keys(row).includes(fk.from)),
139
+ rowid: row.rowid,
140
+ },
141
+ metadata: {
142
+ table,
143
+ type: "meta",
144
+ },
145
+ };
146
+ }
147
+ /**
148
+ * Builds AFSEntry for actions list
149
+ */
150
+ function buildActionsListEntry(table, pk, actions, options) {
151
+ const basePath = options?.basePath ?? "";
152
+ return actions.map((action) => ({
153
+ id: `${table}:${pk}:@actions:${action}`,
154
+ path: `${basePath}/${table}/${pk}/@actions/${action}`,
155
+ summary: action,
156
+ metadata: {
157
+ execute: {
158
+ name: action,
159
+ description: `Execute ${action} action on ${table}:${pk}`,
160
+ },
161
+ },
162
+ }));
163
+ }
164
+ /**
165
+ * Builds a search result entry with highlights
166
+ */
167
+ function buildSearchEntry(table, schema, row, snippet, options) {
168
+ const entry = buildRowEntry(table, schema, row, options);
169
+ if (snippet) {
170
+ entry.summary = snippet;
171
+ }
172
+ return entry;
173
+ }
174
+ /**
175
+ * Parses a date from various formats
176
+ */
177
+ function parseDate(value) {
178
+ if (!value)
179
+ return undefined;
180
+ if (value instanceof Date)
181
+ return value;
182
+ if (typeof value === "string")
183
+ return new Date(value);
184
+ if (typeof value === "number")
185
+ return new Date(value);
186
+ return undefined;
187
+ }
@@ -0,0 +1,64 @@
1
+ import type { AFSDeleteResult, AFSListOptions, AFSListResult, AFSReadResult, AFSWriteResult } from "@aigne/afs";
2
+ import type { LibSQLDatabase } from "drizzle-orm/libsql";
3
+ import type { TableSchema } from "../schema/types.js";
4
+ /**
5
+ * CRUD operations for SQLite AFS
6
+ */
7
+ export declare class CRUDOperations {
8
+ private db;
9
+ private schemas;
10
+ private basePath;
11
+ constructor(db: LibSQLDatabase, schemas: Map<string, TableSchema>, basePath?: string);
12
+ /**
13
+ * Lists all tables
14
+ */
15
+ listTables(): Promise<AFSListResult>;
16
+ /**
17
+ * Lists rows in a table
18
+ */
19
+ listTable(table: string, options?: AFSListOptions): Promise<AFSListResult>;
20
+ /**
21
+ * Reads a single row by primary key
22
+ */
23
+ readRow(table: string, pk: string): Promise<AFSReadResult>;
24
+ /**
25
+ * Gets table schema
26
+ */
27
+ getSchema(table: string): AFSReadResult;
28
+ /**
29
+ * Lists attributes (columns) for a row
30
+ */
31
+ listAttributes(table: string, pk: string): Promise<AFSListResult>;
32
+ /**
33
+ * Gets a single attribute (column value) for a row
34
+ */
35
+ getAttribute(table: string, pk: string, column: string): Promise<AFSReadResult>;
36
+ /**
37
+ * Gets row metadata
38
+ */
39
+ getMeta(table: string, pk: string): Promise<AFSReadResult>;
40
+ /**
41
+ * Creates a new row in a table
42
+ */
43
+ createRow(table: string, content: Record<string, unknown>): Promise<AFSWriteResult>;
44
+ /**
45
+ * Updates an existing row
46
+ */
47
+ updateRow(table: string, pk: string, content: Record<string, unknown>): Promise<AFSWriteResult>;
48
+ /**
49
+ * Deletes a row by primary key
50
+ */
51
+ deleteRow(table: string, pk: string): Promise<AFSDeleteResult>;
52
+ /**
53
+ * Checks if a table exists
54
+ */
55
+ hasTable(table: string): boolean;
56
+ /**
57
+ * Gets the schema for a table
58
+ */
59
+ getTableSchema(table: string): TableSchema | undefined;
60
+ /**
61
+ * Updates the schemas map (after refresh)
62
+ */
63
+ setSchemas(schemas: Map<string, TableSchema>): void;
64
+ }