okmodel 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/README.md +727 -37
  2. package/dist/adapters/capabilities.d.ts +34 -0
  3. package/dist/adapters/pg/bunsql-extra.d.ts +17 -0
  4. package/dist/adapters/pg/bunsql.d.ts +60 -0
  5. package/dist/adapters/pg/call.d.ts +2 -12
  6. package/dist/adapters/pg/nodepostgres-extra.d.ts +18 -0
  7. package/dist/adapters/pg/nodepostgres.d.ts +57 -0
  8. package/dist/adapters/pg/postgres-extra.d.ts +50 -1
  9. package/dist/adapters/pg/postgresjs.d.ts +0 -4
  10. package/dist/adapters/pg/session.d.ts +94 -0
  11. package/dist/adapters/pg/watch.d.ts +18 -0
  12. package/dist/contracts/catalog/enum.d.ts +31 -4
  13. package/dist/contracts/catalog/extension.d.ts +61 -0
  14. package/dist/contracts/catalog/object.d.ts +1 -0
  15. package/dist/contracts/catalog/privilege.d.ts +64 -0
  16. package/dist/contracts/catalog/routine.d.ts +56 -0
  17. package/dist/contracts/catalog/types.d.ts +179 -18
  18. package/dist/contracts/catalog/view.d.ts +73 -0
  19. package/dist/contracts/connection.d.ts +13 -0
  20. package/dist/contracts/driver.d.ts +18 -5
  21. package/dist/contracts/error.d.ts +17 -1
  22. package/dist/contracts/generator.d.ts +63 -0
  23. package/dist/contracts/index.d.ts +2 -2
  24. package/dist/contracts/index.js +1 -1
  25. package/dist/contracts/internal.js +11 -9
  26. package/dist/contracts/location.d.ts +24 -0
  27. package/dist/contracts/rows.d.ts +15 -0
  28. package/dist/dialects/pg/archive-bind.d.ts +22 -0
  29. package/dist/dialects/pg/column.d.ts +129 -10
  30. package/dist/dialects/pg/compile.d.ts +1 -0
  31. package/dist/dialects/pg/custom.d.ts +6 -0
  32. package/dist/dialects/pg/ddl.d.ts +65 -1
  33. package/dist/dialects/pg/enum.d.ts +10 -8
  34. package/dist/dialects/pg/ext/citext.d.ts +14 -0
  35. package/dist/dialects/pg/ext/citext.js +11 -0
  36. package/dist/dialects/pg/ext/index.d.ts +52 -0
  37. package/dist/dialects/pg/ext/index.js +6 -0
  38. package/dist/dialects/pg/ext/pg-trgm.d.ts +53 -0
  39. package/dist/dialects/pg/ext/pg-trgm.js +42 -0
  40. package/dist/dialects/pg/filter-parse.d.ts +19 -0
  41. package/dist/dialects/pg/filters.d.ts +19 -0
  42. package/dist/dialects/pg/fn/index.d.ts +107 -0
  43. package/dist/dialects/pg/fn/index.js +175 -0
  44. package/dist/dialects/pg/fn/touch.d.ts +14 -0
  45. package/dist/dialects/pg/index.d.ts +11 -2
  46. package/dist/dialects/pg/index.js +243 -1062
  47. package/dist/dialects/pg/introspect.d.ts +10 -1
  48. package/dist/dialects/pg/keys.d.ts +19 -3
  49. package/dist/dialects/pg/model.d.ts +223 -2
  50. package/dist/dialects/pg/operator-fit.d.ts +86 -0
  51. package/dist/dialects/pg/operators.d.ts +46 -1
  52. package/dist/dialects/pg/ops/arr-append.d.ts +9 -0
  53. package/dist/dialects/pg/ops/arr-ns.d.ts +7 -0
  54. package/dist/dialects/pg/ops/arr-remove.d.ts +9 -0
  55. package/dist/dialects/pg/ops/containedBy.d.ts +9 -0
  56. package/dist/dialects/pg/ops/contains.d.ts +8 -4
  57. package/dist/dialects/pg/ops/hasAnyKey.d.ts +8 -0
  58. package/dist/dialects/pg/ops/hasKey.d.ts +8 -0
  59. package/dist/dialects/pg/ops/json-ns.d.ts +6 -0
  60. package/dist/dialects/pg/ops/json-set.d.ts +12 -0
  61. package/dist/dialects/pg/ops/matches.d.ts +16 -0
  62. package/dist/dialects/pg/ops/overlaps.d.ts +9 -0
  63. package/dist/dialects/pg/ops/path.d.ts +13 -0
  64. package/dist/dialects/pg/preset.d.ts +76 -0
  65. package/dist/dialects/pg/relations.d.ts +59 -2
  66. package/dist/dialects/pg/role/check.d.ts +45 -0
  67. package/dist/dialects/pg/role/index.d.ts +50 -0
  68. package/dist/dialects/pg/role/sql.d.ts +44 -0
  69. package/dist/dialects/pg/schema.d.ts +109 -5
  70. package/dist/dialects/pg/table.d.ts +137 -11
  71. package/dist/dialects/pg/temporal.d.ts +9 -7
  72. package/dist/dialects/pg/tenancy.d.ts +103 -0
  73. package/dist/dialects/pg/through.d.ts +21 -0
  74. package/dist/dialects/pg/trait.d.ts +141 -0
  75. package/dist/dialects/pg/view/client.d.ts +30 -0
  76. package/dist/dialects/pg/view/depend.d.ts +35 -0
  77. package/dist/dialects/pg/view/index.d.ts +114 -0
  78. package/dist/dialects/pg/view/index.js +289 -0
  79. package/dist/dialects/pg/view/scratch.d.ts +21 -0
  80. package/dist/dialects/pg/where.d.ts +37 -0
  81. package/dist/runtime/aggregate.d.ts +25 -0
  82. package/dist/runtime/archive-handle.d.ts +18 -0
  83. package/dist/runtime/archive-rules.d.ts +12 -0
  84. package/dist/runtime/archive.d.ts +73 -0
  85. package/dist/runtime/batch.d.ts +20 -0
  86. package/dist/runtime/client.d.ts +114 -5
  87. package/dist/runtime/conflict.d.ts +2 -1
  88. package/dist/runtime/exposure.d.ts +27 -0
  89. package/dist/runtime/hookm.d.ts +39 -0
  90. package/dist/runtime/ids/index.d.ts +11 -0
  91. package/dist/runtime/ids/index.js +193 -0
  92. package/dist/runtime/ids/okid-extended.d.ts +19 -0
  93. package/dist/runtime/ids/okid-generator.d.ts +21 -0
  94. package/dist/runtime/ids/okid-shared.d.ts +89 -0
  95. package/dist/runtime/ids/okid.d.ts +24 -0
  96. package/dist/runtime/ids/uuid.d.ts +21 -0
  97. package/dist/runtime/lock.d.ts +22 -0
  98. package/dist/runtime/operator-sql.d.ts +8 -0
  99. package/dist/runtime/page.d.ts +25 -0
  100. package/dist/runtime/pg/bun.d.ts +30 -0
  101. package/dist/runtime/pg/bun.js +52 -0
  102. package/dist/runtime/pg/pg.d.ts +29 -0
  103. package/dist/runtime/pg/pg.js +52 -0
  104. package/dist/runtime/pg/pglite.js +16 -17
  105. package/dist/runtime/pg/postgresjs.js +14 -24
  106. package/dist/runtime/plan.d.ts +146 -7
  107. package/dist/runtime/preset-stack.d.ts +17 -0
  108. package/dist/runtime/presets.d.ts +48 -0
  109. package/dist/runtime/read-stream.d.ts +26 -0
  110. package/dist/runtime/safety/index.d.ts +159 -0
  111. package/dist/runtime/safety/index.js +294 -0
  112. package/dist/runtime/safety-hook.d.ts +42 -0
  113. package/dist/runtime/server-check.d.ts +34 -0
  114. package/dist/runtime/tenancy/index.d.ts +37 -0
  115. package/dist/runtime/tenancy/index.js +368 -0
  116. package/dist/runtime/trait-read.d.ts +30 -0
  117. package/dist/runtime/traits/index.d.ts +133 -0
  118. package/dist/runtime/traits/index.js +646 -0
  119. package/dist/runtime/transaction.d.ts +19 -0
  120. package/dist/runtime/tx.d.ts +22 -7
  121. package/dist/runtime/types.d.ts +376 -27
  122. package/dist/runtime/validate/bind.d.ts +11 -0
  123. package/dist/runtime/validate/closed.d.ts +16 -0
  124. package/dist/runtime/validate/engine.d.ts +41 -0
  125. package/dist/runtime/validate/hook.d.ts +17 -0
  126. package/dist/runtime/validate/index.d.ts +21 -0
  127. package/dist/runtime/validate/index.js +175 -0
  128. package/dist/runtime/validate/load.d.ts +13 -0
  129. package/dist/runtime/validate/places.d.ts +41 -0
  130. package/dist/runtime/validate/rules.d.ts +108 -0
  131. package/dist/runtime/validate/surface.d.ts +102 -0
  132. package/dist/runtime/write.d.ts +30 -5
  133. package/dist/shared/01yr20s5.js +149 -0
  134. package/dist/shared/0jc3ffzf.js +154 -0
  135. package/dist/shared/0v1g424p.js +26 -0
  136. package/dist/shared/1g0drakv.js +225 -0
  137. package/dist/shared/{kprcwaha.js → 1tfdfvhh.js} +5 -3
  138. package/dist/shared/{05w44d8g.js → 2ssqrv66.js} +767 -715
  139. package/dist/shared/3ekj9x92.js +210 -0
  140. package/dist/shared/49ypnfwt.js +1122 -0
  141. package/dist/shared/4e4c6vq2.js +62 -0
  142. package/dist/shared/4m9fn52z.js +70 -0
  143. package/dist/shared/{ycwz42az.js → 52szzakm.js} +6 -26
  144. package/dist/shared/5nmdenb6.js +1229 -0
  145. package/dist/shared/672t5vym.js +12 -0
  146. package/dist/shared/6bazvqwm.js +86 -0
  147. package/dist/shared/6w5j844a.js +201 -0
  148. package/dist/shared/{fgqgsb5z.js → 75xkjv0t.js} +2 -2
  149. package/dist/shared/7fvtrkwr.js +84 -0
  150. package/dist/shared/7m88x3e9.js +45 -0
  151. package/dist/shared/{mtkwv8wx.js → 7p2wbz7f.js} +1 -1
  152. package/dist/shared/83eaefwr.js +10 -0
  153. package/dist/shared/94ez8kzh.js +19 -0
  154. package/dist/shared/96ps4g1z.js +32 -0
  155. package/dist/shared/{xqbhg6cn.js → 999ade2q.js} +95 -10
  156. package/dist/shared/9m80mkac.js +158 -0
  157. package/dist/shared/9x25fxhc.js +150 -0
  158. package/dist/shared/a5sxz52y.js +13 -0
  159. package/dist/shared/axhk06q1.js +156 -0
  160. package/dist/shared/{dynq16vb.js → b066g80h.js} +8 -6
  161. package/dist/shared/bqha5cg6.js +229 -0
  162. package/dist/shared/bryqk9k9.js +31 -0
  163. package/dist/shared/c8aghh05.js +442 -0
  164. package/dist/shared/c9z5g0fm.js +309 -0
  165. package/dist/shared/{m3ypagh5.js → dbjej1tw.js} +124 -15
  166. package/dist/shared/dyj86cmj.js +69 -0
  167. package/dist/shared/e6wg00wd.js +164 -0
  168. package/dist/shared/{hadrfb9h.js → edwqqryd.js} +17 -14
  169. package/dist/shared/{qk3hxfja.js → fptpmvxp.js} +5 -5
  170. package/dist/shared/fs0ex00p.js +76 -0
  171. package/dist/shared/{jjb7yht9.js → g4f5kcx9.js} +3 -3
  172. package/dist/shared/gjthds9j.js +48 -0
  173. package/dist/shared/hcmdtwgh.js +102 -0
  174. package/dist/shared/{g5fhqmwe.js → jkc6ws1p.js} +6 -56
  175. package/dist/shared/{yd1kzwn2.js → jns2wx4t.js} +55 -12
  176. package/dist/shared/k34e0xf2.js +450 -0
  177. package/dist/shared/k3ted12y.js +33 -0
  178. package/dist/shared/k7dwkhvq.js +31 -0
  179. package/dist/shared/{8b04d28c.js → kgnc8grt.js} +2 -2
  180. package/dist/shared/{hd293j9g.js → kyxb62mc.js} +14 -2
  181. package/dist/shared/m78s7898.js +376 -0
  182. package/dist/shared/m9tv7xn7.js +404 -0
  183. package/dist/shared/meck53jv.js +99 -0
  184. package/dist/shared/mt5wswjz.js +90 -0
  185. package/dist/shared/n0dgs0pb.js +75 -0
  186. package/dist/shared/n442h54c.js +14 -0
  187. package/dist/shared/n4jqhpqz.js +27 -0
  188. package/dist/shared/n7mcc1gk.js +132 -0
  189. package/dist/shared/ne02ng5c.js +68 -0
  190. package/dist/shared/{mkjmqjv4.js → nfzc5z0w.js} +2 -104
  191. package/dist/shared/nsva2a6b.js +42 -0
  192. package/dist/shared/p24x2334.js +195 -0
  193. package/dist/shared/qbxzvwwc.js +88 -0
  194. package/dist/shared/{w8samvk1.js → qybh48nd.js} +1 -1
  195. package/dist/shared/r6nwkxnt.js +86 -0
  196. package/dist/shared/{xy79j5hq.js → rrstp3kb.js} +25 -36
  197. package/dist/shared/rwmrejjz.js +26 -0
  198. package/dist/shared/{zhc5vmgf.js → s6gqfn5j.js} +2 -2
  199. package/dist/shared/{523ft3e8.js → scwxrpx2.js} +7 -5
  200. package/dist/shared/{j5cn2963.js → t4tbsy9h.js} +13 -64
  201. package/dist/shared/{y9m15dfv.js → t616ynfn.js} +544 -32
  202. package/dist/shared/{ngk8gj61.js → tbszx8mz.js} +15 -1
  203. package/dist/shared/th14ygv2.js +69 -0
  204. package/dist/shared/tmbdywx0.js +178 -0
  205. package/dist/shared/ty3v3hfk.js +64 -0
  206. package/dist/shared/wekze34d.js +246 -0
  207. package/dist/shared/x6wysh83.js +1132 -0
  208. package/dist/shared/{rgs0st28.js → xrfnq5k8.js} +237 -434
  209. package/dist/shared/{x5x59srr.js → xva8v7qk.js} +416 -83
  210. package/dist/shared/{dkn3y60a.js → xw1d69vd.js} +76 -112
  211. package/dist/shared/y005b761.js +112 -0
  212. package/dist/shared/ydaz1sv3.js +55 -0
  213. package/dist/shared/{p0cbr3g2.js → zwmqxb0g.js} +1 -1
  214. package/dist/tooling/cli.js +40 -12
  215. package/dist/tooling/migrate/apply.d.ts +16 -0
  216. package/dist/tooling/migrate/commands.d.ts +12 -1
  217. package/dist/tooling/migrate/config.d.ts +8 -0
  218. package/dist/tooling/migrate/doctor.d.ts +25 -0
  219. package/dist/tooling/migrate/drift.d.ts +19 -0
  220. package/dist/tooling/migrate/engine.d.ts +36 -0
  221. package/dist/tooling/migrate/ext.d.ts +17 -0
  222. package/dist/tooling/migrate/extensions.d.ts +51 -0
  223. package/dist/tooling/migrate/managed.d.ts +17 -0
  224. package/dist/tooling/migrate/plan.d.ts +14 -1
  225. package/dist/tooling/migrate/project.d.ts +25 -1
  226. package/dist/tooling/migrate/type-name.d.ts +19 -0
  227. package/package.json +61 -4
  228. package/dist/shared/gk7cs7sp.js +0 -566
