@kiyeonjeon21/datacontext 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/sqlite.d.ts.map +1 -1
- package/dist/adapters/sqlite.js +13 -0
- package/dist/adapters/sqlite.js.map +1 -1
- package/dist/api/server.d.ts.map +1 -1
- package/dist/api/server.js +115 -0
- package/dist/api/server.js.map +1 -1
- package/dist/cli/index.js +58 -14
- package/dist/cli/index.js.map +1 -1
- package/dist/core/context-service.d.ts +63 -0
- package/dist/core/context-service.d.ts.map +1 -1
- package/dist/core/context-service.js +66 -0
- package/dist/core/context-service.js.map +1 -1
- package/dist/core/harvester.d.ts +57 -5
- package/dist/core/harvester.d.ts.map +1 -1
- package/dist/core/harvester.js +86 -6
- package/dist/core/harvester.js.map +1 -1
- package/dist/core/types.d.ts +21 -5
- package/dist/core/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/knowledge/store.d.ts +186 -3
- package/dist/knowledge/store.d.ts.map +1 -1
- package/dist/knowledge/store.js +389 -5
- package/dist/knowledge/store.js.map +1 -1
- package/dist/knowledge/types.d.ts +252 -4
- package/dist/knowledge/types.d.ts.map +1 -1
- package/dist/knowledge/types.js +138 -1
- package/dist/knowledge/types.js.map +1 -1
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +231 -3
- package/dist/mcp/tools.js.map +1 -1
- package/docs/KNOWLEDGE_GRAPH.md +540 -0
- package/docs/KNOWLEDGE_TYPES.md +261 -0
- package/docs/MULTI_DB_ARCHITECTURE.md +319 -0
- package/package.json +1 -1
- package/scripts/create-sqlite-testdb.sh +75 -0
- package/scripts/test-databases.sh +324 -0
- package/sqlite:./test-sqlite.db +0 -0
- package/src/adapters/sqlite.ts +16 -0
- package/src/api/server.ts +134 -0
- package/src/cli/index.ts +57 -16
- package/src/core/context-service.ts +70 -0
- package/src/core/harvester.ts +120 -8
- package/src/core/types.ts +21 -5
- package/src/index.ts +19 -1
- package/src/knowledge/store.ts +480 -6
- package/src/knowledge/types.ts +321 -4
- package/src/mcp/tools.ts +273 -3
- package/test-sqlite.db +0 -0
- package/tests/knowledge-store.test.ts +130 -0
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Knowledge Types
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* Type definitions for the Knowledge Store / Knowledge Graph.
|
|
5
|
+
* This module defines the core data structures for storing business context
|
|
6
|
+
* about database schemas, including:
|
|
7
|
+
*
|
|
8
|
+
* - **TableDescription**: Business context for tables and columns
|
|
9
|
+
* - **TableRelationship**: Graph edges representing table relationships (FK, implicit, manual)
|
|
10
|
+
* - **QueryExample**: Verified SQL patterns with intent
|
|
11
|
+
* - **BusinessRule**: Domain constraints and conventions
|
|
12
|
+
* - **BusinessTerm**: Glossary entries for natural language → SQL translation
|
|
13
|
+
*
|
|
14
|
+
* ## Knowledge Graph Architecture
|
|
15
|
+
*
|
|
16
|
+
* The Knowledge Store is designed to evolve into a Knowledge Graph:
|
|
17
|
+
*
|
|
18
|
+
* ```
|
|
19
|
+
* ┌─────────────────────────────────────────────────────────────────┐
|
|
20
|
+
* │ Knowledge Graph │
|
|
21
|
+
* │ │
|
|
22
|
+
* │ [Table A] ──JOINS_WITH──> [Table B] ──JOINS_WITH──> [Table C]│
|
|
23
|
+
* │ │ │ │
|
|
24
|
+
* │ │ │ │
|
|
25
|
+
* │ [Description] [Description] │
|
|
26
|
+
* │ [Rules] [Rules] │
|
|
27
|
+
* │ [Examples] [Terms] │
|
|
28
|
+
* │ │
|
|
29
|
+
* └─────────────────────────────────────────────────────────────────┘
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ## Multi-Database Support
|
|
33
|
+
*
|
|
34
|
+
* All references use `TableRef` which includes optional database identifier,
|
|
35
|
+
* enabling future cross-database relationship tracking.
|
|
36
|
+
*
|
|
37
|
+
* @module knowledge/types
|
|
4
38
|
*/
|
|
5
39
|
/** Source of the knowledge entry */
|
|
6
40
|
export type KnowledgeSource = 'auto' | 'user' | 'derived';
|
|
@@ -23,6 +57,115 @@ export interface KnowledgeMeta {
|
|
|
23
57
|
/** Owner/creator */
|
|
24
58
|
owner?: string;
|
|
25
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Fully qualified table reference.
|
|
62
|
+
*
|
|
63
|
+
* Enables cross-database and cross-schema relationship tracking.
|
|
64
|
+
* The `database` field is optional for single-database scenarios.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* // Single database (most common)
|
|
69
|
+
* const ref: TableRef = { schema: 'public', table: 'users' };
|
|
70
|
+
*
|
|
71
|
+
* // Multi-database (future)
|
|
72
|
+
* const ref: TableRef = { database: 'analytics', schema: 'public', table: 'events' };
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export interface TableRef {
|
|
76
|
+
/** Database identifier (optional for single-DB mode) */
|
|
77
|
+
database?: string;
|
|
78
|
+
/** Schema name */
|
|
79
|
+
schema: string;
|
|
80
|
+
/** Table name */
|
|
81
|
+
table: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Relationship type between tables.
|
|
85
|
+
*
|
|
86
|
+
* - `foreign_key`: Relationship derived from database FK constraint
|
|
87
|
+
* - `implicit_join`: Relationship inferred from query patterns (no FK)
|
|
88
|
+
* - `manual`: Relationship defined manually by user
|
|
89
|
+
*/
|
|
90
|
+
export type RelationshipType = 'foreign_key' | 'implicit_join' | 'manual';
|
|
91
|
+
/**
|
|
92
|
+
* Cardinality of a table relationship.
|
|
93
|
+
*
|
|
94
|
+
* - `one-to-one`: Each row in A maps to exactly one row in B
|
|
95
|
+
* - `one-to-many`: Each row in A maps to multiple rows in B (most common for FK)
|
|
96
|
+
* - `many-to-one`: Multiple rows in A map to one row in B
|
|
97
|
+
* - `many-to-many`: Requires junction table
|
|
98
|
+
*/
|
|
99
|
+
export type RelationshipCardinality = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many';
|
|
100
|
+
/**
|
|
101
|
+
* Table relationship representing a graph edge.
|
|
102
|
+
*
|
|
103
|
+
* This is the core type for the Knowledge Graph, representing how tables
|
|
104
|
+
* are connected. Relationships can be:
|
|
105
|
+
* - Auto-harvested from foreign key constraints
|
|
106
|
+
* - Inferred from query patterns (implicit joins)
|
|
107
|
+
* - Manually defined by users
|
|
108
|
+
*
|
|
109
|
+
* ## Graph Traversal
|
|
110
|
+
*
|
|
111
|
+
* Relationships enable graph queries like:
|
|
112
|
+
* - "Find all tables related to users"
|
|
113
|
+
* - "Find the shortest join path from orders to products"
|
|
114
|
+
* - "Which tables reference this column?"
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* // FK-based relationship (auto-harvested)
|
|
119
|
+
* const relationship: TableRelationship = {
|
|
120
|
+
* type: 'table_relationship',
|
|
121
|
+
* from: { schema: 'public', table: 'orders' },
|
|
122
|
+
* to: { schema: 'public', table: 'users' },
|
|
123
|
+
* relationshipType: 'foreign_key',
|
|
124
|
+
* joinCondition: 'orders.user_id = users.id',
|
|
125
|
+
* cardinality: 'many-to-one',
|
|
126
|
+
* fromColumns: ['user_id'],
|
|
127
|
+
* toColumns: ['id'],
|
|
128
|
+
* isPreferred: true,
|
|
129
|
+
* // ... metadata fields
|
|
130
|
+
* };
|
|
131
|
+
*
|
|
132
|
+
* // Implicit join (learned from queries)
|
|
133
|
+
* const implicit: TableRelationship = {
|
|
134
|
+
* type: 'table_relationship',
|
|
135
|
+
* from: { schema: 'public', table: 'events' },
|
|
136
|
+
* to: { schema: 'public', table: 'users' },
|
|
137
|
+
* relationshipType: 'implicit_join',
|
|
138
|
+
* joinCondition: 'events.actor_id = users.id',
|
|
139
|
+
* cardinality: 'many-to-one',
|
|
140
|
+
* isPreferred: false,
|
|
141
|
+
* notes: 'Inferred from query patterns',
|
|
142
|
+
* // ... metadata fields
|
|
143
|
+
* };
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export interface TableRelationship extends KnowledgeMeta {
|
|
147
|
+
type: 'table_relationship';
|
|
148
|
+
/** Source table (FK side for foreign_key type) */
|
|
149
|
+
from: TableRef;
|
|
150
|
+
/** Target table (referenced side for foreign_key type) */
|
|
151
|
+
to: TableRef;
|
|
152
|
+
/** How this relationship was discovered */
|
|
153
|
+
relationshipType: RelationshipType;
|
|
154
|
+
/** SQL join condition */
|
|
155
|
+
joinCondition: string;
|
|
156
|
+
/** Relationship cardinality */
|
|
157
|
+
cardinality?: RelationshipCardinality;
|
|
158
|
+
/** Columns in the source table involved in the join */
|
|
159
|
+
fromColumns?: string[];
|
|
160
|
+
/** Columns in the target table involved in the join */
|
|
161
|
+
toColumns?: string[];
|
|
162
|
+
/** Whether this is the preferred join path (when multiple exist) */
|
|
163
|
+
isPreferred: boolean;
|
|
164
|
+
/** FK constraint name (if from database) */
|
|
165
|
+
constraintName?: string;
|
|
166
|
+
/** Human-readable notes about this relationship */
|
|
167
|
+
notes?: string;
|
|
168
|
+
}
|
|
26
169
|
/** Column description */
|
|
27
170
|
export interface ColumnDescription {
|
|
28
171
|
/** Column name */
|
|
@@ -126,8 +269,36 @@ export interface BusinessTerm extends KnowledgeMeta {
|
|
|
126
269
|
isActive: boolean;
|
|
127
270
|
}
|
|
128
271
|
/** Union type for all knowledge entries */
|
|
129
|
-
export type KnowledgeEntry = TableDescription | QueryExample | BusinessRule | BusinessTerm;
|
|
130
|
-
/**
|
|
272
|
+
export type KnowledgeEntry = TableDescription | TableRelationship | QueryExample | BusinessRule | BusinessTerm;
|
|
273
|
+
/**
|
|
274
|
+
* Complete knowledge data structure.
|
|
275
|
+
*
|
|
276
|
+
* This is the root data structure for the Knowledge Store, containing:
|
|
277
|
+
* - **Nodes**: Tables, columns (within TableDescription)
|
|
278
|
+
* - **Edges**: Relationships between tables (TableRelationship)
|
|
279
|
+
* - **Annotations**: Rules, examples, terms
|
|
280
|
+
*
|
|
281
|
+
* ## Data Model (Graph Perspective)
|
|
282
|
+
*
|
|
283
|
+
* ```
|
|
284
|
+
* Nodes:
|
|
285
|
+
* - TableDescription (with embedded ColumnDescription[])
|
|
286
|
+
*
|
|
287
|
+
* Edges:
|
|
288
|
+
* - TableRelationship (from → to with join condition)
|
|
289
|
+
*
|
|
290
|
+
* Annotations (attached to nodes):
|
|
291
|
+
* - QueryExample (references tables)
|
|
292
|
+
* - BusinessRule (applies to tables/columns)
|
|
293
|
+
* - BusinessTerm (applies to tables/columns)
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* ## Version History
|
|
297
|
+
*
|
|
298
|
+
* - `1.0.0`: Initial version (tables, examples, rules)
|
|
299
|
+
* - `1.1.0`: Added businessTerms (glossary)
|
|
300
|
+
* - `2.0.0`: Added tableRelationships (graph edges)
|
|
301
|
+
*/
|
|
131
302
|
export interface KnowledgeData {
|
|
132
303
|
/** Version of the knowledge format */
|
|
133
304
|
version: string;
|
|
@@ -137,8 +308,10 @@ export interface KnowledgeData {
|
|
|
137
308
|
schemaHash: string;
|
|
138
309
|
/** Last sync timestamp */
|
|
139
310
|
lastSyncAt: string;
|
|
140
|
-
/** Table descriptions */
|
|
311
|
+
/** Table descriptions (graph nodes) */
|
|
141
312
|
tableDescriptions: TableDescription[];
|
|
313
|
+
/** Table relationships (graph edges) - NEW in v2.0 */
|
|
314
|
+
tableRelationships: TableRelationship[];
|
|
142
315
|
/** Query examples */
|
|
143
316
|
queryExamples: QueryExample[];
|
|
144
317
|
/** Business rules */
|
|
@@ -150,4 +323,79 @@ export interface KnowledgeData {
|
|
|
150
323
|
export declare function generateId(): string;
|
|
151
324
|
/** Create base metadata for a new knowledge entry */
|
|
152
325
|
export declare function createKnowledgeMeta(source: KnowledgeSource, schemaHash: string, owner?: string): KnowledgeMeta;
|
|
326
|
+
/**
|
|
327
|
+
* Create a TableRef from table name and optional schema/database.
|
|
328
|
+
*
|
|
329
|
+
* @param table - Table name
|
|
330
|
+
* @param schema - Schema name (defaults to 'public')
|
|
331
|
+
* @param database - Database name (optional)
|
|
332
|
+
* @returns TableRef object
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```typescript
|
|
336
|
+
* const ref = createTableRef('users');
|
|
337
|
+
* // { schema: 'public', table: 'users' }
|
|
338
|
+
*
|
|
339
|
+
* const ref2 = createTableRef('orders', 'sales', 'analytics_db');
|
|
340
|
+
* // { database: 'analytics_db', schema: 'sales', table: 'orders' }
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
export declare function createTableRef(table: string, schema?: string, database?: string): TableRef;
|
|
344
|
+
/**
|
|
345
|
+
* Convert TableRef to a fully qualified string representation.
|
|
346
|
+
*
|
|
347
|
+
* @param ref - TableRef to stringify
|
|
348
|
+
* @returns String like "database.schema.table" or "schema.table"
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```typescript
|
|
352
|
+
* tableRefToString({ schema: 'public', table: 'users' })
|
|
353
|
+
* // "public.users"
|
|
354
|
+
*
|
|
355
|
+
* tableRefToString({ database: 'analytics', schema: 'public', table: 'events' })
|
|
356
|
+
* // "analytics.public.events"
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export declare function tableRefToString(ref: TableRef): string;
|
|
360
|
+
/**
|
|
361
|
+
* Check if two TableRefs refer to the same table.
|
|
362
|
+
*
|
|
363
|
+
* @param a - First TableRef
|
|
364
|
+
* @param b - Second TableRef
|
|
365
|
+
* @returns true if they refer to the same table
|
|
366
|
+
*/
|
|
367
|
+
export declare function tableRefsEqual(a: TableRef, b: TableRef): boolean;
|
|
368
|
+
/**
|
|
369
|
+
* Create a TableRelationship from foreign key information.
|
|
370
|
+
*
|
|
371
|
+
* This is the standard way to create relationships from harvested FK data.
|
|
372
|
+
*
|
|
373
|
+
* @param params - Relationship parameters
|
|
374
|
+
* @returns Complete TableRelationship object
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* const relationship = createRelationshipFromFK({
|
|
379
|
+
* fromTable: 'orders',
|
|
380
|
+
* fromSchema: 'public',
|
|
381
|
+
* fromColumn: 'user_id',
|
|
382
|
+
* toTable: 'users',
|
|
383
|
+
* toSchema: 'public',
|
|
384
|
+
* toColumn: 'id',
|
|
385
|
+
* constraintName: 'orders_user_id_fkey',
|
|
386
|
+
* schemaHash: 'abc123',
|
|
387
|
+
* });
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
export declare function createRelationshipFromFK(params: {
|
|
391
|
+
fromTable: string;
|
|
392
|
+
fromSchema?: string;
|
|
393
|
+
fromColumn: string;
|
|
394
|
+
toTable: string;
|
|
395
|
+
toSchema?: string;
|
|
396
|
+
toColumn: string;
|
|
397
|
+
constraintName?: string;
|
|
398
|
+
schemaHash: string;
|
|
399
|
+
database?: string;
|
|
400
|
+
}): TableRelationship;
|
|
153
401
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/knowledge/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/knowledge/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,oCAAoC;AACpC,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;AAE1D,8CAA8C;AAC9C,MAAM,WAAW,aAAa;IAC5B,wBAAwB;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,+BAA+B;IAC/B,MAAM,EAAE,eAAe,CAAC;IACxB,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,wCAAwC;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,4BAA4B;IA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,EAAE,MAAM,CAAC;CACnB,GAAG,iBAAiB,CAiBpB"}
|
package/dist/knowledge/types.js
CHANGED
|
@@ -1,11 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
3
|
* Knowledge Types
|
|
4
|
-
*
|
|
4
|
+
*
|
|
5
|
+
* Type definitions for the Knowledge Store / Knowledge Graph.
|
|
6
|
+
* This module defines the core data structures for storing business context
|
|
7
|
+
* about database schemas, including:
|
|
8
|
+
*
|
|
9
|
+
* - **TableDescription**: Business context for tables and columns
|
|
10
|
+
* - **TableRelationship**: Graph edges representing table relationships (FK, implicit, manual)
|
|
11
|
+
* - **QueryExample**: Verified SQL patterns with intent
|
|
12
|
+
* - **BusinessRule**: Domain constraints and conventions
|
|
13
|
+
* - **BusinessTerm**: Glossary entries for natural language → SQL translation
|
|
14
|
+
*
|
|
15
|
+
* ## Knowledge Graph Architecture
|
|
16
|
+
*
|
|
17
|
+
* The Knowledge Store is designed to evolve into a Knowledge Graph:
|
|
18
|
+
*
|
|
19
|
+
* ```
|
|
20
|
+
* ┌─────────────────────────────────────────────────────────────────┐
|
|
21
|
+
* │ Knowledge Graph │
|
|
22
|
+
* │ │
|
|
23
|
+
* │ [Table A] ──JOINS_WITH──> [Table B] ──JOINS_WITH──> [Table C]│
|
|
24
|
+
* │ │ │ │
|
|
25
|
+
* │ │ │ │
|
|
26
|
+
* │ [Description] [Description] │
|
|
27
|
+
* │ [Rules] [Rules] │
|
|
28
|
+
* │ [Examples] [Terms] │
|
|
29
|
+
* │ │
|
|
30
|
+
* └─────────────────────────────────────────────────────────────────┘
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ## Multi-Database Support
|
|
34
|
+
*
|
|
35
|
+
* All references use `TableRef` which includes optional database identifier,
|
|
36
|
+
* enabling future cross-database relationship tracking.
|
|
37
|
+
*
|
|
38
|
+
* @module knowledge/types
|
|
5
39
|
*/
|
|
6
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
41
|
exports.generateId = generateId;
|
|
8
42
|
exports.createKnowledgeMeta = createKnowledgeMeta;
|
|
43
|
+
exports.createTableRef = createTableRef;
|
|
44
|
+
exports.tableRefToString = tableRefToString;
|
|
45
|
+
exports.tableRefsEqual = tableRefsEqual;
|
|
46
|
+
exports.createRelationshipFromFK = createRelationshipFromFK;
|
|
47
|
+
// ============================================================
|
|
48
|
+
// Factory Functions
|
|
49
|
+
// ============================================================
|
|
9
50
|
/** Generate a unique ID */
|
|
10
51
|
function generateId() {
|
|
11
52
|
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -24,4 +65,100 @@ function createKnowledgeMeta(source, schemaHash, owner) {
|
|
|
24
65
|
owner,
|
|
25
66
|
};
|
|
26
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a TableRef from table name and optional schema/database.
|
|
70
|
+
*
|
|
71
|
+
* @param table - Table name
|
|
72
|
+
* @param schema - Schema name (defaults to 'public')
|
|
73
|
+
* @param database - Database name (optional)
|
|
74
|
+
* @returns TableRef object
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const ref = createTableRef('users');
|
|
79
|
+
* // { schema: 'public', table: 'users' }
|
|
80
|
+
*
|
|
81
|
+
* const ref2 = createTableRef('orders', 'sales', 'analytics_db');
|
|
82
|
+
* // { database: 'analytics_db', schema: 'sales', table: 'orders' }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
function createTableRef(table, schema = 'public', database) {
|
|
86
|
+
const ref = { schema, table };
|
|
87
|
+
if (database) {
|
|
88
|
+
ref.database = database;
|
|
89
|
+
}
|
|
90
|
+
return ref;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Convert TableRef to a fully qualified string representation.
|
|
94
|
+
*
|
|
95
|
+
* @param ref - TableRef to stringify
|
|
96
|
+
* @returns String like "database.schema.table" or "schema.table"
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* tableRefToString({ schema: 'public', table: 'users' })
|
|
101
|
+
* // "public.users"
|
|
102
|
+
*
|
|
103
|
+
* tableRefToString({ database: 'analytics', schema: 'public', table: 'events' })
|
|
104
|
+
* // "analytics.public.events"
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
function tableRefToString(ref) {
|
|
108
|
+
if (ref.database) {
|
|
109
|
+
return `${ref.database}.${ref.schema}.${ref.table}`;
|
|
110
|
+
}
|
|
111
|
+
return `${ref.schema}.${ref.table}`;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check if two TableRefs refer to the same table.
|
|
115
|
+
*
|
|
116
|
+
* @param a - First TableRef
|
|
117
|
+
* @param b - Second TableRef
|
|
118
|
+
* @returns true if they refer to the same table
|
|
119
|
+
*/
|
|
120
|
+
function tableRefsEqual(a, b) {
|
|
121
|
+
return (a.table === b.table &&
|
|
122
|
+
a.schema === b.schema &&
|
|
123
|
+
(a.database ?? '') === (b.database ?? ''));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create a TableRelationship from foreign key information.
|
|
127
|
+
*
|
|
128
|
+
* This is the standard way to create relationships from harvested FK data.
|
|
129
|
+
*
|
|
130
|
+
* @param params - Relationship parameters
|
|
131
|
+
* @returns Complete TableRelationship object
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const relationship = createRelationshipFromFK({
|
|
136
|
+
* fromTable: 'orders',
|
|
137
|
+
* fromSchema: 'public',
|
|
138
|
+
* fromColumn: 'user_id',
|
|
139
|
+
* toTable: 'users',
|
|
140
|
+
* toSchema: 'public',
|
|
141
|
+
* toColumn: 'id',
|
|
142
|
+
* constraintName: 'orders_user_id_fkey',
|
|
143
|
+
* schemaHash: 'abc123',
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
function createRelationshipFromFK(params) {
|
|
148
|
+
const fromSchema = params.fromSchema ?? 'public';
|
|
149
|
+
const toSchema = params.toSchema ?? 'public';
|
|
150
|
+
return {
|
|
151
|
+
...createKnowledgeMeta('auto', params.schemaHash),
|
|
152
|
+
type: 'table_relationship',
|
|
153
|
+
from: createTableRef(params.fromTable, fromSchema, params.database),
|
|
154
|
+
to: createTableRef(params.toTable, toSchema, params.database),
|
|
155
|
+
relationshipType: 'foreign_key',
|
|
156
|
+
joinCondition: `${params.fromTable}.${params.fromColumn} = ${params.toTable}.${params.toColumn}`,
|
|
157
|
+
cardinality: 'many-to-one', // Default for FK
|
|
158
|
+
fromColumns: [params.fromColumn],
|
|
159
|
+
toColumns: [params.toColumn],
|
|
160
|
+
isPreferred: true,
|
|
161
|
+
constraintName: params.constraintName,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
27
164
|
//# sourceMappingURL=types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/knowledge/types.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/knowledge/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;;AAmUH,gCAEC;AAGD,kDAgBC;AAmBD,wCAUC;AAiBD,4CAKC;AASD,wCAMC;AAwBD,4DA2BC;AA/ID,+DAA+D;AAC/D,oBAAoB;AACpB,+DAA+D;AAE/D,2BAA2B;AAC3B,SAAgB,UAAU;IACxB,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;AACpE,CAAC;AAED,qDAAqD;AACrD,SAAgB,mBAAmB,CACjC,MAAuB,EACvB,UAAkB,EAClB,KAAc;IAEd,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,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