kern-orm 0.1.0 → 0.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.
@@ -1,2 +1,2 @@
1
- import { S as createTestHelpers, _ as PendingTestFixture, b as TestHooks, g as OpenedTestFixture, v as TestFixture, x as TestTransactionOptions, y as TestHelpers } from "./expression-Bc-s_0qJ.mjs";
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";
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,9 +329,18 @@ 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
@@ -330,7 +357,7 @@ function createIsNullNode(operand) {
330
357
  function compare(self, operator, value) {
331
358
  return toBooleanExpression(compareNodes(operator, toNode(self), value, getType(self)));
332
359
  }
333
- /** Creates a comparison node, encoding a plain `right` value with `type`. */
360
+ /** 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
361
  function compareNodes(operator, left, right, type) {
335
362
  return createNode({
336
363
  kind: "comparison",
@@ -410,4 +437,4 @@ const text = /* @__PURE__ */ defineTextType("text");
410
437
  const varchar = /* @__PURE__ */ defineTextType("varchar").withParameters((length) => formatTypeName("varchar", length));
411
438
  /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
412
439
  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 };
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 };
package/dist/types.d.mts CHANGED
@@ -1,5 +1,4 @@
1
- import { Qn as BaseType, Sn as ComparisonSignature, _n as EqualityOperators, er as Type, gn as EqualityOperator, gr as GenericOperator, hr as OperatorDefinitions, mr as NoOperators, pr as MergeOperators, t as AnyExpression, vn as HasEquality, yn as InSignature } from "./expression-Bc-s_0qJ.mjs";
2
- import { s as PatternSignature } from "./index-Chr-UK_t.mjs";
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";
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,131 @@ 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>;
112
+ }>, undefined>;
113
+ /** 4-byte integer as a `number`. */
114
+ export declare const integer: BaseType<number, number, MergeOperators<NoOperators, {
119
115
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
120
116
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
121
117
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
122
118
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
123
- }>, undefined>;
124
- /** 8-byte integer as a `bigint`, which keeps values beyond `Number.MAX_SAFE_INTEGER`; accepts a `number` too. */
125
- export declare const bigint: BaseType<bigint, number | bigint, MergeOperators<NoOperators, {
126
119
  eq: EqualityOperator<ComparisonSignature>;
127
120
  ne: EqualityOperator<ComparisonSignature>;
128
121
  in: EqualityOperator<InSignature>;
122
+ }>, undefined>;
123
+ /** 8-byte integer as a `bigint`, which keeps values beyond `Number.MAX_SAFE_INTEGER`; accepts a `number` too. */
124
+ export declare const bigint: BaseType<bigint, number | bigint, MergeOperators<NoOperators, {
129
125
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
130
126
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
131
127
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
132
128
  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
129
  eq: EqualityOperator<ComparisonSignature>;
137
130
  ne: EqualityOperator<ComparisonSignature>;
138
131
  in: EqualityOperator<InSignature>;
132
+ }>, undefined>;
133
+ /** 4-byte floating-point number as a `number`. */
134
+ export declare const real: BaseType<number, number, MergeOperators<NoOperators, {
139
135
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
140
136
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
141
137
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
142
138
  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
139
  eq: EqualityOperator<ComparisonSignature>;
147
140
  ne: EqualityOperator<ComparisonSignature>;
148
141
  in: EqualityOperator<InSignature>;
142
+ }>, undefined>;
143
+ /** 8-byte floating-point number as a `number`. */
144
+ export declare const doublePrecision: BaseType<number, number, MergeOperators<NoOperators, {
149
145
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
150
146
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
151
147
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
152
148
  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
149
  eq: EqualityOperator<ComparisonSignature>;
157
150
  ne: EqualityOperator<ComparisonSignature>;
158
151
  in: EqualityOperator<InSignature>;
152
+ }>, undefined>;
153
+ /** Exact number as a decimal string, so that no precision is lost; `numeric.with(precision, scale)` constrains it. */
154
+ export declare const numeric: Type<string, string | number | bigint, MergeOperators<NoOperators, {
159
155
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
160
156
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
161
157
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
162
158
  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
159
  eq: EqualityOperator<ComparisonSignature>;
167
160
  ne: EqualityOperator<ComparisonSignature>;
168
161
  in: EqualityOperator<InSignature>;
162
+ }>, [precision: number, scale?: number | undefined]>;
163
+ /** Variable-length string without a limit. */
164
+ export declare const text: BaseType<string, string, MergeOperators<MergeOperators<NoOperators, {
169
165
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
170
166
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
171
167
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
172
168
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
169
+ eq: EqualityOperator<ComparisonSignature>;
170
+ ne: EqualityOperator<ComparisonSignature>;
171
+ in: EqualityOperator<InSignature>;
173
172
  }>, {
174
173
  like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
175
174
  ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
176
175
  }>, undefined>;
