kern-orm 0.3.0 → 0.4.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.
@@ -1,2 +1,2 @@
1
- import { A as TestFixture, M as TestHooks, N as TestTransactionOptions, O as OpenedTestFixture, P as createTestHelpers, j as TestHelpers, k as PendingTestFixture } from "./index-CNqYlONX.mjs";
1
+ import { G as OpenedTestFixture, J as TestHelpers, K as PendingTestFixture, X as TestTransactionOptions, Y as TestHooks, Z as createTestHelpers, q as TestFixture } from "./index--0tyG7sU.mjs";
2
2
  export { type OpenedTestFixture, type PendingTestFixture, type TestFixture, type TestHelpers, type TestHooks, type TestTransactionOptions, createTestHelpers };
@@ -346,6 +346,20 @@ function toValueNode(value, type) {
346
346
  type
347
347
  });
348
348
  }
349
+ /** Converts an operand into a node as `toValueNode` does, casting a plain value to `type`, so that its parameter has the type even where nothing else in the statement determines it. */
350
+ function toCastValueNode(value, type) {
351
+ const node = toValueNode(value, type);
352
+ return node.kind === "param" ? createNode({
353
+ kind: "cast",
354
+ operand: node,
355
+ type
356
+ }) : node;
357
+ }
358
+ /** Converts an operand of the function `name` that skips `NULL`, such as the fallback of `coalesce`, into a node as `toValueNode` does, rejecting a plain `null` and `undefined`, which would have no effect or no value. */
359
+ function toFunctionOperandNode(value, type, name) {
360
+ if (value === null || value === void 0) throw new TypeError(`${name} takes a value other than ${String(value)}`);
361
+ return toValueNode(value, type);
362
+ }
349
363
  /** Creates an `IS NULL` node directly, so that resolving an object condition never calls an `isNull` operator replaced by a type. */
