-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating Node-Service and Activate DuckDB
- Loading branch information
FalkWolsky
committed
Nov 15, 2024
1 parent
870a6e6
commit 86ca61e
Showing
7 changed files
with
22,824 additions
and
6 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
server/node-service/src/plugins/duckdb/dataSourceConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @server/node-service/src/plugins/duckdb/dataSourceConfig.ts | ||
import { ConfigToType } from "lowcoder-sdk/dataSource"; | ||
|
||
const dataSourceConfig = { | ||
type: "dataSource", | ||
params: [ | ||
{ | ||
key: "databaseFile", | ||
type: "textInput", | ||
label: "Database File", | ||
rules: [{ required: true, message: "Please provide a database file path 'db.duckdb' or ':memory:' for an in-memory database" }], | ||
tooltip: "Please provide a database file path 'db.duckdb' or ':memory:' for an in-memory database", | ||
defaultValue: ":memory:", | ||
}, | ||
{ | ||
key: "options", | ||
type: "textInput", | ||
label: "Database Options", | ||
tooltip: "Additional options to pass to the DuckDB constructor (in JSON format)", | ||
defaultValue: `{"access_mode": "READ_WRITE","max_memory": "512MB","threads": "4"}`, | ||
}, | ||
], | ||
} as const; | ||
|
||
export default dataSourceConfig; | ||
|
||
export type DataSourceDataType = ConfigToType<typeof dataSourceConfig>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DataSourcePlugin } from "lowcoder-sdk/dataSource"; | ||
import dataSourceConfig, { DataSourceDataType } from "./dataSourceConfig"; | ||
import queryConfig, { ActionDataType } from "./queryConfig"; | ||
import { Database } from "duckdb-async"; | ||
import { ServiceError } from "../../common/error"; | ||
|
||
// Helper function to handle BigInt serialization | ||
function serializeBigInts(row: any): any { | ||
const newRow: { [key: string]: any } = {}; // Add index signature | ||
for (const [key, value] of Object.entries(row)) { | ||
newRow[key] = typeof value === 'bigint' ? value.toString() : value; | ||
} | ||
return newRow; | ||
} | ||
|
||
const duckdbPlugin: DataSourcePlugin<ActionDataType, DataSourceDataType> = { | ||
id: "duckdb", | ||
name: "DuckDB", | ||
category: "database", | ||
icon: "duckdb.svg", | ||
dataSourceConfig, | ||
queryConfig, | ||
run: async function (actionData, dataSourceConfig): Promise<any> { | ||
const { databaseFile, options } = dataSourceConfig; | ||
const parsedOptions = JSON.parse(options); | ||
const db = await Database.create(databaseFile, parsedOptions); | ||
|
||
if (actionData.actionName === "Query") { | ||
try { | ||
const result = await db.all(actionData.queryString); | ||
// Apply BigInt serialization to each row | ||
return result.map(serializeBigInts); | ||
} catch (error) { | ||
throw new ServiceError((error as Error).message); | ||
} finally { | ||
await db.close(); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
export default duckdbPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// @server/node-service/src/plugins/duckdb/queryConfig.ts | ||
import { ConfigToType } from "lowcoder-sdk/dataSource"; | ||
|
||
const queryConfig = { | ||
type: "query", | ||
label: "Action", | ||
actions: [ | ||
{ | ||
actionName: "Query", | ||
label: "Query", | ||
params: [ | ||
{ | ||
label: "Query String", | ||
key: "queryString", | ||
type: "sqlInput", | ||
}, | ||
], | ||
}, | ||
], | ||
} as const; | ||
|
||
export type ActionDataType = ConfigToType<typeof queryConfig>; | ||
|
||
export default queryConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.