okmodel 0.0.0 → 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.
Files changed (147) hide show
  1. package/README.md +44 -1
  2. package/dist/adapters/capabilities.d.ts +62 -0
  3. package/dist/adapters/error.d.ts +91 -0
  4. package/dist/adapters/failure.d.ts +39 -0
  5. package/dist/adapters/pg/batch.d.ts +36 -0
  6. package/dist/adapters/pg/call.d.ts +59 -0
  7. package/dist/adapters/pg/pglite.d.ts +21 -0
  8. package/dist/adapters/pg/postgres-extra.d.ts +43 -0
  9. package/dist/adapters/pg/postgresjs.d.ts +95 -0
  10. package/dist/adapters/pg/result.d.ts +30 -0
  11. package/dist/contracts/catalog/build.d.ts +18 -0
  12. package/dist/contracts/catalog/document.d.ts +79 -0
  13. package/dist/contracts/catalog/enum.d.ts +35 -0
  14. package/dist/contracts/catalog/identifier.d.ts +71 -0
  15. package/dist/contracts/catalog/identity.d.ts +98 -0
  16. package/dist/contracts/catalog/index.d.ts +11 -0
  17. package/dist/contracts/catalog/json.d.ts +17 -0
  18. package/dist/contracts/catalog/object.d.ts +142 -0
  19. package/dist/contracts/catalog/order.d.ts +43 -0
  20. package/dist/contracts/catalog/rewrite.d.ts +18 -0
  21. package/dist/contracts/catalog/types.d.ts +292 -0
  22. package/dist/contracts/catalog/words.d.ts +13 -0
  23. package/dist/contracts/connection.d.ts +33 -0
  24. package/dist/contracts/driver.d.ts +163 -0
  25. package/dist/contracts/error.d.ts +298 -0
  26. package/dist/contracts/index.d.ts +11 -0
  27. package/dist/contracts/index.js +16 -0
  28. package/dist/contracts/internal.d.ts +15 -0
  29. package/dist/contracts/internal.js +68 -0
  30. package/dist/contracts/nearest.d.ts +15 -0
  31. package/dist/contracts/rows.d.ts +84 -0
  32. package/dist/contracts/sha256.d.ts +14 -0
  33. package/dist/contracts/utf8.d.ts +33 -0
  34. package/dist/dialects/pg/array-literal.d.ts +25 -0
  35. package/dist/dialects/pg/bool.d.ts +46 -0
  36. package/dist/dialects/pg/column.d.ts +386 -0
  37. package/dist/dialects/pg/compile.d.ts +81 -0
  38. package/dist/dialects/pg/custom.d.ts +21 -0
  39. package/dist/dialects/pg/ddl.d.ts +70 -0
  40. package/dist/dialects/pg/decimal.d.ts +62 -0
  41. package/dist/dialects/pg/declarations.d.ts +34 -0
  42. package/dist/dialects/pg/emit.d.ts +18 -0
  43. package/dist/dialects/pg/enum.d.ts +30 -0
  44. package/dist/dialects/pg/errors.d.ts +30 -0
  45. package/dist/dialects/pg/finite.d.ts +15 -0
  46. package/dist/dialects/pg/geometry.d.ts +55 -0
  47. package/dist/dialects/pg/index.d.ts +45 -0
  48. package/dist/dialects/pg/index.js +1821 -0
  49. package/dist/dialects/pg/integer.d.ts +96 -0
  50. package/dist/dialects/pg/introspect.d.ts +43 -0
  51. package/dist/dialects/pg/json.d.ts +43 -0
  52. package/dist/dialects/pg/keys.d.ts +32 -0
  53. package/dist/dialects/pg/misuse.d.ts +31 -0
  54. package/dist/dialects/pg/model.d.ts +71 -0
  55. package/dist/dialects/pg/namespace.d.ts +63 -0
  56. package/dist/dialects/pg/network.d.ts +28 -0
  57. package/dist/dialects/pg/operators.d.ts +71 -0
  58. package/dist/dialects/pg/ops/between.d.ts +10 -0
  59. package/dist/dialects/pg/ops/contains.d.ts +8 -0
  60. package/dist/dialects/pg/ops/endsWith.d.ts +8 -0
  61. package/dist/dialects/pg/ops/eq.d.ts +9 -0
  62. package/dist/dialects/pg/ops/every.d.ts +9 -0
  63. package/dist/dialects/pg/ops/gt.d.ts +9 -0
  64. package/dist/dialects/pg/ops/gte.d.ts +9 -0
  65. package/dist/dialects/pg/ops/has.d.ts +9 -0
  66. package/dist/dialects/pg/ops/ilike.d.ts +8 -0
  67. package/dist/dialects/pg/ops/inList.d.ts +9 -0
  68. package/dist/dialects/pg/ops/inc.d.ts +13 -0
  69. package/dist/dialects/pg/ops/like.d.ts +8 -0
  70. package/dist/dialects/pg/ops/lt.d.ts +9 -0
  71. package/dist/dialects/pg/ops/lte.d.ts +9 -0
  72. package/dist/dialects/pg/ops/none.d.ts +9 -0
  73. package/dist/dialects/pg/ops/not.d.ts +9 -0
  74. package/dist/dialects/pg/ops/notIn.d.ts +9 -0
  75. package/dist/dialects/pg/ops/or.d.ts +9 -0
  76. package/dist/dialects/pg/ops/startsWith.d.ts +8 -0
  77. package/dist/dialects/pg/quote.d.ts +24 -0
  78. package/dist/dialects/pg/range.d.ts +50 -0
  79. package/dist/dialects/pg/relations.d.ts +45 -0
  80. package/dist/dialects/pg/schema.d.ts +77 -0
  81. package/dist/dialects/pg/search.d.ts +23 -0
  82. package/dist/dialects/pg/table.d.ts +201 -0
  83. package/dist/dialects/pg/temporal.d.ts +211 -0
  84. package/dist/dialects/pg/text.d.ts +44 -0
  85. package/dist/dialects/pg/time.d.ts +46 -0
  86. package/dist/okm.js +2 -0
  87. package/dist/okmodel.js +2 -0
  88. package/dist/runtime/client.d.ts +47 -0
  89. package/dist/runtime/conflict.d.ts +38 -0
  90. package/dist/runtime/drift-detail.d.ts +32 -0
  91. package/dist/runtime/drift.d.ts +29 -0
  92. package/dist/runtime/include.d.ts +15 -0
  93. package/dist/runtime/pg/pglite.d.ts +25 -0
  94. package/dist/runtime/pg/pglite.js +48 -0
  95. package/dist/runtime/pg/postgresjs.d.ts +29 -0
  96. package/dist/runtime/pg/postgresjs.js +62 -0
  97. package/dist/runtime/plan.d.ts +167 -0
  98. package/dist/runtime/tx.d.ts +29 -0
  99. package/dist/runtime/types.d.ts +369 -0
  100. package/dist/runtime/write.d.ts +48 -0
  101. package/dist/shared/4j72449d.js +37 -0
  102. package/dist/shared/79ggp945.js +148 -0
  103. package/dist/shared/7ck0dtry.js +259 -0
  104. package/dist/shared/8b04d28c.js +14 -0
  105. package/dist/shared/ax16zpvm.js +171 -0
  106. package/dist/shared/ckb1p0a4.js +566 -0
  107. package/dist/shared/cn0n68ft.js +16 -0
  108. package/dist/shared/dynq16vb.js +81 -0
  109. package/dist/shared/f5thc1s0.js +1050 -0
  110. package/dist/shared/fgqgsb5z.js +281 -0
  111. package/dist/shared/g2v6gnc8.js +347 -0
  112. package/dist/shared/g5fhqmwe.js +188 -0
  113. package/dist/shared/gk7cs7sp.js +566 -0
  114. package/dist/shared/hd293j9g.js +98 -0
  115. package/dist/shared/j5cn2963.js +701 -0
  116. package/dist/shared/kynx7vbm.js +1490 -0
  117. package/dist/shared/mkjmqjv4.js +456 -0
  118. package/dist/shared/mtkwv8wx.js +18 -0
  119. package/dist/shared/ngk8gj61.js +40 -0
  120. package/dist/shared/p0cbr3g2.js +79 -0
  121. package/dist/shared/px90pf9p.js +396 -0
  122. package/dist/shared/qk3hxfja.js +83 -0
  123. package/dist/shared/tb4ze1tr.js +119 -0
  124. package/dist/shared/vkm3q4x1.js +30 -0
  125. package/dist/shared/w8samvk1.js +18 -0
  126. package/dist/shared/xy79j5hq.js +177 -0
  127. package/dist/shared/y9m15dfv.js +888 -0
  128. package/dist/shared/ycwz42az.js +412 -0
  129. package/dist/shared/zhc5vmgf.js +257 -0
  130. package/dist/tooling/cli.d.ts +1 -0
  131. package/dist/tooling/cli.js +193 -0
  132. package/dist/tooling/errors/registry.d.ts +33 -0
  133. package/dist/tooling/migrate/apply.d.ts +86 -0
  134. package/dist/tooling/migrate/commands.d.ts +21 -0
  135. package/dist/tooling/migrate/config.d.ts +48 -0
  136. package/dist/tooling/migrate/dev.d.ts +16 -0
  137. package/dist/tooling/migrate/equal.d.ts +15 -0
  138. package/dist/tooling/migrate/files.d.ts +19 -0
  139. package/dist/tooling/migrate/index.d.ts +7 -0
  140. package/dist/tooling/migrate/index.js +7 -0
  141. package/dist/tooling/migrate/plan.d.ts +76 -0
  142. package/dist/tooling/migrate/policy.d.ts +78 -0
  143. package/dist/tooling/migrate/project.d.ts +69 -0
  144. package/dist/tooling/migrate/status.d.ts +74 -0
  145. package/dist/tooling/migrate/values.d.ts +29 -0
  146. package/package.json +94 -7
  147. package/dist/index.js +0 -0
