@avidian/duckdb-bun 0.0.0-stage → 1.0.0

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.
Files changed (231) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +284 -2
  3. package/package.json +95 -4
  4. package/src/bindings/api/appender.ts +288 -0
  5. package/src/bindings/api/data-chunk.ts +58 -0
  6. package/src/bindings/api/database.ts +232 -0
  7. package/src/bindings/api/datetime.ts +158 -0
  8. package/src/bindings/api/index.ts +13 -0
  9. package/src/bindings/api/logical-types.ts +227 -0
  10. package/src/bindings/api/query.ts +616 -0
  11. package/src/bindings/api/scalar-function.ts +274 -0
  12. package/src/bindings/api/table-function.ts +339 -0
  13. package/src/bindings/api/values.ts +546 -0
  14. package/src/bindings/api/vector.ts +266 -0
  15. package/src/bindings/generated/capi.ts +695 -0
  16. package/src/bindings/generated/contract.ts +162 -0
  17. package/src/bindings/index.ts +10 -0
  18. package/src/bindings/runtime/convert.ts +307 -0
  19. package/src/bindings/runtime/diagnostics.ts +45 -0
  20. package/src/bindings/runtime/dispatch.ts +200 -0
  21. package/src/bindings/runtime/exception.ts +103 -0
  22. package/src/bindings/runtime/flags.ts +19 -0
  23. package/src/bindings/runtime/handle.ts +217 -0
  24. package/src/bindings/runtime/kinds.ts +53 -0
  25. package/src/bindings/runtime/marshal.ts +190 -0
  26. package/src/bindings/runtime/native.ts +150 -0
  27. package/src/bindings/runtime/ring.ts +54 -0
  28. package/src/bindings/runtime/scope.ts +133 -0
  29. package/src/bindings/runtime/trace.ts +65 -0
  30. package/src/connection.ts +35 -0
  31. package/src/database.ts +27 -0
  32. package/src/errors.ts +30 -0
  33. package/src/file-assets.d.ts +10 -0
  34. package/src/index.ts +17 -0
  35. package/src/library.ts +83 -0
  36. package/src/neo.ts +16 -0
  37. package/src/reader.ts +47 -0
  38. package/src/statement.ts +61 -0
  39. package/src/types.ts +52 -0
  40. package/src/version.ts +6 -0
  41. package/vendor/node-api/LICENSE +7 -0
  42. package/vendor/node-api/PROVENANCE.md +54 -0
  43. package/vendor/node-api/node-bindings.d.ts +1669 -0
  44. package/vendor/node-api/src/DuckDBAppender.ts +237 -0
  45. package/vendor/node-api/src/DuckDBClientContext.ts +13 -0
  46. package/vendor/node-api/src/DuckDBConnection.ts +315 -0
  47. package/vendor/node-api/src/DuckDBDataChunk.ts +412 -0
  48. package/vendor/node-api/src/DuckDBDataChunkWriter.ts +144 -0
  49. package/vendor/node-api/src/DuckDBExtractedStatements.ts +37 -0
  50. package/vendor/node-api/src/DuckDBInstance.ts +35 -0
  51. package/vendor/node-api/src/DuckDBInstanceCache.ts +29 -0
  52. package/vendor/node-api/src/DuckDBLogicalType.ts +446 -0
  53. package/vendor/node-api/src/DuckDBMaterializedResult.ts +39 -0
  54. package/vendor/node-api/src/DuckDBPendingResult.ts +83 -0
  55. package/vendor/node-api/src/DuckDBPreparedStatement.ts +376 -0
  56. package/vendor/node-api/src/DuckDBPreparedStatementCollection.ts +5 -0
  57. package/vendor/node-api/src/DuckDBPreparedStatementWeakRefCollection.ts +31 -0
  58. package/vendor/node-api/src/DuckDBResult.ts +403 -0
  59. package/vendor/node-api/src/DuckDBResultReader.ts +283 -0
  60. package/vendor/node-api/src/DuckDBScalarFunction.ts +149 -0
  61. package/vendor/node-api/src/DuckDBScalarFunctionBindInfo.ts +34 -0
  62. package/vendor/node-api/src/DuckDBScalarFunctionInfo.ts +34 -0
  63. package/vendor/node-api/src/DuckDBScalarFunctionInitInfo.ts +35 -0
  64. package/vendor/node-api/src/DuckDBTableFunction.ts +149 -0
  65. package/vendor/node-api/src/DuckDBTableFunctionBindInfo.ts +56 -0
  66. package/vendor/node-api/src/DuckDBTableFunctionInfo.ts +36 -0
  67. package/vendor/node-api/src/DuckDBTableFunctionInitInfo.ts +52 -0
  68. package/vendor/node-api/src/DuckDBType.ts +1 -0
  69. package/vendor/node-api/src/DuckDBTypeId.ts +46 -0
  70. package/vendor/node-api/src/DuckDBValueConverter.ts +23 -0
  71. package/vendor/node-api/src/DuckDBValueConverters.ts +461 -0
  72. package/vendor/node-api/src/DuckDBVector.ts +1 -0
  73. package/vendor/node-api/src/JS.ts +11 -0
  74. package/vendor/node-api/src/JSDuckDBValueConverter.ts +85 -0
  75. package/vendor/node-api/src/JSInputTypeForTypeId.ts +101 -0
  76. package/vendor/node-api/src/JSToDuckDBValueConverter.ts +136 -0
  77. package/vendor/node-api/src/JSTypeForTypeId.ts +78 -0
  78. package/vendor/node-api/src/Json.ts +7 -0
  79. package/vendor/node-api/src/JsonDuckDBValueConverter.ts +72 -0
  80. package/vendor/node-api/src/JsonTypeForTypeId.ts +86 -0
  81. package/vendor/node-api/src/ToDuckDBValueConverter.ts +68 -0
  82. package/vendor/node-api/src/ToDuckDBValueConverters.ts +303 -0
  83. package/vendor/node-api/src/configurationOptionDescriptions.ts +13 -0
  84. package/vendor/node-api/src/conversion/bytesFromString.ts +5 -0
  85. package/vendor/node-api/src/conversion/dateTimeStringConversion.ts +343 -0
  86. package/vendor/node-api/src/conversion/displayStringForDuckDBValue.ts +12 -0
  87. package/vendor/node-api/src/conversion/stringFromBlob.ts +48 -0
  88. package/vendor/node-api/src/conversion/stringFromDecimal.ts +129 -0
  89. package/vendor/node-api/src/conversion/varintDecode.ts +42 -0
  90. package/vendor/node-api/src/convertColumnsFromChunks.ts +28 -0
  91. package/vendor/node-api/src/convertColumnsObjectFromChunks.ts +29 -0
  92. package/vendor/node-api/src/convertRowObjectsFromChunks.ts +21 -0
  93. package/vendor/node-api/src/convertRowsFromChunks.ts +16 -0
  94. package/vendor/node-api/src/createConfig.ts +14 -0
  95. package/vendor/node-api/src/createDuckDBValueConverter.ts +17 -0
  96. package/vendor/node-api/src/createResult.ts +15 -0
  97. package/vendor/node-api/src/createValue.ts +355 -0
  98. package/vendor/node-api/src/duckdb.ts +51 -0
  99. package/vendor/node-api/src/enums.ts +7 -0
  100. package/vendor/node-api/src/getColumnsFromChunks.ts +13 -0
  101. package/vendor/node-api/src/getColumnsObjectFromChunks.ts +16 -0
  102. package/vendor/node-api/src/getRowObjectsFromChunks.ts +13 -0
  103. package/vendor/node-api/src/getRowsFromChunks.ts +12 -0
  104. package/vendor/node-api/src/index.ts +3 -0
  105. package/vendor/node-api/src/readValue.ts +31 -0
  106. package/vendor/node-api/src/sleep.ts +5 -0
  107. package/vendor/node-api/src/sql.ts +7 -0
  108. package/vendor/node-api/src/tsconfig.json +7 -0
  109. package/vendor/node-api/src/typeForValue.ts +144 -0
  110. package/vendor/node-api/src/types/BaseDuckDBType.ts +31 -0
  111. package/vendor/node-api/src/types/DuckDBAnyType.ts +13 -0
  112. package/vendor/node-api/src/types/DuckDBArrayType.ts +43 -0
  113. package/vendor/node-api/src/types/DuckDBBigIntType.ts +21 -0
  114. package/vendor/node-api/src/types/DuckDBBigNumType.ts +21 -0
  115. package/vendor/node-api/src/types/DuckDBBitType.ts +13 -0
  116. package/vendor/node-api/src/types/DuckDBBlobType.ts +13 -0
  117. package/vendor/node-api/src/types/DuckDBBooleanType.ts +13 -0
  118. package/vendor/node-api/src/types/DuckDBDateType.ts +29 -0
  119. package/vendor/node-api/src/types/DuckDBDecimalType.ts +46 -0
  120. package/vendor/node-api/src/types/DuckDBDoubleType.ts +21 -0
  121. package/vendor/node-api/src/types/DuckDBEnumType.ts +111 -0
  122. package/vendor/node-api/src/types/DuckDBFloatType.ts +21 -0
  123. package/vendor/node-api/src/types/DuckDBGeometryType.ts +36 -0
  124. package/vendor/node-api/src/types/DuckDBHugeIntType.ts +21 -0
  125. package/vendor/node-api/src/types/DuckDBIntegerLiteralType.ts +13 -0
  126. package/vendor/node-api/src/types/DuckDBIntegerType.ts +21 -0
  127. package/vendor/node-api/src/types/DuckDBIntervalType.ts +13 -0
  128. package/vendor/node-api/src/types/DuckDBListType.ts +35 -0
  129. package/vendor/node-api/src/types/DuckDBMapType.ts +47 -0
  130. package/vendor/node-api/src/types/DuckDBSQLNullType.ts +17 -0
  131. package/vendor/node-api/src/types/DuckDBSmallIntType.ts +21 -0
  132. package/vendor/node-api/src/types/DuckDBStringLiteralType.ts +13 -0
  133. package/vendor/node-api/src/types/DuckDBStructType.ts +104 -0
  134. package/vendor/node-api/src/types/DuckDBTimeNSType.ts +20 -0
  135. package/vendor/node-api/src/types/DuckDBTimeTZType.ts +23 -0
  136. package/vendor/node-api/src/types/DuckDBTimeType.ts +20 -0
  137. package/vendor/node-api/src/types/DuckDBTimestampMillisecondsType.ts +29 -0
  138. package/vendor/node-api/src/types/DuckDBTimestampNanosecondsType.ts +29 -0
  139. package/vendor/node-api/src/types/DuckDBTimestampSecondsType.ts +29 -0
  140. package/vendor/node-api/src/types/DuckDBTimestampTZNanosecondsType.ts +32 -0
  141. package/vendor/node-api/src/types/DuckDBTimestampTZType.ts +32 -0
  142. package/vendor/node-api/src/types/DuckDBTimestampType.ts +32 -0
  143. package/vendor/node-api/src/types/DuckDBTinyIntType.ts +21 -0
  144. package/vendor/node-api/src/types/DuckDBType.ts +86 -0
  145. package/vendor/node-api/src/types/DuckDBUBigIntType.ts +21 -0
  146. package/vendor/node-api/src/types/DuckDBUHugeIntType.ts +21 -0
  147. package/vendor/node-api/src/types/DuckDBUIntegerType.ts +21 -0
  148. package/vendor/node-api/src/types/DuckDBUSmallIntType.ts +21 -0
  149. package/vendor/node-api/src/types/DuckDBUTinyIntType.ts +21 -0
  150. package/vendor/node-api/src/types/DuckDBUUIDType.ts +20 -0
  151. package/vendor/node-api/src/types/DuckDBUnionType.ts +74 -0
  152. package/vendor/node-api/src/types/DuckDBVarCharType.ts +13 -0
  153. package/vendor/node-api/src/types/DuckDBVariantType.ts +13 -0
  154. package/vendor/node-api/src/types/index.ts +44 -0
  155. package/vendor/node-api/src/values/DuckDBArrayValue.ts +18 -0
  156. package/vendor/node-api/src/values/DuckDBBitValue.ts +117 -0
  157. package/vendor/node-api/src/values/DuckDBBlobValue.ts +26 -0
  158. package/vendor/node-api/src/values/DuckDBDateValue.ts +43 -0
  159. package/vendor/node-api/src/values/DuckDBDecimalValue.ts +44 -0
  160. package/vendor/node-api/src/values/DuckDBGeometryValue.ts +30 -0
  161. package/vendor/node-api/src/values/DuckDBIntervalValue.ts +22 -0
  162. package/vendor/node-api/src/values/DuckDBListValue.ts +18 -0
  163. package/vendor/node-api/src/values/DuckDBMapValue.ts +25 -0
  164. package/vendor/node-api/src/values/DuckDBStructValue.ts +22 -0
  165. package/vendor/node-api/src/values/DuckDBTimeNSValue.ts +23 -0
  166. package/vendor/node-api/src/values/DuckDBTimeTZValue.ts +96 -0
  167. package/vendor/node-api/src/values/DuckDBTimeValue.ts +34 -0
  168. package/vendor/node-api/src/values/DuckDBTimestampMillisecondsValue.ts +29 -0
  169. package/vendor/node-api/src/values/DuckDBTimestampNanosecondsValue.ts +28 -0
  170. package/vendor/node-api/src/values/DuckDBTimestampSecondsValue.ts +28 -0
  171. package/vendor/node-api/src/values/DuckDBTimestampTZNanosecondsValue.ts +44 -0
  172. package/vendor/node-api/src/values/DuckDBTimestampTZValue.ts +54 -0
  173. package/vendor/node-api/src/values/DuckDBTimestampValue.ts +45 -0
  174. package/vendor/node-api/src/values/DuckDBTupleValue.ts +22 -0
  175. package/vendor/node-api/src/values/DuckDBUUIDValue.ts +48 -0
  176. package/vendor/node-api/src/values/DuckDBUnionValue.ts +22 -0
  177. package/vendor/node-api/src/values/DuckDBValue.ts +53 -0
  178. package/vendor/node-api/src/values/DuckDBVariantValue.ts +34 -0
  179. package/vendor/node-api/src/values/index.ts +24 -0
  180. package/vendor/node-api/src/vectors/DuckDBArrayVector.ts +107 -0
  181. package/vendor/node-api/src/vectors/DuckDBBigIntVector.ts +75 -0
  182. package/vendor/node-api/src/vectors/DuckDBBigNumVector.ts +98 -0
  183. package/vendor/node-api/src/vectors/DuckDBBitVector.ts +102 -0
  184. package/vendor/node-api/src/vectors/DuckDBBlobVector.ts +112 -0
  185. package/vendor/node-api/src/vectors/DuckDBBooleanVector.ts +84 -0
  186. package/vendor/node-api/src/vectors/DuckDBDateVector.ts +80 -0
  187. package/vendor/node-api/src/vectors/DuckDBDecimal128Vector.ts +107 -0
  188. package/vendor/node-api/src/vectors/DuckDBDecimal16Vector.ts +104 -0
  189. package/vendor/node-api/src/vectors/DuckDBDecimal32Vector.ts +104 -0
  190. package/vendor/node-api/src/vectors/DuckDBDecimal64Vector.ts +104 -0
  191. package/vendor/node-api/src/vectors/DuckDBDoubleVector.ts +68 -0
  192. package/vendor/node-api/src/vectors/DuckDBEnum16Vector.ts +75 -0
  193. package/vendor/node-api/src/vectors/DuckDBEnum32Vector.ts +75 -0
  194. package/vendor/node-api/src/vectors/DuckDBEnum8Vector.ts +75 -0
  195. package/vendor/node-api/src/vectors/DuckDBFloatVector.ts +68 -0
  196. package/vendor/node-api/src/vectors/DuckDBGeometryVector.ts +97 -0
  197. package/vendor/node-api/src/vectors/DuckDBHugeIntVector.ts +89 -0
  198. package/vendor/node-api/src/vectors/DuckDBIntegerVector.ts +75 -0
  199. package/vendor/node-api/src/vectors/DuckDBIntervalVector.ts +100 -0
  200. package/vendor/node-api/src/vectors/DuckDBListVector.ts +221 -0
  201. package/vendor/node-api/src/vectors/DuckDBMapVector.ts +88 -0
  202. package/vendor/node-api/src/vectors/DuckDBSQLNullVector.ts +53 -0
  203. package/vendor/node-api/src/vectors/DuckDBSmallIntVector.ts +75 -0
  204. package/vendor/node-api/src/vectors/DuckDBStructVector.ts +138 -0
  205. package/vendor/node-api/src/vectors/DuckDBTimeNSVector.ts +80 -0
  206. package/vendor/node-api/src/vectors/DuckDBTimeTZVector.ts +80 -0
  207. package/vendor/node-api/src/vectors/DuckDBTimeVector.ts +80 -0
  208. package/vendor/node-api/src/vectors/DuckDBTimestampMillisecondsVector.ts +88 -0
  209. package/vendor/node-api/src/vectors/DuckDBTimestampNanosecondsVector.ts +88 -0
  210. package/vendor/node-api/src/vectors/DuckDBTimestampSecondsVector.ts +88 -0
  211. package/vendor/node-api/src/vectors/DuckDBTimestampTZNanosecondsVector.ts +88 -0
  212. package/vendor/node-api/src/vectors/DuckDBTimestampTZVector.ts +86 -0
  213. package/vendor/node-api/src/vectors/DuckDBTimestampVector.ts +83 -0
  214. package/vendor/node-api/src/vectors/DuckDBTinyIntVector.ts +75 -0
  215. package/vendor/node-api/src/vectors/DuckDBTupleVector.ts +125 -0
  216. package/vendor/node-api/src/vectors/DuckDBUBigIntVector.ts +75 -0
  217. package/vendor/node-api/src/vectors/DuckDBUHugeIntVector.ts +89 -0
  218. package/vendor/node-api/src/vectors/DuckDBUIntegerVector.ts +75 -0
  219. package/vendor/node-api/src/vectors/DuckDBUSmallIntVector.ts +75 -0
  220. package/vendor/node-api/src/vectors/DuckDBUTinyIntVector.ts +75 -0
  221. package/vendor/node-api/src/vectors/DuckDBUUIDVector.ts +89 -0
  222. package/vendor/node-api/src/vectors/DuckDBUnionVector.ts +84 -0
  223. package/vendor/node-api/src/vectors/DuckDBValidity.ts +104 -0
  224. package/vendor/node-api/src/vectors/DuckDBVarCharVector.ts +100 -0
  225. package/vendor/node-api/src/vectors/DuckDBVariantVector.ts +609 -0
  226. package/vendor/node-api/src/vectors/DuckDBVector.ts +33 -0
  227. package/vendor/node-api/src/vectors/createVector.ts +269 -0
  228. package/vendor/node-api/src/vectors/dataAccessors.ts +387 -0
  229. package/vendor/node-api/src/vectors/index.ts +50 -0
  230. package/vendor/node-api/src/vectors/vectorRegistry.ts +24 -0
  231. package/vendor/node-api/src/version.ts +5 -0