350
364
  function createIsNullNode(operand) {
351
365
  return createNode({
@@ -378,19 +392,58 @@ const orderingOperators = {
378
392
  function createComparison(operator) {
379
393
  return defineGenericOperator((self, value) => compare(self, operator, value));
380
394
  }
395
+ // src/type/number.ts
396
+ /** Defines a type of numbers with the operators of the built-in ones. */
397
+ function defineNumberType(sqlType) {
398
+ return defineType({ sqlType }).transform({
399
+ decode: Number,
400
+ encode: (value) => String(value)
401
+ }).withOperators(orderingOperators);
402
+ }
403
+ let integerType;
404
+ /** Returns the shared `integer` type used by built-in expressions and columns. */
405
+ function getIntegerType() {
406
+ integerType ??= defineNumberType("integer");
407
+ return integerType;
408
+ }
409
+ // src/type/text.ts
410
+ /** Defines a text type with the operators of the built-in text types. */
411
+ function defineTextType(sqlType, castType) {
412
+ return defineType({
413
+ sqlType,
414
+ castType
415
+ }).withOperators(orderingOperators).withOperators(textOperators);
416
+ }
417
+ let textType;
418
+ /** Returns the shared `text` type used by built-in expressions and columns. */
419
+ function getTextType() {
420
+ textType ??= defineTextType("text");
421
+ return textType;
422
+ }
381
423
  // src/expression/operators/text.ts
382
424
  /**
383
- * Operators of text types: pattern matching with `LIKE` and `ILIKE`.
425
+ * Operators of text types: pattern matching with `LIKE` and `ILIKE`, `lower` and `upper`, which convert the case of the text, `trim`, which removes spaces from both of its ends, and `length`, which counts its characters.
384
426
  *
385
427
  * A string pattern is sent as it is; `param(type, pattern)` encodes it with a type.
386
428
  */
387
429
  const textOperators = {
388
430
  like: /* @__PURE__ */ createPatternComparison("LIKE"),
389
- ilike: /* @__PURE__ */ createPatternComparison("ILIKE")
431
+ ilike: /* @__PURE__ */ createPatternComparison("ILIKE"),
432
+ lower: /* @__PURE__ */ createTextConversion("lower"),
433
+ upper: /* @__PURE__ */ createTextConversion("upper"),
434
+ trim: /* @__PURE__ */ createTextConversion("btrim"),
435
+ length: /* @__PURE__ */ defineGenericOperator((self) => callTextFunction("length", self, getIntegerType()))
390
436
  };
391
437
  function createPatternComparison(operator) {
392
438
  return defineGenericOperator((self, pattern) => toBooleanExpression(compareNodes(operator, toNode(self), pattern, void 0)));
393
439
  }
440
+ function createTextConversion(name) {
441
+ return defineGenericOperator((self) => callTextFunction(name, self, getTextType()));
442
+ }
443
+ /** Calls a SQL function of text whose result has `type`, whatever the type of the expression: a decoder of a transformed type expects values that it encoded rather than results of the function. */
444
+ function callTextFunction(name, self, type) {
445
+ return toExpression(createList(`${name}(`, [toNode(self)], ")"), type);
446
+ }
394
447
  // src/type/codecs/bytea.ts
395
448
  const hexDigits = "0123456789abcdef";
396
449
  /** Encodes bytes as a hex `bytea` literal, such as `\\x00ff`. */
@@ -425,16 +478,10 @@ function formatTypeName(name, ...parameters) {
425
478
  return defined.length === 0 ? name : `${name}(${defined.join(",")})`;
426
479
  }
427
480
  // src/type/builtins/text.ts
428
- function defineTextType(sqlType, castType) {
429
- return defineType({
430
- sqlType,
431
- castType
432
- }).withOperators(orderingOperators).withOperators(textOperators);
433
- }
434
481
  /** Variable-length string without a limit. */
435
- const text = /* @__PURE__ */ defineTextType("text");
482
+ const text = /* @__PURE__ */ getTextType();
436
483
  /** Variable-length string; `varchar.with(length)` limits its length. */
437
484
  const varchar = /* @__PURE__ */ defineTextType("varchar").withParameters((length) => formatTypeName("varchar", length));
438
485
  /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
439
486
  const char = /* @__PURE__ */ defineTextType("char", "bpchar").withParameters((length) => formatTypeName("char", length));
440
- export { createList as A, isType as C, isEqualityCompatible as D, hasEquality as E, oneNode as F, trueNode as I, createSelectNode as M, falseNode as N, valuesQueryNode as O, noFilters as P, defineType as S, equalityOperators as T, toBooleanExpression as _, stringifyJson as a, toValueNode as b, textOperators as c, compareNodes as d, createIsNullNode as f, operandNode as g, isOperandSource as h, formatTypeName as i, createNode as j, defineGenericOperator as k, orderingOperators as l, isExpression as m, text as n, decodeBytea as o, getType as p, varchar as r, encodeBytea as s, char as t, compare as u, toExpression as v, Column as w, getBooleanType as x, toNode as y };
487
+ export { hasEquality as A, trueNode as B, toNode as C, isType as D, defineType as E, createNode as F, createSelectNode as I, falseNode as L, valuesQueryNode as M, defineGenericOperator as N, Column as O, createList as P, noFilters as R, toFunctionOperandNode as S, getBooleanType as T, isOperandSource as _, stringifyJson as a, toCastValueNode as b, textOperators as c, orderingOperators as d, compare as f, isExpression as g, getType as h, formatTypeName as i, isEqualityCompatible as j, equalityOperators as k, defineNumberType as l, createIsNullNode as m, text as n, decodeBytea as o, compareNodes as p, varchar as r, encodeBytea as s, char as t, getIntegerType as u, operandNode as v, toValueNode as w, toExpression as x, toBooleanExpression as y, oneNode as z };
package/dist/types.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { Ar as OperatorDefinitions, Ct as EqualityOperator, Et as InSignature, Ln as ComparisonSignature, Or as MergeOperators, Tt as HasEquality, c as PatternSignature, d as AnyExpression, hr as Type, jr as GenericOperator, kr as NoOperators, pr as BaseType, wt as EqualityOperators } from "./index-CNqYlONX.mjs";
1
+ import { Bt as EqualityOperator, C as TextOperators, E as NumberTypeOperators, Fr as Type, Ht as HasEquality, Jr as MergeOperators, Nr as BaseType, O as AnyExpression, Ut as InSignature, Vt as EqualityOperators, Xr as OperatorDefinitions, Yr as NoOperators, Zr as GenericOperator, rr as ComparisonSignature, w as TextTypeOperators } from "./index--0tyG7sU.mjs";
2
2
  /**
3
3
  * Creates a one-dimensional array type of `element` values.
4
4
  *
@@ -111,15 +111,7 @@ export declare const smallint: BaseType<number, number, MergeOperators<NoOperato
111
111
  in: EqualityOperator<InSignature>;
112
112
  }>, undefined>;
113
113
  /** 4-byte integer as a `number`. */
114
- export declare const integer: BaseType<number, number, MergeOperators<NoOperators, {
115
- lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
116
- lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
117
- gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
118
- gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
119
- eq: EqualityOperator<ComparisonSignature>;
120
- ne: EqualityOperator<ComparisonSignature>;
121
- in: EqualityOperator<InSignature>;
122
- }>, undefined>;
114
+ export declare const integer: BaseType<number, number, NumberTypeOperators, undefined>;
123
115
  /** 8-byte integer as a `bigint`, which keeps values beyond `Number.MAX_SAFE_INTEGER`; accepts a `number` too. */
124
116
  export declare const bigint: BaseType<bigint, number | bigint, MergeOperators<NoOperators, {
125
117
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
@@ -161,18 +153,7 @@ export declare const numeric: Type<string, string | number | bigint, MergeOperat
161
153
  in: EqualityOperator<InSignature>;
162
154
  }>, [precision: number, scale?: number | undefined]>;
163
155
  /** Variable-length string without a limit. */
164
- export declare const text: BaseType<string, string, MergeOperators<MergeOperators<NoOperators, {
165
- lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
166
- lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
167
- gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
168
- gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
169
- eq: EqualityOperator<ComparisonSignature>;
170
- ne: EqualityOperator<ComparisonSignature>;
171
- in: EqualityOperator<InSignature>;
172
- }>, {
173
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
174
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
175
- }>, undefined>;
156
+ export declare const text: BaseType<string, string, TextTypeOperators, undefined>;
176
157
  /** Variable-length string; `varchar.with(length)` limits its length. */
177
158
  export declare const varchar: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
178
159
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
@@ -182,10 +163,7 @@ export declare const varchar: Type<string, string, MergeOperators<MergeOperators
182
163
  eq: EqualityOperator<ComparisonSignature>;
183
164
  ne: EqualityOperator<ComparisonSignature>;
184
165
  in: EqualityOperator<InSignature>;
185
- }>, {
186
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
187
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
188
- }>, [length: number]>;
166
+ }>, TextOperators>, [length: number]>;
189
167
  /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
190
168
  export declare const char: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
191
169
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
@@ -195,10 +173,7 @@ export declare const char: Type<string, string, MergeOperators<MergeOperators<No
195
173
  eq: EqualityOperator<ComparisonSignature>;
196
174
  ne: EqualityOperator<ComparisonSignature>;
197
175
  in: EqualityOperator<InSignature>;
198
- }>, {
199
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
200
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
201
- }>, [length: number]>;
176
+ }>, TextOperators>, [length: number]>;
202
177
  /** UUID as a string. */
