@leonardovida-md/drizzle-neo-duckdb 2.1.2 → 2.3.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.
package/dist/helpers.d.ts CHANGED
@@ -1 +1 @@
1
- export { duckDbList, duckDbArray, duckDbMap, duckDbStruct, duckDbJson, duckDbBlob, duckDbInet, duckDbInterval, duckDbTimestamp, duckDbDate, duckDbTime, duckDbArrayContains, duckDbArrayContained, duckDbArrayOverlaps, } from './columns.ts';
1
+ export { duckDbList, duckDbArray, duckDbMap, duckDbStruct, duckDbUnion, duckDbJson, duckDbBlob, duckDbInet, duckDbInterval, duckDbTimestamp, duckDbDate, duckDbTime, duckDbArrayContains, duckDbArrayContained, duckDbArrayOverlaps, } from './columns.ts';
package/dist/helpers.mjs CHANGED
@@ -124,6 +124,13 @@ function splitTopLevel(input, delimiter) {
124
124
 
125
125
  // src/time.ts
126
126
  var MICROS_PER_DAY = 86400000000n;
127
+ var droppedMicros = new WeakMap;
128
+ function dateSubMillisecondMicros(date) {
129
+ return droppedMicros.get(date) ?? 0;
130
+ }
131
+ function dateToMicros(date) {
132
+ return BigInt(date.getTime()) * 1000n + BigInt(dateSubMillisecondMicros(date));
133
+ }
127
134
  function pad(value, length) {
128
135
  return value.toString().padStart(length, "0");
129
136
  }
@@ -184,9 +191,22 @@ function timestampStringFromMicros(micros, withTimezone) {
184
191
  seconds -= 1n;
185
192
  fraction += 1000000n;
186
193
  }
187
- const base = new Date(Number(seconds) * 1000).toISOString().replace(/\.\d+Z$/, "").replace("T", " ");
194
+ const date = new Date(Number(seconds) * 1000);
195
+ const time = date.toISOString().slice(11, 19);
188
196
  const digits = fraction === 0n ? "" : `.${pad(fraction, 6).replace(/0+$/, "")}`;
189
- return `${base}${digits}${withTimezone ? "+00" : ""}`;
197
+ const [day, era] = splitEra(dateStringFromDate(date));
198
+ return `${day}${era} ${time}${digits}${withTimezone ? "+00" : ""}`;
199
+ }
200
+ function splitEra(text) {
201
+ return text.endsWith(" (BC)") ? [text.slice(0, -5), " (BC)"] : [text, ""];
202
+ }
203
+ function dateStringFromDate(value) {
204
+ const year = value.getUTCFullYear();
205
+ const bc = year <= 0;
206
+ const month = String(value.getUTCMonth() + 1).padStart(2, "0");
207
+ const day = String(value.getUTCDate()).padStart(2, "0");
208
+ const text = `${String(bc ? 1 - year : year).padStart(4, "0")}-${month}-${day}`;
209
+ return bc ? `${text} (BC)` : text;
190
210
  }
191
211
  function pluralUnit(value, unit) {
192
212
  return `${value} ${unit}${Math.abs(value) === 1 ? "" : "s"}`;
@@ -262,6 +282,15 @@ function parseStructSchema(typeHint) {
262
282
  }
263
283
  return fields;
264
284
  }
285
+ function parseMapType(typeHint) {
286
+ const match = /^MAP\s*\((.*)\)$/is.exec(typeHint?.trim() ?? "");
287
+ if (!match)
288
+ return;
289
+ const parts = splitTopLevel(match[1], ",");
290
+ if (parts.length !== 2)
291
+ return;
292
+ return { keyType: parts[0].trim(), valueType: parts[1].trim() };
293
+ }
265
294
  function arrayElementType(typeHint) {
266
295
  if (!typeHint)
267
296
  return;
@@ -295,7 +324,7 @@ function formatLiteral(value, typeHint) {
295
324
  return blobLiteral(value);
296
325
  }
297
326
  if (typeof value === "number") {
298
- return Number.isFinite(value) ? value.toString() : `'${value}'::DOUBLE`;
327
+ return Number.isFinite(value) && !Object.is(value, -0) ? value.toString() : `'${Object.is(value, -0) ? "-0" : value}'::DOUBLE`;
299
328
  }
300
329
  if (typeof value === "bigint") {
301
330
  return value.toString();
@@ -315,7 +344,8 @@ function buildListLiteral(values, elementType) {
315
344
  return elementType ? sql.raw(`[]::${elementType}[]`) : sql`[]`;
316
345
  }
317
346
  const chunks = values.map((v) => valueToSqlLiteral(v, elementType));
318
- return sql`list_value(${sql.join(chunks, sql.raw(", "))})`;
347
+ const list = sql`list_value(${sql.join(chunks, sql.raw(", "))})`;
348
+ return elementType ? sql`${list}::${sql.raw(elementType)}[]` : list;
319
349
  }
320
350
  function normalizeArrayPredicateValue(values, elementType) {
321
351
  return Array.isArray(values) ? buildListLiteral(values, elementType) : values;
@@ -331,7 +361,8 @@ function valueToSqlLiteral(value, typeHint) {
331
361
  return sql`${value}`;
332
362
  }
333
363
  if (isRecord(value)) {
334
- return buildStructLiteral(value, parseStructSchema(typeHint));
364
+ const mapType = parseMapType(typeHint);
365
+ return mapType ? buildMapLiteral(value, mapType.valueType, mapType.keyType) : buildStructLiteral(value, parseStructSchema(typeHint));
335
366
  }
336
367
  return sql.raw(formatLiteral(value, typeHint));
337
368
  }
@@ -343,10 +374,10 @@ function buildStructLiteral(value, schema) {
343
374
  });