@@ -0,0 +1,1821 @@
1
+ import {
2
+ tag
3
+ } from "../../shared/cn0n68ft.js";
4
+ import {
5
+ definition,
6
+ unavailable,
7
+ rejected,
8
+ quoteLiteral,
9
+ ColumnBuilder2,
10
+ required,
11
+ openColumn,
12
+ formatType,
13
+ table2,
14
+ index2,
15
+ sql2,
16
+ snakeCase,
17
+ emittedTypeName,
18
+ rejectLater
19
+ } from "../../shared/gk7cs7sp.js";
20
+ import {
21
+ catalog,
22
+ enumType,
23
+ sameEnumLabels
24
+ } from "../../shared/g5fhqmwe.js";
25
+ import {
26
+ staticNamespace2,
27
+ table3,
28
+ column2,
29
+ index3,
30
+ constraint2,
31
+ sequence2,
32
+ compareText
33
+ } from "../../shared/j5cn2963.js";
34
+ import {
35
+ assertIdentifier2,
36
+ fitIdentifier2,
37
+ assertStoredText
38
+ } from "../../shared/mkjmqjv4.js";
39
+ import {
40
+ catalogError2,
41
+ throwNamed2
42
+ } from "../../shared/ycwz42az.js";
43
+
44
+ // src/dialects/pg/compile.ts
45
+ function compileColumn(builder, input) {
46
+ const state = builder.state;
47
+ const name = state.sqlName ?? input.name;
48
+ const dependencies = [];
49
+ if (state.extension !== undefined) {
50
+ assertIdentifier2(state.extension, "extension");
51
+ dependencies.push({ kind: "extension", name: state.extension });
52
+ }
53
+ if (state.typeDependency !== undefined) {
54
+ assertIdentifier2(state.typeDependency, "type");
55
+ dependencies.push({
56
+ kind: "type",
57
+ namespace: input.parent.namespace,
58
+ name: state.typeDependency
59
+ });
60
+ }
61
+ if (state.renamedFrom !== undefined) {
62
+ assertIdentifier2(state.renamedFrom, "renamed column");
63
+ }
64
+ if (state.comment !== undefined) {
65
+ assertStoredText(state.comment, "column comment");
66
+ }
67
+ if (state.picklist !== undefined) {
68
+ for (const value of state.picklist.values) {
69
+ assertStoredText(value, "picklist value");
70
+ }
71
+ }
72
+ const built = column2({
73
+ parent: input.parent,
74
+ name,
75
+ dataType: formatType(state.baseType, state.dims),
76
+ nullable: state.nullable,
77
+ provenance: input.provenance,
78
+ ...input.owner !== undefined ? { owner: input.owner } : {},
79
+ ...state.defaultSql !== undefined ? { defaultExpression: state.defaultSql } : {},
80
+ ...state.identity !== undefined ? { identity: state.identity } : {},
81
+ ...state.generated !== undefined ? { generated: state.generated } : {},
82
+ ...dependencies.length > 0 || (input.dependencies?.length ?? 0) > 0 ? { dependencies: [...dependencies, ...input.dependencies ?? []] } : {}
83
+ });
84
+ const check = state.picklist !== undefined && state.picklist.check ? constraint2({
85
+ parent: input.parent,
86
+ constraintKind: "check",
87
+ columns: [name],
88
+ nameKey: name,
89
+ expression: picklistExpression(name, state.picklist.values),
90
+ provenance: input.provenance,
91
+ ...input.owner !== undefined ? { owner: input.owner } : {}
92
+ }) : undefined;
93
+ const unique = state.unique !== undefined ? constraint2({
94
+ parent: input.parent,
95
+ constraintKind: "unique",
96
+ columns: [name],
97
+ nameKey: name,
98
+ provenance: input.provenance,
99
+ ...input.owner !== undefined ? { owner: input.owner } : {}
100
+ }) : undefined;
101
+ return {
102
+ column: built,
103
+ guarded: state.guarded,
104
+ hidden: state.hidden,
105
+ ...check !== undefined ? { check } : {},
106
+ ...unique !== undefined ? { unique } : {},
107
+ ...state.renamedFrom !== undefined ? { renamedFrom: state.renamedFrom } : {},
108
+ ...state.comment !== undefined ? { comment: state.comment } : {},
109
+ ...state.unique?.reason !== undefined ? { uniqueReason: state.unique.reason } : {},
110
+ ...state.unique?.global !== undefined ? { uniqueGlobal: state.unique.global } : {},
111
+ ...state.domain !== undefined ? { domain: state.domain } : {}
112
+ };
113
+ }
114
+ function picklistExpression(name, values) {
115
+ const list = values.map((value) => quoteLiteral(value)).join(", ");
116
+ return `(${name} IN (${list}))`;
117
+ }
118
+
119
+ // src/dialects/pg/relations.ts
120
+ function one(table, field) {
121
+ return { kind: "one", table, field };
122
+ }
123
+ function many(table, field) {
124
+ return { kind: "many", table, field };
125
+ }
126
+ function isRelationCall(value) {
127
+ if (typeof value !== "object" || value === null)
128
+ return false;
129
+ if (!("kind" in value) || !("table" in value))
130
+ return false;
131
+ const kind = value.kind;
132
+ return (kind === "one" || kind === "many") && typeof value.table === "string";
133
+ }
134
+
135
+ // src/dialects/pg/text.ts
136
+ function text() {
137
+ return required({ baseType: "text", encode: encodeText, decode: decodeText, sqlForm: "quote" });
138
+ }
139
+ function varchar(length) {
140
+ const limit = boundedLength(length, "varchar");
141
+ return required({
142
+ baseType: `varchar(${String(limit)})`,
143
+ encode: (value) => encodeLimited(value, limit, "varchar"),
144
+ decode: decodeText,
145
+ sqlForm: "quote"
146
+ });
147
+ }
148
+ function char(length) {
149
+ const limit = boundedLength(length, "char");
150
+ return required({
151
+ baseType: `char(${String(limit)})`,
152
+ encode: (value) => encodeLimited(value, limit, "char"),
153
+ decode: decodeText,
154
+ sqlForm: "quote"
155
+ });
156
+ }
157
+ function citext() {
158
+ return required({
159
+ baseType: "citext",
160
+ encode: encodeText,
161
+ decode: decodeText,
162
+ sqlForm: "quote",
163
+ extension: "citext"
164
+ });
165
+ }
166
+ function encodeText(value) {
167
+ return value;
168
+ }
169
+ function decodeText(wire) {
170
+ return wire;
171
+ }
172
+ function encodeLimited(value, limit, role) {
173
+ if (characterLength(value) > limit) {
174
+ rejected(`${role} accepts at most ${String(limit)} characters.`);
175
+ }
176
+ return value;
177
+ }
178
+ function boundedLength(length, role) {
179
+ if (!Number.isInteger(length) || length < 1 || length > 10485760) {
180
+ definition(`${role} length ${String(length)} must be an integer from 1 to 10485760.`);
181
+ }
182
+ return length;
183
+ }
184
+ function characterLength(value) {
185
+ let count = 0;
186
+ for (let index = 0;index < value.length; index += 1) {
187
+ const code = value.charCodeAt(index);
188
+ if (code >= 55296 && code <= 56319) {
189
+ index += 1;
190
+ }
191
+ count += 1;
192
+ }
193
+ return count;
194
+ }
195
+
196
+ // src/dialects/pg/schema.ts
197
+ var SCHEMA_KNOWN = new Set(["casing", "codecs", "requires", "tables", "types"]);
198
+ var SCHEMA_LATER = {
199
+ extensions: "0.3",
200
+ functions: "0.3",
201
+ tenancy: "0.2",
202
+ traits: "0.2",
203
+ triggers: "0.3",
204
+ validation: "0.2",
205
+ views: "0.3"
206
+ };
207
+ var BIGINT_CODECS = new Set(["string", "number", "bigint"]);
208
+ var NUMERIC_CODECS = new Set(["string", "number"]);
209
+ function stage(jobs, build) {
210
+ jobs.push((objects) => {
211
+ objects.push(build());
212
+ });
213
+ }
214
+ function schema(config) {
215
+ rejectLater(config, SCHEMA_KNOWN, SCHEMA_LATER, "schema()");
216
+ if (!Array.isArray(config.tables)) {
217
+ definition("schema() needs a tables array.");
218
+ }
219
+ const types = readTypes(config.types);
220
+ const casing = readCasing(config.casing);
221
+ const codecs = readCodecs(config.codecs);
222
+ const requires = readRequires(config.requires);
223
+ const namespace = staticNamespace2("public");
224
+ const accepted = acceptTables(config.tables);
225
+ const jobs = [];
226
+ const prepared = [];
227
+ const sqlNames = new Set;
228
+ const enums = new Map;
229
+ for (const item of config.tables) {
230
+ const built = compileTable(item, namespace, casing, codecs, jobs, enums);
231
+ if (sqlNames.has(built.sqlName)) {
232
+ catalogError2("OKM1023", `Table ${built.tsName} uses SQL name ${built.sqlName}, which another table already uses. Accepted names: ${list(accepted)}.`);
233
+ }
234
+ sqlNames.add(built.sqlName);
235
+ prepared.push(built);
236
+ }
237
+ const byName = new Map;
238
+ for (const item of prepared) {
239
+ byName.set(item.tsName, item);
240
+ }
241
+ const edges = [];
242
+ for (const item of prepared) {
243
+ compileForeignKeys(item, byName, accepted, jobs, edges);
244
+ }
245
+ const model = {};
246
+ for (const item of prepared) {
247
+ model[item.tsName] = tableModel(item, edges, byName, accepted);
248
+ }
249
+ let document;
250
+ return {
251
+ get catalog() {
252
+ if (document === undefined) {
253
+ const objects = [];
254
+ for (const job of jobs)
255
+ job(objects);
256
+ for (const [name, note] of [...enums.entries()].sort((left, right) => compareText(left[0], right[0]))) {
257
+ objects.push(enumType({
258
+ namespace,
259
+ name,
260
+ labels: note.labels,
261
+ provenance: note.provenance
262
+ }));
263
+ }
264
+ document = catalog(objects);
265
+ }
266
+ return document;
267
+ },
268
+ types,
269
+ casing,
270
+ codecs,
271
+ requires,
272
+ tables: config.tables,
273
+ model
274
+ };
275
+ }
276
+ function acceptTables(tables) {
277
+ const names = [];
278
+ const seen = new Set;
279
+ const emitted = new Map;
280
+ for (const item of tables) {
281
+ if (typeof item.name !== "string" || item.name.length === 0) {
282
+ definition("schema() tables need a name.");
283
+ }
284
+ if (seen.has(item.name)) {
285
+ catalogError2("OKM1023", `Table ${item.name} is declared more than once. Accepted names: ${list([...names].sort())}.`);
286
+ }
287
+ seen.add(item.name);
288
+ names.push(item.name);
289
+ const typeName = emittedTypeName(item.name);
290
+ const previous = emitted.get(typeName);
291
+ if (previous !== undefined) {
292
+ catalogError2("OKM1023", `Table ${item.name} and ${previous} both emit ${typeName}. Accepted names: ${list([...names].sort())}.`);
293
+ }
294
+ emitted.set(typeName, item.name);
295
+ }
296
+ names.sort();
297
+ return names;
298
+ }
299
+ function noteEnum(notes, labels, name, table, provenance) {
300
+ const previous = notes.get(name);
301
+ if (previous !== undefined && !sameEnumLabels(previous.labels, labels)) {
302
+ catalogError2("OKM1020", `Enum ${name} lists ${previous.labels.join(", ")} on ${previous.table} and ${labels.join(", ")} on ${table}. One enum has one label list.`);
303
+ }
304
+ if (previous === undefined || compareText(table, previous.table) < 0) {
305
+ notes.set(name, { labels, table, provenance });
306
+ }
307
+ }
308
+ function compileTable(item, namespace, casing, codecs, jobs, enums) {
309
+ const options = item.options;
310
+ readTableNames(item.name, options);
311
+ const sqlName = options?.sqlName ?? (casing === "snake" ? snakeCase(item.name) : item.name);
312
+ assertIdentifier2(sqlName, `table ${item.name}`);
313
+ const provenance = { origin: "file", name: item.name };
314
+ const parent = { namespace, name: sqlName };
315
+ stage(jobs, () => table3({ namespace, name: sqlName, provenance }));
316
+ const columns = [];
317
+ const primary = [];
318
+ const byField = new Map;
319
+ const bySql = new Map;
320
+ const handles = {};
321
+ for (const [field, builder] of Object.entries(item.columns)) {
322
+ if (!(builder instanceof ColumnBuilder2)) {
323
+ definition(`Column ${item.name}.${field} must be a column builder.`);
324
+ }
325
+ const column = builder;
326
+ const columnSql = column.state.sqlName ?? (casing === "snake" ? snakeCase(field) : field);
327
+ assertCodec(item.name, field, column, codecs);
328
+ const identity = column.state.identity;
329
+ if (identity !== undefined) {
330
+ const seqName = fitIdentifier2(`${sqlName}_${columnSql}_seq`);
331
+ jobs.push((objects) => {
332
+ const seq = sequence2({
333
+ namespace,
334
+ name: seqName,
335
+ dataType: "bigint",
336
+ provenance,
337
+ dependencies: [{ kind: "table", namespace, name: sqlName }]
338
+ });
339
+ objects.push(seq);
340
+ pushCompiled(objects, column, {
341
+ parent,
342
+ name: columnSql,
343
+ provenance,
344
+ dependencies: [seq.identity]
345
+ });
346
+ });
347
+ } else {
348
+ jobs.push((objects) => {
349
+ pushCompiled(objects, column, { parent, name: columnSql, provenance });
350
+ });
351
+ }
352
+ const decode = column.state.decode;
353
+ const prepared = {
354
+ field,
355
+ sqlName: columnSql,
356
+ dataType: formatType(column.state.baseType, column.state.dims),
357
+ nullable: column.state.nullable,
358
+ references: column.state.references,
359
+ encode: column.state.encode,
360
+ decode: decode === decodeText ? undefined : decode,
361
+ hidden: column.state.hidden,
362
+ guarded: column.state.guarded,
363
+ writable: column.state.guarded !== true && column.state.omitWrite !== true && column.state.generated === undefined,
364
+ unique: column.state.unique !== undefined
365
+ };
366
+ columns.push(prepared);
367
+ byField.set(field, prepared);
368
+ bySql.set(prepared.sqlName, prepared);
369
+ handles[field] = { name: prepared.sqlName };
370
+ if (column.state.primaryKey) {
371
+ primary.push(prepared.sqlName);
372
+ }
373
+ const labels = column.state.enumLabels;
374
+ if (labels !== undefined) {
375
+ noteEnum(enums, labels, column.state.typeDependency ?? prepared.sqlName, sqlName, provenance);
376
+ }
377
+ }
378
+ if (primary.length > 1) {
379
+ catalogError2("OKM1020", `Table ${item.name} has more than one primary key (${primary.join(", ")}). One primary key is accepted.`);
380
+ }
381
+ if (primary.length === 1) {
382
+ stage(jobs, () => constraint2({
383
+ parent,
384
+ constraintKind: "primaryKey",
385
+ columns: primary,
386
+ provenance
387
+ }));
388
+ }
389
+ if (options?.unique !== undefined) {
390
+ compileUniques(item.name, options, parent, provenance, byField, jobs);
391
+ }
392
+ if (options?.indexes !== undefined) {
393
+ compileIndexes(item.name, options, handles, parent, provenance, jobs);
394
+ }
395
+ if (options?.checks !== undefined) {
396
+ compileChecks(item.name, options, handles, parent, provenance, byField, jobs);
397
+ }
398
+ return {
399
+ tsName: item.name,
400
+ sqlName,
401
+ parent,
402
+ provenance,
403
+ columns,
404
+ primary,
405
+ uniques: uniqueTargets(primary, columns, bySql, options),
406
+ byField,
407
+ bySql,
408
+ relationOptions: relationInput(item.name, options)
409
+ };
410
+ }
411
+ function uniqueTargets(primary, columns, bySql, options) {
412
+ const uniques = [];
413
+ if (primary.length > 0) {
414
+ const fields = [];
415
+ for (const sqlName of primary) {
416
+ const column = bySql.get(sqlName);
417
+ if (column !== undefined)
418
+ fields.push(column.field);
419
+ }
420
+ if (fields.length > 0)
421
+ uniques.push(fields);
422
+ }
423
+ for (const column of columns) {
424
+ if (column.unique)
425
+ uniques.push([column.field]);
426
+ }
427
+ const named = options?.unique;
428
+ if (named !== undefined) {
429
+ for (const fields of Object.values(named))
430
+ uniques.push([...fields]);
431
+ }
432
+ return uniques;
433
+ }
434
+ function relationInput(tableName, options) {
435
+ const relations = options?.relations;
436
+ if (relations === undefined)
437
+ return;
438
+ if (typeof relations !== "object" || relations === null || Array.isArray(relations)) {
439
+ definition(`Table ${tableName} relations must be an object of one() and many() calls.`);
440
+ }
441
+ return relations;
442
+ }
443
+ function pushCompiled(objects, column, input) {
444
+ const compiled = compileColumn(column, input);
445
+ objects.push(compiled.column);
446
+ if (compiled.check !== undefined)
447
+ objects.push(compiled.check);
448
+ if (compiled.unique !== undefined)
449
+ objects.push(compiled.unique);
450
+ }
451
+ function compileUniques(tableName, options, parent, provenance, byField, jobs) {
452
+ const unique = options?.unique;
453
+ if (unique === undefined) {
454
+ return;
455
+ }
456
+ const accepted = [...byField.keys()].sort();
457
+ for (const [name, fields] of Object.entries(unique)) {
458
+ if (fields.length === 0) {
459
+ definition(`Unique ${tableName}.${name} needs a column.`);
460
+ }
461
+ const columns = [];
462
+ for (const field of fields) {
463
+ const column = byField.get(field);
464
+ if (column === undefined) {
465
+ throwNamed2("OKM1020", field, accepted, `Unique ${tableName}.${name} names ${field}, which is not a column of ${tableName}. Accepted names: ${list(accepted)}.`);
466
+ }
467
+ columns.push(column.sqlName);
468
+ }
469
+ stage(jobs, () => constraint2({
470
+ parent,
471
+ constraintKind: "unique",
472
+ columns,
473
+ nameKey: name,
474
+ provenance
475
+ }));
476
+ }
477
+ }
478
+ function compileIndexes(tableName, options, handles, parent, provenance, jobs) {
479
+ const build = options?.indexes;
480
+ if (build === undefined) {
481
+ return;
482
+ }
483
+ const calls = build(handles);
484
+ if (!Array.isArray(calls)) {
485
+ definition(`Table ${tableName} indexes must return an array.`);
486
+ }
487
+ for (const call of calls) {
488
+ const built = readIndex(tableName, call);
489
+ stage(jobs, () => index3({
490
+ parent,
491
+ columns: built.columns,
492
+ unique: built.isUnique === true,
493
+ nameKey: built.columns.join("_"),
494
+ provenance
495
+ }));
496
+ }
497
+ }
498
+ function compileChecks(tableName, options, handles, parent, provenance, byField, jobs) {
499
+ const checks = options?.checks;
500
+ if (checks === undefined) {
501
+ return;
502
+ }
503
+ for (const [name, build] of Object.entries(checks)) {
504
+ const used = [];
505
+ const value = build(track(handles, used));
506
+ const text = readCheck(tableName, name, value);
507
+ const columns = [];
508
+ for (const field of used) {
509
+ const column = byField.get(field);
510
+ if (column !== undefined) {
511
+ columns.push(column.sqlName);
512
+ }
513
+ }
514
+ stage(jobs, () => constraint2({
515
+ parent,
516
+ constraintKind: "check",
517
+ columns,
518
+ nameKey: name,
519
+ expression: text,
520
+ provenance
521
+ }));
522
+ }
523
+ }
524
+ function compileForeignKeys(item, byName, accepted, jobs, edges) {
525
+ for (const column of item.columns) {
526
+ const reference = column.references;
527
+ if (reference === undefined) {
528
+ continue;
529
+ }
530
+ const target = byName.get(reference.table);
531
+ if (target === undefined) {
532
+ throwNamed2("OKM1020", reference.table, accepted, `Table ${reference.table} is not in the schema. Accepted names: ${list(accepted)}.`);
533
+ }
534
+ const targetColumns = resolveTarget(item, column, target, reference);
535
+ if (reference.onDelete === "set null" || reference.onUpdate === "set null") {
536
+ if (!column.nullable) {
537
+ definition(`Foreign key ${item.tsName}.${column.field} uses set null. ${item.tsName}.${column.field} must be nullable.`);
538
+ }
539
+ }
540
+ const localType = column.dataType;
541
+ const remote = targetColumns[0];
542
+ if (remote === undefined || targetColumns.length !== 1) {
543
+ const names = [...target.byField.keys()].sort();
544
+ catalogError2("OKM1021", `Foreign key ${item.tsName}.${column.field} on ${target.tsName} is ambiguous. Accepted columns: ${list(names)}.`);
545
+ }
546
+ if (localType !== remote.dataType) {
547
+ catalogError2("OKM1022", `Foreign key ${item.tsName}.${column.field} is ${localType} and ${target.tsName}.${remote.field} is ${remote.dataType}. Accepted type: ${remote.dataType}.`);
548
+ }
549
+ stage(jobs, () => constraint2({
550
+ parent: item.parent,
551
+ constraintKind: "foreignKey",
552
+ columns: [column.sqlName],
553
+ nameKey: column.sqlName,
554
+ provenance: item.provenance,
555
+ references: {
556
+ parent: target.parent,
557
+ columns: [remote.sqlName],
558
+ ...reference.onDelete !== undefined ? { onDelete: reference.onDelete } : {},
559
+ ...reference.onUpdate !== undefined ? { onUpdate: reference.onUpdate } : {}
560
+ }
561
+ }));
562
+ edges.push({
563
+ fromTable: item.tsName,
564
+ fromField: column.field,
565
+ fromSql: column.sqlName,
566
+ toTable: target.tsName,
567
+ toSql: remote.sqlName
568
+ });
569
+ }
570
+ }
571
+ function resolveTarget(item, column, target, reference) {
572
+ const accepted = [...target.byField.keys()].sort();
573
+ const named = reference.columns;
574
+ if (named !== undefined) {
575
+ const found = [];
576
+ for (const name of named) {
577
+ const match = target.byField.get(name) ?? target.bySql.get(name);
578
+ if (match === undefined) {
579
+ throwNamed2("OKM1021", name, accepted, `Foreign key ${item.tsName}.${column.field} references ${name} on ${target.tsName}, which is not a column. Accepted columns: ${list(accepted)}.`);
580
+ }
581
+ found.push(match);
582
+ }
583
+ if (found.length !== 1) {
584
+ catalogError2("OKM1021", `Foreign key ${item.tsName}.${column.field} on ${target.tsName} is ambiguous. Accepted columns: ${list(accepted)}.`);
585
+ }
586
+ return found;
587
+ }
588
+ if (target.primary.length === 0) {
589
+ catalogError2("OKM1021", `Foreign key ${item.tsName}.${column.field} on ${target.tsName} has no primary key. Accepted columns: ${list(accepted)}.`);
590
+ }
591
+ if (target.primary.length !== 1) {
592
+ catalogError2("OKM1021", `Foreign key ${item.tsName}.${column.field} on ${target.tsName} is ambiguous. Accepted columns: ${list(target.primary)}.`);
593
+ }
594
+ const sqlName = target.primary[0] ?? "";
595
+ const match = target.bySql.get(sqlName);
596
+ if (match === undefined) {
597
+ catalogError2("OKM1021", `Foreign key ${item.tsName}.${column.field} on ${target.tsName} is missing its target. Accepted columns: ${list(accepted)}.`);
598
+ }
599
+ return [match];
600
+ }
601
+ function assertCodec(tableName, field, builder, codecs) {
602
+ const base = builder.state.baseType;
603
+ if (base === "bigint" && codecs.bigint !== "string") {
604
+ const label = builder.state.typeLabel ?? "string";
605
+ if (label !== codecs.bigint) {
606
+ definition(`Schema codecs.bigint is ${codecs.bigint}, and ${tableName}.${field} was built as ${label}. Set as: "${codecs.bigint}" on that column.`);
607
+ }
608
+ }
609
+ if (base.startsWith("numeric") && codecs.numeric !== "string") {
610
+ const label = builder.state.typeLabel ?? "string";
611
+ if (label !== codecs.numeric) {
612
+ definition(`Schema codecs.numeric is ${codecs.numeric}, and ${tableName}.${field} was built as ${label}. Set as: "${codecs.numeric}" on that column.`);
613
+ }
614
+ }
615
+ }
616
+ function readTableNames(tableName, options) {
617
+ if (options?.sqlName !== undefined) {
618
+ if (options.sqlName.length === 0) {
619
+ definition(`Table ${tableName} sqlName must be a non-empty SQL identifier.`);
620
+ }
621
+ assertIdentifier2(options.sqlName, `table ${tableName} sqlName`);
622
+ }
623
+ if (options?.renamedFrom !== undefined) {
624
+ if (options.renamedFrom.length === 0) {
625
+ definition(`Table ${tableName} renamedFrom must be a non-empty previous name.`);
626
+ }
627
+ assertIdentifier2(options.renamedFrom, `table ${tableName} renamedFrom`);
628
+ }
629
+ if (options?.comment !== undefined) {
630
+ assertStoredText(options.comment, `table ${tableName} comment`);
631
+ if (options.comment.length === 0) {
632
+ definition(`Table ${tableName} comment must be non-empty.`);
633
+ }
634
+ }
635
+ }
636
+ function readTypes(types) {
637
+ if (types === undefined) {
638
+ return "emitted";
639
+ }
640
+ if (types !== "emitted" && types !== "inferred") {
641
+ definition(`schema() types ${String(types)} must be emitted or inferred.`);
642
+ }
643
+ return types;
644
+ }
645
+ function readCasing(casing) {
646
+ if (casing === undefined) {
647
+ return;
648
+ }
649
+ if (casing !== "snake") {
650
+ definition(`schema() casing ${String(casing)} must be snake.`);
651
+ }
652
+ return casing;
653
+ }
654
+ function readCodecs(codecs) {
655
+ const bigint = codecs?.bigint ?? "string";
656
+ const numeric = codecs?.numeric ?? "string";
657
+ const timestamps = codecs?.timestamps ?? "temporal";
658
+ if (!BIGINT_CODECS.has(bigint)) {
659
+ definition(`schema() codecs.bigint ${String(bigint)} must be string, number, or bigint.`);
660
+ }
661
+ if (!NUMERIC_CODECS.has(numeric)) {
662
+ definition(`schema() codecs.numeric ${String(numeric)} must be string or number.`);
663
+ }
664
+ if (timestamps !== "temporal") {
665
+ definition(`schema() codecs.timestamps ${String(timestamps)} must be temporal.`);
666
+ }
667
+ return { bigint, numeric, timestamps };
668
+ }
669
+ function tableModel(item, edges, byName, accepted) {
670
+ const columns = item.columns.map((column) => ({
671
+ field: column.field,
672
+ sql: column.sqlName,
673
+ dataType: column.dataType,
674
+ encode: column.encode,
675
+ decode: column.decode,
676
+ hidden: column.hidden,
677
+ guarded: column.guarded,
678
+ writable: column.writable
679
+ }));
680
+ const primary = [];
681
+ for (const sqlName of item.primary) {
682
+ const column = item.bySql.get(sqlName);
683
+ if (column !== undefined)
684
+ primary.push(column.field);
685
+ }
686
+ return {
687
+ name: item.tsName,
688
+ sql: item.sqlName,
689
+ primary,
690
+ uniques: item.uniques,
691
+ columns,
692
+ relations: resolveRelations(item, edges, byName, accepted)
693
+ };
694
+ }
695
+ function resolveRelations(item, edges, byName, accepted) {
696
+ const raw = item.relationOptions;
697
+ if (raw === undefined)
698
+ return [];
699
+ const record = raw;
700
+ const names = Object.keys(record);
701
+ const resolved = [];
702
+ const seen = new Set;
703
+ for (const name of names) {
704
+ if (seen.has(name))
705
+ continue;
706
+ seen.add(name);
707
+ const call = record[name];
708
+ if (!isRelationCall(call)) {
709
+ unavailable(`Table ${item.tsName} relation ${name} is not available yet. one() and many() are accepted. manyThrough arrives in 0.2. morph arrives later.`);
710
+ }
711
+ if (!byName.has(call.table)) {
712
+ throwNamed2("OKM1020", call.table, accepted, `Relation ${item.tsName}.${name} names ${call.table}, which is not in the schema. Accepted names: ${list(accepted)}.`);
713
+ }
714
+ const field = call.field;
715
+ const matches = call.kind === "one" ? edges.filter((edge) => edge.fromTable === item.tsName && edge.toTable === call.table && (field === undefined || edge.fromField === field)) : edges.filter((edge) => edge.toTable === item.tsName && edge.fromTable === call.table && (field === undefined || edge.fromField === field));
716
+ if (matches.length !== 1) {
717
+ const hint = matches.map((edge) => edge.fromField).sort();
718
+ const which = hint.length === 0 ? "none" : hint.join(", ");
719
+ catalogError2("OKM1021", `Relation ${item.tsName}.${name} on ${call.table} is ambiguous. Accepted columns: ${which}.`);
720
+ }
721
+ const edge = matches[0];
722
+ if (edge === undefined) {
723
+ catalogError2("OKM1021", `Relation ${item.tsName}.${name} on ${call.table} is ambiguous. Accepted columns: (none).`);
724
+ }
725
+ resolved.push(call.kind === "one" ? { name, kind: "one", table: call.table, local: [edge.fromSql], remote: [edge.toSql] } : { name, kind: "many", table: call.table, local: [edge.toSql], remote: [edge.fromSql] });
726
+ }
727
+ return resolved;
728
+ }
729
+ function readRequires(requires) {
730
+ if (requires === undefined) {
731
+ return;
732
+ }
733
+ const postgres = requires.postgres;
734
+ if (postgres !== undefined && postgres.length === 0) {
735
+ definition("schema() requires.postgres must be a version range such as >=17.");
736
+ }
737
+ return postgres === undefined ? {} : { postgres };
738
+ }
739
+ function readIndex(tableName, call) {
740
+ if (call.columns.length === 0) {
741
+ definition(`Index on ${tableName} needs a column.`);
742
+ }
743
+ for (const name of call.columns) {
744
+ if (typeof name !== "string" || name.length === 0) {
745
+ definition(`Index on ${tableName} must be built from column handles.`);
746
+ }
747
+ }
748
+ return call;
749
+ }
750
+ function readCheck(tableName, name, value) {
751
+ if (value === undefined || typeof value.text !== "string" || value.text.length === 0) {
752
+ definition(`Check ${tableName}.${name} must return sql text.`);
753
+ }
754
+ assertStoredText(value.text, `check ${tableName}.${name}`);
755
+ return value.text;
756
+ }
757
+ function track(handles, used) {
758
+ return new Proxy(handles, {
759
+ get(target, key) {
760
+ if (typeof key === "string" && Object.hasOwn(target, key)) {
761
+ used.push(key);
762
+ return target[key];
763
+ }
764
+ return;
765
+ }
766
+ });
767
+ }
768
+ function list(names) {
769
+ return names.length === 0 ? "(none)" : names.join(", ");
770
+ }
771
+ // src/dialects/pg/custom.ts
772
+ function custom(spec) {
773
+ if (spec.sqlType.length === 0) {
774
+ definition("custom sqlType must be a non-empty SQL type.");
775
+ }
776
+ return required({
777
+ baseType: spec.sqlType,
778
+ encode: spec.encode,
779
+ decode: spec.decode,
780
+ sqlForm: spec.sqlForm ?? "quote",
781
+ ...spec.extension !== undefined ? { extension: spec.extension } : {}
782
+ });
783
+ }
784
+ // src/dialects/pg/integer.ts
785
+ function smallint() {
786
+ return required({
787
+ baseType: "smallint",
788
+ encode: (value) => integerToken(value, -32768, 32767, "smallint"),
789
+ decode: (wire) => decodeBounded(wire, -32768, 32767, "smallint"),
790
+ sqlForm: "raw"
791
+ });
792
+ }
793
+ function integer() {
794
+ return required({
795
+ baseType: "integer",
796
+ encode: (value) => integerToken(value, -2147483648, 2147483647, "integer"),
797
+ decode: (wire) => decodeBounded(wire, -2147483648, 2147483647, "integer"),
798
+ sqlForm: "raw"
799
+ });
800
+ }
801
+ function bigint(options) {
802
+ const mode = options?.as ?? "string";
803
+ if (mode === "number") {
804
+ return required({
805
+ baseType: "bigint",
806
+ encode: encodeBigintNumber,
807
+ decode: decodeBigintNumber,
808
+ sqlForm: "raw",
809
+ typeLabel: "number"
810
+ });
811
+ }
812
+ if (mode === "bigint") {
813
+ return required({
814
+ baseType: "bigint",
815
+ encode: encodeBigintValue,
816
+ decode: decodeBigintValue,
817
+ sqlForm: "raw",
818
+ typeLabel: "bigint"
819
+ });
820
+ }
821
+ if (mode !== "string") {
822
+ definition(`bigint as ${String(mode)} must be string, number, or bigint.`);
823
+ }
824
+ return required({
825
+ baseType: "bigint",
826
+ encode: encodeBigintString,
827
+ decode: decodeBigintString,
828
+ sqlForm: "raw"
829
+ });
830
+ }
831
+ function identity(options) {
832
+ const mode = options?.as ?? "string";
833
+ if (mode === "number") {
834
+ return openColumn({
835
+ ...identityFlags(),
836
+ encode: encodeBigintNumber,
837
+ decode: decodeBigintNumber,
838
+ typeLabel: "number"
839
+ });
840
+ }
841
+ if (mode === "bigint") {
842
+ return openColumn({
843
+ ...identityFlags(),
844
+ encode: encodeBigintValue,
845
+ decode: decodeBigintValue,
846
+ typeLabel: "bigint"
847
+ });
848
+ }
849
+ if (mode !== "string") {
850
+ definition(`identity as ${String(mode)} must be string, number, or bigint.`);
851
+ }
852
+ return openColumn({
853
+ ...identityFlags(),
854
+ encode: encodeBigintString,
855
+ decode: decodeBigintString
856
+ });
857
+ }
858
+ function encodeBigintString(value) {
859
+ parseBigint(value);
860
+ return value;
861
+ }
862
+ function decodeBigintString(wire) {
863
+ return encodeBigintString(wire);
864
+ }
865
+ function encodeBigintNumber(value) {
866
+ if (!Number.isSafeInteger(value)) {
867
+ rejected(`bigint number ${String(value)} must be an integer from ${String(Number.MIN_SAFE_INTEGER)} to ${String(Number.MAX_SAFE_INTEGER)}.`);
868
+ }
869
+ return String(value);
870
+ }
871
+ function decodeBigintNumber(wire) {
872
+ const parsed = parseBigint(wire);
873
+ const value = Number(parsed);
874
+ if (!Number.isSafeInteger(value)) {
875
+ rejected(`bigint ${wire} must be an integer from ${String(Number.MIN_SAFE_INTEGER)} to ${String(Number.MAX_SAFE_INTEGER)} when as is number.`);
876
+ }
877
+ return value;
878
+ }
879
+ function encodeBigintValue(value) {
880
+ return value.toString();
881
+ }
882
+ function decodeBigintValue(wire) {
883
+ return parseBigint(wire);
884
+ }
885
+ function integerToken(value, min, max, role) {
886
+ if (!Number.isInteger(value) || value < min || value > max) {
887
+ rejected(`${role} ${String(value)} must be an integer from ${String(min)} to ${String(max)}.`);
888
+ }
889
+ return String(value);
890
+ }
891
+ function decodeBounded(wire, min, max, role) {
892
+ if (!integerText().test(wire)) {
893
+ rejected(`${role} ${wire} must be an integer, for example 0 or -12.`);
894
+ }
895
+ const value = Number(wire);
896
+ integerToken(value, min, max, role);
897
+ return value;
898
+ }
899
+ function identityFlags() {
900
+ return {
901
+ baseType: "bigint",
902
+ nullable: false,
903
+ hasDefault: true,
904
+ generated: false,
905
+ guarded: false,
906
+ hidden: false,
907
+ omitWrite: true,
908
+ sqlForm: "raw",
909
+ identity: { always: true },
910
+ primaryKey: true
911
+ };
912
+ }
913
+ function parseBigint(wire) {
914
+ if (!integerText().test(wire)) {
915
+ rejected(`bigint ${wire} must be an integer with no leading zeros, for example 0 or -12.`);
916
+ }
917
+ return BigInt(wire);
918
+ }
919
+ function integerText() {
920
+ integerPattern ??= /^-?(?:0|[1-9]\d*)$/;
921
+ return integerPattern;
922
+ }
923
+ var integerPattern;
924
+ // src/dialects/pg/finite.ts
925
+ function decimalText(value, role) {
926
+ if (!Number.isFinite(value)) {
927
+ rejected(`${role} ${String(value)} must be a finite number. Infinity and NaN are not accepted.`);
928
+ }
929
+ return Object.is(value, -0) ? "0" : JSON.stringify(value);
930
+ }
931
+
932
+ // src/dialects/pg/decimal.ts
933
+ function numeric(precision, scale, options) {
934
+ const baseType = numericType(precision, scale);
935
+ if (options?.as === "number") {
936
+ return required({
937
+ baseType,
938
+ encode: encodeNumericNumber,
939
+ decode: decodeNumericNumber,
940
+ sqlForm: "raw",
941
+ typeLabel: "number"
942
+ });
943
+ }
944
+ if (options !== undefined && options.as !== undefined && options.as !== "string") {
945
+ definition(`numeric as ${String(options.as)} must be string or number.`);
946
+ }
947
+ return required({
948
+ baseType,
949
+ encode: encodeNumericString,
950
+ decode: decodeNumericString,
951
+ sqlForm: "raw"
952
+ });
953
+ }
954
+ function real() {
955
+ return required({
956
+ baseType: "real",
957
+ encode: (value) => decimalText(value, "real"),
958
+ decode: (wire) => decodeFinite(wire, "real"),
959
+ sqlForm: "raw"
960
+ });
961
+ }
962
+ function double() {
963
+ return required({
964
+ baseType: "double precision",
965
+ encode: (value) => decimalText(value, "double"),
966
+ decode: (wire) => decodeFinite(wire, "double"),
967
+ sqlForm: "raw"
968
+ });
969
+ }
970
+ function encodeNumericString(value) {
971
+ if (!numericText().test(value)) {
972
+ rejected(`numeric ${value} must be a decimal literal, for example 1, -2.5, or 1e-3.`);
973
+ }
974
+ return value;
975
+ }
976
+ function decodeNumericString(wire) {
977
+ return encodeNumericString(wire);
978
+ }
979
+ function encodeNumericNumber(value) {
980
+ return decimalText(value, "numeric");
981
+ }
982
+ function decodeNumericNumber(wire) {
983
+ encodeNumericString(wire);
984
+ return Number(wire);
985
+ }
986
+ function numericType(precision, scale) {
987
+ if (precision === undefined && scale === undefined) {
988
+ return "numeric";
989
+ }
990
+ if (precision === undefined || !Number.isInteger(precision) || precision < 1 || precision > 1000) {
991
+ definition(`numeric precision ${String(precision)} must be an integer from 1 to 1000.`);
992
+ }
993
+ if (scale === undefined) {
994
+ return `numeric(${String(precision)})`;
995
+ }
996
+ if (!Number.isInteger(scale) || scale < 0 || scale > precision) {
997
+ definition(`numeric scale ${String(scale)} must be an integer from 0 to ${String(precision)}.`);
998
+ }
999
+ return `numeric(${String(precision)},${String(scale)})`;
1000
+ }
1001
+ function decodeFinite(wire, role) {
1002
+ const value = Number(wire);
1003
+ if (!Number.isFinite(value)) {
1004
+ rejected(`${role} ${wire} must be a finite number.`);
1005
+ }
1006
+ return Object.is(value, -0) ? 0 : value;
1007
+ }
1008
+ function numericText() {
1009
+ numericPattern ??= /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
1010
+ return numericPattern;
1011
+ }
1012
+ var numericPattern;
1013
+ // src/dialects/pg/bool.ts
1014
+ function boolean() {
1015
+ return required({
1016
+ baseType: "boolean",
1017
+ encode: encodeBoolean,
1018
+ decode: decodeBoolean,
1019
+ sqlForm: "raw"
1020
+ });
1021
+ }
1022
+ function bytea() {
1023
+ return required({
1024
+ baseType: "bytea",
1025
+ encode: encodeBytea,
1026
+ decode: decodeBytea,
1027
+ sqlForm: "quote"
1028
+ });
1029
+ }
1030
+ function encodeBoolean(value) {
1031
+ return value ? "true" : "false";
1032
+ }
1033
+ function decodeBoolean(wire) {
1034
+ if (wire === "true" || wire === "t" || wire === "1") {
1035
+ return true;
1036
+ }
1037
+ if (wire === "false" || wire === "f" || wire === "0") {
1038
+ return false;
1039
+ }
1040
+ rejected(`boolean ${wire} must be true, false, t, f, 1, or 0.`);
1041
+ }
1042
+ function encodeBytea(value) {
1043
+ let hex = "";
1044
+ for (const byte of value) {
1045
+ hex += byte.toString(16).padStart(2, "0");
1046
+ }
1047
+ return `\\x${hex}`;
1048
+ }
1049
+ function decodeBytea(wire) {
1050
+ if (!wire.startsWith("\\x") || (wire.length - 2) % 2 !== 0) {
1051
+ rejected(`bytea ${wire} must be \\x followed by an even number of hex digits.`);
1052
+ }
1053
+ const bytes = new Uint8Array((wire.length - 2) / 2);
1054
+ for (let index = 0;index < bytes.length; index += 1) {
1055
+ const hex = wire.slice(2 + index * 2, 4 + index * 2);
1056
+ const byte = Number.parseInt(hex, 16);
1057
+ if (!hexText().test(hex) || Number.isNaN(byte)) {
1058
+ rejected(`bytea ${wire} must be \\x followed by an even number of hex digits.`);
1059
+ }
1060
+ bytes[index] = byte;
1061
+ }
1062
+ return bytes;
1063
+ }
1064
+ function hexText() {
1065
+ hexPattern ??= /^[0-9a-f]{2}$/i;
1066
+ return hexPattern;
1067
+ }
1068
+ var hexPattern;
1069
+ // src/dialects/pg/json.ts
1070
+ function jsonReplacer(_key, value) {
1071
+ if (typeof value === "bigint") {
1072
+ return value.toString();
1073
+ }
1074
+ return value;
1075
+ }
1076
+ function json() {
1077
+ return required({
1078
+ baseType: "json",
1079
+ encode: encodeJson,
1080
+ decode: decodeJson,
1081
+ sqlForm: "json"
1082
+ });
1083
+ }
1084
+ function jsonb() {
1085
+ return required({
1086
+ baseType: "jsonb",
1087
+ encode: encodeJson,
1088
+ decode: decodeJson,
1089
+ sqlForm: "jsonb"
1090
+ });
1091
+ }
1092
+ function encodeJson(value) {
1093
+ const text = JSON.stringify(value, jsonReplacer);
1094
+ if (text === undefined) {
1095
+ rejected("json value must be JSON data. undefined and functions are not accepted. bigint is written as a decimal string.");
1096
+ }
1097
+ return text;
1098
+ }
1099
+ function decodeJson(wire) {
1100
+ try {
1101
+ return JSON.parse(wire);
1102
+ } catch {
1103
+ rejected("json text must be JSON, for example null, a string, a number, true, {}, or [].");
1104
+ }
1105
+ }
1106
+ // src/dialects/pg/keys.ts
1107
+ function id() {
1108
+ return openColumn({
1109
+ baseType: "uuid",
1110
+ nullable: false,
1111
+ hasDefault: true,
1112
+ generated: false,
1113
+ guarded: true,
1114
+ hidden: false,
1115
+ omitWrite: true,
1116
+ defaultSql: "uuidv7()",
1117
+ primaryKey: true,
1118
+ encode: encodeUuid,
1119
+ decode: decodeUuid,
1120
+ sqlForm: "quote"
1121
+ });
1122
+ }
1123
+ function uuid() {
1124
+ return required({
1125
+ baseType: "uuid",
1126
+ encode: encodeUuid,
1127
+ decode: decodeUuid,
1128
+ sqlForm: "quote"
1129
+ });
1130
+ }
1131
+ function encodeUuid(value) {
1132
+ const text = value.toLowerCase();
1133
+ if (!uuidText().test(text)) {
1134
+ rejected(`uuid ${value} must be 8-4-4-4-12 hexadecimal digits, for example 01890c5a-8f0e-7c3a-9b2d-6e4f1a0b9c8d.`);
1135
+ }
1136
+ return text;
1137
+ }
1138
+ function decodeUuid(wire) {
1139
+ return encodeUuid(wire);
1140
+ }
1141
+ function uuidText() {
1142
+ uuidPattern ??= /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
1143
+ return uuidPattern;
1144
+ }
1145
+ var uuidPattern;
1146
+ // src/dialects/pg/temporal.ts
1147
+ var TEMPORAL_MISSING = "Date and time codecs need the Temporal global. okmodel does not ship a polyfill. Use a runtime that provides Temporal, or assign a polyfill to globalThis.Temporal before using these columns.";
1148
+ function requireTemporal() {
1149
+ const temporal = globalThis.Temporal;
1150
+ if (typeof temporal !== "object" || temporal === null || !("Instant" in temporal)) {
1151
+ rejected(TEMPORAL_MISSING);
1152
+ }
1153
+ }
1154
+ function timePrecision(precision, role) {
1155
+ if (precision === undefined) {
1156
+ return;
1157
+ }
1158
+ if (!Number.isInteger(precision) || precision < 0 || precision > 6) {
1159
+ definition(`${role} precision ${String(precision)} must be an integer from 0 to 6.`);
1160
+ }
1161
+ return precision;
1162
+ }
1163
+ function encodeInstant(value, precision) {
1164
+ requireTemporal();
1165
+ if (precision === undefined) {
1166
+ return value.toString();
1167
+ }
1168
+ return value.toString({ fractionalSecondDigits: precision });
1169
+ }
1170
+ function decodeInstant(wire) {
1171
+ requireTemporal();
1172
+ try {
1173
+ return Temporal.Instant.from(wire);
1174
+ } catch {
1175
+ rejected(`timestamptz ${wire} must be an ISO-8601 instant, for example 2020-01-01T00:00:00Z.`);
1176
+ }
1177
+ }
1178
+ function encodeDateTime(value, precision) {
1179
+ requireTemporal();
1180
+ if (precision === undefined) {
1181
+ return value.toString();
1182
+ }
1183
+ return value.toString({ fractionalSecondDigits: precision });
1184
+ }
1185
+ function decodeDateTime(wire) {
1186
+ requireTemporal();
1187
+ try {
1188
+ return Temporal.PlainDateTime.from(wire);
1189
+ } catch {
1190
+ rejected(`timestamp ${wire} must be an ISO-8601 date-time without a zone, for example 2020-01-01T00:00:00.`);
1191
+ }
1192
+ }
1193
+ function encodeDate(value) {
1194
+ requireTemporal();
1195
+ return value.toString();
1196
+ }
1197
+ function decodeDate(wire) {
1198
+ requireTemporal();
1199
+ try {
1200
+ return Temporal.PlainDate.from(wire);
1201
+ } catch {
1202
+ rejected(`date ${wire} must be YYYY-MM-DD.`);
1203
+ }
1204
+ }
1205
+ function encodeTime(value, precision) {
1206
+ requireTemporal();
1207
+ if (precision === undefined) {
1208
+ return value.toString();
1209
+ }
1210
+ return value.toString({ fractionalSecondDigits: precision });
1211
+ }
1212
+ function decodeTime(wire) {
1213
+ requireTemporal();
1214
+ try {
1215
+ return Temporal.PlainTime.from(wire);
1216
+ } catch {
1217
+ rejected(`time ${wire} must be a time of day, for example 00:00:00.`);
1218
+ }
1219
+ }
1220
+ function encodeTimeZone(value, precision) {
1221
+ requireTemporal();
1222
+ if (!offsetText().test(value.offset)) {
1223
+ rejected(`timetz offset ${value.offset} must be ±HH:MM.`);
1224
+ }
1225
+ return `${encodeTime(value.time, precision)}${value.offset}`;
1226
+ }
1227
+ function decodeTimeZone(wire) {
1228
+ const match = timeZoneText().exec(wire);
1229
+ const time = match?.[1];
1230
+ const offset = match?.[2];
1231
+ if (time === undefined || offset === undefined) {
1232
+ rejected(`timetz ${wire} must be a time of day plus ±HH:MM, for example 00:00:00+00:00.`);
1233
+ }
1234
+ return { time: decodeTime(time), offset };
1235
+ }
1236
+ function encodeDuration(value) {
1237
+ requireTemporal();
1238
+ return value.toString();
1239
+ }
1240
+ function decodeDuration(wire) {
1241
+ requireTemporal();
1242
+ try {
1243
+ return Temporal.Duration.from(wire);
1244
+ } catch {
1245
+ rejected(`interval ${wire} must be an ISO-8601 duration, for example PT1H.`);
1246
+ }
1247
+ }
1248
+ function intervalFields(fields) {
1249
+ switch (fields) {
1250
+ case "year":
1251
+ case "month":
1252
+ case "day":
1253
+ case "hour":
1254
+ case "minute":
1255
+ case "second":
1256
+ case "year to month":
1257
+ case "day to hour":
1258
+ case "day to minute":
1259
+ case "day to second":
1260
+ case "hour to minute":
1261
+ case "hour to second":
1262
+ case "minute to second":
1263
+ return fields;
1264
+ default:
1265
+ definition(`interval fields ${fields} must be one of: year, month, day, hour, minute, second, year to month, day to hour, day to minute, day to second, hour to minute, hour to second, minute to second.`);
1266
+ }
1267
+ }
1268
+ function offsetText() {
1269
+ offsetPattern ??= /^[+-]\d{2}:\d{2}$/;
1270
+ return offsetPattern;
1271
+ }
1272
+ function timeZoneText() {
1273
+ timeZonePattern ??= /^(.+)([+-]\d{2}:\d{2})$/;
1274
+ return timeZonePattern;
1275
+ }
1276
+ var offsetPattern;
1277
+ var timeZonePattern;
1278
+
1279
+ // src/dialects/pg/time.ts
1280
+ function timestamptz(precision) {
1281
+ const digits = timePrecision(precision, "timestamptz");
1282
+ return required({
1283
+ baseType: timed("timestamptz", digits),
1284
+ encode: (value) => encodeInstant(value, digits),
1285
+ decode: decodeInstant,
1286
+ sqlForm: "quote"
1287
+ });
1288
+ }
1289
+ function timestamp(precision) {
1290
+ const digits = timePrecision(precision, "timestamp");
1291
+ return required({
1292
+ baseType: timed("timestamp", digits),
1293
+ encode: (value) => encodeDateTime(value, digits),
1294
+ decode: decodeDateTime,
1295
+ sqlForm: "quote"
1296
+ });
1297
+ }
1298
+ function date() {
1299
+ return required({
1300
+ baseType: "date",
1301
+ encode: encodeDate,
1302
+ decode: decodeDate,
1303
+ sqlForm: "quote"
1304
+ });
1305
+ }
1306
+ function time(precision) {
1307
+ const digits = timePrecision(precision, "time");
1308
+ return required({
1309
+ baseType: timed("time", digits),
1310
+ encode: (value) => encodeTime(value, digits),
1311
+ decode: decodeTime,
1312
+ sqlForm: "quote"
1313
+ });
1314
+ }
1315
+ function timetz(precision) {
1316
+ const digits = timePrecision(precision, "timetz");
1317
+ return required({
1318
+ baseType: timed("timetz", digits),
1319
+ encode: (value) => encodeTimeZone(value, digits),
1320
+ decode: decodeTimeZone,
1321
+ sqlForm: "quote"
1322
+ });
1323
+ }
1324
+ function interval(fields) {
1325
+ const qualifier = fields === undefined ? undefined : intervalFields(fields);
1326
+ return required({
1327
+ baseType: qualifier === undefined ? "interval" : `interval ${qualifier}`,
1328
+ encode: encodeDuration,
1329
+ decode: decodeDuration,
1330
+ sqlForm: "quote"
1331
+ });
1332
+ }
1333
+ function timed(name, precision) {
1334
+ if (precision === undefined) {
1335
+ return name;
1336
+ }
1337
+ return `${name}(${String(precision)})`;
1338
+ }
1339
+ // src/dialects/pg/range.ts
1340
+ function tstzrange() {
1341
+ return rangeColumn("tstzrange", encodeInstant, decodeInstant, true);
1342
+ }
1343
+ function daterange() {
1344
+ return rangeColumn("daterange", encodeDate, decodeDate, true);
1345
+ }
1346
+ function int4range() {
1347
+ return rangeColumn("int4range", (value) => integerToken(value, -2147483648, 2147483647, "int4range"), (wire) => decodeBoundNumber(wire, "int4range"), false);
1348
+ }
1349
+ function int8range() {
1350
+ return rangeColumn("int8range", encodeBigintString, decodeBigintString, false);
1351
+ }
1352
+ function numrange() {
1353
+ return rangeColumn("numrange", encodeNumericString, decodeNumericString, false);
1354
+ }
1355
+ function rangeColumn(baseType, encodeBound, decodeBound, quote) {
1356
+ return required({
1357
+ baseType,
1358
+ encode: (value) => writeRange(value, encodeBound, quote),
1359
+ decode: (wire) => readRange(wire, decodeBound, quote),
1360
+ sqlForm: "cast"
1361
+ });
1362
+ }
1363
+ function writeRange(value, encodeBound, quote) {
1364
+ if (value.empty) {
1365
+ return "empty";
1366
+ }
1367
+ const lower = value.lower === null ? "" : boundText(encodeBound(value.lower), quote);
1368
+ const upper = value.upper === null ? "" : boundText(encodeBound(value.upper), quote);
1369
+ const open = value.lowerInclusive ? "[" : "(";
1370
+ const close = value.upperInclusive ? "]" : ")";
1371
+ return `${open}${lower},${upper}${close}`;
1372
+ }
1373
+ function boundText(encoded, quote) {
1374
+ if (!quote) {
1375
+ return encoded;
1376
+ }
1377
+ return `"${encoded.replaceAll("\\", "\\\\").replaceAll('"', "\\\"")}"`;
1378
+ }
1379
+ function readRange(wire, decodeBound, quote) {
1380
+ if (wire === "empty") {
1381
+ return { empty: true };
1382
+ }
1383
+ const open = wire[0];
1384
+ const close = wire[wire.length - 1];
1385
+ if (open !== "[" && open !== "(" || close !== "]" && close !== ")") {
1386
+ rejected(`range ${wire} is not a range literal.`);
1387
+ }
1388
+ const body = wire.slice(1, -1);
1389
+ const split = splitBound(body);
1390
+ if (split === undefined) {
1391
+ rejected(`range ${wire} is not a range literal.`);
1392
+ }
1393
+ return {
1394
+ empty: false,
1395
+ lowerInclusive: open === "[",
1396
+ upperInclusive: close === "]",
1397
+ lower: split.lower.length === 0 ? null : decodeBound(unquote(split.lower, quote)),
1398
+ upper: split.upper.length === 0 ? null : decodeBound(unquote(split.upper, quote))
1399
+ };
1400
+ }
1401
+ function splitBound(body) {
1402
+ let quoted = false;
1403
+ for (let index = 0;index < body.length; index += 1) {
1404
+ const char = body[index];
1405
+ if (char === "\\") {
1406
+ index += 1;
1407
+ continue;
1408
+ }
1409
+ if (char === '"') {
1410
+ quoted = !quoted;
1411
+ continue;
1412
+ }
1413
+ if (char === "," && !quoted) {
1414
+ return { lower: body.slice(0, index), upper: body.slice(index + 1) };
1415
+ }
1416
+ }
1417
+ return;
1418
+ }
1419
+ function unquote(token, quote) {
1420
+ if (!quote) {
1421
+ return token;
1422
+ }
1423
+ if (!token.startsWith('"') || !token.endsWith('"')) {
1424
+ rejected(`range bound ${token} is not quoted.`);
1425
+ }
1426
+ return token.slice(1, -1).replaceAll("\\\"", '"').replaceAll("\\\\", "\\");
1427
+ }
1428
+ function decodeBoundNumber(wire, role) {
1429
+ const value = Number(wire);
1430
+ integerToken(value, -2147483648, 2147483647, role);
1431
+ return value;
1432
+ }
1433
+ // src/dialects/pg/network.ts
1434
+ function inet() {
1435
+ return network("inet", false);
1436
+ }
1437
+ function cidr() {
1438
+ return network("cidr", true);
1439
+ }
1440
+ function macaddr() {
1441
+ return mac(6, "macaddr");
1442
+ }
1443
+ function macaddr8() {
1444
+ return mac(8, "macaddr8");
1445
+ }
1446
+ function network(baseType, prefixRequired) {
1447
+ return required({
1448
+ baseType,
1449
+ encode: (value) => encodeNetwork(value, baseType, prefixRequired),
1450
+ decode: (wire) => encodeNetwork(wire, baseType, prefixRequired),
1451
+ sqlForm: "quote"
1452
+ });
1453
+ }
1454
+ function mac(groups, baseType) {
1455
+ return required({
1456
+ baseType,
1457
+ encode: (value) => encodeMac(value, groups, baseType),
1458
+ decode: (wire) => encodeMac(wire, groups, baseType),
1459
+ sqlForm: "quote"
1460
+ });
1461
+ }
1462
+ function encodeNetwork(value, role, prefixRequired) {
1463
+ const text = value.toLowerCase();
1464
+ const slash = text.lastIndexOf("/");
1465
+ const host = slash === -1 ? text : text.slice(0, slash);
1466
+ const prefix = slash === -1 ? undefined : text.slice(slash + 1);
1467
+ if (prefixRequired && prefix === undefined) {
1468
+ rejected(`${role} ${value} needs a prefix length.`);
1469
+ }
1470
+ if (prefix !== undefined && (!/^\d{1,3}$/.test(prefix) || Number(prefix) > 128)) {
1471
+ rejected(`${role} prefix ${prefix} is not a length.`);
1472
+ }
1473
+ if (host.includes(":")) {
1474
+ if (!/^[0-9a-f:]+$/.test(host) || host.length > 39) {
1475
+ rejected(`${role} ${value} is not an address.`);
1476
+ }
1477
+ return prefix === undefined ? host : `${host}/${prefix}`;
1478
+ }
1479
+ const octets = host.split(".");
1480
+ if (octets.length !== 4 || octets.some((octet) => !octetOk(octet))) {
1481
+ rejected(`${role} ${value} is not an address.`);
1482
+ }
1483
+ if (prefix !== undefined && Number(prefix) > 32) {
1484
+ rejected(`${role} prefix ${prefix} is not a length.`);
1485
+ }
1486
+ return prefix === undefined ? host : `${host}/${prefix}`;
1487
+ }
1488
+ function octetOk(octet) {
1489
+ if (!/^\d{1,3}$/.test(octet)) {
1490
+ return false;
1491
+ }
1492
+ const value = Number(octet);
1493
+ return value <= 255 && octet === String(value);
1494
+ }
1495
+ function encodeMac(value, groups, role) {
1496
+ const text = value.toLowerCase();
1497
+ const parts = text.split(":");
1498
+ if (parts.length !== groups || parts.some((part) => !/^[0-9a-f]{2}$/.test(part))) {
1499
+ rejected(`${role} ${value} is not a MAC address.`);
1500
+ }
1501
+ return text;
1502
+ }
1503
+ // src/dialects/pg/geometry.ts
1504
+ function point() {
1505
+ return required({
1506
+ baseType: "point",
1507
+ encode: encodePoint,
1508
+ decode: decodePoint,
1509
+ sqlForm: "quote"
1510
+ });
1511
+ }
1512
+ function line() {
1513
+ return required({
1514
+ baseType: "line",
1515
+ encode: encodeLine,
1516
+ decode: decodeLine,
1517
+ sqlForm: "quote"
1518
+ });
1519
+ }
1520
+ function encodePoint(value) {
1521
+ return `(${decimalText(value.x, "point")},${decimalText(value.y, "point")})`;
1522
+ }
1523
+ function decodePoint(wire) {
1524
+ const match = pointText().exec(wire);
1525
+ const x = match?.[1];
1526
+ const y = match?.[2];
1527
+ if (x === undefined || y === undefined) {
1528
+ rejected(`point ${wire} must be (x,y), for example (1,2).`);
1529
+ }
1530
+ return { x: finite(Number(x), "point"), y: finite(Number(y), "point") };
1531
+ }
1532
+ function encodeLine(value) {
1533
+ return `{${decimalText(value.a, "line")},${decimalText(value.b, "line")},${decimalText(value.c, "line")}}`;
1534
+ }
1535
+ function decodeLine(wire) {
1536
+ const match = lineText().exec(wire);
1537
+ const a = match?.[1];
1538
+ const b = match?.[2];
1539
+ const c = match?.[3];
1540
+ if (a === undefined || b === undefined || c === undefined) {
1541
+ rejected(`line ${wire} must be {a,b,c}, for example {1,0,-1}.`);
1542
+ }
1543
+ return {
1544
+ a: finite(Number(a), "line"),
1545
+ b: finite(Number(b), "line"),
1546
+ c: finite(Number(c), "line")
1547
+ };
1548
+ }
1549
+ function finite(value, role) {
1550
+ if (!Number.isFinite(value)) {
1551
+ rejected(`${role} ${String(value)} must be a finite number. Infinity and NaN are not accepted.`);
1552
+ }
1553
+ return Object.is(value, -0) ? 0 : value;
1554
+ }
1555
+ function pointText() {
1556
+ pointPattern ??= /^\(([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?),([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)\)$/;
1557
+ return pointPattern;
1558
+ }
1559
+ function lineText() {
1560
+ linePattern ??= /^\{([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?),([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?),([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)\}$/;
1561
+ return linePattern;
1562
+ }
1563
+ var pointPattern;
1564
+ var linePattern;
1565
+ // src/dialects/pg/search.ts
1566
+ function tsvector() {
1567
+ return required({
1568
+ baseType: "tsvector",
1569
+ encode: encodeText,
1570
+ decode: decodeText,
1571
+ sqlForm: "quote"
1572
+ });
1573
+ }
1574
+ function ltree() {
1575
+ return required({
1576
+ baseType: "ltree",
1577
+ encode: encodeLtree,
1578
+ decode: encodeLtree,
1579
+ sqlForm: "quote",
1580
+ extension: "ltree"
1581
+ });
1582
+ }
1583
+ function encodeLtree(value) {
1584
+ if (!ltreeText().test(value)) {
1585
+ rejected(`ltree ${value} must be labels of letters, digits, or underscores separated by dots, for example top.science.`);
1586
+ }
1587
+ return value;
1588
+ }
1589
+ function ltreeText() {
1590
+ ltreePattern ??= /^[A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*$/;
1591
+ return ltreePattern;
1592
+ }
1593
+ var ltreePattern;
1594
+ // src/dialects/pg/enum.ts
1595
+ function enumColumn(name, values) {
1596
+ if (name.length === 0) {
1597
+ definition("enum name must be a non-empty type name.");
1598
+ }
1599
+ if (values.length === 0) {
1600
+ definition("enum must list at least one label.");
1601
+ }
1602
+ const allowed = new Set;
1603
+ for (const value of values) {
1604
+ if (value.length === 0) {
1605
+ definition("enum labels must be non-empty strings.");
1606
+ }
1607
+ if (allowed.has(value)) {
1608
+ definition(`enum label ${value} is repeated. Each label is accepted once.`);
1609
+ }
1610
+ allowed.add(value);
1611
+ }
1612
+ return required({
1613
+ baseType: name,
1614
+ typeDependency: name,
1615
+ enumLabels: [...values],
1616
+ typeLabel: values.map((value) => JSON.stringify(value)).join(" | "),
1617
+ encode: (value) => {
1618
+ if (!allowed.has(value)) {
1619
+ rejected(`enum rejected ${value}. Accepted labels: ${[...allowed].join(", ")}.`);
1620
+ }
1621
+ return value;
1622
+ },
1623
+ decode: (wire) => {
1624
+ if (!allowed.has(wire)) {
1625
+ rejected(`enum rejected ${wire}. Accepted labels: ${[...allowed].join(", ")}.`);
1626
+ }
1627
+ return wire;
1628
+ },
1629
+ sqlForm: "quote"
1630
+ });
1631
+ }
1632
+ function domain(_name, _base, _check) {
1633
+ unavailable("t.domain() is not available yet. It arrives in 0.3.");
1634
+ }
1635
+ // src/dialects/pg/namespace.ts
1636
+ var t = {
1637
+ id,
1638
+ identity,
1639
+ uuid,
1640
+ smallint,
1641
+ integer,
1642
+ bigint,
1643
+ numeric,
1644
+ real,
1645
+ double,
1646
+ text,
1647
+ varchar,
1648
+ char,
1649
+ citext,
1650
+ boolean,
1651
+ bytea,
1652
+ json,
1653
+ jsonb,
1654
+ timestamptz,
1655
+ timestamp,
1656
+ date,
1657
+ time,
1658
+ timetz,
1659
+ interval,
1660
+ tstzrange,
1661
+ daterange,
1662
+ int4range,
1663
+ int8range,
1664
+ numrange,
1665
+ inet,
1666
+ cidr,
1667
+ macaddr,
1668
+ macaddr8,
1669
+ point,
1670
+ line,
1671
+ tsvector,
1672
+ ltree,
1673
+ enum: enumColumn,
1674
+ domain,
1675
+ custom
1676
+ };
1677
+ // src/dialects/pg/ops/between.ts
1678
+ function between(from, to) {
1679
+ return tag("between", [from, to]);
1680
+ }
1681
+ // src/dialects/pg/ops/contains.ts
1682
+ function contains(value) {
1683
+ return tag("contains", value);
1684
+ }
1685
+ // src/dialects/pg/ops/endsWith.ts
1686
+ function endsWith(value) {
1687
+ return tag("endsWith", value);
1688
+ }
1689
+ // src/dialects/pg/ops/eq.ts
1690
+ function eq(value) {
1691
+ return tag("eq", value);
1692
+ }
1693
+ // src/dialects/pg/ops/every.ts
1694
+ function every(where) {
1695
+ return tag("every", where);
1696
+ }
1697
+ // src/dialects/pg/ops/gt.ts
1698
+ function gt(value) {
1699
+ return tag("gt", value);
1700
+ }
1701
+ // src/dialects/pg/ops/inc.ts
1702
+ function inc(value) {
1703
+ return tag("inc", value);
1704
+ }
1705
+ // src/dialects/pg/ops/gte.ts
1706
+ function gte(value) {
1707
+ return tag("gte", value);
1708
+ }
1709
+ // src/dialects/pg/ops/has.ts
1710
+ function has(where) {
1711
+ return tag("has", where);
1712
+ }
1713
+ // src/dialects/pg/ops/ilike.ts
1714
+ function ilike(pattern) {
1715
+ return tag("ilike", pattern);
1716
+ }
1717
+ // src/dialects/pg/ops/inList.ts
1718
+ function inList(values) {
1719
+ return tag("inList", values);
1720
+ }
1721
+ // src/dialects/pg/ops/like.ts
1722
+ function like(pattern) {
1723
+ return tag("like", pattern);
1724
+ }
1725
+ // src/dialects/pg/ops/lt.ts
1726
+ function lt(value) {
1727
+ return tag("lt", value);
1728
+ }
1729
+ // src/dialects/pg/ops/lte.ts
1730
+ function lte(value) {
1731
+ return tag("lte", value);
1732
+ }
1733
+ // src/dialects/pg/ops/none.ts
1734
+ function none(where) {
1735
+ return tag("none", where);
1736
+ }
1737
+ // src/dialects/pg/ops/not.ts
1738
+ function not(value) {
1739
+ return tag("not", value);
1740
+ }
1741
+ // src/dialects/pg/ops/notIn.ts
1742
+ function notIn(values) {
1743
+ return tag("notIn", values);
1744
+ }
1745
+ // src/dialects/pg/ops/or.ts
1746
+ function or(branches) {
1747
+ return tag("or", branches);
1748
+ }
1749
+ // src/dialects/pg/ops/startsWith.ts
1750
+ function startsWith(value) {
1751
+ return tag("startsWith", value);
1752
+ }
1753
+ export {
1754
+ ColumnBuilder2 as ColumnBuilder,
1755
+ between,
1756
+ bigint,
1757
+ boolean,
1758
+ bytea,
1759
+ char,
1760
+ cidr,
1761
+ citext,
1762
+ contains,
1763
+ custom,
1764
+ date,
1765
+ daterange,
1766
+ domain,
1767
+ double,
1768
+ endsWith,
1769
+ enumColumn as enum,
1770
+ eq,
1771
+ every,
1772
+ gt,
1773
+ gte,
1774
+ has,
1775
+ id,
1776
+ identity,
1777
+ ilike,
1778
+ inList,
1779
+ inc,
1780
+ index2 as index,
1781
+ inet,
1782
+ int4range,
1783
+ int8range,
1784
+ integer,
1785
+ interval,
1786
+ json,
1787
+ jsonReplacer,
1788
+ jsonb,
1789
+ like,
1790
+ line,
1791
+ lt,
1792
+ lte,
1793
+ ltree,
1794
+ macaddr,
1795
+ macaddr8,
1796
+ many,
1797
+ none,
1798
+ not,
1799
+ notIn,
1800
+ numeric,
1801
+ numrange,
1802
+ one,
1803
+ or,
1804
+ point,
1805
+ real,
1806
+ schema,
1807
+ smallint,
1808
+ sql2 as sql,
1809
+ startsWith,
1810
+ t,
1811
+ table2 as table,
1812
+ text,
1813
+ time,
1814
+ timestamp,
1815
+ timestamptz,
1816
+ timetz,
1817
+ tstzrange,
1818
+ tsvector,
1819
+ uuid,
1820
+ varchar
1821
+ };