@@ -1,773 +1,80 @@
1
1
  import {
2
- tag
3
- } from "../../shared/cn0n68ft.js";
2
+ jsonReplacer2,
3
+ json2,
4
+ jsonb2
5
+ } from "../../shared/gjthds9j.js";
6
+ import {
7
+ encodeInstant,
8
+ decodeInstant,
9
+ encodeDate,
10
+ decodeDate,
11
+ timestamptz2,
12
+ timestamp2,
13
+ date2,
14
+ time2,
15
+ timetz2,
16
+ interval2
17
+ } from "../../shared/1g0drakv.js";
18
+ import {
19
+ one2,
20
+ many2,
21
+ manyThrough2,
22
+ schema2
23
+ } from "../../shared/x6wysh83.js";
24
+ import {
25
+ domainType
26
+ } from "../../shared/qbxzvwwc.js";
27
+ import {
28
+ id2,
29
+ uuid2
30
+ } from "../../shared/dyj86cmj.js";
31
+ import {
32
+ table2,
33
+ index2,
34
+ sql2
35
+ } from "../../shared/6w5j844a.js";
36
+ import {
37
+ text2,
38
+ varchar2,
39
+ char2,
40
+ citext2,
41
+ encodeText,
42
+ decodeText
43
+ } from "../../shared/4m9fn52z.js";
4
44
  import {
5
- definition,
6
- unavailable,
7
- rejected,
8
- quoteLiteral,
9
45
  ColumnBuilder2,
10
46
  required,
11
47
  openColumn,
12
- formatType,
13
- table2,
14
- index2,
15
- sql2,
16
- snakeCase,
17
- emittedTypeName,
18
- rejectLater
19
- } from "../../shared/gk7cs7sp.js";
48
+ formatType
49
+ } from "../../shared/c9z5g0fm.js";
20
50
  import {
21
- catalog,
22
- enumType,
23
- sameEnumLabels
24
- } from "../../shared/g5fhqmwe.js";
51
+ definition,
52
+ rejected
53
+ } from "../../shared/tmbdywx0.js";
25
54
  import {
26
- staticNamespace2,
27
- table3,
28
- column2,
29
- index3,
30
- constraint2,
31
- sequence2,
55
+ sameNamespace2,
32
56
  compareText
33
- } from "../../shared/j5cn2963.js";
57
+ } from "../../shared/t4tbsy9h.js";
34
58
  import {
35
- assertIdentifier2,
36
- fitIdentifier2,
37
- assertStoredText
38
- } from "../../shared/mkjmqjv4.js";
59
+ tag
60
+ } from "../../shared/cn0n68ft.js";
39
61
  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
