@duckdb/node-api 1.1.3-alpha.8 → 1.1.3-alpha.9

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.
@@ -0,0 +1 @@
1
+ export declare function bytesFromString(str: string): Uint8Array;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bytesFromString = bytesFromString;
4
+ const textEncoder = new TextEncoder();
5
+ function bytesFromString(str) {
6
+ return textEncoder.encode(str);
7
+ }
@@ -0,0 +1,4 @@
1
+ import { Value } from '@duckdb/node-bindings';
2
+ import { DuckDBType } from './DuckDBType';
3
+ import { DuckDBValue } from './values';
4
+ export declare function createValue(type: DuckDBType, input: DuckDBValue): Value;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createValue = createValue;
7
+ const node_bindings_1 = __importDefault(require("@duckdb/node-bindings"));
8
+ const DuckDBTypeId_1 = require("./DuckDBTypeId");
9
+ const values_1 = require("./values");
10
+ function createValue(type, input) {
11
+ switch (type.typeId) {
12
+ case DuckDBTypeId_1.DuckDBTypeId.BOOLEAN:
13
+ if (typeof input === 'boolean') {
14
+ return node_bindings_1.default.create_bool(input);
15
+ }
16
+ throw new Error(`input is not a boolean`);
17
+ case DuckDBTypeId_1.DuckDBTypeId.TINYINT:
18
+ if (typeof input === 'number') {
19
+ return node_bindings_1.default.create_int8(input);
20
+ }
21
+ throw new Error(`input is not a number`);
22
+ case DuckDBTypeId_1.DuckDBTypeId.SMALLINT:
23
+ if (typeof input === 'number') {
24
+ return node_bindings_1.default.create_int16(input);
25
+ }
26
+ throw new Error(`input is not a number`);
27
+ case DuckDBTypeId_1.DuckDBTypeId.INTEGER:
28
+ if (typeof input === 'number') {
29
+ return node_bindings_1.default.create_int32(input);
30
+ }
31
+ throw new Error(`input is not a number`);
32
+ case DuckDBTypeId_1.DuckDBTypeId.BIGINT:
33
+ if (typeof input === 'bigint') {
34
+ return node_bindings_1.default.create_int64(input);
35
+ }
36
+ throw new Error(`input is not a bigint`);
37
+ case DuckDBTypeId_1.DuckDBTypeId.UTINYINT:
38
+ if (typeof input === 'number') {
39
+ return node_bindings_1.default.create_uint8(input);
40
+ }
41
+ throw new Error(`input is not a number`);
42
+ case DuckDBTypeId_1.DuckDBTypeId.USMALLINT:
43
+ if (typeof input === 'number') {
44
+ return node_bindings_1.default.create_uint16(input);
45
+ }
46
+ throw new Error(`input is not a number`);
47
+ case DuckDBTypeId_1.DuckDBTypeId.UINTEGER:
48
+ if (typeof input === 'number') {
49
+ return node_bindings_1.default.create_uint32(input);
50
+ }
51
+ throw new Error(`input is not a number`);
52
+ case DuckDBTypeId_1.DuckDBTypeId.UBIGINT:
53
+ if (typeof input === 'bigint') {
54
+ return node_bindings_1.default.create_uint64(input);
55
+ }
56
+ throw new Error(`input is not a bigint`);
57
+ case DuckDBTypeId_1.DuckDBTypeId.FLOAT:
58
+ if (typeof input === 'number') {
59
+ return node_bindings_1.default.create_float(input);
60
+ }
61
+ throw new Error(`input is not a number`);
62
+ case DuckDBTypeId_1.DuckDBTypeId.DOUBLE:
63
+ if (typeof input === 'number') {
64
+ return node_bindings_1.default.create_double(input);
65
+ }
66
+ throw new Error(`input is not a number`);
67
+ case DuckDBTypeId_1.DuckDBTypeId.TIMESTAMP:
68
+ if (input instanceof values_1.DuckDBTimestampValue) {
69
+ return node_bindings_1.default.create_timestamp(input);
70
+ }
71
+ throw new Error(`input is not a DuckDBTimestampValue`);
72
+ case DuckDBTypeId_1.DuckDBTypeId.DATE:
73
+ if (input instanceof values_1.DuckDBDateValue) {
74
+ return node_bindings_1.default.create_date(input);
75
+ }
76
+ throw new Error(`input is not a DuckDBDateValue`);
77
+ case DuckDBTypeId_1.DuckDBTypeId.TIME:
78
+ if (input instanceof values_1.DuckDBTimeValue) {
79
+ return node_bindings_1.default.create_time(input);
80
+ }
81
+ throw new Error(`input is not a DuckDBTimeValue`);
82
+ case DuckDBTypeId_1.DuckDBTypeId.INTERVAL:
83
+ if (input instanceof values_1.DuckDBIntervalValue) {
84
+ return node_bindings_1.default.create_interval(input);
85
+ }
86
+ throw new Error(`input is not a DuckDBIntervalValue`);
87
+ case DuckDBTypeId_1.DuckDBTypeId.HUGEINT:
88
+ if (typeof input === 'bigint') {
89
+ return node_bindings_1.default.create_hugeint(input);
90
+ }
91
+ throw new Error(`input is not a bigint`);
92
+ case DuckDBTypeId_1.DuckDBTypeId.UHUGEINT:
93
+ if (typeof input === 'bigint') {
94
+ return node_bindings_1.default.create_uhugeint(input);
95
+ }
96
+ throw new Error(`input is not a bigint`);
97
+ case DuckDBTypeId_1.DuckDBTypeId.VARCHAR:
98
+ if (typeof input === 'string') {
99
+ return node_bindings_1.default.create_varchar(input);
100
+ }
101
+ throw new Error(`input is not a string`);
102
+ case DuckDBTypeId_1.DuckDBTypeId.BLOB:
103
+ if (input instanceof values_1.DuckDBBlobValue) {
104
+ return node_bindings_1.default.create_blob(input.bytes);
105
+ }
106
+ throw new Error(`input is not a DuckDBBlobValue`);
107
+ case DuckDBTypeId_1.DuckDBTypeId.DECIMAL:
108
+ throw new Error(`not yet implemented for DECIMAL`); // TODO: implement when available in 1.2.0
109
+ case DuckDBTypeId_1.DuckDBTypeId.TIMESTAMP_S:
110
+ throw new Error(`not yet implemented for TIMESTAMP_S`); // TODO: implement when available in 1.2.0
111
+ case DuckDBTypeId_1.DuckDBTypeId.TIMESTAMP_MS:
112
+ throw new Error(`not yet implemented for TIMESTAMP_MS`); // TODO: implement when available in 1.2.0
113
+ case DuckDBTypeId_1.DuckDBTypeId.TIMESTAMP_NS:
114
+ throw new Error(`not yet implemented for TIMESTAMP_NS`); // TODO: implement when available in 1.2.0
115
+ case DuckDBTypeId_1.DuckDBTypeId.ENUM:
116
+ throw new Error(`not yet implemented for ENUM`); // TODO: implement when available in 1.2.0
117
+ case DuckDBTypeId_1.DuckDBTypeId.LIST:
118
+ if (input instanceof values_1.DuckDBListValue) {
119
+ return node_bindings_1.default.create_list_value(type.valueType.toLogicalType().logical_type, input.items.map((item) => createValue(type.valueType, item)));
120
+ }
121
+ throw new Error(`input is not a DuckDBListValue`);
122
+ case DuckDBTypeId_1.DuckDBTypeId.STRUCT:
123
+ if (input instanceof values_1.DuckDBStructValue) {
124
+ return node_bindings_1.default.create_struct_value(type.toLogicalType().logical_type, Object.values(input.entries).map((value, i) => createValue(type.entryTypes[i], value)));
125
+ }
126
+ throw new Error(`input is not a DuckDBStructValue`);
127
+ case DuckDBTypeId_1.DuckDBTypeId.MAP:
128
+ throw new Error(`not yet implemented for MAP`); // TODO: implement when available, hopefully in 1.2.0
129
+ case DuckDBTypeId_1.DuckDBTypeId.ARRAY:
130
+ if (input instanceof values_1.DuckDBArrayValue) {
131
+ return node_bindings_1.default.create_array_value(type.valueType.toLogicalType().logical_type, input.items.map((item) => createValue(type.valueType, item)));
132
+ }
133
+ throw new Error(`input is not a DuckDBArrayValue`);
134
+ case DuckDBTypeId_1.DuckDBTypeId.UUID:
135
+ throw new Error(`not yet implemented for UUID`); // TODO: implement when available in 1.2.0
136
+ case DuckDBTypeId_1.DuckDBTypeId.UNION:
137
+ throw new Error(`not yet implemented for UNION`); // TODO: implement when available, hopefully in 1.2.0
138
+ case DuckDBTypeId_1.DuckDBTypeId.UNION:
139
+ throw new Error(`not yet implemented for BIT`); // TODO: implement when available in 1.2.0
140
+ case DuckDBTypeId_1.DuckDBTypeId.TIME_TZ:
141
+ if (input instanceof values_1.DuckDBTimeTZValue) {
142
+ return node_bindings_1.default.create_time_tz_value(input);
143
+ }
144
+ throw new Error(`input is not a DuckDBTimeTZValue`);
145
+ case DuckDBTypeId_1.DuckDBTypeId.TIMESTAMP_TZ:
146
+ if (input instanceof values_1.DuckDBTimestampTZValue) {
147
+ return node_bindings_1.default.create_timestamp(input); // TODO: change to create_timestamp_tz when available in 1.2.0
148
+ }
149
+ throw new Error(`input is not a DuckDBTimestampTZValue`);
150
+ case DuckDBTypeId_1.DuckDBTypeId.ANY:
151
+ throw new Error(`cannot create values of type ANY`);
152
+ case DuckDBTypeId_1.DuckDBTypeId.VARINT:
153
+ throw new Error(`not yet implemented for VARINT`); // TODO: implement when available in 1.2.0
154
+ case DuckDBTypeId_1.DuckDBTypeId.SQLNULL:
155
+ throw new Error(`not yet implemented for SQLNUll`); // TODO: implement when available in 1.2.0
156
+ default:
157
+ throw new Error(`unrecognized type id ${type.typeId}`);
158
+ }
159
+ }
@@ -0,0 +1,3 @@
1
+ import { DuckDBType } from './DuckDBType';
2
+ import { DuckDBValue } from './values';
3
+ export declare function typeForValue(value: DuckDBValue): DuckDBType;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.typeForValue = typeForValue;
4
+ const DuckDBType_1 = require("./DuckDBType");
5
+ const values_1 = require("./values");
6
+ function typeForValue(value) {
7
+ if (value === null) {
8
+ return DuckDBType_1.SQLNULL;
9
+ }
10
+ else {
11
+ switch (typeof value) {
12
+ case 'boolean':
13
+ return DuckDBType_1.BOOLEAN;
14
+ case 'number':
15
+ return DuckDBType_1.DOUBLE;
16
+ case 'bigint':
17
+ return DuckDBType_1.HUGEINT;
18
+ case 'string':
19
+ return DuckDBType_1.VARCHAR;
20
+ case 'object':
21
+ if (value instanceof values_1.DuckDBArrayValue) {
22
+ return (0, DuckDBType_1.ARRAY)(typeForValue(value.items[0]), value.items.length);
23
+ }
24
+ else if (value instanceof values_1.DuckDBBitValue) {
25
+ return DuckDBType_1.BIT;
26
+ }
27
+ else if (value instanceof values_1.DuckDBBlobValue) {
28
+ return DuckDBType_1.BLOB;
29
+ }
30
+ else if (value instanceof values_1.DuckDBDateValue) {
31
+ return DuckDBType_1.DATE;
32
+ }
33
+ else if (value instanceof values_1.DuckDBDecimalValue) {
34
+ return (0, DuckDBType_1.DECIMAL)(value.width, value.scale);
35
+ }
36
+ else if (value instanceof values_1.DuckDBIntervalValue) {
37
+ return DuckDBType_1.INTERVAL;
38
+ }
39
+ else if (value instanceof values_1.DuckDBListValue) {
40
+ return (0, DuckDBType_1.LIST)(typeForValue(value.items[0]));
41
+ }
42
+ else if (value instanceof values_1.DuckDBMapValue) {
43
+ return (0, DuckDBType_1.MAP)(typeForValue(value.entries[0].key), typeForValue(value.entries[0].value));
44
+ }
45
+ else if (value instanceof values_1.DuckDBStructValue) {
46
+ const entryTypes = {};
47
+ for (const key in value.entries) {
48
+ entryTypes[key] = typeForValue(value.entries[key]);
49
+ }
50
+ return (0, DuckDBType_1.STRUCT)(entryTypes);
51
+ }
52
+ else if (value instanceof values_1.DuckDBTimestampMillisecondsValue) {
53
+ return DuckDBType_1.TIMESTAMP_MS;
54
+ }
55
+ else if (value instanceof values_1.DuckDBTimestampNanosecondsValue) {
56
+ return DuckDBType_1.TIMESTAMP_NS;
57
+ }
58
+ else if (value instanceof values_1.DuckDBTimestampSecondsValue) {
59
+ return DuckDBType_1.TIMESTAMP_S;
60
+ }
61
+ else if (value instanceof values_1.DuckDBTimestampTZValue) {
62
+ return DuckDBType_1.TIMESTAMPTZ;
63
+ }
64
+ else if (value instanceof values_1.DuckDBTimestampValue) {
65
+ return DuckDBType_1.TIMESTAMP;
66
+ }
67
+ else if (value instanceof values_1.DuckDBTimeTZValue) {
68
+ return DuckDBType_1.TIMETZ;
69
+ }
70
+ else if (value instanceof values_1.DuckDBTimeValue) {
71
+ return DuckDBType_1.TIME;
72
+ }
73
+ else if (value instanceof values_1.DuckDBUnionValue) {
74
+ return (0, DuckDBType_1.UNION)({ [value.tag]: typeForValue(value.value) });
75
+ }
76
+ else if (value instanceof values_1.DuckDBUUIDValue) {
77
+ return DuckDBType_1.UUID;
78
+ }
79
+ break;
80
+ }
81
+ }
82
+ return DuckDBType_1.ANY;
83
+ }
@@ -5,4 +5,4 @@ export declare class DuckDBBlobValue {
5
5
  toString(): string;
6
6
  static fromString(str: string): DuckDBBlobValue;
7
7
  }
