Skip to content

Commit

Permalink
add mongodb and cloudflare headers
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 16, 2024
1 parent 0d6a9eb commit 26b72bc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/connections/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<QueryResult> {
Expand Down
40 changes: 22 additions & 18 deletions src/connections/sqlite/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>[]; // 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;
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
};
}
Expand Down
35 changes: 35 additions & 0 deletions src/utils/transformer.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>[]
): 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<HeaderType>(
headers: HeaderType[],
headersMapper: (header: HeaderType) => {
Expand Down

0 comments on commit 26b72bc

Please sign in to comment.