@@ -16,8 +16,6 @@ This is a high-level API meant for applications. It depends on low-level binding
16
16
### Roadmap
17
17
18
18
Some features are not yet complete:
19
- - Friendlier APIs for convering results to common JS data structures.
20
- - Friendlier APIs for converting values of specialized and complex DuckDB types to common JS types.
21
19
- Appending and binding advanced data types. (Additional DuckDB C API support needed.)
22
20
- Writing to data chunk vectors. (Directly writing to binary buffers is challenging to support using the Node Addon API.)
23
21
- User-defined types & functions. (Support for this was added to the DuckDB C API in v1.1.0.)
@@ -94,36 +92,45 @@ const result = await prepared.run();
94
92
95
93
Get column names and types:
96
94
``` ts
97
- const columnNames = [];
98
- const columnTypes = [];
99
- const columnCount = result .columnCount ;
100
- for (let columnIndex = 0 ; columnIndex < columnCount ; columnIndex ++ ) {
101
- const columnName = result .columnName (columnIndex );
102
- const columnType = result .columnType (columnIndex );
103
- columnNames .push (columnName );
104
- columnTypes .push (columnType );
105
- }
95
+ const columnNames = result .columnNames ();
96
+ const columnTypes = result .columnTypes ();
97
+ ```
98
+
99
+ Fetch all chunks:
100
+ ``` ts
101
+ const chunks = await result .fetchAllChunks ();
106
102
```
107
103
108
- Fetch data chunks :
104
+ Fetch one chunk at a time :
109
105
``` ts
110
106
const chunks = [];
111
107
while (true ) {
112
108
const chunk = await result .fetchChunk ();
109
+ // Last chunk will have zero rows.
113
110
if (chunk .rowCount === 0 ) {
114
111
break ;
115
112
}
116
113
chunks .push (chunk );
117
114
}
118
115
```
119
116
120
- Read column data:
117
+ Read chunk data (column-major):
118
+ ``` ts
119
+ const columns = chunk .getColumns (); // array of columns, each as an array of values
120
+ ```
121
+
122
+ Read chunk data (row-major):
123
+ ``` ts
124
+ const columns = chunk .getRows (); // array of rows, each as an array of values
125
+ ```
126
+
127
+ Read chunk data (one value at a time)
121
128
``` ts
122
129
const columns = [];
123
- const columnCount = result .columnCount ;
130
+ const columnCount = chunk .columnCount ;
124
131
for (let columnIndex = 0 ; columnIndex < columnCount ; columnIndex ++ ) {
125
132
const columnValues = [];
126
- const columnVector = chunk .getColumn (columnIndex );
133
+ const columnVector = chunk .getColumnVector (columnIndex );
127
134
const itemCount = columnVector .itemCount ;
128
135
for (let itemIndex = 0 ; itemIndex < itemCount ; itemIndex ++ ) {
129
136
const value = columnVector .getItem (itemIndex );
@@ -207,13 +214,15 @@ if (columnType.typeId === DuckDBTypeId.BLOB) {
207
214
if (columnType .typeId === DuckDBTypeId .DATE ) {
208
215
const dateDays = columnValue .days ;
209
216
const dateString = columnValue .toString ();
217
+ const { year, month, day } = columnValue .toParts ();
210
218
}
211
219
212
220
if (columnType .typeId === DuckDBTypeId .DECIMAL ) {
213
221
const decimalWidth = columnValue .width ;
214
222
const decimalScale = columnValue .scale ;
215
- const decimalValue = columnValue .value ; // bigint (raw fixed-point integer; `scale` indicates number of fractional digits )
223
+ const decimalValue = columnValue .value ; // bigint (Scaled-up value. Represented number is value/(10^scale). )
216
224
const decimalString = columnValue .toString ();
225
+ const decimalDouble = columnValue .toDouble ();
217
226
}
218
227
219
228
if (columnType .typeId === DuckDBTypeId .INTERVAL ) {
@@ -256,22 +265,26 @@ if (columnType.typeId === DuckDBTypeId.TIMESTAMP_S) {
256
265
if (columnType .typeId === DuckDBTypeId .TIMESTAMP_TZ ) {
257
266
const timestampTZMicros = columnValue .micros ; // bigint
258
267
const timestampTZString = columnValue .toString ();
268
+ const { date : { year, month, day }, time : { hour, min, sec, micros } } = columnValue .toParts ();
259
269
}
260
270
261
271
if (columnType .typeId === DuckDBTypeId .TIMESTAMP ) {
262
272
const timestampMicros = columnValue .micros ; // bigint
263
273
const timestampString = columnValue .toString ();
274
+ const { date : { year, month, day }, time : { hour, min, sec, micros } } = columnValue .toParts ();
264
275
}
265
276
266
277
if (columnType .typeId === DuckDBTypeId .TIME_TZ ) {
267
278
const timeTZMicros = columnValue .micros ; // bigint
268
279
const timeTZOffset = columnValue .offset ;
269
280
const timeTZString = columnValue .toString ();
281
+ const { time : { hour, min, sec, micros }, offset } = columnValue .toParts ();
270
282
}
271
283
272
284
if (columnType .typeId === DuckDBTypeId .TIME ) {
273
285
const timeMicros = columnValue .micros ; // bigint
274
286
const timeString = columnValue .toString ();
287
+ const { hour, min, sec, micros } = columnValue .toParts ();
275
288
}
276
289
277
290
if (columnType .typeId === DuckDBTypeId .UNION ) {
@@ -333,7 +346,7 @@ for (let statementIndex = 0; statementIndex < statementCount; statementIndex++)
333
346
}
334
347
```
335
348
336
- ### Control Evaluation
349
+ ### Control Evaluation of Tasks
337
350
338
351
``` ts
339
352
import { DuckDBPendingResultState } from ' @duckdb/node-api' ;
0 commit comments