@indiekitai/pg-complete 0.1.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/README.md +114 -0
- package/dist/cli.js +1269 -0
- package/dist/index.d.mts +181 -0
- package/dist/index.d.ts +181 -0
- package/dist/index.js +1219 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1187 -0
- package/dist/index.mjs.map +1 -0
- package/dist/mcp.js +1303 -0
- package/package.json +41 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** A single completion suggestion */
|
|
2
|
+
interface Completion {
|
|
3
|
+
/** The text to insert */
|
|
4
|
+
text: string;
|
|
5
|
+
/** Type of the completion */
|
|
6
|
+
type: CompletionType;
|
|
7
|
+
/** Optional display text (if different from text) */
|
|
8
|
+
display?: string;
|
|
9
|
+
/** Optional description / metadata */
|
|
10
|
+
meta?: string;
|
|
11
|
+
/** Priority score (higher = more relevant) */
|
|
12
|
+
priority?: number;
|
|
13
|
+
}
|
|
14
|
+
type CompletionType = 'keyword' | 'table' | 'view' | 'column' | 'function' | 'schema' | 'database' | 'alias' | 'join' | 'datatype';
|
|
15
|
+
/** Column metadata stored for each table/view */
|
|
16
|
+
interface ColumnMetadata {
|
|
17
|
+
name: string;
|
|
18
|
+
datatype: string;
|
|
19
|
+
hasDefault: boolean;
|
|
20
|
+
default_: string | null;
|
|
21
|
+
foreignKeys: ForeignKey[];
|
|
22
|
+
}
|
|
23
|
+
/** Foreign key relationship */
|
|
24
|
+
interface ForeignKey {
|
|
25
|
+
parentSchema: string;
|
|
26
|
+
parentTable: string;
|
|
27
|
+
parentColumn: string;
|
|
28
|
+
childSchema: string;
|
|
29
|
+
childTable: string;
|
|
30
|
+
childColumn: string;
|
|
31
|
+
}
|
|
32
|
+
/** Function metadata */
|
|
33
|
+
interface FunctionMetadata {
|
|
34
|
+
schemaName: string;
|
|
35
|
+
funcName: string;
|
|
36
|
+
argNames: string[] | null;
|
|
37
|
+
argTypes: string[] | null;
|
|
38
|
+
argModes: string[] | null;
|
|
39
|
+
returnType: string;
|
|
40
|
+
isAggregate: boolean;
|
|
41
|
+
isWindow: boolean;
|
|
42
|
+
isSetReturning: boolean;
|
|
43
|
+
isExtension: boolean;
|
|
44
|
+
isPublic: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** A table reference parsed from the SQL statement */
|
|
47
|
+
interface TableReference {
|
|
48
|
+
schema: string | null;
|
|
49
|
+
name: string;
|
|
50
|
+
alias: string | null;
|
|
51
|
+
isFunction: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Schema metadata (tables, views, columns, functions, foreign keys) */
|
|
54
|
+
interface SchemaMetadata {
|
|
55
|
+
/** schema -> table -> column[] */
|
|
56
|
+
tables: Record<string, Record<string, ColumnMetadata[]>>;
|
|
57
|
+
/** schema -> view -> column[] */
|
|
58
|
+
views: Record<string, Record<string, ColumnMetadata[]>>;
|
|
59
|
+
/** schema -> funcName -> FunctionMetadata[] */
|
|
60
|
+
functions: Record<string, Record<string, FunctionMetadata[]>>;
|
|
61
|
+
/** schema -> typeName -> null */
|
|
62
|
+
datatypes: Record<string, Record<string, null>>;
|
|
63
|
+
}
|
|
64
|
+
/** Options for PgCompleter */
|
|
65
|
+
interface PgCompleterOptions {
|
|
66
|
+
/** Connection string or pg.Pool config */
|
|
67
|
+
connectionString?: string;
|
|
68
|
+
/** Pre-loaded metadata (skip database connection) */
|
|
69
|
+
metadata?: SchemaMetadata;
|
|
70
|
+
/** Search path schemas, default ['public'] */
|
|
71
|
+
searchPath?: string[];
|
|
72
|
+
/** Whether to qualify column names when multiple tables in scope */
|
|
73
|
+
qualifyColumns?: 'always' | 'never' | 'if_more_than_one_table';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Main completion engine — ported from pgcli's pgcompleter.py
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
declare class PgCompleter {
|
|
81
|
+
private pool;
|
|
82
|
+
private metadata;
|
|
83
|
+
private searchPath;
|
|
84
|
+
private qualifyColumns;
|
|
85
|
+
private connectionString?;
|
|
86
|
+
constructor(connectionStringOrOpts?: string | PgCompleterOptions);
|
|
87
|
+
/** Connect to the database and load schema metadata */
|
|
88
|
+
refresh(): Promise<void>;
|
|
89
|
+
/** Close any open connections */
|
|
90
|
+
close(): Promise<void>;
|
|
91
|
+
/** Set metadata directly (for testing or pre-loaded scenarios) */
|
|
92
|
+
setMetadata(meta: SchemaMetadata, searchPath?: string[]): void;
|
|
93
|
+
/**
|
|
94
|
+
* Get completions for the given SQL text.
|
|
95
|
+
* @param text - The full SQL text typed so far
|
|
96
|
+
* @param cursorPos - Cursor position (defaults to end of text)
|
|
97
|
+
* @returns Array of completion suggestions
|
|
98
|
+
*/
|
|
99
|
+
complete(text: string, cursorPos?: number): Completion[];
|
|
100
|
+
private dedup;
|
|
101
|
+
private getKeywordCompletions;
|
|
102
|
+
private getSchemaCompletions;
|
|
103
|
+
private getTableCompletions;
|
|
104
|
+
private getViewCompletions;
|
|
105
|
+
private getColumnCompletions;
|
|
106
|
+
private getFunctionCompletions;
|
|
107
|
+
private getJoinCompletions;
|
|
108
|
+
private getJoinConditionCompletions;
|
|
109
|
+
private getDatatypeCompletions;
|
|
110
|
+
/** Find columns for a table reference, searching through schemas */
|
|
111
|
+
private findColumns;
|
|
112
|
+
private matchesTable;
|
|
113
|
+
private getVisibleSchemas;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Simple SQL statement parser to understand context for completions.
|
|
118
|
+
* This is a lightweight port of pgcli's sqlcompletion.suggest_type logic.
|
|
119
|
+
* Instead of using sqlparse (Python), we do regex + token-based parsing.
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
/** Represents the context we've determined from the SQL */
|
|
123
|
+
type SuggestionContext = {
|
|
124
|
+
type: 'keyword';
|
|
125
|
+
lastToken?: string;
|
|
126
|
+
} | {
|
|
127
|
+
type: 'table';
|
|
128
|
+
schema: string | null;
|
|
129
|
+
} | {
|
|
130
|
+
type: 'view';
|
|
131
|
+
schema: string | null;
|
|
132
|
+
} | {
|
|
133
|
+
type: 'column';
|
|
134
|
+
tableRefs: TableReference[];
|
|
135
|
+
qualifiable: boolean;
|
|
136
|
+
parent?: string;
|
|
137
|
+
} | {
|
|
138
|
+
type: 'function';
|
|
139
|
+
schema: string | null;
|
|
140
|
+
} | {
|
|
141
|
+
type: 'schema';
|
|
142
|
+
} | {
|
|
143
|
+
type: 'from_clause';
|
|
144
|
+
schema: string | null;
|
|
145
|
+
tableRefs: TableReference[];
|
|
146
|
+
} | {
|
|
147
|
+
type: 'join';
|
|
148
|
+
tableRefs: TableReference[];
|
|
149
|
+
schema: string | null;
|
|
150
|
+
} | {
|
|
151
|
+
type: 'join_condition';
|
|
152
|
+
tableRefs: TableReference[];
|
|
153
|
+
parent?: string;
|
|
154
|
+
} | {
|
|
155
|
+
type: 'alias';
|
|
156
|
+
aliases: string[];
|
|
157
|
+
} | {
|
|
158
|
+
type: 'database';
|
|
159
|
+
} | {
|
|
160
|
+
type: 'datatype';
|
|
161
|
+
schema: string | null;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Determine what types of completions to suggest based on SQL text.
|
|
165
|
+
*/
|
|
166
|
+
declare function suggestType(fullText: string, textBeforeCursor: string): SuggestionContext[];
|
|
167
|
+
/**
|
|
168
|
+
* Extract table references from SQL text.
|
|
169
|
+
* Handles: FROM tbl, FROM schema.tbl, FROM tbl alias, FROM tbl AS alias,
|
|
170
|
+
* JOIN tbl, UPDATE tbl, INSERT INTO tbl, etc.
|
|
171
|
+
*/
|
|
172
|
+
declare function extractTables(sql: string): TableReference[];
|
|
173
|
+
|
|
174
|
+
/** SQL keywords organized by category for context-aware completion */
|
|
175
|
+
declare const KEYWORDS: string[];
|
|
176
|
+
/** Keywords that commonly follow other keywords */
|
|
177
|
+
declare const KEYWORD_TREE: Record<string, string[]>;
|
|
178
|
+
/** Built-in PostgreSQL functions */
|
|
179
|
+
declare const BUILTIN_FUNCTIONS: string[];
|
|
180
|
+
|
|
181
|
+
export { BUILTIN_FUNCTIONS, type ColumnMetadata, type Completion, type CompletionType, type ForeignKey, type FunctionMetadata, KEYWORDS, KEYWORD_TREE, PgCompleter, type PgCompleterOptions, type SchemaMetadata, type TableReference, extractTables, suggestType };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** A single completion suggestion */
|
|
2
|
+
interface Completion {
|
|
3
|
+
/** The text to insert */
|
|
4
|
+
text: string;
|
|
5
|
+
/** Type of the completion */
|
|
6
|
+
type: CompletionType;
|
|
7
|
+
/** Optional display text (if different from text) */
|
|
8
|
+
display?: string;
|
|
9
|
+
/** Optional description / metadata */
|
|
10
|
+
meta?: string;
|
|
11
|
+
/** Priority score (higher = more relevant) */
|
|
12
|
+
priority?: number;
|
|
13
|
+
}
|
|
14
|
+
type CompletionType = 'keyword' | 'table' | 'view' | 'column' | 'function' | 'schema' | 'database' | 'alias' | 'join' | 'datatype';
|
|
15
|
+
/** Column metadata stored for each table/view */
|
|
16
|
+
interface ColumnMetadata {
|
|
17
|
+
name: string;
|
|
18
|
+
datatype: string;
|
|
19
|
+
hasDefault: boolean;
|
|
20
|
+
default_: string | null;
|
|
21
|
+
foreignKeys: ForeignKey[];
|
|
22
|
+
}
|
|
23
|
+
/** Foreign key relationship */
|
|
24
|
+
interface ForeignKey {
|
|
25
|
+
parentSchema: string;
|
|
26
|
+
parentTable: string;
|
|
27
|
+
parentColumn: string;
|
|
28
|
+
childSchema: string;
|
|
29
|
+
childTable: string;
|
|
30
|
+
childColumn: string;
|
|
31
|
+
}
|
|
32
|
+
/** Function metadata */
|
|
33
|
+
interface FunctionMetadata {
|
|
34
|
+
schemaName: string;
|
|
35
|
+
funcName: string;
|
|
36
|
+
argNames: string[] | null;
|
|
37
|
+
argTypes: string[] | null;
|
|
38
|
+
argModes: string[] | null;
|
|
39
|
+
returnType: string;
|
|
40
|
+
isAggregate: boolean;
|
|
41
|
+
isWindow: boolean;
|
|
42
|
+
isSetReturning: boolean;
|
|
43
|
+
isExtension: boolean;
|
|
44
|
+
isPublic: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** A table reference parsed from the SQL statement */
|
|
47
|
+
interface TableReference {
|
|
48
|
+
schema: string | null;
|
|
49
|
+
name: string;
|
|
50
|
+
alias: string | null;
|
|
51
|
+
isFunction: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Schema metadata (tables, views, columns, functions, foreign keys) */
|
|
54
|
+
interface SchemaMetadata {
|
|
55
|
+
/** schema -> table -> column[] */
|
|
56
|
+
tables: Record<string, Record<string, ColumnMetadata[]>>;
|
|
57
|
+
/** schema -> view -> column[] */
|
|
58
|
+
views: Record<string, Record<string, ColumnMetadata[]>>;
|
|
59
|
+
/** schema -> funcName -> FunctionMetadata[] */
|
|
60
|
+
functions: Record<string, Record<string, FunctionMetadata[]>>;
|
|
61
|
+
/** schema -> typeName -> null */
|
|
62
|
+
datatypes: Record<string, Record<string, null>>;
|
|
63
|
+
}
|
|
64
|
+
/** Options for PgCompleter */
|
|
65
|
+
interface PgCompleterOptions {
|
|
66
|
+
/** Connection string or pg.Pool config */
|
|
67
|
+
connectionString?: string;
|
|
68
|
+
/** Pre-loaded metadata (skip database connection) */
|
|
69
|
+
metadata?: SchemaMetadata;
|
|
70
|
+
/** Search path schemas, default ['public'] */
|
|
71
|
+
searchPath?: string[];
|
|
72
|
+
/** Whether to qualify column names when multiple tables in scope */
|
|
73
|
+
qualifyColumns?: 'always' | 'never' | 'if_more_than_one_table';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Main completion engine — ported from pgcli's pgcompleter.py
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
declare class PgCompleter {
|
|
81
|
+
private pool;
|
|
82
|
+
private metadata;
|
|
83
|
+
private searchPath;
|
|
84
|
+
private qualifyColumns;
|
|
85
|
+
private connectionString?;
|
|
86
|
+
constructor(connectionStringOrOpts?: string | PgCompleterOptions);
|
|
87
|
+
/** Connect to the database and load schema metadata */
|
|
88
|
+
refresh(): Promise<void>;
|
|
89
|
+
/** Close any open connections */
|
|
90
|
+
close(): Promise<void>;
|
|
91
|
+
/** Set metadata directly (for testing or pre-loaded scenarios) */
|
|
92
|
+
setMetadata(meta: SchemaMetadata, searchPath?: string[]): void;
|
|
93
|
+
/**
|
|
94
|
+
* Get completions for the given SQL text.
|
|
95
|
+
* @param text - The full SQL text typed so far
|
|
96
|
+
* @param cursorPos - Cursor position (defaults to end of text)
|
|
97
|
+
* @returns Array of completion suggestions
|
|
98
|
+
*/
|
|
99
|
+
complete(text: string, cursorPos?: number): Completion[];
|
|
100
|
+
private dedup;
|
|
101
|
+
private getKeywordCompletions;
|
|
102
|
+
private getSchemaCompletions;
|
|
103
|
+
private getTableCompletions;
|
|
104
|
+
private getViewCompletions;
|
|
105
|
+
private getColumnCompletions;
|
|
106
|
+
private getFunctionCompletions;
|
|
107
|
+
private getJoinCompletions;
|
|
108
|
+
private getJoinConditionCompletions;
|
|
109
|
+
private getDatatypeCompletions;
|
|
110
|
+
/** Find columns for a table reference, searching through schemas */
|
|
111
|
+
private findColumns;
|
|
112
|
+
private matchesTable;
|
|
113
|
+
private getVisibleSchemas;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Simple SQL statement parser to understand context for completions.
|
|
118
|
+
* This is a lightweight port of pgcli's sqlcompletion.suggest_type logic.
|
|
119
|
+
* Instead of using sqlparse (Python), we do regex + token-based parsing.
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
/** Represents the context we've determined from the SQL */
|
|
123
|
+
type SuggestionContext = {
|
|
124
|
+
type: 'keyword';
|
|
125
|
+
lastToken?: string;
|
|
126
|
+
} | {
|
|
127
|
+
type: 'table';
|
|
128
|
+
schema: string | null;
|
|
129
|
+
} | {
|
|
130
|
+
type: 'view';
|
|
131
|
+
schema: string | null;
|
|
132
|
+
} | {
|
|
133
|
+
type: 'column';
|
|
134
|
+
tableRefs: TableReference[];
|
|
135
|
+
qualifiable: boolean;
|
|
136
|
+
parent?: string;
|
|
137
|
+
} | {
|
|
138
|
+
type: 'function';
|
|
139
|
+
schema: string | null;
|
|
140
|
+
} | {
|
|
141
|
+
type: 'schema';
|
|
142
|
+
} | {
|
|
143
|
+
type: 'from_clause';
|
|
144
|
+
schema: string | null;
|
|
145
|
+
tableRefs: TableReference[];
|
|
146
|
+
} | {
|
|
147
|
+
type: 'join';
|
|
148
|
+
tableRefs: TableReference[];
|
|
149
|
+
schema: string | null;
|
|
150
|
+
} | {
|
|
151
|
+
type: 'join_condition';
|
|
152
|
+
tableRefs: TableReference[];
|
|
153
|
+
parent?: string;
|
|
154
|
+
} | {
|
|
155
|
+
type: 'alias';
|
|
156
|
+
aliases: string[];
|
|
157
|
+
} | {
|
|
158
|
+
type: 'database';
|
|
159
|
+
} | {
|
|
160
|
+
type: 'datatype';
|
|
161
|
+
schema: string | null;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Determine what types of completions to suggest based on SQL text.
|
|
165
|
+
*/
|
|
166
|
+
declare function suggestType(fullText: string, textBeforeCursor: string): SuggestionContext[];
|
|
167
|
+
/**
|
|
168
|
+
* Extract table references from SQL text.
|
|
169
|
+
* Handles: FROM tbl, FROM schema.tbl, FROM tbl alias, FROM tbl AS alias,
|
|
170
|
+
* JOIN tbl, UPDATE tbl, INSERT INTO tbl, etc.
|
|
171
|
+
*/
|
|
172
|
+
declare function extractTables(sql: string): TableReference[];
|
|
173
|
+
|
|
174
|
+
/** SQL keywords organized by category for context-aware completion */
|
|
175
|
+
declare const KEYWORDS: string[];
|
|
176
|
+
/** Keywords that commonly follow other keywords */
|
|
177
|
+
declare const KEYWORD_TREE: Record<string, string[]>;
|
|
178
|
+
/** Built-in PostgreSQL functions */
|
|
179
|
+
declare const BUILTIN_FUNCTIONS: string[];
|
|
180
|
+
|
|
181
|
+
export { BUILTIN_FUNCTIONS, type ColumnMetadata, type Completion, type CompletionType, type ForeignKey, type FunctionMetadata, KEYWORDS, KEYWORD_TREE, PgCompleter, type PgCompleterOptions, type SchemaMetadata, type TableReference, extractTables, suggestType };
|