62
+ catalogError2
63
+ } from "../../shared/52szzakm.js";
64
+ var __defProp = Object.defineProperty;
65
+ var __returnValue = (v) => v;
66
+ function __exportSetter(name, newValue) {
67
+ this[name] = __returnValue.bind(null, newValue);
68
+ }
69
+ var __export = (target, all) => {
70
+ for (var name in all)
71
+ __defProp(target, name, {
72
+ get: all[name],
73
+ enumerable: true,
74
+ configurable: true,
75
+ set: __exportSetter.bind(all, name)
59
76
  });
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
77
  };
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
78
  // src/dialects/pg/custom.ts
772
79
  function custom(spec) {
773
80
  if (spec.sqlType.length === 0) {
@@ -777,6 +84,7 @@ function custom(spec) {
777
84
  baseType: spec.sqlType,
778
85
  encode: spec.encode,
779
86
  decode: spec.decode,
87
+ ...spec.accepts !== undefined ? { accepts: spec.accepts } : {},
780
88
  sqlForm: spec.sqlForm ?? "quote",
781
89
  ...spec.extension !== undefined ? { extension: spec.extension } : {}
782
90
  });
@@ -1024,6 +332,7 @@ function bytea() {
1024
332
  baseType: "bytea",
1025
333
  encode: encodeBytea,
1026
334
  decode: decodeBytea,
335
+ accepts: ["Uint8Array"],
1027
336
  sqlForm: "quote"
1028
337
  });