344
375
  return sql`struct_pack(${sql.join(parts, sql.raw(", "))})`;
345
376
  }
346
- function buildMapLiteral(value, valueType) {
377
+ function buildMapLiteral(value, valueType, keyType = "TEXT") {
347
378
  const keys = Object.keys(value);
348
379
  const vals = Object.values(value);
349
- const keyList = buildListLiteral(keys, "TEXT");
380
+ const keyList = buildListLiteral(keys, keyType);
350
381
  const valList = buildListLiteral(vals, valueType);
351
382
  return sql`map(${keyList}, ${valList})`;
352
383
  }
@@ -390,20 +421,26 @@ var duckDbMap = (name, valueType, options = {}) => customType({
390
421
  },
391
422
  toDriver(value) {
392
423
  if (Object.keys(value).length === 0 || Object.values(value).some(needsLiteral)) {
393
- return buildMapLiteral(value, valueType);
424
+ return buildMapLiteral(value, valueType, options.keyType);
394
425
  }
395
426
  return wrapMap(value, valueType);
396
427
  },
397
428
  fromDriver(value) {
398
429
  if (options.mode === "object" && Array.isArray(value)) {
399
430
  return Object.fromEntries(value.map((entry) => [
400
- String(entry.key),
431
+ mapKeyString(entry.key, options.keyType),
401
432
  entry.value
402
433
  ]));
403
434
  }
404
435
  return value;
405
436
  }
406
437
  })(name);
438
+ function mapKeyString(key, keyType) {
439
+ if (key instanceof Date) {
440
+ return /^DATE$/i.test(keyType?.trim() ?? "") ? dateStringFromDate(key) : timestampStringFromMicros(BigInt(key.getTime()) * 1000n, false);
441
+ }
442
+ return String(key);
443
+ }
407
444
  var duckDbStruct = (name, schema) => customType({
408
445
  dataType() {
409
446
  const fields = Object.entries(schema).map(([key, type]) => `"${key.replace(/"/g, '""')}" ${type}`);
@@ -423,6 +460,23 @@ var duckDbStruct = (name, schema) => customType({
423
460
  return value;
424
461
  }
425
462
  })(name);
463
+ var duckDbUnion = (name, members) => customType({
464
+ dataType() {
465
+ const fields = Object.entries(members).map(([tag, type]) => `"${tag.replace(/"/g, '""')}" ${type}`);
466
+ return `UNION (${fields.join(", ")})`;
467
+ },
468
+ toDriver(value) {
469
+ const memberType = Object.hasOwn(members, value.tag) ? members[value.tag] : undefined;
470
+ if (memberType === undefined) {
471
+ throw new Error(`UNION column "${name}" has no member "${String(value.tag)}". Members: ${Object.keys(members).join(", ")}`);
472
+ }
473
+ const tag = sql.raw(`"${value.tag.replace(/"/g, '""')}"`);
474
+ return sql`union_value(${tag} := CAST(${valueToSqlLiteral(value.value, memberType)} AS ${sql.raw(memberType)}))`;
475
+ },
476
+ fromDriver(value) {
477
+ return value;
478
+ }
479
+ })(name);
426
480
  var duckDbJson = (name) => customType({
427
481
  dataType() {
428
482
  return "JSON";
@@ -431,8 +485,8 @@ var duckDbJson = (name) => customType({
431
485
  if (typeof value === "string") {
432
486
  return value;
433
487
  }
434
- if (value !== null && typeof value === "object" && "queryChunks" in value) {
435
- return value;
488
+ if (isSQLWrapper(value)) {
489
+ return sql`${value}`;
436
490
  }
437
491
  return wrapJson(value);
438
492
  },
@@ -550,7 +604,7 @@ function timestampLiteral(value, withTimezone) {
550
604
  }
551
605
  function timestampModeString(value, withTimezone) {
552
606
  if (value instanceof Date) {
553
- return timestampStringFromMicros(BigInt(value.getTime()) * 1000n, withTimezone);
607
+ return timestampStringFromMicros(dateToMicros(value), withTimezone);
554
608
  }
555
609
  if (value !== null && typeof value === "object" && "micros" in value && typeof value.micros === "bigint") {
556
610
  return timestampStringFromMicros(value.micros, withTimezone);
@@ -609,6 +663,9 @@ var duckDbTimestamp = (name, options = {}) => customType({
609
663
  if (value instanceof Date) {
610
664
  return value;
611
665
  }
666
+ if (value === "infinity" || value === "-infinity") {
667
+ return value;
668
+ }
612
669
  const stringValue = typeof value === "string" ? value : value.toString();
613
670
  return new Date(timestampStringToDateInput(stringValue));
614
671
  }
@@ -621,8 +678,7 @@ var duckDbDate = (name) => customType({
621
678
  return value;
622
679
  },
623
680
  fromDriver(value) {
624
- const str = value instanceof Date ? value.toISOString().slice(0, 10) : value;
625
- return str;
681
+ return value instanceof Date ? dateStringFromDate(value) : value;
626
682
  }
627
683
  })(name);
628
684
  var duckDbTime = (name, options = {}) => customType({
@@ -652,6 +708,7 @@ function duckDbArrayOverlaps(column, values) {
652
708
  return sql`array_has_any(${column}, ${rhs})`;
653
709
  }
654
710
  export {
711
+ duckDbUnion,
655
712
  duckDbTimestamp,
656
713
  duckDbTime,
657
714
  duckDbStruct,