@@ -0,0 +1,136 @@
1
+ import { DuckDBType } from './DuckDBType';
2
+ import { DuckDBTypeId } from './DuckDBTypeId';
3
+ import { JS } from './JS';
4
+ import {
5
+ JSInputTypeFor,
6
+ JSInputTypeForTypeId,
7
+ } from './JSInputTypeForTypeId';
8
+ import {
9
+ ToDuckDBValueConverterFor,
10
+ createToDuckDBValueConverter,
11
+ } from './ToDuckDBValueConverter';
12
+ import {
13
+ arrayValueFromArray,
14
+ bigintFromNumeric,
15
+ bitValueFromBytes,
16
+ blobValueFromBytes,
17
+ dateValueFromDate,
18
+ decimalValueFromNumeric,
19
+ geometryValueFromBytes,
20
+ intervalValueFromObject,
21
+ listValueFromArray,
22
+ mapValueFromEntries,
23
+ nullFromNull,
24
+ structValueFromObject,
25
+ timeNSValueFromNanos,
26
+ timeTZValueFromObject,
27
+ timeValueFromMicros,
28
+ timestampMillisValueFromDate,
29
+ timestampNanosValueFromDate,
30
+ timestampSecondsValueFromDate,
31
+ timestampTZValueFromDate,
32
+ timestampTZNanosValueFromDate,
33
+ timestampValueFromDate,
34
+ unionValueFromObject,
35
+ unwritableConverter,
36
+ uuidValueFromString,
37
+ valueFromBoolean,
38
+ valueFromNumber,
39
+ valueFromString,
40
+ variantValueFromJS,
41
+ } from './ToDuckDBValueConverters';
42
+ import { DuckDBValue } from './values';
43
+
44
+ const JSInputConvertersByTypeId: {
45
+ [Id in DuckDBTypeId]: ToDuckDBValueConverterFor<
46
+ JSInputTypeForTypeId[Id],
47
+ JS
48
+ >;
49
+ } = {
50
+ [DuckDBTypeId.INVALID]: unwritableConverter,
51
+ [DuckDBTypeId.BOOLEAN]: valueFromBoolean,
52
+ [DuckDBTypeId.TINYINT]: valueFromNumber,
53
+ [DuckDBTypeId.SMALLINT]: valueFromNumber,
54
+ [DuckDBTypeId.INTEGER]: valueFromNumber,
55
+ [DuckDBTypeId.BIGINT]: bigintFromNumeric,
56
+ [DuckDBTypeId.UTINYINT]: valueFromNumber,
57
+ [DuckDBTypeId.USMALLINT]: valueFromNumber,
58
+ [DuckDBTypeId.UINTEGER]: valueFromNumber,
59
+ [DuckDBTypeId.UBIGINT]: bigintFromNumeric,
60
+ [DuckDBTypeId.FLOAT]: valueFromNumber,
61
+ [DuckDBTypeId.DOUBLE]: valueFromNumber,
62
+ [DuckDBTypeId.TIMESTAMP]: timestampValueFromDate,
63
+ [DuckDBTypeId.DATE]: dateValueFromDate,
64
+ [DuckDBTypeId.TIME]: timeValueFromMicros,
65
+ [DuckDBTypeId.INTERVAL]: intervalValueFromObject,
66
+ [DuckDBTypeId.HUGEINT]: bigintFromNumeric,
67
+ [DuckDBTypeId.UHUGEINT]: bigintFromNumeric,
68
+ [DuckDBTypeId.VARCHAR]: valueFromString,
69
+ [DuckDBTypeId.BLOB]: blobValueFromBytes,
70
+ [DuckDBTypeId.DECIMAL]: decimalValueFromNumeric,
71
+ [DuckDBTypeId.TIMESTAMP_S]: timestampSecondsValueFromDate,
72
+ [DuckDBTypeId.TIMESTAMP_MS]: timestampMillisValueFromDate,
73
+ [DuckDBTypeId.TIMESTAMP_NS]: timestampNanosValueFromDate,
74
+ [DuckDBTypeId.ENUM]: valueFromString,
75
+ [DuckDBTypeId.LIST]: listValueFromArray,
76
+ [DuckDBTypeId.STRUCT]: structValueFromObject,
77
+ [DuckDBTypeId.MAP]: mapValueFromEntries,
78
+ [DuckDBTypeId.ARRAY]: arrayValueFromArray,
79
+ [DuckDBTypeId.UUID]: uuidValueFromString,
80
+ [DuckDBTypeId.UNION]: unionValueFromObject,
81
+ [DuckDBTypeId.BIT]: bitValueFromBytes,
82
+ [DuckDBTypeId.TIME_TZ]: timeTZValueFromObject,
83
+ [DuckDBTypeId.TIMESTAMP_TZ]: timestampTZValueFromDate,
84
+ [DuckDBTypeId.ANY]: unwritableConverter,
85
+ [DuckDBTypeId.BIGNUM]: bigintFromNumeric,
86
+ [DuckDBTypeId.SQLNULL]: nullFromNull,
87
+ [DuckDBTypeId.STRING_LITERAL]: unwritableConverter,
88
+ [DuckDBTypeId.INTEGER_LITERAL]: unwritableConverter,
89
+ [DuckDBTypeId.TIME_NS]: timeNSValueFromNanos,
90
+ [DuckDBTypeId.GEOMETRY]: geometryValueFromBytes,
91
+ [DuckDBTypeId.VARIANT]: variantValueFromJS,
92
+ [DuckDBTypeId.TIMESTAMP_TZ_NS]: timestampTZNanosValueFromDate,
93
+ };
94
+
95
+ /**
96
+ * Converts a JS value into the `DuckDBValue` representation for a DuckDB type:
97
+ * the inverse of `JSDuckDBValueConverter`.
98
+ *
99
+ * Pass this to `DuckDBDataChunk.setRowsConverted` and friends to fill a chunk
100
+ * from JS values. `jsToDuckDBValue` below is the single-value form, and checks
101
+ * its input against a statically known type.
102
+ */
103
+ export const JSToDuckDBValueConverter = createToDuckDBValueConverter<
104
+ JS,
105
+ JSInputTypeForTypeId
106
+ >(JSInputConvertersByTypeId);
107
+
108
+ /**
109
+ * Converts a JS value into the `DuckDBValue` representation for `type`.
110
+ *
111
+ * The inverse of `JSDuckDBValueConverter`. Accepts what
112
+ * `JSInputTypeForTypeId` declares for the type's id, plus `null` for any type.
113
+ * Generic over the type, so a statically known one checks its input exactly:
114
+ * `jsToDuckDBValue(new Date(), TIMESTAMP)` is accepted and
115
+ * `jsToDuckDBValue(new Date(), INTEGER)` is not. Passing a `DuckDBType` that
116
+ * is only known as the union widens the input to the union of every entry,
117
+ * which is as precise as the type information allows.
118
+ *
119
+ * Pure TypeScript: it constructs no `duckdb_value` and needs no connection, so
120
+ * it composes with every write entry point — `DuckDBAppender.appendValue`,
121
+ * `DuckDBPreparedStatement.bindValue`, and `DuckDBDataChunk.setRows` all take
122
+ * a `DuckDBValue`.
123
+ */
124
+ export function jsToDuckDBValue<T extends DuckDBType>(
125
+ value: JSInputTypeFor<T> | null,
126
+ type: T
127
+ ): DuckDBValue {
128
+ // The converter chain is typed against JS, which the input table exceeds for
129
+ // VARIANT alone (a DuckDBVariantValue is not a JS value). Narrowed back at
130
+ // the same boundary the factory documents.
131
+ return JSToDuckDBValueConverter(
132
+ value as JS,
133
+ type,
134
+ JSToDuckDBValueConverter
135
+ );
136
+ }
@@ -0,0 +1,78 @@
1
+ import type { DuckDBType } from './DuckDBType';
2
+ import { DuckDBTypeId } from './DuckDBTypeId';
3
+ import type { JS } from './JS';
4
+
5
+ /**
6
+ * The JS type produced by `JSDuckDBValueConverter` for each DuckDB type id.
7
+ *
8
+ * Values are non-NULL results only: SQL NULL is handled by
9
+ * `createDuckDBValueConverter`, which yields `null` for any type. Callers
10
+ * representing a nullable value should use `JSTypeForTypeId[Id] | null`.
11
+ *
12
+ * Type ids that cannot appear in a result (`INVALID`, `ANY`,
13
+ * `STRING_LITERAL`, `INTEGER_LITERAL`) map to `never`, since their
14
+ * converters throw.
15
+ *
16
+ * Nested types bottom out at `JS`, because `DuckDBListType`,
17
+ * `DuckDBArrayType`, `DuckDBStructType`, `DuckDBMapType`, and
18
+ * `DuckDBUnionType` do not carry their child types at the type level.
19
+ *
20
+ * STRUCT is an object keyed by entry name, except for a tuple (see
21
+ * `DuckDBStructType.isTuple`), which is an array of its items.
22
+ *
23
+ * This interface is exhaustive over `DuckDBTypeId`, and is the annotation
24
+ * source for the converter map in `JSDuckDBValueConverter`; adding a type id
25
+ * without adding it here is a compile error.
26
+ */
27
+ export interface JSTypeForTypeId {
28
+ [DuckDBTypeId.INVALID]: never;
29
+ [DuckDBTypeId.BOOLEAN]: boolean;
30
+ [DuckDBTypeId.TINYINT]: number;
31
+ [DuckDBTypeId.SMALLINT]: number;
32
+ [DuckDBTypeId.INTEGER]: number;
33
+ [DuckDBTypeId.BIGINT]: bigint;
34
+ [DuckDBTypeId.UTINYINT]: number;
35
+ [DuckDBTypeId.USMALLINT]: number;
36
+ [DuckDBTypeId.UINTEGER]: number;
37
+ [DuckDBTypeId.UBIGINT]: bigint;
38
+ [DuckDBTypeId.FLOAT]: number;
39
+ [DuckDBTypeId.DOUBLE]: number;
40
+ [DuckDBTypeId.TIMESTAMP]: Date;
41
+ [DuckDBTypeId.DATE]: Date;
42
+ [DuckDBTypeId.TIME]: bigint;
43
+ [DuckDBTypeId.INTERVAL]: { months: number; days: number; micros: bigint };
44
+ [DuckDBTypeId.HUGEINT]: bigint;
45
+ [DuckDBTypeId.UHUGEINT]: bigint;
46
+ [DuckDBTypeId.VARCHAR]: string;
47
+ [DuckDBTypeId.BLOB]: Uint8Array;
48
+ [DuckDBTypeId.DECIMAL]: number;
49
+ [DuckDBTypeId.TIMESTAMP_S]: Date;
50
+ [DuckDBTypeId.TIMESTAMP_MS]: Date;
51
+ [DuckDBTypeId.TIMESTAMP_NS]: Date;
52
+ [DuckDBTypeId.ENUM]: string;
53
+ [DuckDBTypeId.LIST]: (JS | null)[];
54
+ [DuckDBTypeId.STRUCT]: { [key: string]: JS | null } | (JS | null)[];
55
+ [DuckDBTypeId.MAP]: { key: JS | null; value: JS | null }[];
56
+ [DuckDBTypeId.ARRAY]: (JS | null)[];
57
+ [DuckDBTypeId.UUID]: string;
58
+ [DuckDBTypeId.UNION]: { tag: string; value: JS | null };
59
+ [DuckDBTypeId.BIT]: Uint8Array;
60
+ [DuckDBTypeId.TIME_TZ]: { micros: bigint; offset: number };
61
+ [DuckDBTypeId.TIMESTAMP_TZ]: Date;
62
+ [DuckDBTypeId.ANY]: never;
63
+ [DuckDBTypeId.BIGNUM]: bigint;
64
+ [DuckDBTypeId.SQLNULL]: null;
65
+ [DuckDBTypeId.STRING_LITERAL]: never;
66
+ [DuckDBTypeId.INTEGER_LITERAL]: never;
67
+ [DuckDBTypeId.TIME_NS]: bigint;
68
+ [DuckDBTypeId.GEOMETRY]: Uint8Array;
69
+ [DuckDBTypeId.VARIANT]: JS | null;
70
+ [DuckDBTypeId.TIMESTAMP_TZ_NS]: Date;
71
+ }
72
+
73
+ /**
74
+ * The JS type produced by `JSDuckDBValueConverter` for a given `DuckDBType`.
75
+ *
76
+ * Excludes NULL; use `JSTypeFor<T> | null` for a nullable value.
77
+ */
78
+ export type JSTypeFor<T extends DuckDBType> = JSTypeForTypeId[T['typeId']];
@@ -0,0 +1,7 @@
1
+ export type Json =
2
+ | null
3
+ | boolean
4
+ | number
5
+ | string
6
+ | Json[]
7
+ | { [key: string]: Json };
@@ -0,0 +1,72 @@
1
+ import { createDuckDBValueConverter } from './createDuckDBValueConverter';
2
+ import { DuckDBTypeId } from './DuckDBTypeId';
3
+ import { DuckDBValueConverterFor } from './DuckDBValueConverter';
4
+ import {
5
+ arrayFromArrayValue,
6
+ arrayFromListValue,
7
+ booleanFromValue,
8
+ fromVariantValue,
9
+ jsonNumberFromValue,
10
+ jsonObjectFromIntervalValue,
11
+ nullConverter,
12
+ numberFromValue,
13
+ objectArrayFromMapValue,
14
+ objectOrArrayFromStructValue,
15
+ objectFromUnionValue,
16
+ stringFromValue,
17
+ unsupportedConverter,
18
+ } from './DuckDBValueConverters';
19
+ import { Json } from './Json';
20
+ import { JsonTypeForTypeId } from './JsonTypeForTypeId';
21
+
22
+ const JsonConvertersByTypeId: {
23
+ [Id in DuckDBTypeId]: DuckDBValueConverterFor<JsonTypeForTypeId[Id], Json>;
24
+ } = {
25
+ [DuckDBTypeId.INVALID]: unsupportedConverter,
26
+ [DuckDBTypeId.BOOLEAN]: booleanFromValue,
27
+ [DuckDBTypeId.TINYINT]: numberFromValue,
28
+ [DuckDBTypeId.SMALLINT]: numberFromValue,
29
+ [DuckDBTypeId.INTEGER]: numberFromValue,
30
+ [DuckDBTypeId.BIGINT]: stringFromValue,
31
+ [DuckDBTypeId.UTINYINT]: numberFromValue,
32
+ [DuckDBTypeId.USMALLINT]: numberFromValue,
33
+ [DuckDBTypeId.UINTEGER]: numberFromValue,
34
+ [DuckDBTypeId.UBIGINT]: stringFromValue,
35
+ [DuckDBTypeId.FLOAT]: jsonNumberFromValue,
36
+ [DuckDBTypeId.DOUBLE]: jsonNumberFromValue,
37
+ [DuckDBTypeId.TIMESTAMP]: stringFromValue,
38
+ [DuckDBTypeId.DATE]: stringFromValue,
39
+ [DuckDBTypeId.TIME]: stringFromValue,
40
+ [DuckDBTypeId.INTERVAL]: jsonObjectFromIntervalValue,
41
+ [DuckDBTypeId.HUGEINT]: stringFromValue,
42
+ [DuckDBTypeId.UHUGEINT]: stringFromValue,
43
+ [DuckDBTypeId.VARCHAR]: stringFromValue,
44
+ [DuckDBTypeId.BLOB]: stringFromValue,
45
+ [DuckDBTypeId.DECIMAL]: stringFromValue,
46
+ [DuckDBTypeId.TIMESTAMP_S]: stringFromValue,
47
+ [DuckDBTypeId.TIMESTAMP_MS]: stringFromValue,
48
+ [DuckDBTypeId.TIMESTAMP_NS]: stringFromValue,
49
+ [DuckDBTypeId.ENUM]: stringFromValue,
50
+ [DuckDBTypeId.LIST]: arrayFromListValue,
51
+ [DuckDBTypeId.STRUCT]: objectOrArrayFromStructValue,
52
+ [DuckDBTypeId.MAP]: objectArrayFromMapValue,
53
+ [DuckDBTypeId.ARRAY]: arrayFromArrayValue,
54
+ [DuckDBTypeId.UUID]: stringFromValue,
55
+ [DuckDBTypeId.UNION]: objectFromUnionValue,
56
+ [DuckDBTypeId.BIT]: stringFromValue,
57
+ [DuckDBTypeId.TIME_TZ]: stringFromValue,
58
+ [DuckDBTypeId.TIMESTAMP_TZ]: stringFromValue,
59
+ [DuckDBTypeId.ANY]: unsupportedConverter,
60
+ [DuckDBTypeId.BIGNUM]: stringFromValue,
61
+ [DuckDBTypeId.SQLNULL]: nullConverter,
62
+ [DuckDBTypeId.STRING_LITERAL]: unsupportedConverter,
63
+ [DuckDBTypeId.INTEGER_LITERAL]: unsupportedConverter,
64
+ [DuckDBTypeId.TIME_NS]: stringFromValue,
65
+ [DuckDBTypeId.GEOMETRY]: stringFromValue,
66
+ [DuckDBTypeId.VARIANT]: fromVariantValue,
67
+ [DuckDBTypeId.TIMESTAMP_TZ_NS]: stringFromValue,
68
+ };
69
+
70
+ export const JsonDuckDBValueConverter = createDuckDBValueConverter<Json>(
71
+ JsonConvertersByTypeId
72
+ );
@@ -0,0 +1,86 @@
1
+ import type { DuckDBType } from './DuckDBType';
2
+ import { DuckDBTypeId } from './DuckDBTypeId';
3
+ import type { Json } from './Json';
4
+
5
+ /**
6
+ * The JSON-compatible type produced by `JsonDuckDBValueConverter` for each
7
+ * DuckDB type id.
8
+ *
9
+ * Values are non-NULL results only: SQL NULL is handled by
10
+ * `createDuckDBValueConverter`, which yields `null` for any type. Callers
11
+ * representing a nullable value should use `JsonTypeForTypeId[Id] | null`.
12
+ *
13
+ * This differs from `JSTypeForTypeId` wherever a DuckDB value has no lossless
14
+ * JSON representation: 64-bit and wider integers, temporal types, DECIMAL,
15
+ * BLOB, BIT, and GEOMETRY are rendered as strings rather than as `bigint`,
16
+ * `Date`, or `Uint8Array`, and FLOAT and DOUBLE widen to `number | string` so
17
+ * that Infinity and NaN survive as strings.
18
+ *
19
+ * Type ids that cannot appear in a result (`INVALID`, `ANY`,
20
+ * `STRING_LITERAL`, `INTEGER_LITERAL`) map to `never`, since their
21
+ * converters throw.
22
+ *
23
+ * Nested types bottom out at `Json`, because `DuckDBListType`,
24
+ * `DuckDBArrayType`, `DuckDBStructType`, `DuckDBMapType`, and
25
+ * `DuckDBUnionType` do not carry their child types at the type level.
26
+ *
27
+ * STRUCT is an object keyed by entry name, except for a tuple (see
28
+ * `DuckDBStructType.isTuple`), which is an array of its items.
29
+ *
30
+ * This interface is exhaustive over `DuckDBTypeId`, and is the annotation
31
+ * source for the converter map in `JsonDuckDBValueConverter`; adding a type id
32
+ * without adding it here is a compile error.
33
+ */
34
+ export interface JsonTypeForTypeId {
35
+ [DuckDBTypeId.INVALID]: never;
36
+ [DuckDBTypeId.BOOLEAN]: boolean;
37
+ [DuckDBTypeId.TINYINT]: number;
38
+ [DuckDBTypeId.SMALLINT]: number;
39
+ [DuckDBTypeId.INTEGER]: number;
40
+ [DuckDBTypeId.BIGINT]: string;
41
+ [DuckDBTypeId.UTINYINT]: number;
42
+ [DuckDBTypeId.USMALLINT]: number;
43
+ [DuckDBTypeId.UINTEGER]: number;
44
+ [DuckDBTypeId.UBIGINT]: string;
45
+ [DuckDBTypeId.FLOAT]: number | string;
46
+ [DuckDBTypeId.DOUBLE]: number | string;
47
+ [DuckDBTypeId.TIMESTAMP]: string;
48
+ [DuckDBTypeId.DATE]: string;
49
+ [DuckDBTypeId.TIME]: string;
50
+ [DuckDBTypeId.INTERVAL]: { months: number; days: number; micros: string };
51
+ [DuckDBTypeId.HUGEINT]: string;
52
+ [DuckDBTypeId.UHUGEINT]: string;
53
+ [DuckDBTypeId.VARCHAR]: string;
54
+ [DuckDBTypeId.BLOB]: string;
55
+ [DuckDBTypeId.DECIMAL]: string;
56
+ [DuckDBTypeId.TIMESTAMP_S]: string;
57
+ [DuckDBTypeId.TIMESTAMP_MS]: string;
58
+ [DuckDBTypeId.TIMESTAMP_NS]: string;
59
+ [DuckDBTypeId.ENUM]: string;
60
+ [DuckDBTypeId.LIST]: (Json | null)[];
61
+ [DuckDBTypeId.STRUCT]: { [key: string]: Json | null } | (Json | null)[];
62
+ [DuckDBTypeId.MAP]: { key: Json | null; value: Json | null }[];
63
+ [DuckDBTypeId.ARRAY]: (Json | null)[];
64
+ [DuckDBTypeId.UUID]: string;
65
+ [DuckDBTypeId.UNION]: { tag: string; value: Json | null };
66
+ [DuckDBTypeId.BIT]: string;
67
+ [DuckDBTypeId.TIME_TZ]: string;
68
+ [DuckDBTypeId.TIMESTAMP_TZ]: string;
69
+ [DuckDBTypeId.ANY]: never;
70
+ [DuckDBTypeId.BIGNUM]: string;
71
+ [DuckDBTypeId.SQLNULL]: null;
72
+ [DuckDBTypeId.STRING_LITERAL]: never;
73
+ [DuckDBTypeId.INTEGER_LITERAL]: never;
74
+ [DuckDBTypeId.TIME_NS]: string;
75
+ [DuckDBTypeId.GEOMETRY]: string;
76
+ [DuckDBTypeId.VARIANT]: Json | null;
77
+ [DuckDBTypeId.TIMESTAMP_TZ_NS]: string;
78
+ }
79
+
80
+ /**
81
+ * The JSON-compatible type produced by `JsonDuckDBValueConverter` for a given
82
+ * `DuckDBType`.
83
+ *
84
+ * Excludes NULL; use `JsonTypeFor<T> | null` for a nullable value.
85
+ */
86
+ export type JsonTypeFor<T extends DuckDBType> = JsonTypeForTypeId[T['typeId']];
@@ -0,0 +1,68 @@
1
+ import { DuckDBType } from './DuckDBType';
2
+ import { DuckDBTypeId } from './DuckDBTypeId';
3
+ import { DuckDBValue } from './values';
4
+
5
+ /**
6
+ * Converts a `T` into the `DuckDBValue` representation for a DuckDB type: the
7
+ * inverse of `DuckDBValueConverter<T>`, which converts the other way.
8
+ *
9
+ * `T` is whatever representation a given converter accepts — see
10
+ * `JSToDuckDBValueConverter` for the JS one. `DuckDBValue` is itself a
11
+ * TypeScript representation, plain JS for the scalars and the value wrapper
12
+ * classes, which hold nothing but data, for the rest. So a conversion of this
13
+ * shape touches no C API, which is what lets it survive the move to the V2 C
14
+ * API, where constructing a `duckdb_value` requires a connection or context.
15
+ * Nothing here constructs one.
16
+ */
17
+ export type ToDuckDBValueConverter<T> = (
18
+ value: T,
19
+ type: DuckDBType,
20
+ converter: ToDuckDBValueConverter<T>
21
+ ) => DuckDBValue;
22
+
23
+ /**
24
+ * A converter for a single DuckDB type id, whose accepted input `In` is
25
+ * narrower than the `Nested` the top-level converter it recurses through
26
+ * accepts.
27
+ *
28
+ * The write-side counterpart of `DuckDBValueConverterFor`, and narrowing for
29
+ * the same reason: in a per-type-id map only the accepted input varies by id,
30
+ * while the `converter` argument is always the map's top-level converter.
31
+ */
32
+ export type ToDuckDBValueConverterFor<In, Nested> = (
33
+ value: In,
34
+ type: DuckDBType,
35
+ converter: ToDuckDBValueConverter<Nested>
36
+ ) => DuckDBValue;
37
+
38
+ /**
39
+ * Assembles a per-type-id converter map into a single converter.
40
+ *
41
+ * `InputTable` is the type table the map was annotated from — one entry per
42
+ * type id — so each entry may accept a type narrower than `T`. That narrowing
43
+ * is contravariant, so unlike the read direction it cannot be checked through
44
+ * to the dispatch: the type id is only known at runtime, and the compiler
45
+ * cannot relate `type.typeId` to the static type of `value`. The one unchecked
46
+ * cast that closes the gap lives here rather than at each call site — callers
47
+ * get a checked signature, and the mismatch surfaces as the converter's own
48
+ * `Expected ...` error.
49
+ */
50
+ export function createToDuckDBValueConverter<
51
+ T,
52
+ InputTable extends Record<DuckDBTypeId, unknown>,
53
+ >(convertersByTypeId: {
54
+ [Id in DuckDBTypeId]: ToDuckDBValueConverterFor<InputTable[Id], T>;
55
+ }): ToDuckDBValueConverter<T> {
56
+ return (value, type, converter) => {
57
+ if (value == null) {
58
+ return null;
59
+ }
60
+ const converterForTypeId = convertersByTypeId[type.typeId] as
61
+ | ToDuckDBValueConverter<T>
62
+ | undefined;
63
+ if (!converterForTypeId) {
64
+ throw new Error(`No converter for typeId: ${type.typeId}`);
65
+ }
66
+ return converterForTypeId(value, type, converter);
67
+ };
68
+ }