@malloydata/db-duckdb 0.0.331 → 0.0.333

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { DuckDBCommon } from './duckdb_common';
2
- import type { Connection, TableData } from 'duckdb';
3
- import { Database } from 'duckdb';
2
+ import { DuckDBInstance } from '@duckdb/node-api';
3
+ import type { DuckDBConnection as DuckDBNodeConnection } from '@duckdb/node-api';
4
4
  import type { ConnectionConfig, QueryDataRow, QueryOptionsReader, RunSQLOptions } from '@malloydata/malloy';
5
5
  export interface DuckDBConnectionOptions extends ConnectionConfig {
6
6
  additionalExtensions?: string[];
@@ -10,8 +10,8 @@ export interface DuckDBConnectionOptions extends ConnectionConfig {
10
10
  readOnly?: boolean;
11
11
  }
12
12
  interface ActiveDB {
13
- database: Database;
14
- connections: Connection[];
13
+ instance: DuckDBInstance;
14
+ connections: DuckDBNodeConnection[];
15
15
  }
16
16
  export declare class DuckDBConnection extends DuckDBCommon {
17
17
  readonly name: string;
@@ -20,7 +20,7 @@ export declare class DuckDBConnection extends DuckDBCommon {
20
20
  private workingDirectory;
21
21
  private readOnly;
22
22
  connecting: Promise<void>;
23
- protected connection: Connection | null;
23
+ protected connection: DuckDBNodeConnection | null;
24
24
  protected setupError: Error | undefined;
25
25
  protected isSetup: Promise<void> | undefined;
26
26
  static activeDBs: Record<string, ActiveDB>;
@@ -30,7 +30,7 @@ export declare class DuckDBConnection extends DuckDBCommon {
30
30
  loadExtension(ext: string): Promise<void>;
31
31
  protected setup(): Promise<void>;
32
32
  protected runDuckDBQuery(sql: string): Promise<{
33
- rows: TableData;
33
+ rows: QueryDataRow[];
34
34
  totalRows: number;
35
35
  }>;
36
36
  runSQLStream(sql: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryDataRow>;
@@ -28,7 +28,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
28
28
  exports.DuckDBConnection = void 0;
29
29
  const crypto_1 = __importDefault(require("crypto"));
30
30
  const duckdb_common_1 = require("./duckdb_common");
31
- const duckdb_1 = require("duckdb");
31
+ const node_api_1 = require("@duckdb/node-api");
32
32
  const package_json_1 = __importDefault(require("@malloydata/malloy/package.json"));
33
33
  class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
34
34
  constructor(arg, arg2, workingDirectory, queryOptions) {
@@ -80,24 +80,22 @@ class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
80
80
  this.connecting = this.init();
81
81
  }
82
82
  async init() {
83
- return new Promise(resolve => {
83
+ try {
84
84
  if (this.databasePath in DuckDBConnection.activeDBs) {
85
85
  const activeDB = DuckDBConnection.activeDBs[this.databasePath];
86
- this.connection = activeDB.database.connect();
86
+ this.connection = await activeDB.instance.connect();
87
87
  activeDB.connections.push(this.connection);
88
- resolve();
89
88
  }
90
89
  else {
91
90
  const config = {
92
- 'custom_user_agent': `Malloy/${package_json_1.default.version}`,
91
+ custom_user_agent: `Malloy/${package_json_1.default.version}`,
93
92
  };
94
93
  if (this.isMotherDuck) {
95
94
  if (!this.motherDuckToken &&
96
95
  !process.env['motherduck_token'] &&
97
96
  !process.env['MOTHERDUCK_TOKEN']) {
98
97
  this.setupError = new Error('Please set your MotherDuck Token');
99
- // Resolve instead of error because errors cannot be caught.
100
- return resolve();
98
+ return;
101
99
  }
102
100
  if (this.motherDuckToken) {
103
101
  config['motherduck_token'] = this.motherDuckToken;
@@ -106,23 +104,18 @@ class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
106
104
  if (this.readOnly) {
107
105
  config['access_mode'] = 'READ_ONLY';
108
106
  }
109
- const database = new duckdb_1.Database(this.databasePath, config, err => {
110
- if (err) {
111
- this.setupError = err;
112
- }
113
- else {
114
- this.connection = database.connect();
115
- const activeDB = {
116
- database,
117
- connections: [],
118
- };
119
- DuckDBConnection.activeDBs[this.databasePath] = activeDB;
120
- activeDB.connections.push(this.connection);
121
- }
122
- resolve();
123
- });
107
+ const instance = await node_api_1.DuckDBInstance.create(this.databasePath, config);
108
+ this.connection = await instance.connect();
109
+ const activeDB = {
110
+ instance,
111
+ connections: [this.connection],
112
+ };
113
+ DuckDBConnection.activeDBs[this.databasePath] = activeDB;
124
114
  }
125
- });
115
+ }
116
+ catch (err) {
117
+ this.setupError = err instanceof Error ? err : new Error(String(err));
118
+ }
126
119
  }
127
120
  async loadExtension(ext) {
128
121
  try {
@@ -172,25 +165,16 @@ class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
172
165
  await this.isSetup;
173
166
  }
174
167
  async runDuckDBQuery(sql) {
175
- return new Promise((resolve, reject) => {
176
- if (this.connection) {
177
- this.connection.all(sql, (err, rows) => {
178
- if (err) {
179
- reject(err);
180
- }
181
- else {
182
- // rows = processBigInts(rows);
183
- resolve({
184
- rows,
185
- totalRows: rows.length,
186
- });
187
- }
188
- });
189
- }
190
- else {
191
- reject(new Error('Connection not open'));
192
- }
193
- });
168
+ if (!this.connection) {
169
+ throw new Error('Connection not open');
170
+ }
171
+ const result = await this.connection.run(sql);
172
+ // getRowObjectsJson() converts nested types (LIST, STRUCT) to JS arrays/objects
173
+ const rows = (await result.getRowObjectsJson());
174
+ return {
175
+ rows,
176
+ totalRows: rows.length,
177
+ };
194
178
  }
195
179
  async *runSQLStream(sql, { rowLimit, abortSignal } = {}) {
196
180
  const defaultOptions = this.readQueryOptions();
@@ -204,14 +188,17 @@ class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
204
188
  await this.runDuckDBQuery(statements[0]);
205
189
  statements.shift();
206
190
  }
191
+ const result = await this.connection.stream(statements[0]);
207
192
  let index = 0;
208
- for await (const row of this.connection.stream(statements[0])) {
209
- if ((rowLimit !== undefined && index >= rowLimit) ||
210
- (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted)) {
211
- break;
193
+ for await (const chunk of result.yieldRowObjectJson()) {
194
+ for (const row of chunk) {
195
+ if ((rowLimit !== undefined && index >= rowLimit) ||
196
+ (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted)) {
197
+ return;
198
+ }
199
+ index++;
200
+ yield row;
212
201
  }
213
- index++;
214
- yield row;
215
202
  }
216
203
  }
217
204
  async createHash(sqlCommand) {
@@ -222,7 +209,7 @@ class DuckDBConnection extends duckdb_common_1.DuckDBCommon {
222
209
  if (activeDB) {
223
210
  activeDB.connections = activeDB.connections.filter(connection => connection !== this.connection);
224
211
  if (activeDB.connections.length === 0) {
225
- activeDB.database.close();
212
+ activeDB.instance.closeSync();
226
213
  delete DuckDBConnection.activeDBs[this.databasePath];
227
214
  }
228
215
  }
@@ -292,6 +292,7 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
292
292
  if (this.databasePath) {
293
293
  await this._database.open({
294
294
  path: this.databasePath,
295
+ accessMode: duckdb.DuckDBAccessMode.AUTOMATIC,
295
296
  });
296
297
  }
297
298
  URL.revokeObjectURL(workerUrl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-duckdb",
3
- "version": "0.0.331",
3
+ "version": "0.0.333",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -44,11 +44,11 @@
44
44
  "prepublishOnly": "npm run build"
45
45
  },
46
46
  "dependencies": {
47
- "@duckdb/duckdb-wasm": "1.29.1-dev132.0",
48
- "@malloydata/malloy": "0.0.331",
47
+ "@duckdb/duckdb-wasm": "1.33.1-dev13.0",
48
+ "@duckdb/node-api": "1.4.3-r.1",
49
+ "@malloydata/malloy": "0.0.333",
49
50
  "@motherduck/wasm-client": "^0.6.6",
50
51
  "apache-arrow": "^17.0.0",
51
- "duckdb": "1.3.4",
52
52
  "web-worker": "^1.3.0"
53
53
  }
54
54
  }