1029
338
  }
@@ -1066,276 +375,6 @@ function hexText() {
1066
375
  return hexPattern;
1067
376
  }
1068
377
  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
378
  // src/dialects/pg/range.ts
1340
379
  function tstzrange() {
1341
380
  return rangeColumn("tstzrange", encodeInstant, decodeInstant, true);
@@ -1357,6 +396,7 @@ function rangeColumn(baseType, encodeBound, decodeBound, quote) {
1357
396
  baseType,
1358
397
  encode: (value) => writeRange(value, encodeBound, quote),
1359
398
  decode: (wire) => readRange(wire, decodeBound, quote),
399
+ accepts: ["Object"],
1360
400
  sqlForm: "cast"
1361
401
  });
1362
402
  }
@@ -1364,6 +404,9 @@ function writeRange(value, encodeBound, quote) {
1364
404
  if (value.empty) {
1365
405
  return "empty";
1366
406
  }
407
+ if (value.empty !== false) {
408
+ rejected("range needs empty: true, or empty: false with lower and upper.");
409
+ }
1367
410
  const lower = value.lower === null ? "" : boundText(encodeBound(value.lower), quote);
1368
411
  const upper = value.upper === null ? "" : boundText(encodeBound(value.upper), quote);
1369
412
  const open = value.lowerInclusive ? "[" : "(";
@@ -1506,6 +549,7 @@ function point() {
1506
549
  baseType: "point",
1507
550
  encode: encodePoint,
1508
551
  decode: decodePoint,
552
+ accepts: ["Object"],
1509
553
  sqlForm: "quote"
1510
554
  });
