turbine-orm 0.93.0 → 0.93.1

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.
@@ -842,6 +842,39 @@ export declare function jsonPathParam(qi: BuilderCtx, path: readonly (string | n
842
842
  * SQL Server have no `::` operator) as a float cast.
843
843
  */
844
844
  export declare function castJsonNumeric(qi: BuilderCtx, extract: string): string;
845
+ /**
846
+ * The text every supported PostgreSQL version (14 to 17) reads as a numeric:
847
+ * a signed decimal with an optional fraction and exponent, or a signed
848
+ * `inf` / `infinity`, or an unsigned `NaN` (a signed one is refused), with
849
+ * surrounding white space. The white space is exactly what the numeric parser
850
+ * skips, the six ASCII characters C's `isspace` names, NOT `[[:space:]]`: in a
851
+ * UTF-8 database that class also matches Unicode spaces such as U+2003, which
852
+ * the parser rejects, so a row holding one would still fail the query.
853
+ *
854
+ * PostgreSQL 16 added digit separators and base prefixes. Those spellings use
855
+ * NUMERIC_TEXT_PATTERN_PG16 instead, selected by the connected server version.
856
+ *
857
+ * Written for an `E'...'` literal: `\t` and friends are escapes there, read
858
+ * the same whatever `standard_conforming_strings` says.
859
+ */
860
+ export declare const NUMERIC_TEXT_PATTERN = "^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$";
861
+ /** PostgreSQL 16+ numeric input grammar, written for an E'...' literal. */
862
+ export declare const NUMERIC_TEXT_PATTERN_PG16 = "^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9](_?[0-9])*([.]([0-9](_?[0-9])*)?)?|[.][0-9](_?[0-9])*)([eE][-+]?[0-9](_?[0-9])*)?|0([xX]_?[0-9A-Fa-f](_?[0-9A-Fa-f])*|[oO]_?[0-7](_?[0-7])*|[bB]_?[01](_?[01])*)|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$";
863
+ /**
864
+ * {@link castJsonNumeric} for a WHERE comparison against a number. On
865
+ * PostgreSQL a plain `(expr)::numeric` raises `invalid input syntax for type
866
+ * numeric` the moment ONE row holds text, a boolean, an object or an array at
867
+ * the path, so a single stray value failed the whole query. Here only text that
868
+ * reads as a number on the connected server is cast; anything else is NULL,
869
+ * and NULL compares as neither true nor false, so that row is not matched.
870
+ * Select the grammar in SQL so the same builder works with PostgreSQL 14/15
871
+ * and 16+, without requiring a version probe before building queries.
872
+ * `extract` is a `#>>` text expression over bound params only, and the pattern
873
+ * is a constant, so no value reaches the SQL text.
874
+ *
875
+ * Other engines keep {@link castJsonNumeric}: their float casts do not raise.
876
+ */
877
+ export declare function castJsonNumericOrNull(qi: BuilderCtx, extract: string): string;
845
878
  /**
846
879
  * Build SQL clauses for Array filter operators on a column.
847
880
  * Supports: has, hasEvery, hasSome, isEmpty.
@@ -12,7 +12,7 @@
12
12
  * primitives this module needs. See builder.ts for the thin delegating methods.
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.TEXT_COMPARED_TYPES = exports.assertJsonFilterKeys = void 0;
15
+ exports.NUMERIC_TEXT_PATTERN_PG16 = exports.NUMERIC_TEXT_PATTERN = exports.TEXT_COMPARED_TYPES = exports.assertJsonFilterKeys = void 0;
16
16
  exports.adviceLogger = adviceLogger;
17
17
  exports.fingerprintWhere = fingerprintWhere;
18
18
  exports.fingerprintRelationParts = fingerprintRelationParts;
@@ -81,6 +81,7 @@ exports.jsonPathEqualsOperand = jsonPathEqualsOperand;
81
81
  exports.buildJsonFilterClauses = buildJsonFilterClauses;
82
82
  exports.jsonPathParam = jsonPathParam;
83
83
  exports.castJsonNumeric = castJsonNumeric;
84
+ exports.castJsonNumericOrNull = castJsonNumericOrNull;
84
85
  exports.buildArrayFilterClauses = buildArrayFilterClauses;
85
86
  exports.buildVectorFilterClauses = buildVectorFilterClauses;
86
87
  exports.vectorThresholdEntries = vectorThresholdEntries;
@@ -2367,11 +2368,13 @@ function buildJsonFilterClauses(qi, column, filter, params) {
2367
2368
  clauses.push(`${column} ? ${qi.p(params.length)}`);
2368
2369
  }
2369
2370
  // Range comparisons on the extracted path: numbers compare numerically
2370
- // (cast through the dialect), strings compare as text.
2371
+ // (cast through the dialect), strings compare as text. A row whose value at
2372
+ // the path is not a number is left out of a numeric comparison rather than
2373
+ // failing the whole query (see castJsonNumericOrNull).
2371
2374
  for (const { sqlOp, value } of jsonRangeEntries(qi, filter, column)) {
2372
2375
  const extract = pathExtract();
2373
2376
  params.push(value);
2374
- const lhs = typeof value === 'number' ? castJsonNumeric(qi, extract) : extract;
2377
+ const lhs = typeof value === 'number' ? castJsonNumericOrNull(qi, extract) : extract;
2375
2378
  clauses.push(`${lhs} ${sqlOp} ${qi.p(params.length)}`);
2376
2379
  }
2377
2380
  // Substring comparisons on the extracted path. The operand is LIKE-escaped
@@ -2421,6 +2424,51 @@ function castJsonNumeric(qi, extract) {
2421
2424
  return `(${extract})::numeric`;
2422
2425
  return qi.dialect.castAggregate ? qi.dialect.castAggregate(`(${extract})`, 'float') : `(${extract})::numeric`;
2423
2426
  }
2427
+ /**
2428
+ * The text every supported PostgreSQL version (14 to 17) reads as a numeric:
2429
+ * a signed decimal with an optional fraction and exponent, or a signed
2430
+ * `inf` / `infinity`, or an unsigned `NaN` (a signed one is refused), with
2431
+ * surrounding white space. The white space is exactly what the numeric parser
2432
+ * skips, the six ASCII characters C's `isspace` names, NOT `[[:space:]]`: in a
2433
+ * UTF-8 database that class also matches Unicode spaces such as U+2003, which
2434
+ * the parser rejects, so a row holding one would still fail the query.
2435
+ *
2436
+ * PostgreSQL 16 added digit separators and base prefixes. Those spellings use
2437
+ * NUMERIC_TEXT_PATTERN_PG16 instead, selected by the connected server version.
2438
+ *
2439
+ * Written for an `E'...'` literal: `\t` and friends are escapes there, read
2440
+ * the same whatever `standard_conforming_strings` says.
2441
+ */
2442
+ exports.NUMERIC_TEXT_PATTERN = '^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$';
2443
+ // Separators must occur between digits; base prefixes also allow one leading
2444
+ // separator (0x_FF). A point or exponent sign cannot adjoin an underscore.
2445
+ const PG16_NUMERIC_DIGITS = '[0-9](_?[0-9])*';
2446
+ const PG16_NUMERIC_DECIMAL = `(${PG16_NUMERIC_DIGITS}([.](${PG16_NUMERIC_DIGITS})?)?|[.]${PG16_NUMERIC_DIGITS})` +
2447
+ `([eE][-+]?${PG16_NUMERIC_DIGITS})?`;
2448
+ const PG16_NUMERIC_INTEGER = '0([xX]_?[0-9A-Fa-f](_?[0-9A-Fa-f])*|[oO]_?[0-7](_?[0-7])*|[bB]_?[01](_?[01])*)';
2449
+ /** PostgreSQL 16+ numeric input grammar, written for an E'...' literal. */
2450
+ exports.NUMERIC_TEXT_PATTERN_PG16 = `^[ \\t\\n\\r\\f\\013]*([-+]?(${PG16_NUMERIC_DECIMAL}|${PG16_NUMERIC_INTEGER}|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$`;
2451
+ /**
2452
+ * {@link castJsonNumeric} for a WHERE comparison against a number. On
2453
+ * PostgreSQL a plain `(expr)::numeric` raises `invalid input syntax for type
2454
+ * numeric` the moment ONE row holds text, a boolean, an object or an array at
2455
+ * the path, so a single stray value failed the whole query. Here only text that
2456
+ * reads as a number on the connected server is cast; anything else is NULL,
2457
+ * and NULL compares as neither true nor false, so that row is not matched.
2458
+ * Select the grammar in SQL so the same builder works with PostgreSQL 14/15
2459
+ * and 16+, without requiring a version probe before building queries.
2460
+ * `extract` is a `#>>` text expression over bound params only, and the pattern
2461
+ * is a constant, so no value reaches the SQL text.
2462
+ *
2463
+ * Other engines keep {@link castJsonNumeric}: their float casts do not raise.
2464
+ */
2465
+ function castJsonNumericOrNull(qi, extract) {
2466
+ if (!qi.dialect.capabilities.jsonExactNumericCast)
2467
+ return castJsonNumeric(qi, extract);
2468
+ const pattern = `CASE WHEN current_setting('server_version_num')::integer >= 160000 ` +
2469
+ `THEN E'${exports.NUMERIC_TEXT_PATTERN_PG16}' ELSE E'${exports.NUMERIC_TEXT_PATTERN}' END`;
2470
+ return `CASE WHEN (${extract}) ~ (${pattern}) THEN (${extract})::numeric END`;
2471
+ }
2424
2472
  /**
2425
2473
  * Build SQL clauses for Array filter operators on a column.
2426
2474
  * Supports: has, hasEvery, hasSome, isEmpty.
@@ -842,6 +842,39 @@ export declare function jsonPathParam(qi: BuilderCtx, path: readonly (string | n
842
842
  * SQL Server have no `::` operator) as a float cast.
843
843
  */
844
844
  export declare function castJsonNumeric(qi: BuilderCtx, extract: string): string;
845
+ /**
846
+ * The text every supported PostgreSQL version (14 to 17) reads as a numeric:
847
+ * a signed decimal with an optional fraction and exponent, or a signed
848
+ * `inf` / `infinity`, or an unsigned `NaN` (a signed one is refused), with
849
+ * surrounding white space. The white space is exactly what the numeric parser
850
+ * skips, the six ASCII characters C's `isspace` names, NOT `[[:space:]]`: in a
851
+ * UTF-8 database that class also matches Unicode spaces such as U+2003, which
852
+ * the parser rejects, so a row holding one would still fail the query.
853
+ *
854
+ * PostgreSQL 16 added digit separators and base prefixes. Those spellings use
855
+ * NUMERIC_TEXT_PATTERN_PG16 instead, selected by the connected server version.
856
+ *
857
+ * Written for an `E'...'` literal: `\t` and friends are escapes there, read
858
+ * the same whatever `standard_conforming_strings` says.
859
+ */
860
+ export declare const NUMERIC_TEXT_PATTERN = "^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$";
861
+ /** PostgreSQL 16+ numeric input grammar, written for an E'...' literal. */
862
+ export declare const NUMERIC_TEXT_PATTERN_PG16 = "^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9](_?[0-9])*([.]([0-9](_?[0-9])*)?)?|[.][0-9](_?[0-9])*)([eE][-+]?[0-9](_?[0-9])*)?|0([xX]_?[0-9A-Fa-f](_?[0-9A-Fa-f])*|[oO]_?[0-7](_?[0-7])*|[bB]_?[01](_?[01])*)|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$";
863
+ /**
864
+ * {@link castJsonNumeric} for a WHERE comparison against a number. On
865
+ * PostgreSQL a plain `(expr)::numeric` raises `invalid input syntax for type
866
+ * numeric` the moment ONE row holds text, a boolean, an object or an array at
867
+ * the path, so a single stray value failed the whole query. Here only text that
868
+ * reads as a number on the connected server is cast; anything else is NULL,
869
+ * and NULL compares as neither true nor false, so that row is not matched.
870
+ * Select the grammar in SQL so the same builder works with PostgreSQL 14/15
871
+ * and 16+, without requiring a version probe before building queries.
872
+ * `extract` is a `#>>` text expression over bound params only, and the pattern
873
+ * is a constant, so no value reaches the SQL text.
874
+ *
875
+ * Other engines keep {@link castJsonNumeric}: their float casts do not raise.
876
+ */
877
+ export declare function castJsonNumericOrNull(qi: BuilderCtx, extract: string): string;
845
878
  /**
846
879
  * Build SQL clauses for Array filter operators on a column.
847
880
  * Supports: has, hasEvery, hasSome, isEmpty.
@@ -2290,11 +2290,13 @@ export function buildJsonFilterClauses(qi, column, filter, params) {
2290
2290
  clauses.push(`${column} ? ${qi.p(params.length)}`);
2291
2291
  }
2292
2292
  // Range comparisons on the extracted path: numbers compare numerically
2293
- // (cast through the dialect), strings compare as text.
2293
+ // (cast through the dialect), strings compare as text. A row whose value at
2294
+ // the path is not a number is left out of a numeric comparison rather than
2295
+ // failing the whole query (see castJsonNumericOrNull).
2294
2296
  for (const { sqlOp, value } of jsonRangeEntries(qi, filter, column)) {
2295
2297
  const extract = pathExtract();
2296
2298
  params.push(value);
2297
- const lhs = typeof value === 'number' ? castJsonNumeric(qi, extract) : extract;
2299
+ const lhs = typeof value === 'number' ? castJsonNumericOrNull(qi, extract) : extract;
2298
2300
  clauses.push(`${lhs} ${sqlOp} ${qi.p(params.length)}`);
2299
2301
  }
2300
2302
  // Substring comparisons on the extracted path. The operand is LIKE-escaped
@@ -2344,6 +2346,51 @@ export function castJsonNumeric(qi, extract) {
2344
2346
  return `(${extract})::numeric`;
2345
2347
  return qi.dialect.castAggregate ? qi.dialect.castAggregate(`(${extract})`, 'float') : `(${extract})::numeric`;
2346
2348
  }
2349
+ /**
2350
+ * The text every supported PostgreSQL version (14 to 17) reads as a numeric:
2351
+ * a signed decimal with an optional fraction and exponent, or a signed
2352
+ * `inf` / `infinity`, or an unsigned `NaN` (a signed one is refused), with
2353
+ * surrounding white space. The white space is exactly what the numeric parser
2354
+ * skips, the six ASCII characters C's `isspace` names, NOT `[[:space:]]`: in a
2355
+ * UTF-8 database that class also matches Unicode spaces such as U+2003, which
2356
+ * the parser rejects, so a row holding one would still fail the query.
2357
+ *
2358
+ * PostgreSQL 16 added digit separators and base prefixes. Those spellings use
2359
+ * NUMERIC_TEXT_PATTERN_PG16 instead, selected by the connected server version.
2360
+ *
2361
+ * Written for an `E'...'` literal: `\t` and friends are escapes there, read
2362
+ * the same whatever `standard_conforming_strings` says.
2363
+ */
2364
+ export const NUMERIC_TEXT_PATTERN = '^[ \\t\\n\\r\\f\\013]*([-+]?(([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$';
2365
+ // Separators must occur between digits; base prefixes also allow one leading
2366
+ // separator (0x_FF). A point or exponent sign cannot adjoin an underscore.
2367
+ const PG16_NUMERIC_DIGITS = '[0-9](_?[0-9])*';
2368
+ const PG16_NUMERIC_DECIMAL = `(${PG16_NUMERIC_DIGITS}([.](${PG16_NUMERIC_DIGITS})?)?|[.]${PG16_NUMERIC_DIGITS})` +
2369
+ `([eE][-+]?${PG16_NUMERIC_DIGITS})?`;
2370
+ const PG16_NUMERIC_INTEGER = '0([xX]_?[0-9A-Fa-f](_?[0-9A-Fa-f])*|[oO]_?[0-7](_?[0-7])*|[bB]_?[01](_?[01])*)';
2371
+ /** PostgreSQL 16+ numeric input grammar, written for an E'...' literal. */
2372
+ export const NUMERIC_TEXT_PATTERN_PG16 = `^[ \\t\\n\\r\\f\\013]*([-+]?(${PG16_NUMERIC_DECIMAL}|${PG16_NUMERIC_INTEGER}|[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])?)|[Nn][Aa][Nn])[ \\t\\n\\r\\f\\013]*$`;
2373
+ /**
2374
+ * {@link castJsonNumeric} for a WHERE comparison against a number. On
2375
+ * PostgreSQL a plain `(expr)::numeric` raises `invalid input syntax for type
2376
+ * numeric` the moment ONE row holds text, a boolean, an object or an array at
2377
+ * the path, so a single stray value failed the whole query. Here only text that
2378
+ * reads as a number on the connected server is cast; anything else is NULL,
2379
+ * and NULL compares as neither true nor false, so that row is not matched.
2380
+ * Select the grammar in SQL so the same builder works with PostgreSQL 14/15
2381
+ * and 16+, without requiring a version probe before building queries.
2382
+ * `extract` is a `#>>` text expression over bound params only, and the pattern
2383
+ * is a constant, so no value reaches the SQL text.
2384
+ *
2385
+ * Other engines keep {@link castJsonNumeric}: their float casts do not raise.
2386
+ */
2387
+ export function castJsonNumericOrNull(qi, extract) {
2388
+ if (!qi.dialect.capabilities.jsonExactNumericCast)
2389
+ return castJsonNumeric(qi, extract);
2390
+ const pattern = `CASE WHEN current_setting('server_version_num')::integer >= 160000 ` +
2391
+ `THEN E'${NUMERIC_TEXT_PATTERN_PG16}' ELSE E'${NUMERIC_TEXT_PATTERN}' END`;
2392
+ return `CASE WHEN (${extract}) ~ (${pattern}) THEN (${extract})::numeric END`;
2393
+ }
2347
2394
  /**
2348
2395
  * Build SQL clauses for Array filter operators on a column.
2349
2396
  * Supports: has, hasEvery, hasSome, isEmpty.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbine-orm",
3
- "version": "0.93.0",
3
+ "version": "0.93.1",
4
4
  "description": "Postgres-native TypeScript ORM, runs on Neon, Vercel Postgres, Cloudflare, Supabase. Streaming cursors, typed errors, single-query nested relations. One dependency, no WASM engine",
5
5
  "type": "module",
6
6
  "//exports": "Each subpath declares its types PER CONDITION. A single shared top-level \"types\" resolves to the ESM declarations for `require` too, which is TS1479 (\"is an ES module ... cannot be require()d\") for any CJS consumer on moduleResolution node16/nodenext. The require condition points at dist/cjs, which ships its own {\"type\":\"commonjs\"} package.json, so those declarations are CJS declarations. Gated in CI by publint + @arethetypeswrong/cli + a real .cts consumer typecheck (see the package-types job in ci.yml).",