177
176
  /** Variable-length string; `varchar.with(length)` limits its length. */
178
177
  export declare const varchar: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
179
- eq: EqualityOperator<ComparisonSignature>;
180
- ne: EqualityOperator<ComparisonSignature>;
181
- in: EqualityOperator<InSignature>;
182
178
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
183
179
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
184
180
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
185
181
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
182
+ eq: EqualityOperator<ComparisonSignature>;
183
+ ne: EqualityOperator<ComparisonSignature>;
184
+ in: EqualityOperator<InSignature>;
186
185
  }>, {
187
186
  like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
188
187
  ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
189
188
  }>, [length: number]>;
190
189
  /** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
191
190
  export declare const char: Type<string, string, MergeOperators<MergeOperators<NoOperators, {
192
- eq: EqualityOperator<ComparisonSignature>;
193
- ne: EqualityOperator<ComparisonSignature>;
194
- in: EqualityOperator<InSignature>;
195
191
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
196
192
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
197
193
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
198
194
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
195
+ eq: EqualityOperator<ComparisonSignature>;
196
+ ne: EqualityOperator<ComparisonSignature>;
197
+ in: EqualityOperator<InSignature>;
199
198
  }>, {
200
199
  like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
201
200
  ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
202
201
  }>, [length: number]>;
203
202
  /** UUID as a string. */
204
203
  export declare const uuid: BaseType<string, string, MergeOperators<NoOperators, {
205
- eq: EqualityOperator<ComparisonSignature>;
206
- ne: EqualityOperator<ComparisonSignature>;
207
- in: EqualityOperator<InSignature>;
208
204
  lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
209
205
  lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
210
206
  gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
211
207
  gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
208
+ eq: EqualityOperator<ComparisonSignature>;
209
+ ne: EqualityOperator<ComparisonSignature>;
210
+ in: EqualityOperator<InSignature>;
212
211
  }>, undefined>;
213
212
  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 { 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";
2
2
  // src/type/builtins/array.ts
3
3
  /**
4
4
  * Creates a one-dimensional array type of `element` values.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kern-orm",
3
- "version": "0.1.0",
3
+ "version": "0.3.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,6 +42,7 @@
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
46
  "postgres": "catalog:",
46
47
  "typescript-6": "catalog:"
47
48
  },
@@ -1,134 +0,0 @@
1
- import { Cn as Operand, Dn as AnyTable, K as SchemaContext, L as NoRelations, Sn as ComparisonSignature, St as FilteredStateRows, Tt as TableOrQuery, Wn as ResultRows, X as UsableRelation, Z as UsedRelation, a as UntypedExpression, er as Type, gn as EqualityOperator, gr as GenericOperator, hn as SelectQueryState, hr as OperatorDefinitions, mn as QueryStateOf, mt as MergeSelection, on as OneRow, pt as HelperFields, r as Expression, t as AnyExpression, vr as OperatorSignature, w as SelectQueryKind, wn as OperandValue, wt as SelectQuery, xn as BooleanResult, xt as ChangeState, yn as InSignature } from "./expression-Bc-s_0qJ.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 };