@aigne/afs-sqlite 1.11.0-beta → 1.11.0-beta.1
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/LICENSE.md +17 -84
- package/README.md +0 -3
- package/dist/actions/built-in.cjs +142 -0
- package/dist/actions/built-in.d.cts +10 -0
- package/dist/actions/built-in.d.cts.map +1 -0
- package/dist/actions/built-in.d.mts +10 -0
- package/dist/actions/built-in.d.mts.map +1 -0
- package/dist/actions/built-in.mjs +143 -0
- package/dist/actions/built-in.mjs.map +1 -0
- package/dist/actions/registry.cjs +91 -0
- package/dist/actions/registry.d.cts +54 -0
- package/dist/actions/registry.d.cts.map +1 -0
- package/dist/actions/registry.d.mts +54 -0
- package/dist/actions/registry.d.mts.map +1 -0
- package/dist/actions/registry.mjs +91 -0
- package/dist/actions/registry.mjs.map +1 -0
- package/dist/actions/types.d.cts +56 -0
- package/dist/actions/types.d.cts.map +1 -0
- package/dist/actions/types.d.mts +56 -0
- package/dist/actions/types.d.mts.map +1 -0
- package/dist/config.cjs +27 -0
- package/dist/config.d.cts +81 -0
- package/dist/config.d.cts.map +1 -0
- package/dist/config.d.mts +81 -0
- package/dist/config.d.mts.map +1 -0
- package/dist/config.mjs +28 -0
- package/dist/config.mjs.map +1 -0
- package/dist/index.cjs +38 -1324
- package/dist/index.d.cts +14 -758
- package/dist/index.d.mts +14 -758
- package/dist/index.mjs +12 -1299
- package/dist/node/builder.cjs +179 -0
- package/dist/node/builder.d.cts +48 -0
- package/dist/node/builder.d.cts.map +1 -0
- package/dist/node/builder.d.mts +48 -0
- package/dist/node/builder.d.mts.map +1 -0
- package/dist/node/builder.mjs +172 -0
- package/dist/node/builder.mjs.map +1 -0
- package/dist/operations/crud.cjs +176 -0
- package/dist/operations/crud.d.cts +69 -0
- package/dist/operations/crud.d.cts.map +1 -0
- package/dist/operations/crud.d.mts +69 -0
- package/dist/operations/crud.d.mts.map +1 -0
- package/dist/operations/crud.mjs +177 -0
- package/dist/operations/crud.mjs.map +1 -0
- package/dist/operations/query-builder.cjs +77 -0
- package/dist/operations/query-builder.d.cts +34 -0
- package/dist/operations/query-builder.d.cts.map +1 -0
- package/dist/operations/query-builder.d.mts +34 -0
- package/dist/operations/query-builder.d.mts.map +1 -0
- package/dist/operations/query-builder.mjs +72 -0
- package/dist/operations/query-builder.mjs.map +1 -0
- package/dist/operations/search.cjs +141 -0
- package/dist/operations/search.d.cts +79 -0
- package/dist/operations/search.d.cts.map +1 -0
- package/dist/operations/search.d.mts +79 -0
- package/dist/operations/search.d.mts.map +1 -0
- package/dist/operations/search.mjs +141 -0
- package/dist/operations/search.mjs.map +1 -0
- package/dist/router/path-router.cjs +79 -0
- package/dist/router/path-router.d.cts +42 -0
- package/dist/router/path-router.d.cts.map +1 -0
- package/dist/router/path-router.d.mts +42 -0
- package/dist/router/path-router.d.mts.map +1 -0
- package/dist/router/path-router.mjs +76 -0
- package/dist/router/path-router.mjs.map +1 -0
- package/dist/router/types.d.cts +34 -0
- package/dist/router/types.d.cts.map +1 -0
- package/dist/router/types.d.mts +34 -0
- package/dist/router/types.d.mts.map +1 -0
- package/dist/schema/introspector.cjs +160 -0
- package/dist/schema/introspector.d.cts +49 -0
- package/dist/schema/introspector.d.cts.map +1 -0
- package/dist/schema/introspector.d.mts +49 -0
- package/dist/schema/introspector.d.mts.map +1 -0
- package/dist/schema/introspector.mjs +161 -0
- package/dist/schema/introspector.mjs.map +1 -0
- package/dist/schema/types.cjs +15 -0
- package/dist/schema/types.d.cts +104 -0
- package/dist/schema/types.d.cts.map +1 -0
- package/dist/schema/types.d.mts +104 -0
- package/dist/schema/types.d.mts.map +1 -0
- package/dist/schema/types.mjs +15 -0
- package/dist/schema/types.mjs.map +1 -0
- package/dist/sqlite-afs.cjs +264 -0
- package/dist/sqlite-afs.d.cts +152 -0
- package/dist/sqlite-afs.d.cts.map +1 -0
- package/dist/sqlite-afs.d.mts +152 -0
- package/dist/sqlite-afs.d.mts.map +1 -0
- package/dist/sqlite-afs.mjs +265 -0
- package/dist/sqlite-afs.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const require_builder = require('../node/builder.cjs');
|
|
2
|
+
let _aigne_sqlite = require("@aigne/sqlite");
|
|
3
|
+
|
|
4
|
+
//#region src/operations/search.ts
|
|
5
|
+
/**
|
|
6
|
+
* Executes a raw SQL query and returns all rows
|
|
7
|
+
*/
|
|
8
|
+
async function execAll(db, query) {
|
|
9
|
+
return db.all(_aigne_sqlite.sql.raw(query)).execute();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* FTS5 Search operations for SQLite AFS
|
|
13
|
+
*/
|
|
14
|
+
var FTSSearch = class {
|
|
15
|
+
constructor(db, schemas, config, basePath = "") {
|
|
16
|
+
this.db = db;
|
|
17
|
+
this.schemas = schemas;
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.basePath = basePath;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Performs full-text search across configured tables
|
|
23
|
+
*/
|
|
24
|
+
async search(query, options) {
|
|
25
|
+
if (!this.config.enabled) return {
|
|
26
|
+
data: [],
|
|
27
|
+
message: "Full-text search is not enabled"
|
|
28
|
+
};
|
|
29
|
+
const results = [];
|
|
30
|
+
const limit = options?.limit ?? 50;
|
|
31
|
+
const buildOptions = { basePath: this.basePath };
|
|
32
|
+
const tablesToSearch = options?.tables ? options.tables.filter((t) => this.config.tables.has(t)) : Array.from(this.config.tables.keys());
|
|
33
|
+
const ftsQuery = this.prepareFTSQuery(query, options?.caseSensitive);
|
|
34
|
+
for (const tableName of tablesToSearch) {
|
|
35
|
+
const tableConfig = this.config.tables.get(tableName);
|
|
36
|
+
const schema = this.schemas.get(tableName);
|
|
37
|
+
if (!tableConfig || !schema) continue;
|
|
38
|
+
const ftsTableName = `${tableName}_fts`;
|
|
39
|
+
try {
|
|
40
|
+
if (!await this.ftsTableExists(ftsTableName)) continue;
|
|
41
|
+
const highlightColumn = tableConfig.columns[0] ?? "";
|
|
42
|
+
const highlightIndex = highlightColumn ? tableConfig.columns.indexOf(highlightColumn) : 0;
|
|
43
|
+
const rows = await execAll(this.db, `
|
|
44
|
+
SELECT t.*, highlight("${ftsTableName}", ${highlightIndex}, '<mark>', '</mark>') as snippet
|
|
45
|
+
FROM "${ftsTableName}" fts
|
|
46
|
+
JOIN "${tableName}" t ON fts.rowid = t.rowid
|
|
47
|
+
WHERE "${ftsTableName}" MATCH '${ftsQuery}'
|
|
48
|
+
LIMIT ${Math.ceil(limit / tablesToSearch.length)}
|
|
49
|
+
`);
|
|
50
|
+
for (const row of rows) {
|
|
51
|
+
const { snippet, ...rowData } = row;
|
|
52
|
+
results.push(require_builder.buildSearchEntry(tableName, schema, rowData, snippet, buildOptions));
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.warn(`FTS search failed for table ${tableName}:`, error);
|
|
56
|
+
}
|
|
57
|
+
if (results.length >= limit) break;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
data: results.slice(0, limit),
|
|
61
|
+
message: results.length === 0 ? `No results found for "${query}"` : void 0
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Searches within a specific table
|
|
66
|
+
*/
|
|
67
|
+
async searchTable(tableName, query, options) {
|
|
68
|
+
return this.search(query, {
|
|
69
|
+
...options,
|
|
70
|
+
tables: [tableName]
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if FTS is configured for a table
|
|
75
|
+
*/
|
|
76
|
+
hasFTS(tableName) {
|
|
77
|
+
return this.config.enabled && this.config.tables.has(tableName);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Gets FTS configuration for a table
|
|
81
|
+
*/
|
|
82
|
+
getFTSConfig(tableName) {
|
|
83
|
+
return this.config.tables.get(tableName);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Checks if an FTS table exists
|
|
87
|
+
*/
|
|
88
|
+
async ftsTableExists(ftsTableName) {
|
|
89
|
+
return (await execAll(this.db, `SELECT name FROM sqlite_master WHERE type = 'table' AND name = '${ftsTableName}'`)).length > 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Prepares a query string for FTS5
|
|
93
|
+
* Handles special characters and case sensitivity
|
|
94
|
+
*/
|
|
95
|
+
prepareFTSQuery(query, _caseSensitive) {
|
|
96
|
+
let prepared = query.replace(/"/g, "\"\"").replace(/'/g, "''");
|
|
97
|
+
if (prepared.includes(" ") && !prepared.startsWith("\"")) prepared = `"${prepared}"`;
|
|
98
|
+
return prepared;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Updates the schemas map (after refresh)
|
|
102
|
+
*/
|
|
103
|
+
setSchemas(schemas) {
|
|
104
|
+
this.schemas = schemas;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Simple search fallback when FTS is not available
|
|
108
|
+
* Uses LIKE queries on specified columns
|
|
109
|
+
*/
|
|
110
|
+
async simpleLikeSearch(tableName, query, columns, options) {
|
|
111
|
+
const schema = this.schemas.get(tableName);
|
|
112
|
+
if (!schema) return {
|
|
113
|
+
data: [],
|
|
114
|
+
message: `Table '${tableName}' not found`
|
|
115
|
+
};
|
|
116
|
+
const buildOptions = { basePath: this.basePath };
|
|
117
|
+
const limit = options?.limit ?? 50;
|
|
118
|
+
const escapedQuery = query.replace(/'/g, "''");
|
|
119
|
+
const conditions = columns.filter((col) => schema.columns.some((c) => c.name === col)).map((col) => `"${col}" LIKE '%${escapedQuery}%'`).join(" OR ");
|
|
120
|
+
if (!conditions) return {
|
|
121
|
+
data: [],
|
|
122
|
+
message: "No valid columns to search"
|
|
123
|
+
};
|
|
124
|
+
return { data: (await execAll(this.db, `SELECT * FROM "${tableName}" WHERE ${conditions} LIMIT ${limit}`)).map((row) => require_builder.buildSearchEntry(tableName, schema, row, void 0, buildOptions)) };
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Creates FTS configuration from options
|
|
129
|
+
*/
|
|
130
|
+
function createFTSConfig(options) {
|
|
131
|
+
const config = {
|
|
132
|
+
enabled: options?.enabled ?? false,
|
|
133
|
+
tables: /* @__PURE__ */ new Map()
|
|
134
|
+
};
|
|
135
|
+
if (options?.tables) for (const [table, columns] of Object.entries(options.tables)) config.tables.set(table, { columns });
|
|
136
|
+
return config;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
exports.FTSSearch = FTSSearch;
|
|
141
|
+
exports.createFTSConfig = createFTSConfig;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { TableSchema } from "../schema/types.cjs";
|
|
2
|
+
import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
3
|
+
import { AFSSearchOptions, AFSSearchResult } from "@aigne/afs";
|
|
4
|
+
|
|
5
|
+
//#region src/operations/search.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* FTS5 search configuration for a table
|
|
8
|
+
*/
|
|
9
|
+
interface FTSTableConfig {
|
|
10
|
+
/** Columns to include in FTS index */
|
|
11
|
+
columns: string[];
|
|
12
|
+
/** Whether FTS table has been created */
|
|
13
|
+
initialized?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* FTS5 search configuration
|
|
17
|
+
*/
|
|
18
|
+
interface FTSConfig {
|
|
19
|
+
/** Whether FTS is enabled */
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
/** Per-table FTS configuration */
|
|
22
|
+
tables: Map<string, FTSTableConfig>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* FTS5 Search operations for SQLite AFS
|
|
26
|
+
*/
|
|
27
|
+
declare class FTSSearch {
|
|
28
|
+
private db;
|
|
29
|
+
private schemas;
|
|
30
|
+
private config;
|
|
31
|
+
private basePath;
|
|
32
|
+
constructor(db: LibSQLDatabase, schemas: Map<string, TableSchema>, config: FTSConfig, basePath?: string);
|
|
33
|
+
/**
|
|
34
|
+
* Performs full-text search across configured tables
|
|
35
|
+
*/
|
|
36
|
+
search(query: string, options?: AFSSearchOptions & {
|
|
37
|
+
/** Specific tables to search (defaults to all FTS-enabled tables) */tables?: string[];
|
|
38
|
+
}): Promise<AFSSearchResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Searches within a specific table
|
|
41
|
+
*/
|
|
42
|
+
searchTable(tableName: string, query: string, options?: AFSSearchOptions): Promise<AFSSearchResult>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if FTS is configured for a table
|
|
45
|
+
*/
|
|
46
|
+
hasFTS(tableName: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Gets FTS configuration for a table
|
|
49
|
+
*/
|
|
50
|
+
getFTSConfig(tableName: string): FTSTableConfig | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Checks if an FTS table exists
|
|
53
|
+
*/
|
|
54
|
+
private ftsTableExists;
|
|
55
|
+
/**
|
|
56
|
+
* Prepares a query string for FTS5
|
|
57
|
+
* Handles special characters and case sensitivity
|
|
58
|
+
*/
|
|
59
|
+
private prepareFTSQuery;
|
|
60
|
+
/**
|
|
61
|
+
* Updates the schemas map (after refresh)
|
|
62
|
+
*/
|
|
63
|
+
setSchemas(schemas: Map<string, TableSchema>): void;
|
|
64
|
+
/**
|
|
65
|
+
* Simple search fallback when FTS is not available
|
|
66
|
+
* Uses LIKE queries on specified columns
|
|
67
|
+
*/
|
|
68
|
+
simpleLikeSearch(tableName: string, query: string, columns: string[], options?: AFSSearchOptions): Promise<AFSSearchResult>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates FTS configuration from options
|
|
72
|
+
*/
|
|
73
|
+
declare function createFTSConfig(options?: {
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
tables?: Record<string, string[]>;
|
|
76
|
+
}): FTSConfig;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { FTSConfig, FTSSearch, FTSTableConfig, createFTSConfig };
|
|
79
|
+
//# sourceMappingURL=search.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.d.cts","names":[],"sources":["../../src/operations/search.ts"],"mappings":";;;;;;AAgBA;AAUA;UAViB,cAAA;EAAA;EAAA,OAAA;EAAA;EAAA,WAAA;AAAA;AAAA;AAUjB;AAUA;AApBiB,UAUA,SAAA;EAAA;EAAA,OAAA;EAAA;EAAA,MAAA,EAIP,GAAA,SAAY,cAAA;AAAA;AAAA;AAMtB;;AANsB,cAMT,SAAA;EAAA,QAAA,EAAA;EAAA,QAAA,OAAA;EAAA,QAAA,MAAA;EAAA,QAAA,QAAA;EAAA,YAAA,EAAA,EAEG,cAAA,EAAA,OAAA,EACK,GAAA,SAAY,WAAA,GAAA,MAAA,EACb,SAAA,EAAA,QAAA;EAAA;;;EAAA,OAAA,KAAA,UAAA,OAAA,GASN,gBAAA;IAAA,qEAAA,MAAA;EAAA,IAIT,OAAA,CAAQ,eAAA;EAAA;;;EAAA,YAAA,SAAA,UAAA,KAAA,UAAA,OAAA,GA+EC,gBAAA,GACT,OAAA,CAAQ,eAAA;EAAA;;;EAAA,OAAA,SAAA;EAAA;;;EAAA,aAAA,SAAA,WAcsB,cAAA;EAAA;;;EAAA,QAAA,cAAA;EAAA;;;;EAAA,QAAA,eAAA;EAAA;;;EAAA,WAAA,OAAA,EAuCb,GAAA,SAAY,WAAA;EAAA;;;;EAAA,iBAAA,SAAA,UAAA,KAAA,UAAA,OAAA,YAAA,OAAA,GAYpB,gBAAA,GACT,OAAA,CAAQ,eAAA;AAAA;AAAA;;;AAAA,iBAkCG,eAAA,CAAA,OAAA;EAAA,OAAA;EAAA,MAAA,GAEL,MAAA;AAAA,IACP,SAAA"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { TableSchema } from "../schema/types.mjs";
|
|
2
|
+
import { AFSSearchOptions, AFSSearchResult } from "@aigne/afs";
|
|
3
|
+
import { LibSQLDatabase } from "drizzle-orm/libsql";
|
|
4
|
+
|
|
5
|
+
//#region src/operations/search.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* FTS5 search configuration for a table
|
|
8
|
+
*/
|
|
9
|
+
interface FTSTableConfig {
|
|
10
|
+
/** Columns to include in FTS index */
|
|
11
|
+
columns: string[];
|
|
12
|
+
/** Whether FTS table has been created */
|
|
13
|
+
initialized?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* FTS5 search configuration
|
|
17
|
+
*/
|
|
18
|
+
interface FTSConfig {
|
|
19
|
+
/** Whether FTS is enabled */
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
/** Per-table FTS configuration */
|
|
22
|
+
tables: Map<string, FTSTableConfig>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* FTS5 Search operations for SQLite AFS
|
|
26
|
+
*/
|
|
27
|
+
declare class FTSSearch {
|
|
28
|
+
private db;
|
|
29
|
+
private schemas;
|
|
30
|
+
private config;
|
|
31
|
+
private basePath;
|
|
32
|
+
constructor(db: LibSQLDatabase, schemas: Map<string, TableSchema>, config: FTSConfig, basePath?: string);
|
|
33
|
+
/**
|
|
34
|
+
* Performs full-text search across configured tables
|
|
35
|
+
*/
|
|
36
|
+
search(query: string, options?: AFSSearchOptions & {
|
|
37
|
+
/** Specific tables to search (defaults to all FTS-enabled tables) */tables?: string[];
|
|
38
|
+
}): Promise<AFSSearchResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Searches within a specific table
|
|
41
|
+
*/
|
|
42
|
+
searchTable(tableName: string, query: string, options?: AFSSearchOptions): Promise<AFSSearchResult>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if FTS is configured for a table
|
|
45
|
+
*/
|
|
46
|
+
hasFTS(tableName: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Gets FTS configuration for a table
|
|
49
|
+
*/
|
|
50
|
+
getFTSConfig(tableName: string): FTSTableConfig | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Checks if an FTS table exists
|
|
53
|
+
*/
|
|
54
|
+
private ftsTableExists;
|
|
55
|
+
/**
|
|
56
|
+
* Prepares a query string for FTS5
|
|
57
|
+
* Handles special characters and case sensitivity
|
|
58
|
+
*/
|
|
59
|
+
private prepareFTSQuery;
|
|
60
|
+
/**
|
|
61
|
+
* Updates the schemas map (after refresh)
|
|
62
|
+
*/
|
|
63
|
+
setSchemas(schemas: Map<string, TableSchema>): void;
|
|
64
|
+
/**
|
|
65
|
+
* Simple search fallback when FTS is not available
|
|
66
|
+
* Uses LIKE queries on specified columns
|
|
67
|
+
*/
|
|
68
|
+
simpleLikeSearch(tableName: string, query: string, columns: string[], options?: AFSSearchOptions): Promise<AFSSearchResult>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates FTS configuration from options
|
|
72
|
+
*/
|
|
73
|
+
declare function createFTSConfig(options?: {
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
tables?: Record<string, string[]>;
|
|
76
|
+
}): FTSConfig;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { FTSConfig, FTSSearch, FTSTableConfig, createFTSConfig };
|
|
79
|
+
//# sourceMappingURL=search.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.d.mts","names":[],"sources":["../../src/operations/search.ts"],"mappings":";;;;;;AAgBA;AAUA;UAViB,cAAA;EAAA;EAAA,OAAA;EAAA;EAAA,WAAA;AAAA;AAAA;AAUjB;AAUA;AApBiB,UAUA,SAAA;EAAA;EAAA,OAAA;EAAA;EAAA,MAAA,EAIP,GAAA,SAAY,cAAA;AAAA;AAAA;AAMtB;;AANsB,cAMT,SAAA;EAAA,QAAA,EAAA;EAAA,QAAA,OAAA;EAAA,QAAA,MAAA;EAAA,QAAA,QAAA;EAAA,YAAA,EAAA,EAEG,cAAA,EAAA,OAAA,EACK,GAAA,SAAY,WAAA,GAAA,MAAA,EACb,SAAA,EAAA,QAAA;EAAA;;;EAAA,OAAA,KAAA,UAAA,OAAA,GASN,gBAAA;IAAA,qEAAA,MAAA;EAAA,IAIT,OAAA,CAAQ,eAAA;EAAA;;;EAAA,YAAA,SAAA,UAAA,KAAA,UAAA,OAAA,GA+EC,gBAAA,GACT,OAAA,CAAQ,eAAA;EAAA;;;EAAA,OAAA,SAAA;EAAA;;;EAAA,aAAA,SAAA,WAcsB,cAAA;EAAA;;;EAAA,QAAA,cAAA;EAAA;;;;EAAA,QAAA,eAAA;EAAA;;;EAAA,WAAA,OAAA,EAuCb,GAAA,SAAY,WAAA;EAAA;;;;EAAA,iBAAA,SAAA,UAAA,KAAA,UAAA,OAAA,YAAA,OAAA,GAYpB,gBAAA,GACT,OAAA,CAAQ,eAAA;AAAA;AAAA;;;AAAA,iBAkCG,eAAA,CAAA,OAAA;EAAA,OAAA;EAAA,MAAA,GAEL,MAAA;AAAA,IACP,SAAA"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { buildSearchEntry } from "../node/builder.mjs";
|
|
2
|
+
import { sql } from "@aigne/sqlite";
|
|
3
|
+
|
|
4
|
+
//#region src/operations/search.ts
|
|
5
|
+
/**
|
|
6
|
+
* Executes a raw SQL query and returns all rows
|
|
7
|
+
*/
|
|
8
|
+
async function execAll(db, query) {
|
|
9
|
+
return db.all(sql.raw(query)).execute();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* FTS5 Search operations for SQLite AFS
|
|
13
|
+
*/
|
|
14
|
+
var FTSSearch = class {
|
|
15
|
+
constructor(db, schemas, config, basePath = "") {
|
|
16
|
+
this.db = db;
|
|
17
|
+
this.schemas = schemas;
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.basePath = basePath;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Performs full-text search across configured tables
|
|
23
|
+
*/
|
|
24
|
+
async search(query, options) {
|
|
25
|
+
if (!this.config.enabled) return {
|
|
26
|
+
data: [],
|
|
27
|
+
message: "Full-text search is not enabled"
|
|
28
|
+
};
|
|
29
|
+
const results = [];
|
|
30
|
+
const limit = options?.limit ?? 50;
|
|
31
|
+
const buildOptions = { basePath: this.basePath };
|
|
32
|
+
const tablesToSearch = options?.tables ? options.tables.filter((t) => this.config.tables.has(t)) : Array.from(this.config.tables.keys());
|
|
33
|
+
const ftsQuery = this.prepareFTSQuery(query, options?.caseSensitive);
|
|
34
|
+
for (const tableName of tablesToSearch) {
|
|
35
|
+
const tableConfig = this.config.tables.get(tableName);
|
|
36
|
+
const schema = this.schemas.get(tableName);
|
|
37
|
+
if (!tableConfig || !schema) continue;
|
|
38
|
+
const ftsTableName = `${tableName}_fts`;
|
|
39
|
+
try {
|
|
40
|
+
if (!await this.ftsTableExists(ftsTableName)) continue;
|
|
41
|
+
const highlightColumn = tableConfig.columns[0] ?? "";
|
|
42
|
+
const highlightIndex = highlightColumn ? tableConfig.columns.indexOf(highlightColumn) : 0;
|
|
43
|
+
const rows = await execAll(this.db, `
|
|
44
|
+
SELECT t.*, highlight("${ftsTableName}", ${highlightIndex}, '<mark>', '</mark>') as snippet
|
|
45
|
+
FROM "${ftsTableName}" fts
|
|
46
|
+
JOIN "${tableName}" t ON fts.rowid = t.rowid
|
|
47
|
+
WHERE "${ftsTableName}" MATCH '${ftsQuery}'
|
|
48
|
+
LIMIT ${Math.ceil(limit / tablesToSearch.length)}
|
|
49
|
+
`);
|
|
50
|
+
for (const row of rows) {
|
|
51
|
+
const { snippet, ...rowData } = row;
|
|
52
|
+
results.push(buildSearchEntry(tableName, schema, rowData, snippet, buildOptions));
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.warn(`FTS search failed for table ${tableName}:`, error);
|
|
56
|
+
}
|
|
57
|
+
if (results.length >= limit) break;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
data: results.slice(0, limit),
|
|
61
|
+
message: results.length === 0 ? `No results found for "${query}"` : void 0
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Searches within a specific table
|
|
66
|
+
*/
|
|
67
|
+
async searchTable(tableName, query, options) {
|
|
68
|
+
return this.search(query, {
|
|
69
|
+
...options,
|
|
70
|
+
tables: [tableName]
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if FTS is configured for a table
|
|
75
|
+
*/
|
|
76
|
+
hasFTS(tableName) {
|
|
77
|
+
return this.config.enabled && this.config.tables.has(tableName);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Gets FTS configuration for a table
|
|
81
|
+
*/
|
|
82
|
+
getFTSConfig(tableName) {
|
|
83
|
+
return this.config.tables.get(tableName);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Checks if an FTS table exists
|
|
87
|
+
*/
|
|
88
|
+
async ftsTableExists(ftsTableName) {
|
|
89
|
+
return (await execAll(this.db, `SELECT name FROM sqlite_master WHERE type = 'table' AND name = '${ftsTableName}'`)).length > 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Prepares a query string for FTS5
|
|
93
|
+
* Handles special characters and case sensitivity
|
|
94
|
+
*/
|
|
95
|
+
prepareFTSQuery(query, _caseSensitive) {
|
|
96
|
+
let prepared = query.replace(/"/g, "\"\"").replace(/'/g, "''");
|
|
97
|
+
if (prepared.includes(" ") && !prepared.startsWith("\"")) prepared = `"${prepared}"`;
|
|
98
|
+
return prepared;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Updates the schemas map (after refresh)
|
|
102
|
+
*/
|
|
103
|
+
setSchemas(schemas) {
|
|
104
|
+
this.schemas = schemas;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Simple search fallback when FTS is not available
|
|
108
|
+
* Uses LIKE queries on specified columns
|
|
109
|
+
*/
|
|
110
|
+
async simpleLikeSearch(tableName, query, columns, options) {
|
|
111
|
+
const schema = this.schemas.get(tableName);
|
|
112
|
+
if (!schema) return {
|
|
113
|
+
data: [],
|
|
114
|
+
message: `Table '${tableName}' not found`
|
|
115
|
+
};
|
|
116
|
+
const buildOptions = { basePath: this.basePath };
|
|
117
|
+
const limit = options?.limit ?? 50;
|
|
118
|
+
const escapedQuery = query.replace(/'/g, "''");
|
|
119
|
+
const conditions = columns.filter((col) => schema.columns.some((c) => c.name === col)).map((col) => `"${col}" LIKE '%${escapedQuery}%'`).join(" OR ");
|
|
120
|
+
if (!conditions) return {
|
|
121
|
+
data: [],
|
|
122
|
+
message: "No valid columns to search"
|
|
123
|
+
};
|
|
124
|
+
return { data: (await execAll(this.db, `SELECT * FROM "${tableName}" WHERE ${conditions} LIMIT ${limit}`)).map((row) => buildSearchEntry(tableName, schema, row, void 0, buildOptions)) };
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Creates FTS configuration from options
|
|
129
|
+
*/
|
|
130
|
+
function createFTSConfig(options) {
|
|
131
|
+
const config = {
|
|
132
|
+
enabled: options?.enabled ?? false,
|
|
133
|
+
tables: /* @__PURE__ */ new Map()
|
|
134
|
+
};
|
|
135
|
+
if (options?.tables) for (const [table, columns] of Object.entries(options.tables)) config.tables.set(table, { columns });
|
|
136
|
+
return config;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
export { FTSSearch, createFTSConfig };
|
|
141
|
+
//# sourceMappingURL=search.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search.mjs","names":[],"sources":["../../src/operations/search.ts"],"sourcesContent":["import type { AFSEntry, AFSSearchOptions, AFSSearchResult } from \"@aigne/afs\";\nimport { sql } from \"@aigne/sqlite\";\nimport type { LibSQLDatabase } from \"drizzle-orm/libsql\";\nimport { type BuildEntryOptions, buildSearchEntry } from \"../node/builder.js\";\nimport type { TableSchema } from \"../schema/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 * FTS5 search configuration for a table\n */\nexport interface FTSTableConfig {\n /** Columns to include in FTS index */\n columns: string[];\n /** Whether FTS table has been created */\n initialized?: boolean;\n}\n\n/**\n * FTS5 search configuration\n */\nexport interface FTSConfig {\n /** Whether FTS is enabled */\n enabled: boolean;\n /** Per-table FTS configuration */\n tables: Map<string, FTSTableConfig>;\n}\n\n/**\n * FTS5 Search operations for SQLite AFS\n */\nexport class FTSSearch {\n constructor(\n private db: LibSQLDatabase,\n private schemas: Map<string, TableSchema>,\n private config: FTSConfig,\n private basePath: string = \"\",\n ) {}\n\n /**\n * Performs full-text search across configured tables\n */\n async search(\n query: string,\n options?: AFSSearchOptions & {\n /** Specific tables to search (defaults to all FTS-enabled tables) */\n tables?: string[];\n },\n ): Promise<AFSSearchResult> {\n if (!this.config.enabled) {\n return { data: [], message: \"Full-text search is not enabled\" };\n }\n\n const results: AFSEntry[] = [];\n const limit = options?.limit ?? 50;\n const buildOptions: BuildEntryOptions = { basePath: this.basePath };\n\n // Determine which tables to search\n const tablesToSearch = options?.tables\n ? options.tables.filter((t) => this.config.tables.has(t))\n : Array.from(this.config.tables.keys());\n\n // Escape and prepare the query for FTS5\n const ftsQuery = this.prepareFTSQuery(query, options?.caseSensitive);\n\n for (const tableName of tablesToSearch) {\n const tableConfig = this.config.tables.get(tableName);\n const schema = this.schemas.get(tableName);\n\n if (!tableConfig || !schema) continue;\n\n const ftsTableName = `${tableName}_fts`;\n\n try {\n // Check if FTS table exists\n const ftsExists = await this.ftsTableExists(ftsTableName);\n if (!ftsExists) continue;\n\n // Get the first column for highlighting\n const highlightColumn = tableConfig.columns[0] ?? \"\";\n const highlightIndex = highlightColumn ? tableConfig.columns.indexOf(highlightColumn) : 0;\n\n // Build FTS query with highlight\n const rows = await execAll<Record<string, unknown> & { snippet?: string }>(\n this.db,\n `\n SELECT t.*, highlight(\"${ftsTableName}\", ${highlightIndex}, '<mark>', '</mark>') as snippet\n FROM \"${ftsTableName}\" fts\n JOIN \"${tableName}\" t ON fts.rowid = t.rowid\n WHERE \"${ftsTableName}\" MATCH '${ftsQuery}'\n LIMIT ${Math.ceil(limit / tablesToSearch.length)}\n `,\n );\n\n for (const row of rows) {\n const { snippet, ...rowData } = row;\n results.push(\n buildSearchEntry(\n tableName,\n schema,\n rowData,\n snippet as string | undefined,\n buildOptions,\n ),\n );\n }\n } catch (error) {\n // Log but continue with other tables\n console.warn(`FTS search failed for table ${tableName}:`, error);\n }\n\n // Stop if we have enough results\n if (results.length >= limit) break;\n }\n\n return {\n data: results.slice(0, limit),\n message: results.length === 0 ? `No results found for \"${query}\"` : undefined,\n };\n }\n\n /**\n * Searches within a specific table\n */\n async searchTable(\n tableName: string,\n query: string,\n options?: AFSSearchOptions,\n ): Promise<AFSSearchResult> {\n return this.search(query, { ...options, tables: [tableName] });\n }\n\n /**\n * Checks if FTS is configured for a table\n */\n hasFTS(tableName: string): boolean {\n return this.config.enabled && this.config.tables.has(tableName);\n }\n\n /**\n * Gets FTS configuration for a table\n */\n getFTSConfig(tableName: string): FTSTableConfig | undefined {\n return this.config.tables.get(tableName);\n }\n\n /**\n * Checks if an FTS table exists\n */\n private async ftsTableExists(ftsTableName: string): Promise<boolean> {\n const result = await execAll<{ name: string }>(\n this.db,\n `SELECT name FROM sqlite_master WHERE type = 'table' AND name = '${ftsTableName}'`,\n );\n return result.length > 0;\n }\n\n /**\n * Prepares a query string for FTS5\n * Handles special characters and case sensitivity\n */\n private prepareFTSQuery(query: string, _caseSensitive?: boolean): string {\n // Escape special FTS5 characters\n let prepared = query\n .replace(/\"/g, '\"\"') // Escape double quotes\n .replace(/'/g, \"''\"); // Escape single quotes\n\n // For case-insensitive search (default), we don't need to modify\n // FTS5 is case-insensitive by default for ASCII\n\n // If the query contains multiple words, search for the phrase\n if (prepared.includes(\" \") && !prepared.startsWith('\"')) {\n prepared = `\"${prepared}\"`;\n }\n\n return prepared;\n }\n\n /**\n * Updates the schemas map (after refresh)\n */\n setSchemas(schemas: Map<string, TableSchema>): void {\n this.schemas = schemas;\n }\n\n /**\n * Simple search fallback when FTS is not available\n * Uses LIKE queries on specified columns\n */\n async simpleLikeSearch(\n tableName: string,\n query: string,\n columns: string[],\n options?: AFSSearchOptions,\n ): Promise<AFSSearchResult> {\n const schema = this.schemas.get(tableName);\n if (!schema) {\n return { data: [], message: `Table '${tableName}' not found` };\n }\n\n const buildOptions: BuildEntryOptions = { basePath: this.basePath };\n const limit = options?.limit ?? 50;\n const escapedQuery = query.replace(/'/g, \"''\");\n\n // Build LIKE conditions for each column\n const conditions = columns\n .filter((col) => schema.columns.some((c) => c.name === col))\n .map((col) => `\"${col}\" LIKE '%${escapedQuery}%'`)\n .join(\" OR \");\n\n if (!conditions) {\n return { data: [], message: \"No valid columns to search\" };\n }\n\n const rows = await execAll<Record<string, unknown>>(\n this.db,\n `SELECT * FROM \"${tableName}\" WHERE ${conditions} LIMIT ${limit}`,\n );\n\n return {\n data: rows.map((row) => buildSearchEntry(tableName, schema, row, undefined, buildOptions)),\n };\n }\n}\n\n/**\n * Creates FTS configuration from options\n */\nexport function createFTSConfig(options?: {\n enabled?: boolean;\n tables?: Record<string, string[]>;\n}): FTSConfig {\n const config: FTSConfig = {\n enabled: options?.enabled ?? false,\n tables: new Map(),\n };\n\n if (options?.tables) {\n for (const [table, columns] of Object.entries(options.tables)) {\n config.tables.set(table, { columns });\n }\n }\n\n return config;\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
let radix3 = require("radix3");
|
|
2
|
+
|
|
3
|
+
//#region src/router/path-router.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a radix3 router for SQLite AFS path routing
|
|
6
|
+
*
|
|
7
|
+
* Routes:
|
|
8
|
+
* - / → listTables
|
|
9
|
+
* - /:table → listTable
|
|
10
|
+
* - /:table/new → createRow
|
|
11
|
+
* - /:table/@schema → getSchema
|
|
12
|
+
* - /:table/:pk → readRow
|
|
13
|
+
* - /:table/:pk/@attr → listAttributes
|
|
14
|
+
* - /:table/:pk/@attr/:column → getAttribute
|
|
15
|
+
* - /:table/:pk/@meta → getMeta
|
|
16
|
+
* - /:table/:pk/@actions → listActions
|
|
17
|
+
* - /:table/:pk/@actions/:action → executeAction
|
|
18
|
+
*/
|
|
19
|
+
function createPathRouter() {
|
|
20
|
+
return (0, radix3.createRouter)({ routes: {
|
|
21
|
+
"/": { action: "listTables" },
|
|
22
|
+
"/:table": { action: "listTable" },
|
|
23
|
+
"/:table/new": { action: "createRow" },
|
|
24
|
+
"/:table/@schema": { action: "getSchema" },
|
|
25
|
+
"/:table/:pk": { action: "readRow" },
|
|
26
|
+
"/:table/:pk/@attr": { action: "listAttributes" },
|
|
27
|
+
"/:table/:pk/@attr/:column": { action: "getAttribute" },
|
|
28
|
+
"/:table/:pk/@meta": { action: "getMeta" },
|
|
29
|
+
"/:table/:pk/@actions": { action: "listActions" },
|
|
30
|
+
"/:table/:pk/@actions/:action": { action: "executeAction" }
|
|
31
|
+
} });
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parses a path and returns the matched route with params
|
|
35
|
+
* @param router - The radix3 router instance
|
|
36
|
+
* @param path - The path to match
|
|
37
|
+
* @returns RouteMatch if matched, undefined otherwise
|
|
38
|
+
*/
|
|
39
|
+
function matchPath(router, path) {
|
|
40
|
+
const result = router.lookup(path);
|
|
41
|
+
if (!result) return void 0;
|
|
42
|
+
return {
|
|
43
|
+
action: result.action,
|
|
44
|
+
params: result.params ?? {}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Builds a path from components
|
|
49
|
+
*/
|
|
50
|
+
function buildPath(table, pk, suffix) {
|
|
51
|
+
const parts = ["/"];
|
|
52
|
+
if (table) parts.push(table);
|
|
53
|
+
if (pk) parts.push(pk);
|
|
54
|
+
if (suffix) parts.push(suffix);
|
|
55
|
+
return parts.join("/").replace(/\/+/g, "/");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Checks if a path segment is a virtual path (@attr, @meta, @actions, @schema)
|
|
59
|
+
*/
|
|
60
|
+
function isVirtualPath(segment) {
|
|
61
|
+
return segment.startsWith("@");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Gets the type of virtual path
|
|
65
|
+
*/
|
|
66
|
+
function getVirtualPathType(segment) {
|
|
67
|
+
if (segment === "@attr") return "attr";
|
|
68
|
+
if (segment === "@meta") return "meta";
|
|
69
|
+
if (segment === "@actions") return "actions";
|
|
70
|
+
if (segment === "@schema") return "schema";
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
exports.buildPath = buildPath;
|
|
76
|
+
exports.createPathRouter = createPathRouter;
|
|
77
|
+
exports.getVirtualPathType = getVirtualPathType;
|
|
78
|
+
exports.isVirtualPath = isVirtualPath;
|
|
79
|
+
exports.matchPath = matchPath;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { RouteData, RouteMatch } from "./types.cjs";
|
|
2
|
+
import { RadixRouter } from "radix3";
|
|
3
|
+
|
|
4
|
+
//#region src/router/path-router.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a radix3 router for SQLite AFS path routing
|
|
7
|
+
*
|
|
8
|
+
* Routes:
|
|
9
|
+
* - / → listTables
|
|
10
|
+
* - /:table → listTable
|
|
11
|
+
* - /:table/new → createRow
|
|
12
|
+
* - /:table/@schema → getSchema
|
|
13
|
+
* - /:table/:pk → readRow
|
|
14
|
+
* - /:table/:pk/@attr → listAttributes
|
|
15
|
+
* - /:table/:pk/@attr/:column → getAttribute
|
|
16
|
+
* - /:table/:pk/@meta → getMeta
|
|
17
|
+
* - /:table/:pk/@actions → listActions
|
|
18
|
+
* - /:table/:pk/@actions/:action → executeAction
|
|
19
|
+
*/
|
|
20
|
+
declare function createPathRouter(): RadixRouter<RouteData>;
|
|
21
|
+
/**
|
|
22
|
+
* Parses a path and returns the matched route with params
|
|
23
|
+
* @param router - The radix3 router instance
|
|
24
|
+
* @param path - The path to match
|
|
25
|
+
* @returns RouteMatch if matched, undefined otherwise
|
|
26
|
+
*/
|
|
27
|
+
declare function matchPath(router: RadixRouter<RouteData>, path: string): RouteMatch | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Builds a path from components
|
|
30
|
+
*/
|
|
31
|
+
declare function buildPath(table?: string, pk?: string, suffix?: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Checks if a path segment is a virtual path (@attr, @meta, @actions, @schema)
|
|
34
|
+
*/
|
|
35
|
+
declare function isVirtualPath(segment: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the type of virtual path
|
|
38
|
+
*/
|
|
39
|
+
declare function getVirtualPathType(segment: string): "attr" | "meta" | "actions" | "schema" | null;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { buildPath, createPathRouter, getVirtualPathType, isVirtualPath, matchPath };
|
|
42
|
+
//# sourceMappingURL=path-router.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-router.d.cts","names":[],"sources":["../../src/router/path-router.ts"],"mappings":";;;;;AAoBA;AA4BA;;;;;AAaA;AAWA;AAOA;;;;;;iBA3DgB,gBAAA,CAAA,GAAoB,WAAA,CAAY,SAAA;AAAA;AA4BhD;;;;;AA5BgD,iBA4BhC,SAAA,CAAA,MAAA,EAAkB,WAAA,CAAY,SAAA,GAAA,IAAA,WAA2B,UAAA;AAAA;;AAazE;AAbyE,iBAazD,SAAA,CAAA,KAAA,WAAA,EAAA,WAAA,MAAA;AAAA;AAWhB;AAOA;AAlBgB,iBAWA,aAAA,CAAA,OAAA;AAAA;AAOhB;;AAPgB,iBAOA,kBAAA,CAAA,OAAA"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { RouteData, RouteMatch } from "./types.mjs";
|
|
2
|
+
import { RadixRouter } from "radix3";
|
|
3
|
+
|
|
4
|
+
//#region src/router/path-router.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a radix3 router for SQLite AFS path routing
|
|
7
|
+
*
|
|
8
|
+
* Routes:
|
|
9
|
+
* - / → listTables
|
|
10
|
+
* - /:table → listTable
|
|
11
|
+
* - /:table/new → createRow
|
|
12
|
+
* - /:table/@schema → getSchema
|
|
13
|
+
* - /:table/:pk → readRow
|
|
14
|
+
* - /:table/:pk/@attr → listAttributes
|
|
15
|
+
* - /:table/:pk/@attr/:column → getAttribute
|
|
16
|
+
* - /:table/:pk/@meta → getMeta
|
|
17
|
+
* - /:table/:pk/@actions → listActions
|
|
18
|
+
* - /:table/:pk/@actions/:action → executeAction
|
|
19
|
+
*/
|
|
20
|
+
declare function createPathRouter(): RadixRouter<RouteData>;
|
|
21
|
+
/**
|
|
22
|
+
* Parses a path and returns the matched route with params
|
|
23
|
+
* @param router - The radix3 router instance
|
|
24
|
+
* @param path - The path to match
|
|
25
|
+
* @returns RouteMatch if matched, undefined otherwise
|
|
26
|
+
*/
|
|
27
|
+
declare function matchPath(router: RadixRouter<RouteData>, path: string): RouteMatch | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Builds a path from components
|
|
30
|
+
*/
|
|
31
|
+
declare function buildPath(table?: string, pk?: string, suffix?: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Checks if a path segment is a virtual path (@attr, @meta, @actions, @schema)
|
|
34
|
+
*/
|
|
35
|
+
declare function isVirtualPath(segment: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the type of virtual path
|
|
38
|
+
*/
|
|
39
|
+
declare function getVirtualPathType(segment: string): "attr" | "meta" | "actions" | "schema" | null;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { buildPath, createPathRouter, getVirtualPathType, isVirtualPath, matchPath };
|
|
42
|
+
//# sourceMappingURL=path-router.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-router.d.mts","names":[],"sources":["../../src/router/path-router.ts"],"mappings":";;;;;AAoBA;AA4BA;;;;;AAaA;AAWA;AAOA;;;;;;iBA3DgB,gBAAA,CAAA,GAAoB,WAAA,CAAY,SAAA;AAAA;AA4BhD;;;;;AA5BgD,iBA4BhC,SAAA,CAAA,MAAA,EAAkB,WAAA,CAAY,SAAA,GAAA,IAAA,WAA2B,UAAA;AAAA;;AAazE;AAbyE,iBAazD,SAAA,CAAA,KAAA,WAAA,EAAA,WAAA,MAAA;AAAA;AAWhB;AAOA;AAlBgB,iBAWA,aAAA,CAAA,OAAA;AAAA;AAOhB;;AAPgB,iBAOA,kBAAA,CAAA,OAAA"}
|