-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.ts
More file actions
77 lines (69 loc) · 1.75 KB
/
index.ts
File metadata and controls
77 lines (69 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type {
ItemsParams,
ItemsResult,
MentionsParams,
MentionsResult,
MetaResult,
} from '@openctx/provider';
import { PostgresClient } from './client.js';
/** Settings for the Postgres provider. */
export type Settings = {
/** Database URL. */
DB_URL: string;
};
let pgClient: undefined | PostgresClient = undefined;
const postgresContext = {
meta(): MetaResult {
return {
name: 'Postgres',
mentions: { label: 'Search by schema name...' },
};
},
async initializePGClient(settingsInput: Settings) {
if (pgClient === undefined) {
pgClient = new PostgresClient(settingsInput.DB_URL);
await pgClient.initializePGData();
}
},
async mentions(
params: MentionsParams,
settingsInput: Settings
): Promise<MentionsResult> {
await this.initializePGClient(settingsInput);
if (!pgClient) {
return [];
}
const userQuery = params.query ?? '';
const schemas = pgClient.getSchemas();
const schemaList = schemas.filter((schema) => schema.includes(userQuery));
if (!schemaList) {
return [];
}
const mentionRes: MentionsResult = [];
for (const schema of schemaList) {
mentionRes.push({
title: schema,
uri: schema,
data: {
schema,
},
});
}
return mentionRes;
},
async items(
params: ItemsParams,
settingsInput: Settings
): Promise<ItemsResult> {
await this.initializePGClient(settingsInput);
if (!pgClient) {
return [];
}
const schema = params.mention?.data?.schema as string;
let message = params.message || '';
console.log({ schema, message });
const schemaContext = await pgClient.getSchema(schema);
return schemaContext;
},
};
export default postgresContext;