1511
555
  }
@@ -1514,6 +558,7 @@ function line() {
1514
558
  baseType: "line",
1515
559
  encode: encodeLine,
1516
560
  decode: decodeLine,
561
+ accepts: ["Object"],
1517
562
  sqlForm: "quote"
1518
563
  });
1519
564
  }
@@ -1629,34 +674,109 @@ function enumColumn(name, values) {
1629
674
  sqlForm: "quote"
1630
675
  });
1631
676
  }
1632
- function domain(_name, _base, _check) {
1633
- unavailable("t.domain() is not available yet. It arrives in 0.3.");
677
+ function domain(name, base, check) {
678
+ if (name.length === 0)
679
+ definition("domain name must be a non-empty type name.");
680
+ if (check.length === 0)
681
+ definition("domain check must be a non-empty expression.");
682
+ if (base.state.enumLabels !== undefined) {
683
+ definition("A domain cannot use an enum as its base in this version. Use a scalar column type.");
684
+ }
685
+ const baseSql = canonicalBase(formatType(base.state.baseType, base.state.dims));
686
+ const extension = base.state.extension;
687
+ const inner = base.state.baseType;
688
+ const nested = base.state.domain !== undefined;
689
+ return new ColumnBuilder2({
690
+ ...base.state,
691
+ baseType: name,
692
+ dims: 0,
693
+ typeDependency: name,
694
+ enumLabels: undefined,
695
+ domain: {
696
+ base: baseSql,
697
+ check,
698
+ emit(namespace, provenance, objects) {
699
+ base.state.domain?.emit?.(namespace, provenance, objects);
700
+ const dependencies = [];
701
+ if (extension !== undefined)
702
+ dependencies.push({ kind: "extension", name: extension });
703
+ if (nested)
704
+ dependencies.push({ kind: "type", namespace, name: inner });
705
+ const built = domainType({
706
+ namespace,
707
+ name,
708
+ base: baseSql,
709
+ check,
710
+ provenance,
711
+ ...dependencies.length > 0 ? { dependencies } : {}
712
+ });
713
+ const index = objects.findIndex((object) => object.kind === "type" && object.identity.name === name && sameNamespace2(object.identity.namespace, namespace));
714
+ const previous = index >= 0 ? objects[index] : undefined;
715
+ if (previous !== undefined && previous.kind === "type") {
716
+ const definition = previous.definition;
717
+ if (!("base" in definition) || definition.base !== baseSql || definition.check !== check) {
718
+ catalogError2("OKM1020", `Domain ${name} is ${"base" in definition ? definition.base : "an enum"} on ${previous.provenance.name} and ${baseSql} on ${provenance.name}. One domain has one base and one check.`);
719
+ }
720
+ if (compareText(provenance.name, previous.provenance.name) < 0)
721
+ objects[index] = built;
722
+ return;
723
+ }
724
+ objects.push(built);
725
+ }
726
+ }
727
+ });
728
+ }
729
+ function canonicalBase(typeName) {
730
+ const bracket = typeName.indexOf("[");
731
+ const scalar = bracket === -1 ? typeName : typeName.slice(0, bracket);
732
+ const suffix = bracket === -1 ? "" : typeName.slice(bracket);
733
+ return canonicalScalar(scalar) + suffix;
734
+ }
735
+ function canonicalScalar(typeName) {
736
+ const varchar = /^varchar\((\d+)\)$/.exec(typeName);
737
+ if (varchar !== null)
738
+ return `character varying(${varchar[1] ?? ""})`;
739
+ const fixed = /^char\((\d+)\)$/.exec(typeName);
740
+ if (fixed !== null)
741
+ return `character(${fixed[1] ?? ""})`;
742
+ const timed = /^(timestamptz|timestamp|timetz|time)(\(\d+\))?$/.exec(typeName);
743
+ if (timed === null)
744
+ return typeName;
745
+ const name = timed[1];
746
+ const precision = timed[2] ?? "";
747
+ if (name === "timestamptz")
748
+ return `timestamp${precision} with time zone`;
749
+ if (name === "timestamp")
750
+ return `timestamp${precision} without time zone`;
751
+ if (name === "timetz")
752
+ return `time${precision} with time zone`;
753
+ return `time${precision} without time zone`;
1634
754
  }
1635
755
  // src/dialects/pg/namespace.ts
1636
756
  var t = {
1637
- id,
757
+ id: id2,
1638
758
  identity,
1639
- uuid,
759
+ uuid: uuid2,
1640
760
  smallint,
1641
761
  integer,
1642
762
  bigint,
1643
763
  numeric,
1644
764
  real,
1645
765
  double,
1646
- text,
1647
- varchar,
1648
- char,
1649
- citext,
766
+ text: text2,
767
+ varchar: varchar2,
768
+ char: char2,
769
+ citext: citext2,
1650
770
  boolean,
1651
771
  bytea,
1652
- json,
1653
- jsonb,
1654
- timestamptz,
1655
- timestamp,
1656
- date,
1657
- time,
1658
- timetz,
1659
- interval,
772
+ json: json2,
773
+ jsonb: jsonb2,
774
+ timestamptz: timestamptz2,
775
+ timestamp: timestamp2,
776
+ date: date2,
777
+ time: time2,
778
+ timetz: timetz2,
779
+ interval: interval2,
1660
780
  tstzrange,
1661
781
  daterange,
1662
782
  int4range,
@@ -1674,10 +794,39 @@ var t = {
1674
794
  domain,
1675
795
  custom
1676
796
  };
797
+ // src/dialects/pg/ops/arr-ns.ts
798
+ var exports_arr_ns = {};
799
+ __export(exports_arr_ns, {
800
+ append: () => append,
801
+ remove: () => remove
802
+ });
803
+
804
+ // src/dialects/pg/ops/arr-append.ts
805
+ function append(value) {
806
+ return tag("arr.append", value);
807
+ }
808
+ // src/dialects/pg/ops/arr-remove.ts
809
+ function remove(value) {
810
+ return tag("arr.remove", value);
811
+ }
812
+ // src/dialects/pg/ops/json-ns.ts
813
+ var exports_json_ns = {};
814
+ __export(exports_json_ns, {
815
+ set: () => set
816
+ });
817
+
818
+ // src/dialects/pg/ops/json-set.ts
819
+ function set(path, value) {
820
+ return tag("json.set", { path, value });
821
+ }
1677
822
  // src/dialects/pg/ops/between.ts
1678
823
  function between(from, to) {
1679
824
  return tag("between", [from, to]);
1680
825
  }
826
+ // src/dialects/pg/ops/containedBy.ts
827
+ function containedBy(value) {
828
+ return tag("containedBy", value);
829
+ }
1681
830
  // src/dialects/pg/ops/contains.ts
1682
831
  function contains(value) {
1683
832
  return tag("contains", value);
@@ -1710,6 +859,14 @@ function gte(value) {
1710
859
  function has(where) {
1711
860
  return tag("has", where);
1712
861
  }
862
+ // src/dialects/pg/ops/hasAnyKey.ts
863
+ function hasAnyKey(keys) {
864
+ return tag("hasAnyKey", keys);
865
+ }
866
+ // src/dialects/pg/ops/hasKey.ts
867
+ function hasKey(key) {
868
+ return tag("hasKey", key);
869
+ }
1713
870
  // src/dialects/pg/ops/ilike.ts
1714
871
  function ilike(pattern) {
1715
872
  return tag("ilike", pattern);
@@ -1722,6 +879,14 @@ function inList(values) {
1722
879
  function like(pattern) {
1723
880
  return tag("like", pattern);
1724
881
  }
882
+ // src/dialects/pg/ops/matches.ts
883
+ function matches(query, options) {
884
+ return tag("matches", {
885
+ query,
886
+ mode: options?.mode ?? "websearch",
887
+ config: options?.config
888
+ });
889
+ }
1725
890
  // src/dialects/pg/ops/lt.ts
1726
891
  function lt(value) {
1727
892
  return tag("lt", value);
@@ -1746,22 +911,32 @@ function notIn(values) {
1746
911
  function or(branches) {
1747
912
  return tag("or", branches);
1748
913
  }
914
+ // src/dialects/pg/ops/overlaps.ts
915
+ function overlaps(value) {
916
+ return tag("overlaps", value);
917
+ }
918
+ // src/dialects/pg/ops/path.ts
919
+ function path(segments, op) {
920
+ return tag("path", { segments, op });
921
+ }
1749
922
  // src/dialects/pg/ops/startsWith.ts
1750
923
  function startsWith(value) {
1751
924
  return tag("startsWith", value);
1752
925
  }
1753
926
  export {
1754
927
  ColumnBuilder2 as ColumnBuilder,
928
+ exports_arr_ns as arr,
1755
929
  between,
1756
930
  bigint,
1757
931
  boolean,
1758
932
  bytea,
1759
- char,
933
+ char2 as char,
1760
934
  cidr,
1761
- citext,
935
+ citext2 as citext,
936
+ containedBy,
1762
937
  contains,
1763
938
  custom,
1764
- date,
939
+ date2 as date,
1765
940
  daterange,
1766
941
  domain,
1767
942
  double,
@@ -1772,7 +947,9 @@ export {
1772
947
  gt,
1773
948
  gte,
1774
949
  has,
1775
- id,
950
+ hasAnyKey,
951
+ hasKey,
952
+ id2 as id,
1776
953
  identity,
1777
954
  ilike,
1778
955
  inList,
@@ -1782,10 +959,10 @@ export {
1782
959
  int4range,
1783
960
  int8range,
1784
961
  integer,
1785
- interval,
1786
- json,
1787
- jsonReplacer,
1788
- jsonb,
962
+ interval2 as interval,
963
+ exports_json_ns as json,
964
+ jsonReplacer2 as jsonReplacer,
965
+ jsonb2 as jsonb,
1789
966
  like,
1790
967
  line,
1791
968
  lt,
@@ -1793,29 +970,33 @@ export {
1793
970
  ltree,
1794
971
  macaddr,
1795
972
  macaddr8,
1796
- many,
973
+ many2 as many,
974
+ manyThrough2 as manyThrough,
975
+ matches,
1797
976
  none,
1798
977
  not,
1799
978
  notIn,
1800
979
  numeric,
1801
980
  numrange,
1802
- one,
981
+ one2 as one,
1803
982
  or,
983
+ overlaps,
984
+ path,
1804
985
  point,
1805
986
  real,
1806
- schema,
987
+ schema2 as schema,
1807
988
  smallint,
1808
989
  sql2 as sql,
1809
990
  startsWith,
1810
991
  t,
1811
992
  table2 as table,
1812
- text,
1813
- time,
1814
- timestamp,
1815
- timestamptz,
1816
- timetz,
993
+ text2 as text,
994
+ time2 as time,
995
+ timestamp2 as timestamp,
996
+ timestamptz2 as timestamptz,
997
+ timetz2 as timetz,
1817
998
  tstzrange,
1818
999
  tsvector,
1819
- uuid,
1820
- varchar
1000
+ uuid2 as uuid,
1001
+ varchar2 as varchar
1821
1002
  };