8
- export declare function blobValue(bytes: Uint8Array): DuckDBBlobValue;
8
+ export declare function blobValue(input: Uint8Array | string): DuckDBBlobValue;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DuckDBBlobValue = void 0;
4
4
  exports.blobValue = blobValue;
5
+ const bytesFromString_1 = require("../conversion/bytesFromString");
5
6
  const stringFromBlob_1 = require("../conversion/stringFromBlob");
6
- const textEncoder = new TextEncoder();
7
7
  class DuckDBBlobValue {
8
8
  bytes;
9
9
  constructor(bytes) {
@@ -14,10 +14,13 @@ class DuckDBBlobValue {
14
14
  return (0, stringFromBlob_1.stringFromBlob)(this.bytes);
15
15
  }
16
16
  static fromString(str) {
17
- return new DuckDBBlobValue(Buffer.from(textEncoder.encode(str)));
17
+ return new DuckDBBlobValue(Buffer.from((0, bytesFromString_1.bytesFromString)(str)));
18
18
  }
19
19
  }
20
20
  exports.DuckDBBlobValue = DuckDBBlobValue;
21
- function blobValue(bytes) {
22
- return new DuckDBBlobValue(bytes);
21
+ function blobValue(input) {
22
+ if (typeof input === 'string') {
23
+ return DuckDBBlobValue.fromString(input);
24
+ }
25
+ return new DuckDBBlobValue(input);
23
26
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@duckdb/node-api",
3
- "version": "1.1.3-alpha.8",
3
+ "version": "1.1.3-alpha.9",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "dependencies": {
7
- "@duckdb/node-bindings": "1.1.3-alpha.8"
7
+ "@duckdb/node-bindings": "1.1.3-alpha.9"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",