203
178
  export declare const uuid: BaseType<string, string, MergeOperators<NoOperators, {
204
179
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
package/dist/types.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { E as hasEquality, S as defineType, T as equalityOperators, a as stringifyJson, i as formatTypeName, l as orderingOperators, n as text, o as decodeBytea, r as varchar, s as encodeBytea, t as char, x as getBooleanType } from "./text-BUNl8wpl.mjs";
1
+ import { A as hasEquality, E as defineType, T as getBooleanType, a as stringifyJson, d as orderingOperators, i as formatTypeName, k as equalityOperators, l as defineNumberType, n as text, o as decodeBytea, r as varchar, s as encodeBytea, t as char, u as getIntegerType } from "./text-BTFErHIi.mjs";
2
2
  // src/type/builtins/array.ts
3
3
  /**
4
4
  * Creates a one-dimensional array type of `element` values.
@@ -224,16 +224,10 @@ const inet = /* @__PURE__ */ defineType({ sqlType: "inet" }).withOperators(order
224
224
  /** IP network as a string, such as `10.0.0.0/8`. */
225
225
  const cidr = /* @__PURE__ */ defineType({ sqlType: "cidr" }).withOperators(orderingOperators);
226
226
  // src/type/builtins/numeric.ts
227
- function defineNumberType(sqlType) {
228
- return defineType({ sqlType }).transform({
229
- decode: Number,
230
- encode: (value) => String(value)
231
- }).withOperators(orderingOperators);
232
- }
233
227
  /** 2-byte integer as a `number`. */
234
228
  const smallint = /* @__PURE__ */ defineNumberType("smallint");
235
229
  /** 4-byte integer as a `number`. */
236
- const integer = /* @__PURE__ */ defineNumberType("integer");
230
+ const integer = /* @__PURE__ */ getIntegerType();
237
231
  /** 8-byte integer as a `bigint`, which keeps values beyond `Number.MAX_SAFE_INTEGER`; accepts a `number` too. */
238
232
  const bigint = /* @__PURE__ */ defineType({ sqlType: "bigint" }).transform({
239
233
  decode: BigInt,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kern-orm",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Fully typed relational query builder for PostgreSQL with ambient transactions and single-query relation loading",
5
5
  "keywords": [
6
6
  "orm",
@@ -42,8 +42,11 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@kern-orm/postgres-js": "workspace:*",
45
- "kern-orm-minimum": "https://registry.npmjs.org/kern-orm/-/kern-orm-0.2.0.tgz",
45
+ "kern-orm-0.2.0": "https://registry.npmjs.org/kern-orm/-/kern-orm-0.2.0.tgz",
46
+ "kern-orm-0.3.0": "https://registry.npmjs.org/kern-orm/-/kern-orm-0.3.0.tgz",
46
47
  "postgres": "catalog:",
48
+ "postgres-js-published": "https://registry.npmjs.org/@kern-orm/postgres-js/-/postgres-js-0.1.2.tgz",
49
+ "postgres-js-v3-published": "https://registry.npmjs.org/@kern-orm/postgres-js-v3/-/postgres-js-v3-0.1.2.tgz",
47
50
  "typescript-6": "catalog:"
48
51
  },
49
52
  "engines": {