kern-orm 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/expression-Bc-s_0qJ.d.mts +2091 -0
- package/dist/index-Chr-UK_t.d.mts +134 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3359 -0
- package/dist/testing.d.mts +2 -0
- package/dist/testing.mjs +34 -0
- package/dist/text-4Xs09pIf.mjs +413 -0
- package/dist/types.d.mts +213 -0
- package/dist/types.mjs +254 -0
- package/package.json +50 -4
|
@@ -0,0 +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";
|
|
2
|
+
export { type OpenedTestFixture, type PendingTestFixture, type TestFixture, type TestHelpers, type TestHooks, type TestTransactionOptions, createTestHelpers };
|
package/dist/testing.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/execution/testing.ts
|
|
2
|
+
/** Creates the test helpers of a database created with `test: true`, registering fixtures with the hooks of a test runner. */
|
|
3
|
+
function createTestHelpers(database, hooks) {
|
|
4
|
+
const { coordinator } = database;
|
|
5
|
+
if (!coordinator.test) throw new Error("createTestHelpers requires a database created with test: true");
|
|
6
|
+
return {
|
|
7
|
+
testTransaction: (callback, options) => coordinator.testTransaction(callback, options),
|
|
8
|
+
useTestFixture(seed) {
|
|
9
|
+
let pending;
|
|
10
|
+
let fixture;
|
|
11
|
+
hooks.before(async () => {
|
|
12
|
+
const current = coordinator.startTestFixture(seed);
|
|
13
|
+
pending = current;
|
|
14
|
+
try {
|
|
15
|
+
const opened = await current.fixture;
|
|
16
|
+
if (pending === current) fixture = opened;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (pending === current) throw error;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
hooks.after(async () => {
|
|
22
|
+
const current = pending;
|
|
23
|
+
pending = void 0;
|
|
24
|
+
fixture = void 0;
|
|
25
|
+
await current?.close();
|
|
26
|
+
});
|
|
27
|
+
return { get data() {
|
|
28
|
+
if (!fixture) throw new Error("Test fixture is not open: read its data in the tests and hooks of the describe block or test file that uses it");
|
|
29
|
+
return fixture.data;
|
|
30
|
+
} };
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export { createTestHelpers };
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// src/ast/nodes.ts
|
|
2
|
+
/** Freezes an expression node; nodes are shared between queries and never change. */
|
|
3
|
+
function createNode(node) {
|
|
4
|
+
return Object.freeze(node);
|
|
5
|
+
}
|
|
6
|
+
/** Creates SQL of `items` separated by commas between `prefix` and `suffix`, such as the arguments of a function call. */
|
|
7
|
+
function createList(prefix, items, suffix) {
|
|
8
|
+
if (items.length === 0) return createNode({
|
|
9
|
+
kind: "raw",
|
|
10
|
+
fragments: [prefix + suffix],
|
|
11
|
+
values: []
|
|
12
|
+
});
|
|
13
|
+
const fragments = [prefix];
|
|
14
|
+
for (let index = 1; index < items.length; index++) fragments.push(", ");
|
|
15
|
+
fragments.push(suffix);
|
|
16
|
+
return createNode({
|
|
17
|
+
kind: "raw",
|
|
18
|
+
fragments,
|
|
19
|
+
values: items
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** Filters of a query without conditions. */
|
|
23
|
+
const noFilters = /* @__PURE__ */ Object.freeze({});
|
|
24
|
+
/** Creates a query of all rows of `from` with `projections`. */
|
|
25
|
+
function createSelectNode(from, projections = []) {
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
kind: "select",
|
|
28
|
+
from,
|
|
29
|
+
joins: [],
|
|
30
|
+
projections,
|
|
31
|
+
filters: noFilters,
|
|
32
|
+
orderBy: [],
|
|
33
|
+
limit: void 0,
|
|
34
|
+
offset: void 0,
|
|
35
|
+
rows: "many",
|
|
36
|
+
value: false,
|
|
37
|
+
guaranteedBy: void 0
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const trueNode = /* @__PURE__ */ createNode({
|
|
41
|
+
kind: "boolean",
|
|
42
|
+
value: true
|
|
43
|
+
});
|
|
44
|
+
const falseNode = /* @__PURE__ */ createNode({
|
|
45
|
+
kind: "boolean",
|
|
46
|
+
value: false
|
|
47
|
+
});
|
|
48
|
+
/** Number `1`, such as the limit of a query of one row. */
|
|
49
|
+
const oneNode = /* @__PURE__ */ createNode({
|
|
50
|
+
kind: "raw",
|
|
51
|
+
fragments: ["1"],
|
|
52
|
+
values: []
|
|
53
|
+
});
|
|
54
|
+
// src/expression/operators/generic.ts
|
|
55
|
+
/** Runtime mark of generic operators. */
|
|
56
|
+
const genericOperatorBrand = Symbol("kern-orm.genericOperator");
|
|
57
|
+
/**
|
|
58
|
+
* Defines a generic operator: one definition for every type, which works with the values of the expression it is called on.
|
|
59
|
+
*
|
|
60
|
+
* It receives the expression with its own type, including a type derived with `transform`, and its method has signature `S` with the value types of the expression, such as `lt` of `orderingOperators`.
|
|
61
|
+
* An operator defined as a plain function works with the values of the type it was added to instead.
|
|
62
|
+
* `Self` restricts the types it can be added to with `withOperators`; derived types keep the operator after `transform`.
|
|
63
|
+
*/
|
|
64
|
+
function defineGenericOperator(implementation) {
|
|
65
|
+
return Object.assign(implementation.bind(void 0), { [genericOperatorBrand]: true });
|
|
66
|
+
}
|
|
67
|
+
/** Tells whether an operator is generic, created by `defineGenericOperator`. */
|
|
68
|
+
function isGenericOperator(operator) {
|
|
69
|
+
return typeof operator === "function" && genericOperatorBrand in operator;
|
|
70
|
+
}
|
|
71
|
+
// src/expression/operators/equality.ts
|
|
72
|
+
/** Runtime mark of the operators of `equalityOperators`, which tell that a type has SQL `=`. */
|
|
73
|
+
const equalityBrand = Symbol("kern-orm.equality");
|
|
74
|
+
/**
|
|
75
|
+
* Operators of SQL equality: `eq`, `ne`, and `in`; `orderingOperators` include them.
|
|
76
|
+
*
|
|
77
|
+
* A type has SQL `=` only with these operators, so that an array of it has them too; another operator named `eq` does not give it `=`.
|
|
78
|
+
*/
|
|
79
|
+
const equalityOperators = {
|
|
80
|
+
eq: defineEqualityOperator((self, value) => compare(self, "=", value)),
|
|
81
|
+
ne: defineEqualityOperator((self, value) => compare(self, "<>", value)),
|
|
82
|
+
in: defineEqualityOperator((self, list) => {
|
|
83
|
+
if (list.length === 0) return toBooleanExpression(falseNode);
|
|
84
|
+
const type = getType(self);
|
|
85
|
+
const nodes = [];
|
|
86
|
+
for (const item of list) nodes.push(toValueNode(item, type));
|
|
87
|
+
return toBooleanExpression(createNode({
|
|
88
|
+
kind: "in",
|
|
89
|
+
operand: toNode(self),
|
|
90
|
+
list: nodes
|
|
91
|
+
}));
|
|
92
|
+
})
|
|
93
|
+
};
|
|
94
|
+
/** Tells whether a type has SQL `=`, given by an operator of `equalityOperators`. */
|
|
95
|
+
function hasEquality(type) {
|
|
96
|
+
const operators = type.operators;
|
|
97
|
+
for (const name in operators) if (equalityBrand in operators[name]) return true;
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Families of different SQL types, by their cast types, whose values PostgreSQL compares with `=` of one B-tree operator family, so that an index of either column serves the comparison.
|
|
102
|
+
*
|
|
103
|
+
* `varchar` has no `=` of its own and compares as `text`.
|
|
104
|
+
*/
|
|
105
|
+
const equalityFamilies = /* @__PURE__ */ new Map([
|
|
106
|
+
["smallint", "integer"],
|
|
107
|
+
["integer", "integer"],
|
|
108
|
+
["bigint", "integer"],
|
|
109
|
+
["text", "text"],
|
|
110
|
+
["varchar", "text"]
|
|
111
|
+
]);
|
|
112
|
+
/**
|
|
113
|
+
* Tells whether SQL `=` compares the values of two types as they are: both have SQL `=`, and they are one SQL type or of one family of `equalityFamilies`.
|
|
114
|
+
*
|
|
115
|
+
* Types are one SQL type when their cast types are equal, whatever their type parameters and the JavaScript representation of their values.
|
|
116
|
+
* Equal TypeScript types are not enough, such as of `uuid` and `text`, which PostgreSQL does not compare.
|
|
117
|
+
*/
|
|
118
|
+
function isEqualityCompatible(left, right) {
|
|
119
|
+
if (!hasEquality(left) || !hasEquality(right)) return false;
|
|
120
|
+
if (left.castType === right.castType) return true;
|
|
121
|
+
const family = equalityFamilies.get(left.castType);
|
|
122
|
+
return family !== void 0 && family === equalityFamilies.get(right.castType);
|
|
123
|
+
}
|
|
124
|
+
function defineEqualityOperator(implementation) {
|
|
125
|
+
return Object.assign(defineGenericOperator(implementation), { [equalityBrand]: true });
|
|
126
|
+
}
|
|
127
|
+
// src/table/column.ts
|
|
128
|
+
/**
|
|
129
|
+
* Column of a table: a type with column attributes, forming a key of kind `Key` alone, or no key for `undefined`, with a value of the database when an insert omits it if `Default` is `true`.
|
|
130
|
+
*
|
|
131
|
+
* A column is not a type, so SQL fragments and parameters reject it; its methods add attributes and return a new column.
|
|
132
|
+
*/
|
|
133
|
+
var Column = class Column {
|
|
134
|
+
type;
|
|
135
|
+
attributes;
|
|
136
|
+
constructor(type, attributes) {
|
|
137
|
+
this.type = type;
|
|
138
|
+
this.attributes = attributes;
|
|
139
|
+
Object.freeze(attributes);
|
|
140
|
+
Object.freeze(this);
|
|
141
|
+
}
|
|
142
|
+
/** Uses a physical column name different from the column key. */
|
|
143
|
+
columnName(name) {
|
|
144
|
+
return new Column(this.type, {
|
|
145
|
+
...this.attributes,
|
|
146
|
+
name
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/** Marks the column as the primary key; the type must not be nullable. */
|
|
150
|
+
primaryKey() {
|
|
151
|
+
return markKey(this, "primaryKey");
|
|
152
|
+
}
|
|
153
|
+
/** Marks the column as a unique key. */
|
|
154
|
+
unique() {
|
|
155
|
+
return markKey(this, "unique");
|
|
156
|
+
}
|
|
157
|
+
/** Marks the column as one that the database gives a value when an insert omits it, such as by `DEFAULT` or an identity, so that inserts may omit it. */
|
|
158
|
+
hasDefault() {
|
|
159
|
+
return new Column(this.type, {
|
|
160
|
+
...this.attributes,
|
|
161
|
+
hasDefault: true
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
/** Creates a column of `column` that forms a key of kind `keyKind` alone, rejecting a column that already forms a key. */
|
|
166
|
+
function markKey(column, keyKind) {
|
|
167
|
+
if (column.attributes.keyKind !== void 0) throw new Error(`A column marked with ${column.attributes.keyKind}() cannot be marked with ${keyKind}()`);
|
|
168
|
+
return new Column(column.type, {
|
|
169
|
+
...column.attributes,
|
|
170
|
+
keyKind
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
// src/type/type.ts
|
|
174
|
+
/** Runtime mark of types, which tells them apart from columns and other objects. */
|
|
175
|
+
const typeBrand = Symbol("kern-orm.type");
|
|
176
|
+
/**
|
|
177
|
+
* Defines a type whose values are the PostgreSQL text representations as they are.
|
|
178
|
+
*
|
|
179
|
+
* `transform` converts them to other values, such as `number[]` for `vector`.
|
|
180
|
+
*/
|
|
181
|
+
function defineType(options) {
|
|
182
|
+
if (options.castType === void 0 && options.sqlType.includes("(")) throw new TypeError(`Define the type parameters of "${options.sqlType}" using withParameters(), or set castType`);
|
|
183
|
+
return createType({
|
|
184
|
+
sqlType: options.sqlType,
|
|
185
|
+
castType: options.castType ?? options.sqlType,
|
|
186
|
+
encode: (value) => value,
|
|
187
|
+
decode: (text) => text,
|
|
188
|
+
operators: Object.freeze({}),
|
|
189
|
+
isNullable: false,
|
|
190
|
+
formatSqlType: void 0
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/** Tells whether `value` is a type, as opposed to a column or another object. */
|
|
194
|
+
function isType(value) {
|
|
195
|
+
return typeof value === "object" && value !== null && typeBrand in value;
|
|
196
|
+
}
|
|
197
|
+
function createType(state) {
|
|
198
|
+
const derive = (changes) => createType({
|
|
199
|
+
...state,
|
|
200
|
+
...changes
|
|
201
|
+
});
|
|
202
|
+
const { formatSqlType } = state;
|
|
203
|
+
const type = Object.freeze({
|
|
204
|
+
...state,
|
|
205
|
+
[typeBrand]: true,
|
|
206
|
+
withOperators(extra) {
|
|
207
|
+
const operators = { ...state.operators };
|
|
208
|
+
for (const name of Object.keys(extra)) {
|
|
209
|
+
if (name === "then") throw new TypeError("Operator \"then\" is not allowed: it would make expressions thenable");
|
|
210
|
+
const operator = extra[name];
|
|
211
|
+
Object.defineProperty(operators, name, {
|
|
212
|
+
value: isGenericOperator(operator) ? operator : (self, ...args) => operator(getType(self) === type ? self : toExpression(toNode(self), type), ...args),
|
|
213
|
+
enumerable: true
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return derive({ operators: Object.freeze(operators) });
|
|
217
|
+
},
|
|
218
|
+
transform: ({ decode, encode }) => derive({
|
|
219
|
+
encode: encode ? (value) => state.encode(encode(value)) : state.encode,
|
|
220
|
+
decode: (text) => decode(state.decode(text))
|
|
221
|
+
}),
|
|
222
|
+
withParameters: (format) => derive({ formatSqlType: format }),
|
|
223
|
+
...formatSqlType && { with: (...parameters) => derive({ sqlType: formatSqlType(...parameters) }) },
|
|
224
|
+
nullable: () => derive({ isNullable: true }),
|
|
225
|
+
columnName: (name) => new Column(type, {
|
|
226
|
+
name,
|
|
227
|
+
keyKind: void 0,
|
|
228
|
+
hasDefault: false
|
|
229
|
+
}),
|
|
230
|
+
primaryKey: () => new Column(type, {
|
|
231
|
+
name: void 0,
|
|
232
|
+
keyKind: "primaryKey",
|
|
233
|
+
hasDefault: false
|
|
234
|
+
}),
|
|
235
|
+
unique: () => new Column(type, {
|
|
236
|
+
name: void 0,
|
|
237
|
+
keyKind: "unique",
|
|
238
|
+
hasDefault: false
|
|
239
|
+
}),
|
|
240
|
+
hasDefault: () => new Column(type, {
|
|
241
|
+
name: void 0,
|
|
242
|
+
keyKind: void 0,
|
|
243
|
+
hasDefault: true
|
|
244
|
+
})
|
|
245
|
+
});
|
|
246
|
+
return type;
|
|
247
|
+
}
|
|
248
|
+
// src/type/boolean.ts
|
|
249
|
+
let booleanType;
|
|
250
|
+
/** Returns the shared Boolean type used by built-in expressions and columns. */
|
|
251
|
+
function getBooleanType() {
|
|
252
|
+
booleanType ??= defineType({ sqlType: "boolean" }).transform({
|
|
253
|
+
decode: (text) => text === "t",
|
|
254
|
+
encode: (value) => value ? "t" : "f"
|
|
255
|
+
}).withOperators(equalityOperators);
|
|
256
|
+
return booleanType;
|
|
257
|
+
}
|
|
258
|
+
// src/expression/expression.ts
|
|
259
|
+
const expressionNode = Symbol("kern-orm.expression.node");
|
|
260
|
+
const expressionType = Symbol("kern-orm.expression.type");
|
|
261
|
+
/**
|
|
262
|
+
* Runtime class of every expression.
|
|
263
|
+
*
|
|
264
|
+
* An expression inherits the operators of its type from a prototype created once per type.
|
|
265
|
+
*/
|
|
266
|
+
var ExpressionObject = class {
|
|
267
|
+
isNull() {
|
|
268
|
+
return toBooleanExpression(createIsNullNode(this[expressionNode]));
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
let untypedPrototype;
|
|
272
|
+
/** Prototypes of expressions by their types, with the operators of the type as methods. */
|
|
273
|
+
const prototypes = /* @__PURE__ */ new WeakMap();
|
|
274
|
+
function getPrototype(type) {
|
|
275
|
+
let prototype = prototypes.get(type);
|
|
276
|
+
if (!prototype) {
|
|
277
|
+
prototype = createPrototype(type.operators);
|
|
278
|
+
prototypes.set(type, prototype);
|
|
279
|
+
}
|
|
280
|
+
return prototype;
|
|
281
|
+
}
|
|
282
|
+
function createPrototype(operators) {
|
|
283
|
+
const prototype = Object.create(ExpressionObject.prototype);
|
|
284
|
+
for (const name of Object.keys(operators)) {
|
|
285
|
+
const operator = operators[name];
|
|
286
|
+
Object.defineProperty(prototype, name, { value: function(...args) {
|
|
287
|
+
return operator(this, ...args);
|
|
288
|
+
} });
|
|
289
|
+
}
|
|
290
|
+
return prototype;
|
|
291
|
+
}
|
|
292
|
+
/** Returns the node behind a public expression. */
|
|
293
|
+
function toNode(expression) {
|
|
294
|
+
return expression[expressionNode];
|
|
295
|
+
}
|
|
296
|
+
function toExpression(node, type) {
|
|
297
|
+
const expression = Object.create(type ? getPrototype(type) : untypedPrototype ??= createPrototype(equalityOperators));
|
|
298
|
+
expression[expressionNode] = node;
|
|
299
|
+
expression[expressionType] = type;
|
|
300
|
+
return Object.freeze(expression);
|
|
301
|
+
}
|
|
302
|
+
/** Tells whether `value` is an expression. */
|
|
303
|
+
function isExpression(value) {
|
|
304
|
+
return value instanceof ExpressionObject;
|
|
305
|
+
}
|
|
306
|
+
/** Returns the type of an expression, if it has one. */
|
|
307
|
+
function getType(expression) {
|
|
308
|
+
return expression[expressionType];
|
|
309
|
+
}
|
|
310
|
+
/** Exposes a Boolean node with the shared codec and operators. */
|
|
311
|
+
function toBooleanExpression(node) {
|
|
312
|
+
return toExpression(node, getBooleanType());
|
|
313
|
+
}
|
|
314
|
+
/** Converts an expression or a plain value into a node, binding the value as a parameter encoded with `type`. */
|
|
315
|
+
function toValueNode(value, type) {
|
|
316
|
+
return value instanceof ExpressionObject ? value[expressionNode] : createNode({
|
|
317
|
+
kind: "param",
|
|
318
|
+
value,
|
|
319
|
+
type
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
/** Creates an `IS NULL` node directly, so that resolving an object condition never calls an `isNull` operator replaced by a type. */
|
|
323
|
+
function createIsNullNode(operand) {
|
|
324
|
+
return createNode({
|
|
325
|
+
kind: "isNull",
|
|
326
|
+
operand
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
// src/expression/operators/comparison.ts
|
|
330
|
+
function compare(self, operator, value) {
|
|
331
|
+
return toBooleanExpression(compareNodes(operator, toNode(self), value, getType(self)));
|
|
332
|
+
}
|
|
333
|
+
/** Creates a comparison node, encoding a plain `right` value with `type`. */
|
|
334
|
+
function compareNodes(operator, left, right, type) {
|
|
335
|
+
return createNode({
|
|
336
|
+
kind: "comparison",
|
|
337
|
+
operator,
|
|
338
|
+
left,
|
|
339
|
+
right: toValueNode(right, type)
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
// src/expression/operators/ordering.ts
|
|
343
|
+
/** Operators of ordered types: those of `equalityOperators`, and `lt`, `lte`, `gt`, and `gte` comparing with operands of the expression type. */
|
|
344
|
+
const orderingOperators = {
|
|
345
|
+
...equalityOperators,
|
|
346
|
+
lt: createComparison("<"),
|
|
347
|
+
lte: createComparison("<="),
|
|
348
|
+
gt: createComparison(">"),
|
|
349
|
+
gte: createComparison(">=")
|
|
350
|
+
};
|
|
351
|
+
function createComparison(operator) {
|
|
352
|
+
return defineGenericOperator((self, value) => compare(self, operator, value));
|
|
353
|
+
}
|
|
354
|
+
// src/expression/operators/text.ts
|
|
355
|
+
/**
|
|
356
|
+
* Operators of text types: pattern matching with `LIKE` and `ILIKE`.
|
|
357
|
+
*
|
|
358
|
+
* A string pattern is sent as it is; `param(type, pattern)` encodes it with a type.
|
|
359
|
+
*/
|
|
360
|
+
const textOperators = {
|
|
361
|
+
like: /* @__PURE__ */ createPatternComparison("LIKE"),
|
|
362
|
+
ilike: /* @__PURE__ */ createPatternComparison("ILIKE")
|
|
363
|
+
};
|
|
364
|
+
function createPatternComparison(operator) {
|
|
365
|
+
return defineGenericOperator((self, pattern) => toBooleanExpression(compareNodes(operator, toNode(self), pattern, void 0)));
|
|
366
|
+
}
|
|
367
|
+
// src/type/codecs/bytea.ts
|
|
368
|
+
const hexDigits = "0123456789abcdef";
|
|
369
|
+
/** Encodes bytes as a hex `bytea` literal, such as `\\x00ff`. */
|
|
370
|
+
function encodeBytea(bytes) {
|
|
371
|
+
let text = "\\x";
|
|
372
|
+
for (const byte of bytes) text += hexDigits[byte >> 4] + hexDigits[byte & 15];
|
|
373
|
+
return text;
|
|
374
|
+
}
|
|
375
|
+
/** Decodes a hex `bytea` literal into bytes. */
|
|
376
|
+
function decodeBytea(text) {
|
|
377
|
+
if (!text.startsWith("\\x")) throw new Error("Unsupported bytea value; kern-orm expects bytea_output hex");
|
|
378
|
+
const bytes = new Uint8Array((text.length - 2) / 2);
|
|
379
|
+
for (let index = 0; index < bytes.length; index++) bytes[index] = readHexDigit(text, 2 + index * 2) << 4 | readHexDigit(text, 3 + index * 2);
|
|
380
|
+
return bytes;
|
|
381
|
+
}
|
|
382
|
+
/** Reads the hex digit at `index`, in either case. */
|
|
383
|
+
function readHexDigit(text, index) {
|
|
384
|
+
const code = text.charCodeAt(index);
|
|
385
|
+
return code <= 57 ? code - 48 : (code | 32) - 87;
|
|
386
|
+
}
|
|
387
|
+
// src/type/codecs/json.ts
|
|
388
|
+
/** Serializes a value as JSON text; values without a JSON representation, such as functions, are rejected. */
|
|
389
|
+
function stringifyJson(value) {
|
|
390
|
+
const text = JSON.stringify(value);
|
|
391
|
+
if (text === void 0) throw new TypeError(`Cannot encode ${typeof value} as JSON`);
|
|
392
|
+
return text;
|
|
393
|
+
}
|
|
394
|
+
// src/type/type-name.ts
|
|
395
|
+
/** Appends type parameters, such as the precision and scale in `numeric(10,2)`, to a type name. */
|
|
396
|
+
function formatTypeName(name, ...parameters) {
|
|
397
|
+
const defined = parameters.filter((parameter) => parameter !== void 0);
|
|
398
|
+
return defined.length === 0 ? name : `${name}(${defined.join(",")})`;
|
|
399
|
+
}
|
|
400
|
+
// src/type/builtins/text.ts
|
|
401
|
+
function defineTextType(sqlType, castType) {
|
|
402
|
+
return defineType({
|
|
403
|
+
sqlType,
|
|
404
|
+
castType
|
|
405
|
+
}).withOperators(orderingOperators).withOperators(textOperators);
|
|
406
|
+
}
|
|
407
|
+
/** Variable-length string without a limit. */
|
|
408
|
+
const text = /* @__PURE__ */ defineTextType("text");
|
|
409
|
+
/** Variable-length string; `varchar.with(length)` limits its length. */
|
|
410
|
+
const varchar = /* @__PURE__ */ defineTextType("varchar").withParameters((length) => formatTypeName("varchar", length));
|
|
411
|
+
/** Fixed-length string that PostgreSQL pads with spaces; `char` alone has length 1, and `char.with(length)` sets the length. */
|
|
412
|
+
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 };
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
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";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a one-dimensional array type of `element` values.
|
|
5
|
+
*
|
|
6
|
+
* A `NULL` element decodes to `null`; a nullable `element` type, such as `array(text.nullable())`, types such elements.
|
|
7
|
+
* Elements are separated by commas, as in arrays of all built-in types; a custom type with another delimiter, such as `box`, is not supported.
|
|
8
|
+
* An array has `eq`, `ne`, and `in` when its element type has SQL `=`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function array<Output, Input, O extends OperatorDefinitions>(element: Type<Output, Input, O, any>): Type<Output[], Input[], HasEquality<O> extends true ? EqualityOperators : NoOperators>;
|
|
11
|
+
/** Binary data; PostgreSQL represents it in hex, which requires the default `bytea_output` setting. */
|
|
12
|
+
export declare const bytea: BaseType<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, MergeOperators<NoOperators, EqualityOperators>, undefined>;
|
|
13
|
+
/** `boolean` values. */
|
|
14
|
+
export declare const boolean: BaseType<boolean, boolean, EqualityOperators, undefined>;
|
|
15
|
+
/** Point in time as a UTC ISO 8601 string with microseconds, such as `2026-01-15T07:00:00.123456Z`; accepts a `Date` too. */
|
|
16
|
+
export declare const timestamptz: Type<string, string | Date, MergeOperators<NoOperators, {
|
|
17
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
18
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
19
|
+
in: EqualityOperator<InSignature>;
|
|
20
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
21
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
22
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
23
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
28
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
29
|
+
in: EqualityOperator<InSignature>;
|
|
30
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
31
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
32
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
33
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
38
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
39
|
+
in: EqualityOperator<InSignature>;
|
|
40
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
41
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
42
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
43
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
48
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
49
|
+
in: EqualityOperator<InSignature>;
|
|
50
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
51
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
52
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
53
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
58
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
59
|
+
in: EqualityOperator<InSignature>;
|
|
60
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
61
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
62
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
63
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
68
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
69
|
+
in: EqualityOperator<InSignature>;
|
|
70
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
71
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
72
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
73
|
+
gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
74
|
+
}>, undefined>;
|
|
75
|
+
/** JSON type of `unknown` values with operators `O` and `of`, which declares their shape. */
|
|
76
|
+
type JsonType<O extends OperatorDefinitions = NoOperators> = Type<unknown, unknown, O> & {
|
|
77
|
+
/** Declares the shape of the values, which is not validated at runtime. */
|
|
78
|
+
of<T>(): Type<T, T, O>;
|
|
79
|
+
};
|
|
80
|
+
/** `json` values; `json.of<T>()` declares their shape. PostgreSQL has no `=` for `json`, so its expressions have no `eq`, `ne`, or `in`. */
|
|
81
|
+
export declare const json: JsonType<NoOperators>;
|
|
82
|
+
/** `jsonb` values; `jsonb.of<T>()` declares their shape. */
|
|
83
|
+
export declare const jsonb: JsonType<MergeOperators<NoOperators, EqualityOperators>>;
|
|
84
|
+
/** IP address with an optional netmask as a string, such as `192.168.0.1`. */
|
|
85
|
+
export declare const inet: BaseType<string, string, MergeOperators<NoOperators, {
|
|
86
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
87
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
88
|
+
in: EqualityOperator<InSignature>;
|
|
89
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
90
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
91
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
92
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
97
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
98
|
+
in: EqualityOperator<InSignature>;
|
|
99
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
100
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
101
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
102
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
107
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
108
|
+
in: EqualityOperator<InSignature>;
|
|
109
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
110
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
111
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
112
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
117
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
118
|
+
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
|
+
}>, 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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
127
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
128
|
+
in: EqualityOperator<InSignature>;
|
|
129
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
130
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
131
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
132
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
137
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
138
|
+
in: EqualityOperator<InSignature>;
|
|
139
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
140
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
141
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
142
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
147
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
148
|
+
in: EqualityOperator<InSignature>;
|
|
149
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
150
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
151
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
152
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
157
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
158
|
+
in: EqualityOperator<InSignature>;
|
|
159
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
160
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
161
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
162
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
167
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
168
|
+
in: EqualityOperator<InSignature>;
|
|
169
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
170
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
171
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
172
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
180
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
181
|
+
in: EqualityOperator<InSignature>;
|
|
182
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
183
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
184
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
185
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
193
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
194
|
+
in: EqualityOperator<InSignature>;
|
|
195
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
196
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
197
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
198
|
+
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
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
206
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
207
|
+
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
|
+
}>, undefined>;
|
|
213
|
+
export { enumOf as enum };
|