|
| 1 | +import { |
| 2 | + DatabaseHeader, |
| 3 | + DriverFlags, |
| 4 | + DatabaseResultSet, |
| 5 | +} from "@/drivers/base-driver"; |
| 6 | +import { |
| 7 | + OuterbaseAPIQueryRawResponse, |
| 8 | + OuterbaseDatabaseConfig, |
| 9 | +} from "../api-type"; |
| 10 | +import PostgresLikeDriver from "@/drivers/postgres/postgres-driver"; |
| 11 | + |
| 12 | +function transformObjectBasedResult(arr: Record<string, unknown>[]) { |
| 13 | + const usedColumnName = new Set(); |
| 14 | + const columns: DatabaseHeader[] = []; |
| 15 | + |
| 16 | + // Build the headers based on rows |
| 17 | + arr.forEach((row) => { |
| 18 | + Object.keys(row).forEach((key) => { |
| 19 | + if (!usedColumnName.has(key)) { |
| 20 | + usedColumnName.add(key); |
| 21 | + columns.push({ |
| 22 | + name: key, |
| 23 | + displayName: key, |
| 24 | + originalType: null, |
| 25 | + type: undefined, |
| 26 | + }); |
| 27 | + } |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + return { |
| 32 | + data: arr, |
| 33 | + headers: columns, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +export class OuterbasePostgresDriver extends PostgresLikeDriver { |
| 38 | + supportPragmaList = false; |
| 39 | + |
| 40 | + protected token: string; |
| 41 | + protected workspaceId: string; |
| 42 | + protected sourceId: string; |
| 43 | + |
| 44 | + getFlags(): DriverFlags { |
| 45 | + return { |
| 46 | + ...super.getFlags(), |
| 47 | + supportBigInt: false, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + constructor({ workspaceId, sourceId, token }: OuterbaseDatabaseConfig) { |
| 52 | + super(); |
| 53 | + |
| 54 | + this.workspaceId = workspaceId; |
| 55 | + this.sourceId = sourceId; |
| 56 | + this.token = token; |
| 57 | + } |
| 58 | + |
| 59 | + async query(stmt: string): Promise<DatabaseResultSet> { |
| 60 | + const response = await fetch( |
| 61 | + `/api/v1/workspace/${this.workspaceId}/source/${this.sourceId}/query/raw`, |
| 62 | + { |
| 63 | + method: "POST", |
| 64 | + headers: { |
| 65 | + "x-auth-token": this.token, |
| 66 | + "Content-Type": "application/json", |
| 67 | + }, |
| 68 | + body: JSON.stringify({ |
| 69 | + query: stmt, |
| 70 | + }), |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + const jsonResponse = |
| 75 | + (await response.json()) as OuterbaseAPIQueryRawResponse; |
| 76 | + |
| 77 | + if (!jsonResponse.success) { |
| 78 | + throw new Error("Query failed"); |
| 79 | + } |
| 80 | + |
| 81 | + const result = transformObjectBasedResult(jsonResponse.response.items); |
| 82 | + |
| 83 | + return { |
| 84 | + rows: result.data, |
| 85 | + headers: result.headers, |
| 86 | + stat: { |
| 87 | + rowsAffected: 0, |
| 88 | + rowsRead: null, |
| 89 | + rowsWritten: null, |
| 90 | + queryDurationMs: null, |
| 91 | + }, |
| 92 | + lastInsertRowid: undefined, |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + async transaction(stmts: string[]): Promise<DatabaseResultSet[]> { |
| 97 | + const result: DatabaseResultSet[] = []; |
| 98 | + |
| 99 | + for (const stms of stmts) { |
| 100 | + result.push(await this.query(stms)); |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + close() { |
| 107 | + // Nothing to do |
| 108 | + } |
| 109 | +} |
0 commit comments