| 1 | import { getDatabaseAdapter } from "dbinfoz"; |
| 2 | |
| 3 | import { |
| 4 | ContextItem, |
| 5 | ContextProviderDescription, |
| 6 | ContextProviderExtras, |
| 7 | ContextSubmenuItem, |
| 8 | LoadSubmenuItemsArgs, |
| 9 | } from "../../index.js"; |
| 10 | import { BaseContextProvider } from "../index.js"; |
| 11 | |
| 12 | class DatabaseContextProvider extends BaseContextProvider { |
| 13 | static description: ContextProviderDescription = { |
| 14 | title: "database", |
| 15 | displayTitle: "Database", |
| 16 | description: "Table schemas", |
| 17 | type: "submenu", |
| 18 | renderInlineAs: "", |
| 19 | }; |
| 20 | |
| 21 | async getContextItems( |
| 22 | query: string, |
| 23 | extras: ContextProviderExtras, |
| 24 | ): Promise<ContextItem[]> { |
| 25 | const contextItems: ContextItem[] = []; |
| 26 | |
| 27 | const connections = this.options?.connections; |
| 28 | |
| 29 | if (connections === null) { |
| 30 | return contextItems; |
| 31 | } |
| 32 | |
| 33 | const [connectionName, table] = query.split("."); |
| 34 | |
| 35 | for (const connection of connections) { |
| 36 | if (connection.name === connectionName) { |
| 37 | // @ts-ignore (incorrect typings on module's declaration file) |
| 38 | const adapter = getDatabaseAdapter( |
| 39 | connection.connection_type, |
| 40 | connection.connection, |
| 41 | ); |
| 42 | const tablesAndSchemas = await adapter.getAllTablesAndSchemas( |
| 43 | connection.connection.database, |
| 44 | ); |
| 45 | |
| 46 | if (table === "all") { |
| 47 | let prompt = `Schema for all tables on ${connection.connection_type} is `; |
| 48 | prompt += JSON.stringify(tablesAndSchemas); |
| 49 | |
| 50 | const contextItem = { |
| 51 | name: `${connectionName}-all-tables-schemas`, |
| 52 | description: "Schema for all tables.", |
| 53 | content: prompt, |
| 54 | }; |
| 55 | |
| 56 | contextItems.push(contextItem); |
| 57 | } else { |
| 58 | const tables = Object.keys(tablesAndSchemas); |
| 59 | |
| 60 | tables.forEach((tableName) => { |
| 61 | if (table === tableName) { |
| 62 | let prompt = `Schema for ${tableName} on ${connection.connection_type} is `; |
| 63 | prompt += JSON.stringify(tablesAndSchemas[tableName]); |
| 64 | |
| 65 | const contextItem = { |
| 66 | name: `${connectionName}-${tableName}-schema`, |
| 67 | description: `${tableName} Schema`, |
| 68 | content: prompt, |
| 69 | }; |
| 70 | |
| 71 | contextItems.push(contextItem); |
| 72 | } |
| 73 | }); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return contextItems; |
| 79 | } |
| 80 | |
| 81 | async loadSubmenuItems( |
| 82 | args: LoadSubmenuItemsArgs, |
| 83 | ): Promise<ContextSubmenuItem[]> { |
| 84 | const contextItems: ContextSubmenuItem[] = []; |
| 85 | const connections = this.options?.connections; |
| 86 | |
| 87 | if (connections === null) { |
| 88 | return contextItems; |
| 89 | } |
| 90 | |
| 91 | for (const connection of connections) { |
| 92 | // @ts-ignore (incorrect typings on module's declaration file) |
| 93 | const adapter = getDatabaseAdapter( |
| 94 | connection.connection_type, |
| 95 | connection.connection, |
| 96 | ); |
| 97 | const tablesAndSchemas = await adapter.getAllTablesAndSchemas( |
| 98 | connection.connection.database, |
| 99 | ); |
| 100 | const tables = Object.keys(tablesAndSchemas); |
| 101 | |
| 102 | const contextItem = { |
| 103 | id: `${connection.name}.all`, |
| 104 | title: `${connection.name} all table schemas`, |
| 105 | description: "", |
| 106 | }; |
| 107 | |
| 108 | contextItems.push(contextItem); |
| 109 | |
| 110 | tables.forEach((tableName) => { |
| 111 | const contextItem = { |
| 112 | id: `${connection.name}.${tableName}`, |
| 113 | title: `${connection.name}.${tableName} schema`, |
| 114 | description: "", |
| 115 | }; |
| 116 | |
| 117 | contextItems.push(contextItem); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | return contextItems; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | export default DatabaseContextProvider; |