Skip to content

Commit

Permalink
Fixes something about duckdb connection vs our new AsyncConnection
Browse files Browse the repository at this point in the history
thing.
  • Loading branch information
coco98 committed Oct 29, 2024
1 parent cb27286 commit 1ada467
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
19 changes: 18 additions & 1 deletion connector-definition/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import { GoogleCalendar, GMail} from "@hasura/ndc-duckduckapi/services";
const connectorConfig: duckduckapi = {
dbSchema: `
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id integer primary key,
name text
);
INSERT INTO users (id, name) VALUES
(1, 'Alice Johnson'),
(2, 'Bob Smith'),
(3, 'Carol Martinez'),
(4, 'David Kim'),
(5, 'Emma Wilson'),
(6, 'Frank Zhang'),
(7, 'Grace Lee'),
(8, 'Henry Garcia'),
(9, 'Isabel Patel'),
(10, 'Jack Thompson');
-- Add your SQL schema here.
-- This SQL will be run on startup every time.
-- CREATE TABLE SAAS_TABLE_NAME (.....);
Expand All @@ -19,4 +36,4 @@ const connectorConfig: duckduckapi = {
(async () => {
const connector = await makeConnector(connectorConfig);
start(connector);
})();
})();
13 changes: 12 additions & 1 deletion ndc-duckduckapi/src/duckdb-connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ export class DuckDBManager {
this.activeConnections = 0;
}

async getConnection(): Promise<AsyncConnection> {
async getSyncConnection(): Promise<duckdb.Connection> {
if (this.activeConnections >= this.maxConnections) {
await new Promise(resolve => setTimeout(resolve, 100));
return this.getSyncConnection();
}

const connection = await this.db.connect();
this.activeConnections++;
return connection;
}

async getConnection(): Promise<AsyncConnection> {
if (this.activeConnections >= this.maxConnections) {
await new Promise(resolve => setTimeout(resolve, 100));
return this.getConnection();
Expand Down
11 changes: 8 additions & 3 deletions ndc-duckduckapi/src/handlers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,14 @@ export async function perform_query(
// const con = state.client.connect();
const response: RowSet[] = [];
for (let query_plan of query_plans) {
const res = await state.client.query(query_plan.sql);
const row_set = JSON.parse(res[0]["data"] as string) as RowSet;
response.push(row_set);
try {
const connection = await state.client.getSyncConnection();
const res = await do_all(connection, query_plan);
const row_set = JSON.parse(res[0]["data"] as string) as RowSet;
response.push(row_set);
} catch (err) {
console.error('RAAAAAAAAAAAAAAAAAAAAAAR.perform_query:: ' + "Error performing query: " + err);
}
}
return response;
}

0 comments on commit 1ada467

Please sign in to comment.