@leonardovida-md/drizzle-neo-duckdb 2.2.0 → 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/dialect.d.ts CHANGED
@@ -4,12 +4,15 @@ import { PgDialect, type PgColumn, type PgTable } from 'drizzle-orm/pg-core';
4
4
  import { SQL, type DriverValueEncoder, type QueryTypingsValue } from 'drizzle-orm';
5
5
  import type { BuildRelationalQueryResult } from 'drizzle-orm/relations';
6
6
  import { type QueryWithTypings } from 'drizzle-orm/sql/sql';
7
+ import type { RelationalSelectionItem } from './sql/result-mapper.ts';
7
8
  /**
8
9
  * Query typing for params bound to a non-array column. DuckDB sessions never
9
10
  * coerce these params from Postgres array literal strings.
10
11
  * @internal
11
12
  */
12
13
  export declare const DUCKDB_SCALAR_COLUMN_TYPING: QueryTypingsValue;
14
+ /** @internal */
15
+ export declare function relationalSelectionOf(query: object): readonly RelationalSelectionItem[] | undefined;
13
16
  /**
14
17
  * Query typing for params bound to a pg-core array column, so sessions can
15
18
  * bind its empty array as an empty list.
@@ -69,6 +69,13 @@ import {
69
69
 
70
70
  // src/time.ts
71
71
  var MICROS_PER_DAY = 86400000000n;
72
+ var droppedMicros = new WeakMap;
73
+ function rememberDroppedMicros(date, micros) {
74
+ droppedMicros.set(date, micros);
75
+ }
76
+ function dateSubMillisecondMicros(date) {
77
+ return droppedMicros.get(date) ?? 0;
78
+ }
72
79
  function pad(value, length) {
73
80
  return value.toString().padStart(length, "0");
74
81
  }
@@ -114,6 +121,30 @@ function timestampStringToDateInput(value, parts = parseTimestampString(value))
114
121
  const normalized = !value.includes("T") && value.includes(" ") ? value.replace(" ", "T") : value;
115
122
  return normalized.endsWith("Z") || /[+-]\d{2}:?\d{2}$/.test(normalized) ? normalized : `${normalized}Z`;
116
123
  }
124
+ function pluralUnit(value, unit) {
125
+ return `${value} ${unit}${Math.abs(value) === 1 ? "" : "s"}`;
126
+ }
127
+ function intervalToString(months, days, micros) {
128
+ const parts = [];
129
+ const years = Math.trunc(months / 12);
130
+ const extraMonths = months - years * 12;
131
+ if (years !== 0)
132
+ parts.push(pluralUnit(years, "year"));
133
+ if (extraMonths !== 0)
134
+ parts.push(pluralUnit(extraMonths, "month"));
135
+ if (days !== 0)
136
+ parts.push(pluralUnit(days, "day"));
137
+ if (micros !== 0n) {
138
+ const negative = micros < 0n;
139
+ const abs = negative ? -micros : micros;
140
+ const hours = abs / 3600000000n;
141
+ const minutes = abs / 60000000n % 60n;
142
+ const seconds = abs / 1000000n % 60n;
143
+ const fraction = pad(abs % 1000000n, 6).replace(/0+$/, "");
144
+ parts.push(`${negative ? "-" : ""}${pad(hours, 2)}:${pad(minutes, 2)}:${pad(seconds, 2)}${fraction ? `.${fraction}` : ""}`);
145
+ }
146
+ return parts.length > 0 ? parts.join(" ") : "00:00:00";
147
+ }
117
148
  function subMillisecondMicros(fraction) {
118
149
  if (!fraction || fraction.length <= 3) {
119
150
  return 0n;
@@ -1045,7 +1076,13 @@ function dateFromTimestampUnits(units, unitsPerMilli, typeName) {
1045
1076
  if (millis < -MAX_DATE_MILLIS || millis > MAX_DATE_MILLIS) {
1046
1077
  throw new Error(`${typeName} value out of range for JS Date: ${units}`);
1047
1078
  }
1048
- return new Date(Number(millis));
1079
+ const date = new Date(Number(millis));
1080
+ if (unitsPerMilli === 1000n) {
1081
+ const dropped = units - millis * 1000n;
1082
+ if (dropped !== 0n)
1083
+ rememberDroppedMicros(date, Number(dropped));
1084
+ }
1085
+ return date;
1049
1086
  }
1050
1087
  var JSValueConverter = (value, type, converter) => {
1051
1088
  if (value !== null && typeof value === "object") {
@@ -1785,8 +1822,10 @@ function normalizeTimestampString(value, withTimezone) {
1785
1822
  return value;
1786
1823
  }
1787
1824
  if (value instanceof Date) {
1788
- const iso = value.toISOString().replace("T", " ");
1789
- return withTimezone ? iso.replace("Z", "+00") : iso.replace("Z", "");
1825
+ const micros = dateSubMillisecondMicros(value);
1826
+ const digits = micros ? String(micros).padStart(3, "0").replace(/0+$/, "") : "";
1827
+ const iso = value.toISOString().replace("T", " ").replace("Z", digits);
1828
+ return withTimezone ? `${iso}+00` : iso;
1790
1829
  }
1791
1830
  if (typeof value === "string") {
1792
1831
  const normalized = value.replace("T", " ");
@@ -1840,13 +1879,7 @@ function normalizeTime(value) {
1840
1879
  function normalizeInterval(value) {
1841
1880
  if (value && typeof value === "object" && "days" in value && "months" in value) {
1842
1881
  const { months, days, micros } = value;
1843
- if (months === 0 && days !== undefined) {
1844
- if (micros && Number(micros) !== 0) {
1845
- const seconds = Number(micros) / 1e6;
1846
- return `${days} day${days === 1 ? "" : "s"} ${seconds} seconds`.trim();
1847
- }
1848
- return `${days} day${days === 1 ? "" : "s"}`;
1849
- }
1882
+ return intervalToString(Number(months), Number(days), BigInt(micros ?? 0));
1850
1883
  }
1851
1884
  return value;
1852
1885
  }
@@ -1859,20 +1892,13 @@ function decodesInet(decoder) {
1859
1892
  }
1860
1893
  return true;
1861
1894
  }
1862
- function compileValueMapper(decoder) {
1895
+ function compileValueNormalizer(decoder) {
1863
1896
  const inet = decodesInet(decoder) ? normalizeInet : (value) => value;
1864
1897
  let normalize = (value) => value;
1865
1898
  if (is(decoder, PgTimestampString)) {
1866
1899
  normalize = (value) => normalizeTimestampString(value, decoder.withTimezone);
1867
1900
  } else if (is(decoder, PgTimestamp)) {
1868
- return (value) => {
1869
- if (value === null)
1870
- return null;
1871
- if (isInfiniteTimestamp(value))
1872
- return value;
1873
- const normalized = normalizeTimestamp(inet(value), decoder.withTimezone);
1874
- return normalized instanceof Date ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
1875
- };
1901
+ normalize = (value) => isInfiniteTimestamp(value) ? value : normalizeTimestamp(value, decoder.withTimezone);
1876
1902
  } else if (is(decoder, PgDateString)) {
1877
1903
  normalize = normalizeDateString;
1878
1904
  } else if (is(decoder, PgDate)) {
@@ -1882,7 +1908,30 @@ function compileValueMapper(decoder) {
1882
1908
  } else if (is(decoder, PgInterval)) {
1883
1909
  normalize = normalizeInterval;
1884
1910
  }
1885
- return (value) => value === null ? null : decoder.mapFromDriverValue(toDecoderInput(decoder, normalize(inet(value))));
1911
+ return (value) => value === null ? null : normalize(inet(value));
1912
+ }
1913
+ function compileValueMapper(decoder) {
1914
+ const normalize = compileValueNormalizer(decoder);
1915
+ if (is(decoder, PgTimestamp)) {
1916
+ return (value) => {
1917
+ const normalized = normalize(value);
1918
+ return normalized === null || normalized instanceof Date || isInfiniteTimestamp(normalized) ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
1919
+ };
1920
+ }
1921
+ return (value) => {
1922
+ const normalized = normalize(value);
1923
+ return normalized === null ? null : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
1924
+ };
1925
+ }
1926
+ function compileRelationalRowNormalizer(selection) {
1927
+ const normalizers = selection.map((item) => item.isJson ? undefined : compileValueNormalizer(resolveFieldDecoder(item.field)));
1928
+ return (row) => {
1929
+ for (let index = 0;index < normalizers.length; index += 1) {
1930
+ const normalize = normalizers[index];
1931
+ if (normalize)
1932
+ row[index] = normalize(row[index]);
1933
+ }
1934
+ };
1886
1935
  }
1887
1936
  function assignResultPath(result, path, value) {
1888
1937
  if (path.length === 0) {
@@ -3185,6 +3234,10 @@ function transformWithAst(query, {
3185
3234
  // src/dialect.ts
3186
3235
  var PG_JSON_UNSUPPORTED_MESSAGE = "Pg JSON/JSONB columns are not supported in DuckDB. Replace them with duckDbJson() to use DuckDB's native JSON type.";
3187
3236
  var DUCKDB_SCALAR_COLUMN_TYPING = "duckdb:scalar";
3237
+ var relationalSelections = new WeakMap;
3238
+ function relationalSelectionOf(query) {
3239
+ return relationalSelections.get(query);
3240
+ }
3188
3241
  var DUCKDB_ARRAY_COLUMN_TYPING = "duckdb:array";
3189
3242
  var MIGRATION_CONFLICT_MAX_ATTEMPTS = 10;
3190
3243
  var MIGRATION_RETRY_BASE_DELAY_MS = 20;
@@ -3241,10 +3294,11 @@ function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
3241
3294
  const replacement = value === undefined ? undefined : RELATIONAL_JSON_CHUNKS.get(value);
3242
3295
  const next = chunks[index + 1];
3243
3296
  if (replacement) {
3244
- if (exactDecimals && value === "json_build_array(" && is2(next, SQL2)) {
3297
+ if (value === "json_build_array(" && is2(next, SQL2)) {
3245
3298
  const fields = next.queryChunks.map((item) => {
3246
- if (!is2(item, Column2))
3299
+ if (!is2(item, Column2) || !exactDecimals && !item.columnType.startsWith("PgNumeric")) {
3247
3300
  return item;
3301
+ }
3248
3302
  const originalType = item.getSQLType();
3249
3303
  const exactType = decimalStringType(originalType);
3250
3304
  return exactType === originalType ? item : sql2`cast(${item} as ${sql2.raw(exactType)})`;
@@ -3402,6 +3456,7 @@ class DuckDBDialect extends PgDialect {
3402
3456
  const result = super.buildRelationalQueryWithoutPK(config);
3403
3457
  if (!config.nestedQueryRelation && is2(result.sql, SQL2)) {
3404
3458
  rewriteRelationalJsonChunks(result.sql.queryChunks, this.options.decimalMode === "string");
3459
+ relationalSelections.set(result.sql, result.selection);
3405
3460
  }
3406
3461
  return result;
3407
3462
  }
@@ -3410,10 +3465,14 @@ class DuckDBDialect extends PgDialect {
3410
3465
  const transformed = transformSQL(result.sql, {
3411
3466
  qualifyJoinColumns: this.options.qualifyRawJoinColumns === true
3412
3467
  });
3413
- return {
3468
+ const query = {
3414
3469
  ...result,
3415
3470
  sql: transformed.sql
3416
3471
  };
3472
+ const selection = relationalSelections.get(sqlObj);
3473
+ if (selection)
3474
+ relationalSelections.set(query, selection);
3475
+ return query;
3417
3476
  }
3418
3477
  }
3419
3478
 
@@ -3853,9 +3912,11 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3853
3912
  onStatementError;
3854
3913
  decimalMode;
3855
3914
  waitForTransactions;
3915
+ relationalSelection;
3856
3916
  static [entityKind3] = "DuckDBPreparedQuery";
3857
3917
  resultMapper;
3858
- constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false) {
3918
+ relationalRowNormalizer;
3919
+ constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false, relationalSelection) {
3859
3920
  super(...[
3860
3921
  { sql: queryString, params },
3861
3922
  undefined,
@@ -3877,6 +3938,7 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3877
3938
  this.onStatementError = onStatementError;
3878
3939
  this.decimalMode = decimalMode;
3879
3940
  this.waitForTransactions = waitForTransactions;
3941
+ this.relationalSelection = relationalSelection;
3880
3942
  }
3881
3943
  async execute(placeholderValues = {}) {
3882
3944
  try {
@@ -3908,17 +3970,24 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3908
3970
  });
3909
3971
  return rows2;
3910
3972
  }
3973
+ const relationalSelection = customResultMapper ? this.relationalSelection : undefined;
3911
3974
  const { rows, exactDecimalColumns } = await executeArraysOnClient(this.client, queryString, params, {
3912
3975
  prepareCache: this.prepareCache,
3913
- exactDecimals: !customResultMapper,
3976
+ exactDecimals: !customResultMapper || relationalSelection !== undefined,
3914
3977
  decimalMode: this.decimalMode
3915
3978
  });
3979
+ const decimalFields = relationalSelection ?? fields;
3980
+ if (exactDecimalColumns && decimalFields && this.decimalMode !== "string") {
3981
+ restoreNonNumericDecimals(rows, decimalFields, exactDecimalColumns);
3982
+ }
3916
3983
  if (customResultMapper) {
3984
+ if (relationalSelection) {
3985
+ this.relationalRowNormalizer ??= compileRelationalRowNormalizer(relationalSelection);
3986
+ for (const row of rows)
3987
+ this.relationalRowNormalizer(row);
3988
+ }
3917
3989
  return customResultMapper(rows);
3918
3990
  }
3919
- if (exactDecimalColumns && this.decimalMode !== "string") {
3920
- restoreNonNumericDecimals(rows, fields, exactDecimalColumns);
3921
- }
3922
3991
  this.resultMapper ??= compileResultMapper(fields, joinsNotNullableMap);
3923
3992
  return rows.map(this.resultMapper);
3924
3993
  }
@@ -3959,7 +4028,7 @@ class DuckDBSession extends PgSession {
3959
4028
  };
3960
4029
  }
3961
4030
  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
3962
- return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions());
4031
+ return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions(), relationalSelectionOf(query));
3963
4032
  }
3964
4033
  waitsForTransactions() {
3965
4034
  return !this.statementFailures && !isPool(this.client);
@@ -4324,8 +4393,12 @@ function resolvePoolSize(pool) {
4324
4393
  return false;
4325
4394
  if (pool === undefined)
4326
4395
  return DEFAULT_POOL_SIZE;
4327
- if (typeof pool === "string")
4328
- return POOL_PRESETS[pool] ?? DEFAULT_POOL_SIZE;
4396
+ if (typeof pool === "string") {
4397
+ if (!Object.hasOwn(POOL_PRESETS, pool)) {
4398
+ throw new Error(`Unknown pool preset ${JSON.stringify(pool)}. Expected one of: ${Object.keys(POOL_PRESETS).join(", ")}.`);
4399
+ }
4400
+ return POOL_PRESETS[pool];
4401
+ }
4329
4402
  return resolveSize(pool.size);
4330
4403
  }
4331
4404
  var POOL_CLOSED_MESSAGE = "DuckDB connection pool is closed";
@@ -5713,26 +5786,24 @@ function emitConstraints(plan, plans, tableIdentifiers) {
5713
5786
  const param = allocateIdentifier("t", referencedTables);
5714
5787
  const ownColumns = (columns) => columns.map((col) => memberAccess(param, plan.columnProperties.get(col) ?? columnProperty(col))).join(", ");
5715
5788
  const entries = [];
5716
- const usedKeys = new Set;
5717
5789
  for (const constraint of constraints) {
5718
- const key = allocateIdentifier(toIdentifier(constraint.name || `${table.name}_constraint`), usedKeys);
5719
5790
  const name = JSON.stringify(constraint.name);
5720
5791
  if (constraint.type === "PRIMARY KEY") {
5721
- entries.push(`${key}: primaryKey({ columns: [${ownColumns(constraint.columns)}], name: ${name} })`);
5792
+ entries.push(`primaryKey({ columns: [${ownColumns(constraint.columns)}], name: ${name} })`);
5722
5793
  } else if (constraint.type === "UNIQUE") {
5723
- entries.push(`${key}: unique(${name}).on(${ownColumns(constraint.columns)})`);
5794
+ entries.push(`unique(${name}).on(${ownColumns(constraint.columns)})`);
5724
5795
  } else {
5725
5796
  const target = resolveTarget(constraint);
5726
5797
  const targetVar = target.key === selfKey ? param : target.identifier;
5727
5798
  const foreignColumns = constraint.referencedTable.columns.map((col) => memberAccess(targetVar, target.columnProperties?.get(col) ?? columnProperty(col))).join(", ");
5728
- entries.push(`${key}: foreignKey({ columns: [${ownColumns(constraint.columns)}], foreignColumns: [${foreignColumns}], name: ${name} })`);
5799
+ entries.push(`foreignKey({ columns: [${ownColumns(constraint.columns)}], foreignColumns: [${foreignColumns}], name: ${name} })`);
5729
5800
  }
5730
5801
  }
5731
- const lines = [`(${param}) => ({`];
5802
+ const lines = [`(${param}) => [`];
5732
5803
  for (const entry of entries) {
5733
5804
  lines.push(` ${entry},`);
5734
5805
  }
5735
- lines.push("})");
5806
+ lines.push("]");
5736
5807
  return lines.join(`
5737
5808
  `);
5738
5809
  }
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
  }
@@ -597,7 +604,7 @@ function timestampLiteral(value, withTimezone) {
597
604
  }
598
605
  function timestampModeString(value, withTimezone) {
599
606
  if (value instanceof Date) {
600
- return timestampStringFromMicros(BigInt(value.getTime()) * 1000n, withTimezone);
607
+ return timestampStringFromMicros(dateToMicros(value), withTimezone);
601
608
  }
602
609
  if (value !== null && typeof value === "object" && "micros" in value && typeof value.micros === "bigint") {
603
610
  return timestampStringFromMicros(value.micros, withTimezone);
package/dist/index.mjs CHANGED
@@ -185,6 +185,16 @@ function assignOwnProperty(target, key, value) {
185
185
 
186
186
  // src/time.ts
187
187
  var MICROS_PER_DAY = 86400000000n;
188
+ var droppedMicros = new WeakMap;
189
+ function rememberDroppedMicros(date, micros) {
190
+ droppedMicros.set(date, micros);
191
+ }
192
+ function dateSubMillisecondMicros(date) {
193
+ return droppedMicros.get(date) ?? 0;
194
+ }
195
+ function dateToMicros(date) {
196
+ return BigInt(date.getTime()) * 1000n + BigInt(dateSubMillisecondMicros(date));
197
+ }
188
198
  function pad(value, length) {
189
199
  return value.toString().padStart(length, "0");
190
200
  }
@@ -415,8 +425,10 @@ function normalizeTimestampString(value, withTimezone) {
415
425
  return value;
416
426
  }
417
427
  if (value instanceof Date) {
418
- const iso = value.toISOString().replace("T", " ");
419
- return withTimezone ? iso.replace("Z", "+00") : iso.replace("Z", "");
428
+ const micros = dateSubMillisecondMicros(value);
429
+ const digits = micros ? String(micros).padStart(3, "0").replace(/0+$/, "") : "";
430
+ const iso = value.toISOString().replace("T", " ").replace("Z", digits);
431
+ return withTimezone ? `${iso}+00` : iso;
420
432
  }
421
433
  if (typeof value === "string") {
422
434
  const normalized = value.replace("T", " ");
@@ -470,13 +482,7 @@ function normalizeTime(value) {
470
482
  function normalizeInterval(value) {
471
483
  if (value && typeof value === "object" && "days" in value && "months" in value) {
472
484
  const { months, days, micros } = value;
473
- if (months === 0 && days !== undefined) {
474
- if (micros && Number(micros) !== 0) {
475
- const seconds = Number(micros) / 1e6;
476
- return `${days} day${days === 1 ? "" : "s"} ${seconds} seconds`.trim();
477
- }
478
- return `${days} day${days === 1 ? "" : "s"}`;
479
- }
485
+ return intervalToString(Number(months), Number(days), BigInt(micros ?? 0));
480
486
  }
481
487
  return value;
482
488
  }
@@ -489,20 +495,13 @@ function decodesInet(decoder) {
489
495
  }
490
496
  return true;
491
497
  }
492
- function compileValueMapper(decoder) {
498
+ function compileValueNormalizer(decoder) {
493
499
  const inet = decodesInet(decoder) ? normalizeInet : (value) => value;
494
500
  let normalize = (value) => value;
495
501
  if (is(decoder, PgTimestampString)) {
496
502
  normalize = (value) => normalizeTimestampString(value, decoder.withTimezone);
497
503
  } else if (is(decoder, PgTimestamp)) {
498
- return (value) => {
499
- if (value === null)
500
- return null;
501
- if (isInfiniteTimestamp(value))
502
- return value;
503
- const normalized = normalizeTimestamp(inet(value), decoder.withTimezone);
504
- return normalized instanceof Date ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
505
- };
504
+ normalize = (value) => isInfiniteTimestamp(value) ? value : normalizeTimestamp(value, decoder.withTimezone);
506
505
  } else if (is(decoder, PgDateString)) {
507
506
  normalize = normalizeDateString;
508
507
  } else if (is(decoder, PgDate)) {
@@ -512,7 +511,30 @@ function compileValueMapper(decoder) {
512
511
  } else if (is(decoder, PgInterval)) {
513
512
  normalize = normalizeInterval;
514
513
  }
515
- return (value) => value === null ? null : decoder.mapFromDriverValue(toDecoderInput(decoder, normalize(inet(value))));
514
+ return (value) => value === null ? null : normalize(inet(value));
515
+ }
516
+ function compileValueMapper(decoder) {
517
+ const normalize = compileValueNormalizer(decoder);
518
+ if (is(decoder, PgTimestamp)) {
519
+ return (value) => {
520
+ const normalized = normalize(value);
521
+ return normalized === null || normalized instanceof Date || isInfiniteTimestamp(normalized) ? normalized : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
522
+ };
523
+ }
524
+ return (value) => {
525
+ const normalized = normalize(value);
526
+ return normalized === null ? null : decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
527
+ };
528
+ }
529
+ function compileRelationalRowNormalizer(selection) {
530
+ const normalizers = selection.map((item) => item.isJson ? undefined : compileValueNormalizer(resolveFieldDecoder(item.field)));
531
+ return (row) => {
532
+ for (let index = 0;index < normalizers.length; index += 1) {
533
+ const normalize = normalizers[index];
534
+ if (normalize)
535
+ row[index] = normalize(row[index]);
536
+ }
537
+ };
516
538
  }
517
539
  function assignResultPath(result, path, value) {
518
540
  if (path.length === 0) {
@@ -1815,6 +1837,10 @@ function transformWithAst(query, {
1815
1837
  // src/dialect.ts
1816
1838
  var PG_JSON_UNSUPPORTED_MESSAGE = "Pg JSON/JSONB columns are not supported in DuckDB. Replace them with duckDbJson() to use DuckDB's native JSON type.";
1817
1839
  var DUCKDB_SCALAR_COLUMN_TYPING = "duckdb:scalar";
1840
+ var relationalSelections = new WeakMap;
1841
+ function relationalSelectionOf(query) {
1842
+ return relationalSelections.get(query);
1843
+ }
1818
1844
  var DUCKDB_ARRAY_COLUMN_TYPING = "duckdb:array";
1819
1845
  var MIGRATION_CONFLICT_MAX_ATTEMPTS = 10;
1820
1846
  var MIGRATION_RETRY_BASE_DELAY_MS = 20;
@@ -1871,10 +1897,11 @@ function rewriteRelationalJsonChunks(chunks, exactDecimals = false) {
1871
1897
  const replacement = value === undefined ? undefined : RELATIONAL_JSON_CHUNKS.get(value);
1872
1898
  const next = chunks[index + 1];
1873
1899
  if (replacement) {
1874
- if (exactDecimals && value === "json_build_array(" && is2(next, SQL2)) {
1900
+ if (value === "json_build_array(" && is2(next, SQL2)) {
1875
1901
  const fields = next.queryChunks.map((item) => {
1876
- if (!is2(item, Column2))
1902
+ if (!is2(item, Column2) || !exactDecimals && !item.columnType.startsWith("PgNumeric")) {
1877
1903
  return item;
1904
+ }
1878
1905
  const originalType = item.getSQLType();
1879
1906
  const exactType = decimalStringType(originalType);
1880
1907
  return exactType === originalType ? item : sql2`cast(${item} as ${sql2.raw(exactType)})`;
@@ -2032,6 +2059,7 @@ class DuckDBDialect extends PgDialect {
2032
2059
  const result = super.buildRelationalQueryWithoutPK(config);
2033
2060
  if (!config.nestedQueryRelation && is2(result.sql, SQL2)) {
2034
2061
  rewriteRelationalJsonChunks(result.sql.queryChunks, this.options.decimalMode === "string");
2062
+ relationalSelections.set(result.sql, result.selection);
2035
2063
  }
2036
2064
  return result;
2037
2065
  }
@@ -2040,10 +2068,14 @@ class DuckDBDialect extends PgDialect {
2040
2068
  const transformed = transformSQL(result.sql, {
2041
2069
  qualifyJoinColumns: this.options.qualifyRawJoinColumns === true
2042
2070
  });
2043
- return {
2071
+ const query = {
2044
2072
  ...result,
2045
2073
  sql: transformed.sql
2046
2074
  };
2075
+ const selection = relationalSelections.get(sqlObj);
2076
+ if (selection)
2077
+ relationalSelections.set(query, selection);
2078
+ return query;
2047
2079
  }
2048
2080
  }
2049
2081
 
@@ -2169,7 +2201,7 @@ function wrapJson(data) {
2169
2201
  }
2170
2202
 
2171
2203
  // src/value-wrappers.ts
2172
- function dateToMicros(value) {
2204
+ function dateToMicros2(value) {
2173
2205
  if (value instanceof Date) {
2174
2206
  return dateToEpochMicros(value);
2175
2207
  }
@@ -2227,7 +2259,7 @@ function wrapperToNodeApiValue(wrapper, toValue) {
2227
2259
  return withNodeApiItemTypeHint(mapValue(convertMapEntries(wrapper.data, toValue, valueType)), valueType);
2228
2260
  }
2229
2261
  case "timestamp":
2230
- return wrapper.withTimezone ? timestampTZValue(dateToMicros(wrapper.data)) : timestampValue(dateToMicros(wrapper.data));
2262
+ return wrapper.withTimezone ? timestampTZValue(dateToMicros2(wrapper.data)) : timestampValue(dateToMicros2(wrapper.data));
2231
2263
  case "blob":
2232
2264
  return blobValue(toUint8Array(wrapper.data));
2233
2265
  case "json":
@@ -3079,7 +3111,13 @@ function dateFromTimestampUnits(units, unitsPerMilli, typeName) {
3079
3111
  if (millis < -MAX_DATE_MILLIS || millis > MAX_DATE_MILLIS) {
3080
3112
  throw new Error(`${typeName} value out of range for JS Date: ${units}`);
3081
3113
  }
3082
- return new Date(Number(millis));
3114
+ const date = new Date(Number(millis));
3115
+ if (unitsPerMilli === 1000n) {
3116
+ const dropped = units - millis * 1000n;
3117
+ if (dropped !== 0n)
3118
+ rememberDroppedMicros(date, Number(dropped));
3119
+ }
3120
+ return date;
3083
3121
  }
3084
3122
  var JSValueConverter = (value, type, converter) => {
3085
3123
  if (value !== null && typeof value === "object") {
@@ -3964,9 +4002,11 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3964
4002
  onStatementError;
3965
4003
  decimalMode;
3966
4004
  waitForTransactions;
4005
+ relationalSelection;
3967
4006
  static [entityKind3] = "DuckDBPreparedQuery";
3968
4007
  resultMapper;
3969
- constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false) {
4008
+ relationalRowNormalizer;
4009
+ constructor(client, dialect, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, rejectStringArrayLiterals, prepareCache, queryMetadata, cacheConfig, warnOnStringArrayLiteral, scalarParamIndexes, onStatementError, decimalMode = "number", waitForTransactions = false, relationalSelection) {
3970
4010
  super(...[
3971
4011
  { sql: queryString, params },
3972
4012
  undefined,
@@ -3988,6 +4028,7 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
3988
4028
  this.onStatementError = onStatementError;
3989
4029
  this.decimalMode = decimalMode;
3990
4030
  this.waitForTransactions = waitForTransactions;
4031
+ this.relationalSelection = relationalSelection;
3991
4032
  }
3992
4033
  async execute(placeholderValues = {}) {
3993
4034
  try {
@@ -4019,17 +4060,24 @@ class DuckDBPreparedQuery extends PgPreparedQuery {
4019
4060
  });
4020
4061
  return rows2;
4021
4062
  }
4063
+ const relationalSelection = customResultMapper ? this.relationalSelection : undefined;
4022
4064
  const { rows, exactDecimalColumns } = await executeArraysOnClient(this.client, queryString, params, {
4023
4065
  prepareCache: this.prepareCache,
4024
- exactDecimals: !customResultMapper,
4066
+ exactDecimals: !customResultMapper || relationalSelection !== undefined,
4025
4067
  decimalMode: this.decimalMode
4026
4068
  });
4069
+ const decimalFields = relationalSelection ?? fields;
4070
+ if (exactDecimalColumns && decimalFields && this.decimalMode !== "string") {
4071
+ restoreNonNumericDecimals(rows, decimalFields, exactDecimalColumns);
4072
+ }
4027
4073
  if (customResultMapper) {
4074
+ if (relationalSelection) {
4075
+ this.relationalRowNormalizer ??= compileRelationalRowNormalizer(relationalSelection);
4076
+ for (const row of rows)
4077
+ this.relationalRowNormalizer(row);
4078
+ }
4028
4079
  return customResultMapper(rows);
4029
4080
  }
4030
- if (exactDecimalColumns && this.decimalMode !== "string") {
4031
- restoreNonNumericDecimals(rows, fields, exactDecimalColumns);
4032
- }
4033
4081
  this.resultMapper ??= compileResultMapper(fields, joinsNotNullableMap);
4034
4082
  return rows.map(this.resultMapper);
4035
4083
  }
@@ -4070,7 +4118,7 @@ class DuckDBSession extends PgSession {
4070
4118
  };
4071
4119
  }
4072
4120
  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
4073
- return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions());
4121
+ return new DuckDBPreparedQuery(this.client, this.dialect, query.sql, bindEmptyArrays(query.params, query.params, query.typings), this.logger, fields, isResponseInArrayMode, customResultMapper, this.rejectStringArrayLiterals, this.prepareCache, queryMetadata, cacheConfig, this.rejectStringArrayLiterals ? undefined : this.warnOnStringArrayLiteral, getScalarParamIndexes(query.params, query.typings), this.statementFailures || this.waitsForTransactions() ? this.recordStatementFailure : undefined, this.options.decimalMode, this.waitsForTransactions(), relationalSelectionOf(query));
4074
4122
  }
4075
4123
  waitsForTransactions() {
4076
4124
  return !this.statementFailures && !isPool(this.client);
@@ -4438,8 +4486,12 @@ function resolvePoolSize(pool) {
4438
4486
  return false;
4439
4487
  if (pool === undefined)
4440
4488
  return DEFAULT_POOL_SIZE;
4441
- if (typeof pool === "string")
4442
- return POOL_PRESETS[pool] ?? DEFAULT_POOL_SIZE;
4489
+ if (typeof pool === "string") {
4490
+ if (!Object.hasOwn(POOL_PRESETS, pool)) {
4491
+ throw new Error(`Unknown pool preset ${JSON.stringify(pool)}. Expected one of: ${Object.keys(POOL_PRESETS).join(", ")}.`);
4492
+ }
4493
+ return POOL_PRESETS[pool];
4494
+ }
4443
4495
  return resolveSize(pool.size);
4444
4496
  }
4445
4497
  var POOL_CLOSED_MESSAGE = "DuckDB connection pool is closed";
@@ -5758,7 +5810,7 @@ function timestampLiteral(value, withTimezone) {
5758
5810
  }
5759
5811
  function timestampModeString(value, withTimezone) {
5760
5812
  if (value instanceof Date) {
5761
- return timestampStringFromMicros(BigInt(value.getTime()) * 1000n, withTimezone);
5813
+ return timestampStringFromMicros(dateToMicros(value), withTimezone);
5762
5814
  }
5763
5815
  if (value !== null && typeof value === "object" && "micros" in value && typeof value.micros === "bigint") {
5764
5816
  return timestampStringFromMicros(value.micros, withTimezone);
@@ -6390,26 +6442,24 @@ function emitConstraints(plan, plans, tableIdentifiers) {
6390
6442
  const param = allocateIdentifier("t", referencedTables);
6391
6443
  const ownColumns = (columns) => columns.map((col) => memberAccess(param, plan.columnProperties.get(col) ?? columnProperty(col))).join(", ");
6392
6444
  const entries = [];
6393
- const usedKeys = new Set;
6394
6445
  for (const constraint of constraints) {
6395
- const key = allocateIdentifier(toIdentifier(constraint.name || `${table.name}_constraint`), usedKeys);
6396
6446
  const name = JSON.stringify(constraint.name);
6397
6447
  if (constraint.type === "PRIMARY KEY") {
6398
- entries.push(`${key}: primaryKey({ columns: [${ownColumns(constraint.columns)}], name: ${name} })`);
6448
+ entries.push(`primaryKey({ columns: [${ownColumns(constraint.columns)}], name: ${name} })`);
6399
6449
  } else if (constraint.type === "UNIQUE") {
6400
- entries.push(`${key}: unique(${name}).on(${ownColumns(constraint.columns)})`);
6450
+ entries.push(`unique(${name}).on(${ownColumns(constraint.columns)})`);
6401
6451
  } else {
6402
6452
  const target = resolveTarget(constraint);
6403
6453
  const targetVar = target.key === selfKey ? param : target.identifier;
6404
6454
  const foreignColumns = constraint.referencedTable.columns.map((col) => memberAccess(targetVar, target.columnProperties?.get(col) ?? columnProperty(col))).join(", ");
6405
- entries.push(`${key}: foreignKey({ columns: [${ownColumns(constraint.columns)}], foreignColumns: [${foreignColumns}], name: ${name} })`);
6455
+ entries.push(`foreignKey({ columns: [${ownColumns(constraint.columns)}], foreignColumns: [${foreignColumns}], name: ${name} })`);
6406
6456
  }
6407
6457
  }
6408
- const lines = [`(${param}) => ({`];
6458
+ const lines = [`(${param}) => [`];
6409
6459
  for (const entry of entries) {
6410
6460
  lines.push(` ${entry},`);
6411
6461
  }
6412
- lines.push("})");
6462
+ lines.push("]");
6413
6463
  return lines.join(`
6414
6464
  `);
6415
6465
  }
package/dist/session.d.ts CHANGED
@@ -10,6 +10,7 @@ import type { RelationalSchemaConfig, TablesRelationalConfig } from 'drizzle-orm
10
10
  import { type Query, SQL, type SQLWrapper } from 'drizzle-orm/sql/sql';
11
11
  import type { WithSubquery } from 'drizzle-orm/subquery';
12
12
  import type { Assume } from 'drizzle-orm/utils';
13
+ import { type RelationalSelectionItem } from './sql/result-mapper.ts';
13
14
  import type { DuckDBDialect } from './dialect.ts';
14
15
  import type { DuckDBClientLike, RowData } from './client.ts';
15
16
  import { type ExecuteBatchesRawChunk, type ExecuteInBatchesOptions } from './client.ts';
@@ -42,9 +43,11 @@ export declare class DuckDBPreparedQuery<T extends PreparedQueryConfig> extends
42
43
  private onStatementError?;
43
44
  private decimalMode;
44
45
  private waitForTransactions;
46
+ private relationalSelection?;
45
47
  static readonly [entityKind]: string;
46
48
  private resultMapper?;
47
- constructor(client: DuckDBClientLike, dialect: DuckDBDialect, queryString: string, params: unknown[], logger: Logger, fields: SelectedFieldsOrdered | undefined, _isResponseInArrayMode: boolean, customResultMapper: ((rows: unknown[][]) => T['execute']) | undefined, rejectStringArrayLiterals: boolean, prepareCache: PreparedStatementCacheConfig | undefined, queryMetadata?: QueryMetadata, cacheConfig?: CacheConfig, warnOnStringArrayLiteral?: ((sql: string) => void) | undefined, scalarParamIndexes?: ReadonlySet<number> | undefined, onStatementError?: ((error: unknown) => void) | undefined, decimalMode?: 'number' | 'string', waitForTransactions?: boolean);
49
+ private relationalRowNormalizer?;
50
+ constructor(client: DuckDBClientLike, dialect: DuckDBDialect, queryString: string, params: unknown[], logger: Logger, fields: SelectedFieldsOrdered | undefined, _isResponseInArrayMode: boolean, customResultMapper: ((rows: unknown[][]) => T['execute']) | undefined, rejectStringArrayLiterals: boolean, prepareCache: PreparedStatementCacheConfig | undefined, queryMetadata?: QueryMetadata, cacheConfig?: CacheConfig, warnOnStringArrayLiteral?: ((sql: string) => void) | undefined, scalarParamIndexes?: ReadonlySet<number> | undefined, onStatementError?: ((error: unknown) => void) | undefined, decimalMode?: 'number' | 'string', waitForTransactions?: boolean, relationalSelection?: readonly RelationalSelectionItem[] | undefined);
48
51
  execute(placeholderValues?: Record<string, unknown> | undefined): Promise<T['execute']>;
49
52
  private executeQuery;
50
53
  all(placeholderValues?: Record<string, unknown> | undefined): Promise<T['all']>;
@@ -11,7 +11,24 @@ export declare function normalizeTimestamp(value: unknown, withTimezone: boolean
11
11
  export declare function normalizeDateString(value: unknown): string | unknown;
12
12
  export declare function normalizeDateValue(value: unknown): Date | unknown;
13
13
  export declare function normalizeTime(value: unknown): string | unknown;
14
+ /**
15
+ * DuckDB returns INTERVAL values as `{ months, days, micros }`. Format them
16
+ * the way DuckDB casts an INTERVAL to VARCHAR, for example
17
+ * `1 year 2 months 3 days 04:05:06`, which DuckDB and Postgres parse back.
18
+ */
14
19
  export declare function normalizeInterval(value: unknown): string | unknown;
