kern-orm 0.2.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 { C as createTestHelpers, S as TestTransactionOptions, _ as OpenedTestFixture, b as TestHelpers, v as PendingTestFixture, x as TestHooks, y as TestFixture } from "./expression-tYmWRuKR.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 };
@@ -71,6 +71,8 @@ function isGenericOperator(operator) {
71
71
  // src/expression/operators/equality.ts
72
72
  /** Runtime mark of the operators of `equalityOperators`, which tell that a type has SQL `=`. */
73
73
  const equalityBrand = Symbol("kern-orm.equality");
74
+ /** Key of the method of a query of a table that returns its node as the values of `in`, or rejects a query of records. */
75
+ const valuesQueryNode = Symbol("kern-orm.valuesQueryNode");
74
76
  /**
75
77
  * Operators of SQL equality: `eq`, `ne`, and `in`; `orderingOperators` include them.
76
78
  *
@@ -80,6 +82,11 @@ const equalityOperators = {
80
82
  eq: defineEqualityOperator((self, value) => compare(self, "=", value)),
81
83
  ne: defineEqualityOperator((self, value) => compare(self, "<>", value)),
82
84
  in: defineEqualityOperator((self, list) => {
85
+ if (!Array.isArray(list)) return toBooleanExpression(createNode({
86
+ kind: "inQuery",
87
+ operand: toNode(self),
88
+ query: toValuesQueryNode(list)
89
+ }));
83
90
  if (list.length === 0) return toBooleanExpression(falseNode);
84
91
  const type = getType(self);
85
92
  const nodes = [];
@@ -91,6 +98,11 @@ const equalityOperators = {
91
98
  }));
92
99
  })
93
100
  };
101
+ /** Converts a query of values of a table, such as of `pluck`, into the node of the values of `in`, and rejects any other value. */
102
+ function toValuesQueryNode(value) {
103
+ if (typeof value === "object" && value !== null && valuesQueryNode in value) return value[valuesQueryNode]();
104
+ throw new TypeError("in takes an array or a query of values of a table, such as of pluck");
105
+ }
94
106
  /** Tells whether a type has SQL `=`, given by an operator of `equalityOperators`. */
95
107
  function hasEquality(type) {
96
108
  const operators = type.operators;
@@ -258,6 +270,8 @@ function getBooleanType() {
258
270
  // src/expression/expression.ts
259
271
  const expressionNode = Symbol("kern-orm.expression.node");
260
272
  const expressionType = Symbol("kern-orm.expression.type");
273
+ /** Key of the method of an expression and of every class of queries that returns its node as an operand of an expression, or rejects a query that cannot be one. */
274
+ const operandNode = Symbol("kern-orm.operandNode");
261
275
  /**
262
276
  * Runtime class of every expression.
263
277
  *
@@ -267,6 +281,10 @@ var ExpressionObject = class {
267
281
  isNull() {
268
282
  return toBooleanExpression(createIsNullNode(this[expressionNode]));
269
283
  }
284
+ /** Returns the node of the expression as an operand of another expression. */
285
+ [operandNode]() {
286
+ return this[expressionNode];
287
+ }
270
288
  };
271
289
  let untypedPrototype;
272
290
  /** Prototypes of expressions by their types, with the operators of the type as methods. */
@@ -311,14 +329,37 @@ function getType(expression) {
311
329
  function toBooleanExpression(node) {
312
330
  return toExpression(node, getBooleanType());
313
331
  }
314
- /** Converts an expression or a plain value into a node, binding the value as a parameter encoded with `type`. */
332
+ /** Tells whether `value` is an expression or a query, which has the method of `operandNode`, whether or not that method takes it as an operand. */
333
+ function isOperandSource(value) {
334
+ return typeof value === "object" && value !== null && operandNode in value;
335
+ }
336
+ /**
337
+ * Converts an operand into a node: an expression or a query by the method of `operandNode`, or a plain value, bound as a parameter encoded with `type`.
338
+ *
339
+ * The method of a query takes a query of one value of a table, which lowering replaces with a scalar subquery, or with `EXISTS` for a query of `exists`, and rejects another query, such as of records or of `$query`.
340
+ */
315
341
  function toValueNode(value, type) {
316
- return value instanceof ExpressionObject ? value[expressionNode] : createNode({
342
+ if (typeof value === "object" && value !== null && operandNode in value) return value[operandNode]();
343
+ return createNode({
317
344
  kind: "param",
318
345
  value,
319
346
  type
320
347
  });
321
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
+ }
322
363
  /** Creates an `IS NULL` node directly, so that resolving an object condition never calls an `isNull` operator replaced by a type. */
323
364
  function createIsNullNode(operand) {
324
365
  return createNode({
@@ -330,7 +371,7 @@ function createIsNullNode(operand) {
330
371
  function compare(self, operator, value) {
331
372
  return toBooleanExpression(compareNodes(operator, toNode(self), value, getType(self)));
332
373
  }
333
- /** Creates a comparison node, encoding a plain `right` value with `type`. */
374
+ /** Creates a comparison node, encoding a plain `right` value with `type`; a query of one value becomes its node in an expression, as `toValueNode` describes. */
334
375
  function compareNodes(operator, left, right, type) {
335
376
  return createNode({
336
377
  kind: "comparison",
@@ -351,19 +392,58 @@ const orderingOperators = {
351
392
  function createComparison(operator) {
352
393
  return defineGenericOperator((self, value) => compare(self, operator, value));
353
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
+ }
354
423
  // src/expression/operators/text.ts
355
424
  /**
356
- * 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.
357
426
  *
358
427
  * A string pattern is sent as it is; `param(type, pattern)` encodes it with a type.
359
428
  */
360
429
  const textOperators = {
361
430
  like: /* @__PURE__ */ createPatternComparison("LIKE"),
362
- 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()))
363
436
  };
364
437
  function createPatternComparison(operator) {
365
438
  return defineGenericOperator((self, pattern) => toBooleanExpression(compareNodes(operator, toNode(self), pattern, void 0)));
366
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
+ }
367
447
  // src/type/codecs/bytea.ts
368
448
  const hexDigits = "0123456789abcdef";
369
449
  /** Encodes bytes as a hex `bytea` literal, such as `\\x00ff`. */
@@ -398,16 +478,10 @@ function formatTypeName(name, ...parameters) {
398
478
  return defined.length === 0 ? name : `${name}(${defined.join(",")})`;
399
479
  }
400
480
  // src/type/builtins/text.ts
401
- function defineTextType(sqlType, castType) {
402
- return defineType({
403
- sqlType,
404
- castType
405
- }).withOperators(orderingOperators).withOperators(textOperators);
406
- }
407
481
  /** Variable-length string without a limit. */
408
- const text = /* @__PURE__ */ defineTextType("text");
482
+ const text = /* @__PURE__ */ getTextType();
409
483
  /** Variable-length string; `varchar.with(length)` limits its length. */
410
484
  const varchar = /* @__PURE__ */ defineTextType("varchar").withParameters((length) => formatTypeName("varchar", length));
411
485
  /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
412
486
  const char = /* @__PURE__ */ defineTextType("char", "bpchar").withParameters((length) => formatTypeName("char", length));
413
- export { falseNode as A, equalityOperators as C, createList as D, defineGenericOperator as E, oneNode as M, trueNode as N, createNode as O, Column as S, isEqualityCompatible as T, toNode as _, stringifyJson as a, defineType as b, textOperators as c, compareNodes as d, createIsNullNode as f, toExpression as g, toBooleanExpression as h, formatTypeName as i, noFilters as j, createSelectNode 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, toValueNode as v, hasEquality as w, isType as x, getBooleanType 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,5 +1,4 @@
1
- import { $n as BaseType, Cn as ComparisonSignature, _n as EqualityOperator, _r as GenericOperator, bn as InSignature, gr as OperatorDefinitions, hr as NoOperators, mr as MergeOperators, t as AnyExpression, tr as Type, vn as EqualityOperators, yn as HasEquality } from "./expression-tYmWRuKR.mjs";
2
- import { s as PatternSignature } from "./index-pj2AhTRY.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";
3
2
  /**
4
3
  * Creates a one-dimensional array type of `element` values.
5
4
  *
@@ -14,63 +13,63 @@ export declare const bytea: BaseType<Uint8Array<ArrayBufferLike>, Uint8Array<Arr
14
13
  export declare const boolean: BaseType<boolean, boolean, EqualityOperators, undefined>;
15
14
  /** Point in time as a UTC ISO 8601 string with microseconds, such as `2026-01-15T07:00:00.123456Z`; accepts a `Date` too. */
16
15
  export declare const timestamptz: Type<string, string | Date, MergeOperators<NoOperators, {
17
- eq: EqualityOperator<ComparisonSignature>;
18
- ne: EqualityOperator<ComparisonSignature>;
19
- in: EqualityOperator<InSignature>;
20
16
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
21
17
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
22
18
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
23
19
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
24
- }>, [precision: number]>;
25
- /** Date and time without a time zone as an ISO 8601 string with microseconds, such as `2026-01-15T10:00:00.000000`. */
26
- export declare const timestamp: Type<string, string, MergeOperators<NoOperators, {
27
20
  eq: EqualityOperator<ComparisonSignature>;
28
21
  ne: EqualityOperator<ComparisonSignature>;
29
22
  in: EqualityOperator<InSignature>;
23
+ }>, [precision: number]>;
24
+ /** Date and time without a time zone as an ISO 8601 string with microseconds, such as `2026-01-15T10:00:00.000000`. */
25
+ export declare const timestamp: Type<string, string, MergeOperators<NoOperators, {
30
26
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
31
27
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
32
28
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
33
29
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
34
- }>, [precision: number]>;
35
- /** Calendar date as an ISO 8601 string, such as `2026-01-15`. */
36
- export declare const date: BaseType<string, string, MergeOperators<NoOperators, {
37
30
  eq: EqualityOperator<ComparisonSignature>;
38
31
  ne: EqualityOperator<ComparisonSignature>;
39
32
  in: EqualityOperator<InSignature>;
33
+ }>, [precision: number]>;
34
+ /** Calendar date as an ISO 8601 string, such as `2026-01-15`. */
35
+ export declare const date: BaseType<string, string, MergeOperators<NoOperators, {
40
36
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
41
37
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
42
38
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
43
39
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
44
- }>, undefined>;
45
- /** Time of day as a string with microseconds, such as `10:00:00.000000`. */
46
- export declare const time: Type<string, string, MergeOperators<NoOperators, {
47
40
  eq: EqualityOperator<ComparisonSignature>;
48
41
  ne: EqualityOperator<ComparisonSignature>;
49
42
  in: EqualityOperator<InSignature>;
43
+ }>, undefined>;
44
+ /** Time of day as a string with microseconds, such as `10:00:00.000000`. */
45
+ export declare const time: Type<string, string, MergeOperators<NoOperators, {
50
46
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
51
47
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
52
48
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
53
49
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
54
- }>, [precision: number]>;
55
- /** Interval in the PostgreSQL output format, such as `1 day 02:00:00`. */
56
- export declare const interval: BaseType<string, string, MergeOperators<NoOperators, {
57
50
  eq: EqualityOperator<ComparisonSignature>;
58
51
  ne: EqualityOperator<ComparisonSignature>;
59
52
  in: EqualityOperator<InSignature>;
53
+ }>, [precision: number]>;
54
+ /** Interval in the PostgreSQL output format, such as `1 day 02:00:00`. */
55
+ export declare const interval: BaseType<string, string, MergeOperators<NoOperators, {
60
56
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
61
57
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
62
58
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
63
59
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
64
- }>, undefined>;
65
- /** Creates a type of an existing PostgreSQL enum; values are ordered as declared in the enum. */
66
- declare function enumOf<const Values extends readonly string[]>(name: string, _values: Values): BaseType<Values[number], Values[number], MergeOperators<NoOperators, {
67
60
  eq: EqualityOperator<ComparisonSignature>;
68
61
  ne: EqualityOperator<ComparisonSignature>;
69
62
  in: EqualityOperator<InSignature>;
63
+ }>, undefined>;
64
+ /** Creates a type of an existing PostgreSQL enum; values are ordered as declared in the enum. */
65
+ declare function enumOf<const Values extends readonly string[]>(name: string, _values: Values): BaseType<Values[number], Values[number], MergeOperators<NoOperators, {
70
66
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
71
67
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
72
68
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
73
69
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
70
+ eq: EqualityOperator<ComparisonSignature>;
71
+ ne: EqualityOperator<ComparisonSignature>;
72
+ in: EqualityOperator<InSignature>;
74
73
  }>, undefined>;
75
74
  /** JSON type of `unknown` values with operators `O` and `of`, which declares their shape. */
76
75
  type JsonType<O extends OperatorDefinitions = NoOperators> = Type<unknown, unknown, O> & {
@@ -83,131 +82,106 @@ export declare const json: JsonType<NoOperators>;
83
82
  export declare const jsonb: JsonType<MergeOperators<NoOperators, EqualityOperators>>;
84
83
  /** IP address with an optional netmask as a string, such as `192.168.0.1`. */
85
84
  export declare const inet: BaseType<string, string, MergeOperators<NoOperators, {
86
- eq: EqualityOperator<ComparisonSignature>;
87
- ne: EqualityOperator<ComparisonSignature>;
88
- in: EqualityOperator<InSignature>;
89
85
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
90
86
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
91
87
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
92
88
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
93
- }>, undefined>;
94
- /** IP network as a string, such as `10.0.0.0/8`. */
95
- export declare const cidr: BaseType<string, string, MergeOperators<NoOperators, {
96
89
  eq: EqualityOperator<ComparisonSignature>;
97
90
  ne: EqualityOperator<ComparisonSignature>;
98
91
  in: EqualityOperator<InSignature>;
92
+ }>, undefined>;
93
+ /** IP network as a string, such as `10.0.0.0/8`. */
94
+ export declare const cidr: BaseType<string, string, MergeOperators<NoOperators, {
99
95
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
100
96
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
101
97
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
102
98
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
103
- }>, undefined>;
104
- /** 2-byte integer as a `number`. */
105
- export declare const smallint: BaseType<number, number, MergeOperators<NoOperators, {
106
99
  eq: EqualityOperator<ComparisonSignature>;
107
100
  ne: EqualityOperator<ComparisonSignature>;
108
101
  in: EqualityOperator<InSignature>;
102
+ }>, undefined>;
103
+ /** 2-byte integer as a `number`. */
104
+ export declare const smallint: BaseType<number, number, MergeOperators<NoOperators, {
109
105
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
110
106
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
111
107
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
112
108
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
113
- }>, undefined>;
114
- /** 4-byte integer as a `number`. */
115
- export declare const integer: BaseType<number, number, MergeOperators<NoOperators, {
116
109
  eq: EqualityOperator<ComparisonSignature>;
117
110
  ne: EqualityOperator<ComparisonSignature>;
118
111
  in: EqualityOperator<InSignature>;
119
- lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
120
- lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
121
- gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
122
- gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
123
112
  }>, undefined>;
113
+ /** 4-byte integer as a `number`. */
114
+ export declare const integer: BaseType<number, number, NumberTypeOperators, undefined>;
124
115
  /** 8-byte integer as a `bigint`, which keeps values beyond `Number.MAX_SAFE_INTEGER`; accepts a `number` too. */
125
116
  export declare const bigint: BaseType<bigint, number | bigint, MergeOperators<NoOperators, {
126
- eq: EqualityOperator<ComparisonSignature>;
127
- ne: EqualityOperator<ComparisonSignature>;
128
- in: EqualityOperator<InSignature>;
129
117
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
130
118
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
131
119
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
132
120
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
133
- }>, undefined>;
134
- /** 4-byte floating-point number as a `number`. */
135
- export declare const real: BaseType<number, number, MergeOperators<NoOperators, {
136
121
  eq: EqualityOperator<ComparisonSignature>;
137
122
  ne: EqualityOperator<ComparisonSignature>;
138
123
  in: EqualityOperator<InSignature>;
124
+ }>, undefined>;
125
+ /** 4-byte floating-point number as a `number`. */
126
+ export declare const real: BaseType<number, number, MergeOperators<NoOperators, {
139
127
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
140
128
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
141
129
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
142
130
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
143
- }>, undefined>;
144
- /** 8-byte floating-point number as a `number`. */
145
- export declare const doublePrecision: BaseType<number, number, MergeOperators<NoOperators, {
146
131
  eq: EqualityOperator<ComparisonSignature>;
147
132
  ne: EqualityOperator<ComparisonSignature>;
148
133
  in: EqualityOperator<InSignature>;
134
+ }>, undefined>;
135
+ /** 8-byte floating-point number as a `number`. */
136
+ export declare const doublePrecision: BaseType<number, number, MergeOperators<NoOperators, {
149
137
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
150
138
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
151
139
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
152
140
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
153
- }>, undefined>;
154
- /** Exact number as a decimal string, so that no precision is lost; `numeric.with(precision, scale)` constrains it. */
155
- export declare const numeric: Type<string, string | number | bigint, MergeOperators<NoOperators, {
156
141
  eq: EqualityOperator<ComparisonSignature>;
157
142
  ne: EqualityOperator<ComparisonSignature>;
158
143
  in: EqualityOperator<InSignature>;
144
+ }>, undefined>;
145
+ /** Exact number as a decimal string, so that no precision is lost; `numeric.with(precision, scale)` constrains it. */
146
+ export declare const numeric: Type<string, string | number | bigint, MergeOperators<NoOperators, {
159
147
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
160
148
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
161
149
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
162
150
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
163
- }>, [precision: number, scale?: number | undefined]>;
164
- /** Variable-length string without a limit. */
165
- export declare const text: BaseType<string, string, MergeOperators<MergeOperators<NoOperators, {
166
151
  eq: EqualityOperator<ComparisonSignature>;
167
152
  ne: EqualityOperator<ComparisonSignature>;
168
153
  in: EqualityOperator<InSignature>;
154
+ }>, [precision: number, scale?: number | undefined]>;
155
+ /** Variable-length string without a limit. */
156
+ export declare const text: BaseType<string, string, TextTypeOperators, undefined>;
157
+ /** Variable-length string; `varchar.with(length)` limits its length. */
158
+ export declare const varchar: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
169
159
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
170
160
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
171
161
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
172
162
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
173
- }>, {
174
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
175
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
176
- }>, undefined>;
177
- /** Variable-length string; `varchar.with(length)` limits its length. */
178
- export declare const varchar: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
179
163
  eq: EqualityOperator<ComparisonSignature>;
180
164
  ne: EqualityOperator<ComparisonSignature>;
181
165
  in: EqualityOperator<InSignature>;
166
+ }>, TextOperators>, [length: number]>;
167
+ /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
168
+ export declare const char: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
182
169
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
183
170
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
184
171
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
185
172
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
186
- }>, {
187
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
188
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
189
- }>, [length: number]>;
190
- /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
191
- export declare const char: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
192
173
  eq: EqualityOperator<ComparisonSignature>;
193
174
  ne: EqualityOperator<ComparisonSignature>;
194
175
  in: EqualityOperator<InSignature>;
176
+ }>, TextOperators>, [length: number]>;
177
+ /** UUID as a string. */
178
+ export declare const uuid: BaseType<string, string, MergeOperators<NoOperators, {
195
179
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
196
180
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
197
181
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
198
182
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
199
- }>, {
200
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
201
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
202
- }>, [length: number]>;
203
- /** UUID as a string. */
204
- export declare const uuid: BaseType<string, string, MergeOperators<NoOperators, {
205
183
  eq: EqualityOperator<ComparisonSignature>;
206
184
  ne: EqualityOperator<ComparisonSignature>;
207
185
  in: EqualityOperator<InSignature>;
208
- lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
209
- lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
210
- gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
211
- gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
212
186
  }>, undefined>;
213
187
  export { enumOf as enum };
package/dist/types.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { C as equalityOperators, a as stringifyJson, b as defineType, i as formatTypeName, l as orderingOperators, n as text, o as decodeBytea, r as varchar, s as encodeBytea, t as char, w as hasEquality, y as getBooleanType } from "./text-4Xs09pIf.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.2.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,7 +42,11 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@kern-orm/postgres-js": "workspace:*",
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",
45
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",
46
50
  "typescript-6": "catalog:"
47
51
  },
48
52
  "engines": {
@@ -1,134 +0,0 @@
1
- import { Cn as ComparisonSignature, Ct as FilteredStateRows, Et as TableOrQuery, Gn as ResultRows, On as AnyTable, Q as UsedRelation, R as NoRelations, Sn as BooleanResult, St as ChangeState, T as SelectQueryKind, Tn as OperandValue, Tt as SelectQuery, Z as UsableRelation, _n as EqualityOperator, _r as GenericOperator, a as UntypedExpression, bn as InSignature, gn as SelectQueryState, gr as OperatorDefinitions, hn as QueryStateOf, ht as MergeSelection, mt as HelperFields, q as SchemaContext, r as Expression, sn as OneRow, t as AnyExpression, tr as Type, wn as Operand, yr as OperatorSignature } from "./expression-tYmWRuKR.mjs";
2
- /** Operators of ordered types: those of `equalityOperators`, and `lt`, `lte`, `gt`, and `gte` comparing with operands of the expression type. */
3
- declare const orderingOperators: {
4
- eq: EqualityOperator<ComparisonSignature>;
5
- ne: EqualityOperator<ComparisonSignature>;
6
- in: EqualityOperator<InSignature>;
7
- lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
8
- lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
9
- gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
10
- gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
11
- };
12
- /** Signature of pattern matching: patterns stay strings even when the text values are transformed. */
13
- interface PatternSignature extends OperatorSignature {
14
- readonly method: <V extends Operand<string>>(pattern: V) => Expression<BooleanResult<this["T"] | OperandValue<V>>>;
15
- }
16
- /**
17
- * Operators of text types: pattern matching with `LIKE` and `ILIKE`.
18
- *
19
- * A string pattern is sent as it is; `param(type, pattern)` encodes it with a type.
20
- */
21
- declare const textOperators: {
22
- like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
23
- ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
24
- };
25
- /**
26
- * State of the query that the callback of a helper of table `T` with schema context `C` receives: the fields of the query the helper is applied to, and all its rows.
27
- *
28
- * It is guaranteed, so that the state that the callback returns tells whether the callback ends the guarantee of the query the helper is applied to.
29
- */
30
- interface HelperState<T extends AnyTable, C extends SchemaContext> extends QueryStateOf<T, C> {
31
- readonly result: HelperFields<never>;
32
- readonly input: never;
33
- readonly rows: "many";
34
- readonly value: false;
35
- readonly uses: never;
36
- readonly guaranteed: true;
37
- readonly aggregate: false;
38
- }
39
- /**
40
- * Effect of a helper: the fields it selects, its rows mode on a query of many rows, `one` after `take` or `required` and `nullable` or `optional` after the method of the same name, the relations it selects records of, and whether it keeps the guarantee of the row of a query of a required relation.
41
- *
42
- * `reads` are the relations of the schema of the helper among them, which its callback may read from the references of the query it is applied to, and `key` is the key of its table there.
43
- */
44
- interface HelperEffect {
45
- readonly fields: object;
46
- readonly rows: ResultRows;
47
- readonly uses: UsedRelation;
48
- readonly reads: UsedRelation;
49
- readonly key: string;
50
- readonly guaranteed: boolean;
51
- }
52
- /** Effect of a helper whose callback returns a query of state `S`, for each state of a union. */
53
- type ReadEffect<S extends SelectQueryState> = S extends SelectQueryState ? {
54
- readonly fields: { [K in keyof S["result"] as K extends keyof HelperFields ? never : K]: S["result"][K]; };
55
- readonly rows: S["rows"];
56
- readonly uses: S["uses"];
57
- readonly reads: Extract<UsableRelation<S["schema"]["relations"]>, S["uses"]>;
58
- readonly key: S["schema"]["key"];
59
- readonly guaranteed: S["guaranteed"];
60
- } : never;
61
- /** Error of a helper that a query does not accept, or whose callback returns a query it does not accept. */
62
- interface HelperError<Message extends string> {
63
- readonly error: Message;
64
- }
65
- /** `unknown` when every result state retains the input marker and returns records, or the error. */
66
- type CheckResult<S extends SelectQueryState> = [S] extends [{
67
- readonly result: HelperFields;
68
- readonly value: false;
69
- }] ? unknown : HelperError<"A helper can filter, select fields, and take one row, but cannot clear the selection or use get, pluck, or aggregates">;
70
- /** State of a query over table `T` that accepts a helper of effect `E`. */
71
- type HelperInput<T extends AnyTable, E extends HelperEffect> = QueryStateOf<T, ReadsContext<E>> & ValueCheck<E>;
72
- /** Constraint of `value` of a query that accepts a helper of effect `E`. */
73
- type ValueCheck<E extends HelperEffect> = [E extends HelperEffect ? keyof E["fields"] : never] extends [never] ? [Extract<E["rows"], "nullable">] extends [never] ? unknown : RecordRows<"A helper cannot apply nullable to a query made by get, pluck, or an aggregate"> : RecordRows<"A helper cannot select fields on a query made by get, pluck, or an aggregate">;
74
- /** Constraint of a query whose rows are records, which a query of values fails with error `Message`. */
75
- interface RecordRows<Message extends string> {
76
- readonly value: false | HelperError<Message>;
77
- }
78
- /** Schema context of a query that has the relations of its schema that a helper of effect `E` may read. */
79
- type ReadsContext<E extends HelperEffect> = [E["reads"]] extends [never] ? SchemaContext : SchemaContext<{ readonly [K in E["reads"]["key"]]: { readonly [N in Extract<E["reads"], {
80
- readonly key: K;
81
- }>["name"]]: Extract<E["reads"], {
82
- readonly key: K;
83
- readonly name: N;
84
- }>["relation"]; }; }, E["key"]>;
85
- /** State of a query of state `S` after a helper of effect `E`. */
86
- type ApplyEffect<S extends SelectQueryState, E extends HelperEffect> = ChangeState<S, {
87
- result: [keyof E["fields"]] extends [never] ? S["result"] : MergeSelection<S["result"], E["fields"]>;
88
- rows: E["rows"] extends "many" ? EffectRows<S, E> : E["rows"] extends "one" ? OneRow<EffectRows<S, E>> : E["rows"];
89
- uses: S["uses"] | E["uses"];
90
- guaranteed: E["guaranteed"] extends true ? S["guaranteed"] : false;
91
- }>;
92
- /** Rows mode of a query of state `S` after a helper of effect `E` that keeps the rows, before `take`: relaxed as after a filter when the helper ends the guarantee. */
93
- type EffectRows<S extends SelectQueryState, E extends HelperEffect> = E["guaranteed"] extends true ? S["rows"] : FilteredStateRows<S>;
94
- /**
95
- * Helper of `defineHelper` for queries over table `T`: it applies effect `E` to the query it receives with arguments `A` and returns a query of the same kind.
96
- *
97
- * Selected fields replace earlier fields under the same keys; a query made by `get`, `pluck`, or an aggregate method does not accept a helper that selects fields.
98
- */
99
- type Helper<T extends AnyTable, E extends HelperEffect, A extends unknown[]> = <S extends HelperInput<T, E>, Kind extends SelectQueryKind>(query: SelectQuery<S, Kind>, ...args: A) => E extends HelperEffect ? SelectQuery<ApplyEffect<S, E>, Kind> : never;
100
- /**
101
- * Defines a reusable helper of a table that filters, selects fields, and applies `take`, `optional`, `nullable`, and `required`, used with `use` or called with a query.
102
- *
103
- * `helper` receives the query it is applied to and any further arguments, and returns the query with its clauses.
104
- * Defined with a query of a schema, such as `schema.form`, it can select the relations of the table in that schema, and applies to queries whose schema has them.
105
- * The helper returns a query of the same kind as the one it is applied to, bound to the same `Db` if any.
106
- */
107
- declare function defineHelper<T extends AnyTable, R extends SelectQuery<any, SelectQueryKind>, A extends unknown[], C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, helper: (query: SelectQuery<HelperState<T, C>, SelectQueryKind>, ...args: A) => R & CheckResult<R["types"]>): Helper<T, ReadEffect<R["types"]>, A>;
108
- /** Tagged template building a SQL fragment of values `T` that accepts compared values `I`. */
109
- type SqlTemplate<T, O extends OperatorDefinitions, I = T> = (strings: TemplateStringsArray, ...values: unknown[]) => Expression<T, O, I>;
110
- /**
111
- * Builds a SQL fragment.
112
- *
113
- * Interpolated expressions such as column references stay structured, a query of a table without clauses, such as `db.form`, becomes the name of the table, and other values become query parameters.
114
- * The type argument declares the TypeScript type of the fragment and does not convert values at runtime, so the fragment cannot be selected; the fragment has `isNull` and the operators of `equalityOperators` only.
115
- */
116
- declare function sql<T = unknown>(strings: TemplateStringsArray, ...values: unknown[]): UntypedExpression<T>;
117
- /**
118
- * Creates a template building SQL fragments of `type`.
119
- *
120
- * The fragments have the operators of the type and encode compared values with it; the type adds no SQL cast.
121
- */
122
- declare function sql<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>): SqlTemplate<Output, O, Input>;
123
- /**
124
- * Creates an expression of a query parameter encoded with a type, for a value interpolated into `sql` or compared on its own.
125
- *
126
- * With a type, the parameter is cast to it, such as `$1::integer`, so that PostgreSQL applies the operators of the type even where nothing else in the statement determines it.
127
- * The cast omits type parameters, such as `$1::numeric` for `numeric(10,2)`, so that their precision, scale, or length do not constrain the value.
128
- *
129
- * With an expression, such as `param(self, other)` in an operator definition, the type of the expression encodes the value, and the parameter is not cast: PostgreSQL types it by the expression it is used with.
130
- * `value` may then also be an expression, such as an operand of a generic operator, which is returned as it is, with its own type and nullability.
131
- */
132
- declare function param<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>, value: Input): Expression<Output, O, Input>;
133
- declare function param<E extends AnyExpression, V extends (E extends AnyExpression<infer T, infer I> ? Operand<T, I> : never)>(type: E, value: V): V extends AnyExpression ? V : E;
134
- export { HelperEffect as a, textOperators as c, Helper as i, orderingOperators as l, param as n, defineHelper as o, sql as r, PatternSignature as s, SqlTemplate as t };