@aigne/afs-sqlite 1.11.0-beta.5 → 1.11.0-beta.7
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 +1262 -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 +1262 -15
- package/dist/actions/built-in.mjs.map +1 -1
- package/dist/actions/operators.cjs +156 -0
- package/dist/actions/operators.mjs +157 -0
- package/dist/actions/operators.mjs.map +1 -0
- 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.cjs +33 -0
- package/dist/actions/types.d.cts +46 -5
- package/dist/actions/types.d.cts.map +1 -1
- package/dist/actions/types.d.mts +46 -5
- package/dist/actions/types.d.mts.map +1 -1
- package/dist/actions/types.mjs +32 -0
- package/dist/actions/types.mjs.map +1 -0
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts.map +1 -1
- package/dist/index.cjs +4 -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 +74 -91
- 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 +72 -89
- 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.cjs +2 -2
- package/dist/operations/query-builder.d.cts.map +1 -1
- package/dist/operations/query-builder.d.mts.map +1 -1
- package/dist/operations/query-builder.mjs +2 -2
- package/dist/operations/query-builder.mjs.map +1 -1
- package/dist/operations/search.cjs +5 -11
- 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 +5 -11
- 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 +672 -121
- package/dist/sqlite-afs.d.cts +257 -56
- package/dist/sqlite-afs.d.cts.map +1 -1
- package/dist/sqlite-afs.d.mts +257 -56
- package/dist/sqlite-afs.d.mts.map +1 -1
- package/dist/sqlite-afs.mjs +674 -123
- package/dist/sqlite-afs.mjs.map +1 -1
- package/package.json +4 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"introspector.d.cts","names":[],"sources":["../../src/schema/introspector.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"introspector.d.cts","names":[],"sources":["../../src/schema/introspector.ts"],"mappings":";;;;;;AAwEA;cAAa,kBAAA;;;;;;;EAOL,UAAA,CACJ,EAAA,EAAI,cAAA,EACJ,OAAA;IAkD4D,qCAhD1D,MAAA,aAiGa;IA/Fb,aAAA;EAAA,IAED,OAAA,CAAQ,GAAA,SAAY,WAAA;EAuHpB;;;;;;EA3EG,eAAA,CAAgB,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA,CAAQ,WAAA;EAnDpE;;;;EAyFF,mBAAA,CAAoB,MAAA,EAAQ,WAAA;EAlFjB;;;EA6FL,MAAA,CAAO,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA;EAjD/B;;;EAiEhB,SAAA,CACJ,EAAA,EAAI,cAAA,EACJ,SAAA,UACA,OAAA,YACA,OAAA;IA/BF,4CAiCI,YAAA,WAjCgB;IAmChB,YAAA;EAAA,IAED,OAAA;EA1BU;;;EA6EP,UAAA,CAAW,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA;AAAA"}
|
|
@@ -7,41 +7,41 @@ import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
|
7
7
|
*/
|
|
8
8
|
declare class SchemaIntrospector {
|
|
9
9
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
* Introspects all tables in the database
|
|
11
|
+
* @param db - Drizzle database instance
|
|
12
|
+
* @param options - Introspection options
|
|
13
|
+
* @returns Map of table name to TableSchema
|
|
14
|
+
*/
|
|
15
15
|
introspect(db: LibSQLDatabase, options?: {
|
|
16
16
|
/** Whitelist of tables to include */tables?: string[]; /** Tables to exclude */
|
|
17
17
|
excludeTables?: string[];
|
|
18
18
|
}): Promise<Map<string, TableSchema>>;
|
|
19
19
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
* Introspects a single table
|
|
21
|
+
* @param db - Drizzle database instance
|
|
22
|
+
* @param tableName - Name of the table to introspect
|
|
23
|
+
* @returns TableSchema for the specified table
|
|
24
|
+
*/
|
|
25
25
|
introspectTable(db: LibSQLDatabase, tableName: string): Promise<TableSchema>;
|
|
26
26
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
* Gets the primary key column name for a table
|
|
28
|
+
* Returns the first PK column, or 'rowid' if no explicit PK
|
|
29
|
+
*/
|
|
30
30
|
getPrimaryKeyColumn(schema: TableSchema): string;
|
|
31
31
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
* Checks if a table has FTS (Full-Text Search) enabled
|
|
33
|
+
*/
|
|
34
34
|
hasFTS(db: LibSQLDatabase, tableName: string): Promise<boolean>;
|
|
35
35
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
* Creates FTS5 table for full-text search on specified columns
|
|
37
|
+
*/
|
|
38
38
|
createFTS(db: LibSQLDatabase, tableName: string, columns: string[], options?: {
|
|
39
39
|
/** Content table (defaults to tableName) */contentTable?: string; /** Content rowid column (defaults to 'rowid') */
|
|
40
40
|
contentRowid?: string;
|
|
41
41
|
}): Promise<void>;
|
|
42
42
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
* Rebuilds FTS index for a table (useful after bulk inserts)
|
|
44
|
+
*/
|
|
45
45
|
rebuildFTS(db: LibSQLDatabase, tableName: string): Promise<void>;
|
|
46
46
|
}
|
|
47
47
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"introspector.d.mts","names":[],"sources":["../../src/schema/introspector.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"introspector.d.mts","names":[],"sources":["../../src/schema/introspector.ts"],"mappings":";;;;;;AAwEA;cAAa,kBAAA;;;;;;;EAOL,UAAA,CACJ,EAAA,EAAI,cAAA,EACJ,OAAA;IAkD4D,qCAhD1D,MAAA,aAiGa;IA/Fb,aAAA;EAAA,IAED,OAAA,CAAQ,GAAA,SAAY,WAAA;EAuHpB;;;;;;EA3EG,eAAA,CAAgB,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA,CAAQ,WAAA;EAnDpE;;;;EAyFF,mBAAA,CAAoB,MAAA,EAAQ,WAAA;EAlFjB;;;EA6FL,MAAA,CAAO,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA;EAjD/B;;;EAiEhB,SAAA,CACJ,EAAA,EAAI,cAAA,EACJ,SAAA,UACA,OAAA,YACA,OAAA;IA/BF,4CAiCI,YAAA,WAjCgB;IAmChB,YAAA;EAAA,IAED,OAAA;EA1BU;;;EA6EP,UAAA,CAAW,EAAA,EAAI,cAAA,EAAgB,SAAA,WAAoB,OAAA;AAAA"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const require_types = require('./types.cjs');
|
|
2
|
+
const require_introspector = require('./introspector.cjs');
|
|
3
|
+
let _aigne_sqlite = require("@aigne/sqlite");
|
|
4
|
+
|
|
5
|
+
//#region src/schema/service.ts
|
|
6
|
+
/**
|
|
7
|
+
* Executes a raw SQL query and returns all rows
|
|
8
|
+
*/
|
|
9
|
+
async function execAll(db, query) {
|
|
10
|
+
return db.all(_aigne_sqlite.sql.raw(query)).execute();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Schema service that queries schema on-demand (no caching)
|
|
14
|
+
*
|
|
15
|
+
* SQLite PRAGMA queries are extremely fast, so real-time queries
|
|
16
|
+
* are preferred over caching to avoid staleness issues.
|
|
17
|
+
*/
|
|
18
|
+
var SchemaService = class {
|
|
19
|
+
introspector = new require_introspector.SchemaIntrospector();
|
|
20
|
+
constructor(db, options) {
|
|
21
|
+
this.db = db;
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets schema for a specific table (real-time query)
|
|
26
|
+
*/
|
|
27
|
+
async getSchema(tableName) {
|
|
28
|
+
if (!tableName) return;
|
|
29
|
+
if (require_types.SYSTEM_TABLES.includes(tableName)) return;
|
|
30
|
+
if (this.options?.tables && !this.options.tables.includes(tableName)) return;
|
|
31
|
+
if (this.options?.excludeTables?.includes(tableName)) return;
|
|
32
|
+
if (!await this.hasTable(tableName)) return;
|
|
33
|
+
return this.introspector.introspectTable(this.db, tableName);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a table exists (real-time query)
|
|
37
|
+
*/
|
|
38
|
+
async hasTable(tableName) {
|
|
39
|
+
if (!tableName) return false;
|
|
40
|
+
if (require_types.SYSTEM_TABLES.includes(tableName)) return false;
|
|
41
|
+
if (this.options?.tables && !this.options.tables.includes(tableName)) return false;
|
|
42
|
+
if (this.options?.excludeTables?.includes(tableName)) return false;
|
|
43
|
+
return ((await execAll(this.db, `SELECT COUNT(*) as count FROM sqlite_master WHERE type = 'table' AND name = '${tableName.replace(/'/g, "''")}'`))[0]?.count ?? 0) > 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets all table schemas (real-time query)
|
|
47
|
+
*/
|
|
48
|
+
async getAllSchemas() {
|
|
49
|
+
return this.introspector.introspect(this.db, {
|
|
50
|
+
tables: this.options?.tables,
|
|
51
|
+
excludeTables: this.options?.excludeTables
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Lists all table names (real-time query)
|
|
56
|
+
*/
|
|
57
|
+
async listTableNames() {
|
|
58
|
+
return (await execAll(this.db, `
|
|
59
|
+
SELECT name FROM sqlite_master
|
|
60
|
+
WHERE type = 'table'
|
|
61
|
+
AND name NOT LIKE 'sqlite_%'
|
|
62
|
+
AND name NOT LIKE '%_fts%'
|
|
63
|
+
ORDER BY name
|
|
64
|
+
`)).map((r) => r.name).filter((name) => {
|
|
65
|
+
if (require_types.SYSTEM_TABLES.includes(name)) return false;
|
|
66
|
+
if (this.options?.tables && !this.options.tables.includes(name)) return false;
|
|
67
|
+
if (this.options?.excludeTables?.includes(name)) return false;
|
|
68
|
+
return true;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Gets the count of tables
|
|
73
|
+
*/
|
|
74
|
+
async getTableCount() {
|
|
75
|
+
return (await this.listTableNames()).length;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Gets the primary key column name for a table
|
|
79
|
+
*/
|
|
80
|
+
getPrimaryKeyColumn(schema) {
|
|
81
|
+
return this.introspector.getPrimaryKeyColumn(schema);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.SchemaService = SchemaService;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { TableSchema } from "./types.cjs";
|
|
2
|
+
import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
3
|
+
|
|
4
|
+
//#region src/schema/service.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options for schema service
|
|
7
|
+
*/
|
|
8
|
+
interface SchemaServiceOptions {
|
|
9
|
+
/** Whitelist of tables to include */
|
|
10
|
+
tables?: string[];
|
|
11
|
+
/** Tables to exclude */
|
|
12
|
+
excludeTables?: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Schema service that queries schema on-demand (no caching)
|
|
16
|
+
*
|
|
17
|
+
* SQLite PRAGMA queries are extremely fast, so real-time queries
|
|
18
|
+
* are preferred over caching to avoid staleness issues.
|
|
19
|
+
*/
|
|
20
|
+
declare class SchemaService {
|
|
21
|
+
private db;
|
|
22
|
+
private options?;
|
|
23
|
+
private introspector;
|
|
24
|
+
constructor(db: LibSQLDatabase, options?: SchemaServiceOptions | undefined);
|
|
25
|
+
/**
|
|
26
|
+
* Gets schema for a specific table (real-time query)
|
|
27
|
+
*/
|
|
28
|
+
getSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a table exists (real-time query)
|
|
31
|
+
*/
|
|
32
|
+
hasTable(tableName: string): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Gets all table schemas (real-time query)
|
|
35
|
+
*/
|
|
36
|
+
getAllSchemas(): Promise<Map<string, TableSchema>>;
|
|
37
|
+
/**
|
|
38
|
+
* Lists all table names (real-time query)
|
|
39
|
+
*/
|
|
40
|
+
listTableNames(): Promise<string[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Gets the count of tables
|
|
43
|
+
*/
|
|
44
|
+
getTableCount(): Promise<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the primary key column name for a table
|
|
47
|
+
*/
|
|
48
|
+
getPrimaryKeyColumn(schema: TableSchema): string;
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { SchemaService, SchemaServiceOptions };
|
|
52
|
+
//# sourceMappingURL=service.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.cts","names":[],"sources":["../../src/schema/service.ts"],"mappings":";;;;;;AAeA;UAAiB,oBAAA;;EAEf,MAAA;EAEa;EAAb,aAAA;AAAA;;;;;;;cASW,aAAA;EAAA,QAID,EAAA;EAAA,QACA,OAAA;EAAA,QAJF,YAAA;cAGE,EAAA,EAAI,cAAA,EACJ,OAAA,GAAU,oBAAA;EA0HQ;;;EApHtB,SAAA,CAAU,SAAA,WAAoB,OAAA,CAAQ,WAAA;EANlC;;;EAuCJ,QAAA,CAAS,SAAA,WAAoB,OAAA;EAxCzB;;;EAuEJ,aAAA,CAAA,GAAiB,OAAA,CAAQ,GAAA,SAAY,WAAA;EAhE3B;;;EA0EV,cAAA,CAAA,GAAkB,OAAA;EAzCT;;;EA2ET,aAAA,CAAA,GAAiB,OAAA;EA5CQ;;;EAoD/B,mBAAA,CAAoB,MAAA,EAAQ,WAAA;AAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { TableSchema } from "./types.mjs";
|
|
2
|
+
import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
3
|
+
|
|
4
|
+
//#region src/schema/service.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options for schema service
|
|
7
|
+
*/
|
|
8
|
+
interface SchemaServiceOptions {
|
|
9
|
+
/** Whitelist of tables to include */
|
|
10
|
+
tables?: string[];
|
|
11
|
+
/** Tables to exclude */
|
|
12
|
+
excludeTables?: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Schema service that queries schema on-demand (no caching)
|
|
16
|
+
*
|
|
17
|
+
* SQLite PRAGMA queries are extremely fast, so real-time queries
|
|
18
|
+
* are preferred over caching to avoid staleness issues.
|
|
19
|
+
*/
|
|
20
|
+
declare class SchemaService {
|
|
21
|
+
private db;
|
|
22
|
+
private options?;
|
|
23
|
+
private introspector;
|
|
24
|
+
constructor(db: LibSQLDatabase, options?: SchemaServiceOptions | undefined);
|
|
25
|
+
/**
|
|
26
|
+
* Gets schema for a specific table (real-time query)
|
|
27
|
+
*/
|
|
28
|
+
getSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a table exists (real-time query)
|
|
31
|
+
*/
|
|
32
|
+
hasTable(tableName: string): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Gets all table schemas (real-time query)
|
|
35
|
+
*/
|
|
36
|
+
getAllSchemas(): Promise<Map<string, TableSchema>>;
|
|
37
|
+
/**
|
|
38
|
+
* Lists all table names (real-time query)
|
|
39
|
+
*/
|
|
40
|
+
listTableNames(): Promise<string[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Gets the count of tables
|
|
43
|
+
*/
|
|
44
|
+
getTableCount(): Promise<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the primary key column name for a table
|
|
47
|
+
*/
|
|
48
|
+
getPrimaryKeyColumn(schema: TableSchema): string;
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { SchemaService, SchemaServiceOptions };
|
|
52
|
+
//# sourceMappingURL=service.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.mts","names":[],"sources":["../../src/schema/service.ts"],"mappings":";;;;;;AAeA;UAAiB,oBAAA;;EAEf,MAAA;EAEa;EAAb,aAAA;AAAA;;;;;;;cASW,aAAA;EAAA,QAID,EAAA;EAAA,QACA,OAAA;EAAA,QAJF,YAAA;cAGE,EAAA,EAAI,cAAA,EACJ,OAAA,GAAU,oBAAA;EA0HQ;;;EApHtB,SAAA,CAAU,SAAA,WAAoB,OAAA,CAAQ,WAAA;EANlC;;;EAuCJ,QAAA,CAAS,SAAA,WAAoB,OAAA;EAxCzB;;;EAuEJ,aAAA,CAAA,GAAiB,OAAA,CAAQ,GAAA,SAAY,WAAA;EAhE3B;;;EA0EV,cAAA,CAAA,GAAkB,OAAA;EAzCT;;;EA2ET,aAAA,CAAA,GAAiB,OAAA;EA5CQ;;;EAoD/B,mBAAA,CAAoB,MAAA,EAAQ,WAAA;AAAA"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { SYSTEM_TABLES } from "./types.mjs";
|
|
2
|
+
import { SchemaIntrospector } from "./introspector.mjs";
|
|
3
|
+
import { sql } from "@aigne/sqlite";
|
|
4
|
+
|
|
5
|
+
//#region src/schema/service.ts
|
|
6
|
+
/**
|
|
7
|
+
* Executes a raw SQL query and returns all rows
|
|
8
|
+
*/
|
|
9
|
+
async function execAll(db, query) {
|
|
10
|
+
return db.all(sql.raw(query)).execute();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Schema service that queries schema on-demand (no caching)
|
|
14
|
+
*
|
|
15
|
+
* SQLite PRAGMA queries are extremely fast, so real-time queries
|
|
16
|
+
* are preferred over caching to avoid staleness issues.
|
|
17
|
+
*/
|
|
18
|
+
var SchemaService = class {
|
|
19
|
+
introspector = new SchemaIntrospector();
|
|
20
|
+
constructor(db, options) {
|
|
21
|
+
this.db = db;
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets schema for a specific table (real-time query)
|
|
26
|
+
*/
|
|
27
|
+
async getSchema(tableName) {
|
|
28
|
+
if (!tableName) return;
|
|
29
|
+
if (SYSTEM_TABLES.includes(tableName)) return;
|
|
30
|
+
if (this.options?.tables && !this.options.tables.includes(tableName)) return;
|
|
31
|
+
if (this.options?.excludeTables?.includes(tableName)) return;
|
|
32
|
+
if (!await this.hasTable(tableName)) return;
|
|
33
|
+
return this.introspector.introspectTable(this.db, tableName);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a table exists (real-time query)
|
|
37
|
+
*/
|
|
38
|
+
async hasTable(tableName) {
|
|
39
|
+
if (!tableName) return false;
|
|
40
|
+
if (SYSTEM_TABLES.includes(tableName)) return false;
|
|
41
|
+
if (this.options?.tables && !this.options.tables.includes(tableName)) return false;
|
|
42
|
+
if (this.options?.excludeTables?.includes(tableName)) return false;
|
|
43
|
+
return ((await execAll(this.db, `SELECT COUNT(*) as count FROM sqlite_master WHERE type = 'table' AND name = '${tableName.replace(/'/g, "''")}'`))[0]?.count ?? 0) > 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets all table schemas (real-time query)
|
|
47
|
+
*/
|
|
48
|
+
async getAllSchemas() {
|
|
49
|
+
return this.introspector.introspect(this.db, {
|
|
50
|
+
tables: this.options?.tables,
|
|
51
|
+
excludeTables: this.options?.excludeTables
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Lists all table names (real-time query)
|
|
56
|
+
*/
|
|
57
|
+
async listTableNames() {
|
|
58
|
+
return (await execAll(this.db, `
|
|
59
|
+
SELECT name FROM sqlite_master
|
|
60
|
+
WHERE type = 'table'
|
|
61
|
+
AND name NOT LIKE 'sqlite_%'
|
|
62
|
+
AND name NOT LIKE '%_fts%'
|
|
63
|
+
ORDER BY name
|
|
64
|
+
`)).map((r) => r.name).filter((name) => {
|
|
65
|
+
if (SYSTEM_TABLES.includes(name)) return false;
|
|
66
|
+
if (this.options?.tables && !this.options.tables.includes(name)) return false;
|
|
67
|
+
if (this.options?.excludeTables?.includes(name)) return false;
|
|
68
|
+
return true;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Gets the count of tables
|
|
73
|
+
*/
|
|
74
|
+
async getTableCount() {
|
|
75
|
+
return (await this.listTableNames()).length;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Gets the primary key column name for a table
|
|
79
|
+
*/
|
|
80
|
+
getPrimaryKeyColumn(schema) {
|
|
81
|
+
return this.introspector.getPrimaryKeyColumn(schema);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
export { SchemaService };
|
|
87
|
+
//# sourceMappingURL=service.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.mjs","names":[],"sources":["../../src/schema/service.ts"],"sourcesContent":["import { sql } from \"@aigne/sqlite\";\nimport type { LibSQLDatabase } from \"drizzle-orm/libsql\";\nimport { SchemaIntrospector } from \"./introspector.js\";\nimport { SYSTEM_TABLES, type TableSchema } from \"./types.js\";\n\n/**\n * Executes a raw SQL query and returns all rows\n */\nasync function execAll<T>(db: LibSQLDatabase, query: string): Promise<T[]> {\n return db.all<T>(sql.raw(query)).execute();\n}\n\n/**\n * Options for schema service\n */\nexport interface SchemaServiceOptions {\n /** Whitelist of tables to include */\n tables?: string[];\n /** Tables to exclude */\n excludeTables?: string[];\n}\n\n/**\n * Schema service that queries schema on-demand (no caching)\n *\n * SQLite PRAGMA queries are extremely fast, so real-time queries\n * are preferred over caching to avoid staleness issues.\n */\nexport class SchemaService {\n private introspector = new SchemaIntrospector();\n\n constructor(\n private db: LibSQLDatabase,\n private options?: SchemaServiceOptions,\n ) {}\n\n /**\n * Gets schema for a specific table (real-time query)\n */\n async getSchema(tableName: string): Promise<TableSchema | undefined> {\n // Guard against undefined/null\n if (!tableName) {\n return undefined;\n }\n\n // Skip system tables\n if (SYSTEM_TABLES.includes(tableName as (typeof SYSTEM_TABLES)[number])) {\n return undefined;\n }\n\n // Apply whitelist filter\n if (this.options?.tables && !this.options.tables.includes(tableName)) {\n return undefined;\n }\n\n // Apply exclude filter\n if (this.options?.excludeTables?.includes(tableName)) {\n return undefined;\n }\n\n // Check if table exists\n const exists = await this.hasTable(tableName);\n if (!exists) {\n return undefined;\n }\n\n return this.introspector.introspectTable(this.db, tableName);\n }\n\n /**\n * Checks if a table exists (real-time query)\n */\n async hasTable(tableName: string): Promise<boolean> {\n // Guard against undefined/null\n if (!tableName) {\n return false;\n }\n\n // Skip system tables\n if (SYSTEM_TABLES.includes(tableName as (typeof SYSTEM_TABLES)[number])) {\n return false;\n }\n\n // Apply whitelist filter\n if (this.options?.tables && !this.options.tables.includes(tableName)) {\n return false;\n }\n\n // Apply exclude filter\n if (this.options?.excludeTables?.includes(tableName)) {\n return false;\n }\n\n const result = await execAll<{ count: number }>(\n this.db,\n `SELECT COUNT(*) as count FROM sqlite_master WHERE type = 'table' AND name = '${tableName.replace(/'/g, \"''\")}'`,\n );\n return (result[0]?.count ?? 0) > 0;\n }\n\n /**\n * Gets all table schemas (real-time query)\n */\n async getAllSchemas(): Promise<Map<string, TableSchema>> {\n return this.introspector.introspect(this.db, {\n tables: this.options?.tables,\n excludeTables: this.options?.excludeTables,\n });\n }\n\n /**\n * Lists all table names (real-time query)\n */\n async listTableNames(): Promise<string[]> {\n const result = await execAll<{ name: string }>(\n this.db,\n `\n SELECT name FROM sqlite_master\n WHERE type = 'table'\n AND name NOT LIKE 'sqlite_%'\n AND name NOT LIKE '%_fts%'\n ORDER BY name\n `,\n );\n\n return result\n .map((r) => r.name)\n .filter((name) => {\n // Skip system tables\n if (SYSTEM_TABLES.includes(name as (typeof SYSTEM_TABLES)[number])) {\n return false;\n }\n // Apply whitelist filter\n if (this.options?.tables && !this.options.tables.includes(name)) {\n return false;\n }\n // Apply exclude filter\n if (this.options?.excludeTables?.includes(name)) {\n return false;\n }\n return true;\n });\n }\n\n /**\n * Gets the count of tables\n */\n async getTableCount(): Promise<number> {\n const names = await this.listTableNames();\n return names.length;\n }\n\n /**\n * Gets the primary key column name for a table\n */\n getPrimaryKeyColumn(schema: TableSchema): string {\n return this.introspector.getPrimaryKeyColumn(schema);\n }\n}\n"],"mappings":";;;;;;;;AAQA,eAAe,QAAW,IAAoB,OAA6B;AACzE,QAAO,GAAG,IAAO,IAAI,IAAI,MAAM,CAAC,CAAC,SAAS;;;;;;;;AAmB5C,IAAa,gBAAb,MAA2B;CACzB,AAAQ,eAAe,IAAI,oBAAoB;CAE/C,YACE,AAAQ,IACR,AAAQ,SACR;EAFQ;EACA;;;;;CAMV,MAAM,UAAU,WAAqD;AAEnE,MAAI,CAAC,UACH;AAIF,MAAI,cAAc,SAAS,UAA4C,CACrE;AAIF,MAAI,KAAK,SAAS,UAAU,CAAC,KAAK,QAAQ,OAAO,SAAS,UAAU,CAClE;AAIF,MAAI,KAAK,SAAS,eAAe,SAAS,UAAU,CAClD;AAKF,MAAI,CADW,MAAM,KAAK,SAAS,UAAU,CAE3C;AAGF,SAAO,KAAK,aAAa,gBAAgB,KAAK,IAAI,UAAU;;;;;CAM9D,MAAM,SAAS,WAAqC;AAElD,MAAI,CAAC,UACH,QAAO;AAIT,MAAI,cAAc,SAAS,UAA4C,CACrE,QAAO;AAIT,MAAI,KAAK,SAAS,UAAU,CAAC,KAAK,QAAQ,OAAO,SAAS,UAAU,CAClE,QAAO;AAIT,MAAI,KAAK,SAAS,eAAe,SAAS,UAAU,CAClD,QAAO;AAOT,WAJe,MAAM,QACnB,KAAK,IACL,gFAAgF,UAAU,QAAQ,MAAM,KAAK,CAAC,GAC/G,EACc,IAAI,SAAS,KAAK;;;;;CAMnC,MAAM,gBAAmD;AACvD,SAAO,KAAK,aAAa,WAAW,KAAK,IAAI;GAC3C,QAAQ,KAAK,SAAS;GACtB,eAAe,KAAK,SAAS;GAC9B,CAAC;;;;;CAMJ,MAAM,iBAAoC;AAYxC,UAXe,MAAM,QACnB,KAAK,IACL;;;;;;MAOD,EAGE,KAAK,MAAM,EAAE,KAAK,CAClB,QAAQ,SAAS;AAEhB,OAAI,cAAc,SAAS,KAAuC,CAChE,QAAO;AAGT,OAAI,KAAK,SAAS,UAAU,CAAC,KAAK,QAAQ,OAAO,SAAS,KAAK,CAC7D,QAAO;AAGT,OAAI,KAAK,SAAS,eAAe,SAAS,KAAK,CAC7C,QAAO;AAET,UAAO;IACP;;;;;CAMN,MAAM,gBAAiC;AAErC,UADc,MAAM,KAAK,gBAAgB,EAC5B;;;;;CAMf,oBAAoB,QAA6B;AAC/C,SAAO,KAAK,aAAa,oBAAoB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/schema/types.ts"],"mappings":";;AAGA
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/schema/types.ts"],"mappings":";;AAGA;;UAAiB,UAAA;EAAU;EAEzB,IAAA;EAEA;EAAA,IAAA;EAIA;EAFA,OAAA;EAIS;EAFT,EAAA;EAQe;EANf,SAAA;AAAA;;;;UAMe,cAAA;EAQf;EANA,EAAA;EAUA;EARA,GAAA;EAYA;EAVA,KAAA;EAUK;EARL,IAAA;EAcwB;EAZxB,EAAA;EAYwB;EAVxB,QAAA;EAcA;EAZA,QAAA;EAgBA;EAdA,KAAA;AAAA;;AAsBF;;UAhBiB,SAAA;EAoBN;EAlBT,GAAA;EAwBS;EAtBT,IAAA;EAsBkB;EApBlB,MAAA;EAcA;EAZA,MAAA;EAcA;EAZA,OAAA;AAAA;;;;UAMe,WAAA;EAgBA;EAdf,IAAA;;EAEA,OAAA,EAAS,UAAA;EAaT;EAXA,UAAA;EAaA;EAXA,WAAA,EAAa,cAAA;EAab;EAXA,OAAA,EAAS,SAAA;AAAA;;AAkBX;;UAZiB,kBAAA;EACf,GAAA;EACA,IAAA;EACA,IAAA;EACA,OAAA;EACA,UAAA;EACA,EAAA;AAAA;;;;UAMe,mBAAA;EACf,EAAA;EACA,GAAA;EACA,KAAA;EACA,IAAA;EACA,EAAA;EACA,SAAA;EACA,SAAA;EACA,KAAA;AAAA;;;;UAMe,kBAAA;EACf,GAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;EACA,OAAA;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/schema/types.ts"],"mappings":";;AAGA
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/schema/types.ts"],"mappings":";;AAGA;;UAAiB,UAAA;EAAU;EAEzB,IAAA;EAEA;EAAA,IAAA;EAIA;EAFA,OAAA;EAIS;EAFT,EAAA;EAQe;EANf,SAAA;AAAA;;;;UAMe,cAAA;EAQf;EANA,EAAA;EAUA;EARA,GAAA;EAYA;EAVA,KAAA;EAUK;EARL,IAAA;EAcwB;EAZxB,EAAA;EAYwB;EAVxB,QAAA;EAcA;EAZA,QAAA;EAgBA;EAdA,KAAA;AAAA;;AAsBF;;UAhBiB,SAAA;EAoBN;EAlBT,GAAA;EAwBS;EAtBT,IAAA;EAsBkB;EApBlB,MAAA;EAcA;EAZA,MAAA;EAcA;EAZA,OAAA;AAAA;;;;UAMe,WAAA;EAgBA;EAdf,IAAA;;EAEA,OAAA,EAAS,UAAA;EAaT;EAXA,UAAA;EAaA;EAXA,WAAA,EAAa,cAAA;EAab;EAXA,OAAA,EAAS,SAAA;AAAA;;AAkBX;;UAZiB,kBAAA;EACf,GAAA;EACA,IAAA;EACA,IAAA;EACA,OAAA;EACA,UAAA;EACA,EAAA;AAAA;;;;UAMe,mBAAA;EACf,EAAA;EACA,GAAA;EACA,KAAA;EACA,IAAA;EACA,EAAA;EACA,SAAA;EACA,SAAA;EACA,KAAA;AAAA;;;;UAMe,kBAAA;EACf,GAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;EACA,OAAA;AAAA"}
|