From 26b72bcc59ece9858ef6151dab6545f04803c96b Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Wed, 16 Oct 2024 20:46:37 +0700 Subject: [PATCH] add mongodb and cloudflare headers --- src/connections/mongodb.ts | 4 +-- src/connections/sqlite/cloudflare.ts | 40 +++++++++++++++------------- src/utils/transformer.ts | 35 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/connections/mongodb.ts b/src/connections/mongodb.ts index afd0bf4..b9986fa 100644 --- a/src/connections/mongodb.ts +++ b/src/connections/mongodb.ts @@ -11,6 +11,7 @@ import { import { MongoClient, Db, Document, Collection, ObjectId } from 'mongodb'; import { PostgresDialect } from 'src/query-builder/dialects/postgres'; +import { transformObjectBasedResult } from 'src/utils/transformer'; // import { MongoDialect } from '../query-builder/dialects/mongo' function isValidObjectId(value: string) { @@ -211,8 +212,7 @@ export class MongoDBConnection implements Connection { } const data = await query.toArray(); - - return { error: null, data, query: '' }; + return transformObjectBasedResult(data); } async createTable(): Promise { diff --git a/src/connections/sqlite/cloudflare.ts b/src/connections/sqlite/cloudflare.ts index a74a373..bd672f8 100644 --- a/src/connections/sqlite/cloudflare.ts +++ b/src/connections/sqlite/cloudflare.ts @@ -11,29 +11,29 @@ import { TableIndex, TableIndexType, } from 'src/models/database'; +import { transformArrayBasedResult } from 'src/utils/transformer'; + +interface CloudflareResult { + results: { + columns: string[]; + rows: unknown[][]; + }; -interface SuccessResponse { - result: Record[]; // Array of objects representing results - success: true; meta: { - served_by: string; duration: number; changes: number; last_row_id: number; - changed_db: boolean; - size_after: number; rows_read: number; rows_written: number; }; } -interface ErrorResponse { - result: []; // Empty result on error - success: false; - errors: { code: number; message: string }[]; // Array of errors - messages: any[]; // You can adjust this type based on your use case -} -type APIResponse = SuccessResponse | ErrorResponse; +interface CloudflareResponse { + success?: boolean; + result: CloudflareResult[]; + error?: string; + errors?: string[]; +} export type CloudflareD1ConnectionDetails = { apiKey: string; @@ -117,7 +117,7 @@ export class CloudflareD1Connection extends SqliteBaseConnection { if (!query) throw new Error('A SQL query was not provided'); const response = await fetch( - `https://api.cloudflare.com/client/v4/accounts/${this.accountId}/d1/database/${this.databaseId}/query`, + `https://api.cloudflare.com/client/v4/accounts/${this.accountId}/d1/database/${this.databaseId}/query/raw`, { method: 'POST', headers: { @@ -131,27 +131,31 @@ export class CloudflareD1Connection extends SqliteBaseConnection { } ); - const json: APIResponse = await response.json(); + const json: CloudflareResponse = await response.json(); if (json.success) { const items = json.result[0].results; const rawSQL = constructRawQuery(query); return { - data: items, + data: transformArrayBasedResult( + items.columns, + (column) => ({ name: column }), + items.rows + ), error: null, query: rawSQL, // There's additional metadata here we could pass in the future // meta: json.meta, }; + } else { } - const error = json.errors.map((error) => error.message).join(', '); const rawSQL = constructRawQuery(query); return { data: [], - error: new Error(error), + error: new Error(json.error ?? json.errors?.join(', ')), query: rawSQL, }; } diff --git a/src/utils/transformer.ts b/src/utils/transformer.ts index 28a6792..467605b 100644 --- a/src/utils/transformer.ts +++ b/src/utils/transformer.ts @@ -1,5 +1,40 @@ +/** + * Provides several functions to help transform common + * database result format into our own query result formation + */ + import { QueryResult, QueryResultHeader } from 'src/connections'; +export function transformObjectBasedResult( + arr: Record[] +): QueryResult { + const usedColumnName = new Set(); + const columns: QueryResultHeader[] = []; + + // Build the headers based on rows + arr.forEach((row) => { + Object.keys(row).forEach((key) => { + if (!usedColumnName.has(key)) { + usedColumnName.add(key); + columns.push({ + name: key, + displayName: key, + }); + } + }); + }); + + return { + data: arr, + headers: columns, + error: null, + query: '', + }; +} + +/** + * Transforms the array based result into our own query result format + */ export function transformArrayBasedResult( headers: HeaderType[], headersMapper: (header: HeaderType) => {