20
+ /**
21
+ * Normalize the top-level values of relational query rows (`db.query`) in
22
+ * place. Drizzle decodes them afterwards with each field's decoder, so they
23
+ * must arrive the way flat selects see them. JSON relation fields are left
24
+ * alone.
25
+ */
26
+ export declare function compileRelationalRowNormalizer(selection: readonly RelationalSelectionItem[]): (row: unknown[]) => void;
27
+ /** The part of Drizzle's relational query selection the driver reads. */
28
+ export interface RelationalSelectionItem {
29
+ isJson: boolean;
30
+ field: unknown;
31
+ }
15
32
  export declare function mapResultRow<TResult>(columns: SelectedFieldsOrdered<AnyColumn>, row: unknown[], joinsNotNullableMap: Record<string, boolean> | undefined): TResult;
16
33
  /** Resolve stable field metadata once, before mapping any result rows. */
17
34
  export declare function compileResultMapper<TResult>(columns: SelectedFieldsOrdered<AnyColumn>, joinsNotNullableMap?: Record<string, boolean>): (row: unknown[]) => TResult;
package/dist/time.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ export declare function rememberDroppedMicros(date: Date, micros: number): void;
2
+ /** Microseconds below the Date's millisecond, as read from DuckDB. */
3
+ export declare function dateSubMillisecondMicros(date: Date): number;
4
+ /** Microseconds since the Unix epoch, including digits a Date drops. */
5
+ export declare function dateToMicros(date: Date): bigint;
1
6
  /**
2
7
  * Format bigint TIME microseconds as HH:MM:SS.mmm, adding the microsecond
3
8
  * digits only when the value has sub-millisecond precision.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@leonardovida-md/drizzle-neo-duckdb",
3
3
  "main": "./dist/index.mjs",
4
4
  "types": "./dist/index.d.ts",
5
- "version": "2.2.0",
5
+ "version": "2.3.0",
6
6
  "description": "A drizzle ORM client for use with DuckDB. Based on drizzle's Postgres client.",
7
7
  "type": "module",
8
8
  "scripts": {