okmodel 0.5.1 → 0.7.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 (233) hide show
  1. package/README.md +100 -1116
  2. package/dist/adapters/capabilities.d.ts +3 -0
  3. package/dist/adapters/failure.d.ts +13 -2
  4. package/dist/adapters/lazy-errors.d.ts +22 -0
  5. package/dist/adapters/pg/bunsql-extra.d.ts +54 -3
  6. package/dist/adapters/pg/bunsql.d.ts +14 -1
  7. package/dist/adapters/pg/cursor.d.ts +17 -0
  8. package/dist/adapters/pg/no-inflight-timeout.d.ts +20 -0
  9. package/dist/adapters/pg/nodepostgres-extra.d.ts +22 -1
  10. package/dist/adapters/pg/nodepostgres.d.ts +12 -0
  11. package/dist/adapters/pg/pglite-wire.d.ts +24 -0
  12. package/dist/adapters/pg/pglite.d.ts +10 -0
  13. package/dist/adapters/pg/postgres-extra.d.ts +41 -3
  14. package/dist/adapters/pg/reserved.d.ts +42 -0
  15. package/dist/adapters/pg/session.d.ts +0 -18
  16. package/dist/contracts/catalog/document.d.ts +19 -5
  17. package/dist/contracts/catalog/object.d.ts +2 -1
  18. package/dist/contracts/catalog/policy.d.ts +48 -0
  19. package/dist/contracts/catalog/types.d.ts +25 -1
  20. package/dist/contracts/catalog/view.d.ts +2 -0
  21. package/dist/contracts/connection.d.ts +7 -6
  22. package/dist/contracts/internal.js +1 -1
  23. package/dist/contracts/location.d.ts +10 -0
  24. package/dist/dialects/pg/column.d.ts +13 -2
  25. package/dist/dialects/pg/ddl.d.ts +37 -1
  26. package/dist/dialects/pg/emit.d.ts +19 -1
  27. package/dist/dialects/pg/errors.d.ts +32 -1
  28. package/dist/dialects/pg/ext/citext.js +1 -1
  29. package/dist/dialects/pg/ext/index.js +1 -1
  30. package/dist/dialects/pg/ext/pg-trgm.js +1 -1
  31. package/dist/dialects/pg/filter-parse.d.ts +5 -4
  32. package/dist/dialects/pg/floor.d.ts +68 -0
  33. package/dist/dialects/pg/fn/index.js +2 -2
  34. package/dist/dialects/pg/index.d.ts +8 -2
  35. package/dist/dialects/pg/index.js +67 -171
  36. package/dist/dialects/pg/introspect.d.ts +7 -0
  37. package/dist/dialects/pg/keys.d.ts +11 -8
  38. package/dist/dialects/pg/model.d.ts +47 -2
  39. package/dist/dialects/pg/morph.d.ts +43 -0
  40. package/dist/dialects/pg/operator-fit.d.ts +2 -2
  41. package/dist/dialects/pg/operators.d.ts +14 -7
  42. package/dist/dialects/pg/ops/bucket.d.ts +65 -0
  43. package/dist/dialects/pg/ops/inc.d.ts +2 -2
  44. package/dist/dialects/pg/ops/matches.d.ts +9 -12
  45. package/dist/dialects/pg/ops/relevance.d.ts +13 -0
  46. package/dist/dialects/pg/ops/search-query.d.ts +22 -0
  47. package/dist/dialects/pg/ops/window.d.ts +103 -0
  48. package/dist/dialects/pg/owner.d.ts +42 -0
  49. package/dist/dialects/pg/path-endpoint.d.ts +20 -0
  50. package/dist/dialects/pg/preset.d.ts +1 -1
  51. package/dist/dialects/pg/relations.d.ts +100 -3
  52. package/dist/dialects/pg/role/check.d.ts +3 -0
  53. package/dist/dialects/pg/role/index.d.ts +71 -6
  54. package/dist/dialects/pg/role/sql.d.ts +11 -0
  55. package/dist/dialects/pg/schema.d.ts +33 -3
  56. package/dist/dialects/pg/table.d.ts +25 -3
  57. package/dist/dialects/pg/tenancy.d.ts +45 -3
  58. package/dist/dialects/pg/view/client.d.ts +19 -1
  59. package/dist/dialects/pg/view/index.d.ts +4 -0
  60. package/dist/dialects/pg/view/index.js +21 -178
  61. package/dist/dialects/pg/view/refresh.d.ts +24 -0
  62. package/dist/runtime/aggregate.d.ts +8 -2
  63. package/dist/runtime/batch.d.ts +1 -1
  64. package/dist/runtime/bucket-group.d.ts +70 -0
  65. package/dist/runtime/conflict.d.ts +24 -6
  66. package/dist/runtime/drift.d.ts +9 -0
  67. package/dist/runtime/include.d.ts +0 -8
  68. package/dist/runtime/inflight.d.ts +14 -0
  69. package/dist/runtime/page.d.ts +4 -1
  70. package/dist/runtime/pg/bun.js +12 -5
  71. package/dist/runtime/pg/pg.js +5 -5
  72. package/dist/runtime/pg/pglite.d.ts +1 -0
  73. package/dist/runtime/pg/pglite.js +9 -8
  74. package/dist/runtime/pg/postgresjs.js +5 -5
  75. package/dist/runtime/plan.d.ts +113 -15
  76. package/dist/runtime/predicate.d.ts +19 -0
  77. package/dist/runtime/read-stream.d.ts +13 -16
  78. package/dist/runtime/result-name.d.ts +17 -0
  79. package/dist/runtime/safety/index.js +1 -1
  80. package/dist/runtime/server-check.d.ts +3 -3
  81. package/dist/runtime/sort-bind.d.ts +21 -0
  82. package/dist/runtime/sort.d.ts +56 -0
  83. package/dist/runtime/sync.d.ts +70 -0
  84. package/dist/runtime/tenancy/composite.d.ts +26 -0
  85. package/dist/runtime/tenancy/context.d.ts +16 -0
  86. package/dist/runtime/tenancy/index.d.ts +9 -5
  87. package/dist/runtime/tenancy/index.js +1511 -38
  88. package/dist/runtime/tenancy/path.d.ts +87 -0
  89. package/dist/runtime/tenancy/rls.d.ts +67 -0
  90. package/dist/runtime/tenancy/via.d.ts +22 -0
  91. package/dist/runtime/text-search.d.ts +20 -0
  92. package/dist/runtime/traits/index.d.ts +80 -0
  93. package/dist/runtime/traits/index.js +187 -17
  94. package/dist/runtime/tree.d.ts +38 -0
  95. package/dist/runtime/types.d.ts +231 -23
  96. package/dist/runtime/validate/index.js +4 -4
  97. package/dist/runtime/version.d.ts +39 -0
  98. package/dist/runtime/window.d.ts +21 -0
  99. package/dist/runtime/write.d.ts +55 -3
  100. package/dist/shared/{krxx77g0.js → 14vx95q4.js} +12 -18
  101. package/dist/shared/1est4ezg.js +313 -0
  102. package/dist/shared/{4az561fn.js → 1qa0qajz.js} +8 -23
  103. package/dist/shared/1w508jbr.js +1611 -0
  104. package/dist/shared/2c13q96h.js +18 -0
  105. package/dist/shared/2d3xq4ps.js +9 -0
  106. package/dist/shared/2shdbn8q.js +23 -0
  107. package/dist/shared/{t4tbsy9h.js → 358zsv4r.js} +1 -1
  108. package/dist/shared/3v7t1yrv.js +208 -0
  109. package/dist/shared/5e3tf63j.js +220 -0
  110. package/dist/shared/5fe3kbsh.js +10 -0
  111. package/dist/shared/5v3xf80k.js +37 -0
  112. package/dist/shared/{51mvgj8w.js → 63qtkrca.js} +130 -3
  113. package/dist/shared/{gbtcc2d7.js → 67txcrcw.js} +62 -66
  114. package/dist/shared/79dt02vc.js +12 -0
  115. package/dist/shared/{v36c5g6p.js → 7k9grvf6.js} +407 -14
  116. package/dist/shared/7kms6fvj.js +20 -0
  117. package/dist/shared/7mnme5tn.js +8 -0
  118. package/dist/shared/7n7ddwt4.js +6 -0
  119. package/dist/shared/{87jvvhnt.js → 81ebdx9p.js} +48 -68
  120. package/dist/shared/{szm0cksa.js → 8skf0cam.js} +34 -6
  121. package/dist/shared/{gjthds9j.js → 9628a9xt.js} +1 -1
  122. package/dist/shared/9a4ae4p8.js +31 -0
  123. package/dist/shared/{8xsbagad.js → 9s0ypp87.js} +88 -7
  124. package/dist/shared/9y3mmmh1.js +436 -0
  125. package/dist/shared/ab9gmbwy.js +213 -0
  126. package/dist/shared/{sycnx8gt.js → ax7qv55k.js} +18 -8
  127. package/dist/shared/{f5cw5q5r.js → aytqh5y2.js} +6 -4
  128. package/dist/shared/{fancm5pt.js → b5pftnn2.js} +138 -15
  129. package/dist/shared/{efmy2gcg.js → b76b8d95.js} +2 -2
  130. package/dist/shared/bdk4sty6.js +58 -0
  131. package/dist/shared/{32zqc1dw.js → bg3rym3g.js} +63 -32
  132. package/dist/shared/{0t22wwh1.js → bm22fvzr.js} +16 -14
  133. package/dist/shared/bz84tkzm.js +342 -0
  134. package/dist/shared/{31vm914h.js → cm3w0ywp.js} +15 -16
  135. package/dist/shared/cn957f8d.js +179 -0
  136. package/dist/shared/{pf0d8f77.js → cnkky55j.js} +25 -14
  137. package/dist/shared/{5fqx9yh4.js → dk8v79p3.js} +3 -1
  138. package/dist/shared/{nkaxvt9q.js → dkxgzaqd.js} +224 -19
  139. package/dist/shared/dzj09cye.js +10 -0
  140. package/dist/shared/{4m9fn52z.js → ed0b8k4s.js} +1 -1
  141. package/dist/shared/{z2rnngj4.js → ek2wt2dz.js} +8 -5
  142. package/dist/shared/exn3sd90.js +359 -0
  143. package/dist/shared/g6evj1j3.js +66 -0
  144. package/dist/shared/ga9j92qj.js +25 -0
  145. package/dist/shared/{qv2ydck9.js → gs74dbj5.js} +651 -76
  146. package/dist/shared/gzaveazc.js +285 -0
  147. package/dist/shared/{8rsy3hxz.js → h15jx9bc.js} +1 -1
  148. package/dist/shared/{dyj86cmj.js → hyh73qk6.js} +3 -3
  149. package/dist/shared/je4axnm8.js +22 -0
  150. package/dist/shared/{ycatkxmq.js → jgqj11w4.js} +1 -1
  151. package/dist/shared/{6qd27mx0.js → jvd0mqag.js} +5 -5
  152. package/dist/shared/jznfax0y.js +155 -0
  153. package/dist/shared/{cgbamx3e.js → kb2sdhfb.js} +7 -4
  154. package/dist/shared/{ps7a6h14.js → kr0jzbw2.js} +12 -8
  155. package/dist/shared/{janx09m5.js → ktz42yez.js} +20 -8
  156. package/dist/shared/{gde73es6.js → m6p12kbr.js} +56 -55
  157. package/dist/shared/{r6nwkxnt.js → m7n9h2a5.js} +4 -4
  158. package/dist/shared/{672t5vym.js → mss6ctgv.js} +2 -2
  159. package/dist/shared/{nxdp2ze3.js → mtaszv5a.js} +144 -12
  160. package/dist/shared/n816peh1.js +45 -0
  161. package/dist/shared/nbw1z4yd.js +18 -0
  162. package/dist/shared/{cctw17hj.js → p6qc5gcp.js} +38 -34
  163. package/dist/shared/{6rk2gen5.js → pbp1ss85.js} +20 -17
  164. package/dist/shared/pt94kz7f.js +6 -0
  165. package/dist/shared/{wq9n6pwr.js → pz6rj8ay.js} +82 -9
  166. package/dist/shared/q5c6ngn3.js +13 -0
  167. package/dist/shared/{8bwbwy8h.js → qq3899ht.js} +128 -18
  168. package/dist/shared/{8ktwd3rh.js → r3j8s5y0.js} +1 -1
  169. package/dist/shared/rhs3xe4k.js +7 -0
  170. package/dist/shared/{ntneex2a.js → rpycarcr.js} +6 -6
  171. package/dist/shared/{sdc7r2ww.js → rs73fca9.js} +8 -8
  172. package/dist/shared/{tm5ab8we.js → sfz1ksn6.js} +7 -4
  173. package/dist/shared/{6mnp99hx.js → svncdeh5.js} +16 -12
  174. package/dist/shared/{hf22e0wt.js → tbrvgw6z.js} +62 -42
  175. package/dist/shared/te5as7z7.js +622 -0
  176. package/dist/shared/{ga3sfy93.js → tsvww22q.js} +535 -200
  177. package/dist/shared/{47x8x9ze.js → ttsqgp4p.js} +20 -13
  178. package/dist/shared/{x72jb7ad.js → v8g1vgv8.js} +1 -1
  179. package/dist/shared/vb6qfyve.js +152 -0
  180. package/dist/shared/{r2sp8hrc.js → vczs8gd4.js} +22 -13
  181. package/dist/shared/{1g0drakv.js → vqrjpcqn.js} +145 -3
  182. package/dist/shared/{2pqx26h8.js → w72kgg4q.js} +31 -22
  183. package/dist/shared/{96qcj21c.js → w79hsckn.js} +16 -51
  184. package/dist/shared/wa07eas9.js +730 -0
  185. package/dist/shared/{k8v5v5wj.js → x933k9a3.js} +75 -34
  186. package/dist/shared/{cr74e96e.js → xhfsfkgk.js} +62 -28
  187. package/dist/shared/xjk7hnn5.js +272 -0
  188. package/dist/shared/{5enne4ma.js → xnxsed62.js} +66 -113
  189. package/dist/shared/xqve9598.js +53 -0
  190. package/dist/shared/{9x25fxhc.js → y34a8714.js} +1 -1
  191. package/dist/shared/{k7dwkhvq.js → y6jdds7g.js} +4 -1
  192. package/dist/shared/y6njgyda.js +249 -0
  193. package/dist/shared/{c9z5g0fm.js → ywr8hmey.js} +5 -2
  194. package/dist/shared/{g7ez901n.js → ywspezzx.js} +1 -1
  195. package/dist/shared/z7s6fpxv.js +62 -0
  196. package/dist/shared/{n7mcc1gk.js → zchaghkf.js} +4 -3
  197. package/dist/shared/{9m80mkac.js → ze4hxsmy.js} +1 -1
  198. package/dist/shared/zm0ptry0.js +40 -0
  199. package/dist/shared/{nsva2a6b.js → znm0bpp5.js} +3 -0
  200. package/dist/shared/zp9fmend.js +145 -0
  201. package/dist/shared/{y005b761.js → zy8r7qvz.js} +6 -2
  202. package/dist/tooling/cli.js +41 -15
  203. package/dist/tooling/migrate/apply.d.ts +14 -0
  204. package/dist/tooling/migrate/check.d.ts +3 -4
  205. package/dist/tooling/migrate/classify.d.ts +4 -0
  206. package/dist/tooling/migrate/config.d.ts +3 -2
  207. package/dist/tooling/migrate/engine.d.ts +3 -2
  208. package/dist/tooling/migrate/files.d.ts +11 -1
  209. package/dist/tooling/migrate/index.js +14 -14
  210. package/dist/tooling/migrate/lint.d.ts +1 -1
  211. package/dist/tooling/migrate/lost.d.ts +28 -0
  212. package/dist/tooling/migrate/paths.d.ts +14 -0
  213. package/dist/tooling/migrate/plan.d.ts +6 -3
  214. package/dist/tooling/migrate/runner.d.ts +25 -0
  215. package/dist/tooling/migrate/sandbox.d.ts +57 -0
  216. package/dist/tooling/migrate/sql-lex.d.ts +20 -3
  217. package/dist/tooling/migrate/text.d.ts +42 -0
  218. package/dist/tooling/node-version.d.ts +21 -0
  219. package/dist/tooling/testing/facts.d.ts +33 -5
  220. package/dist/tooling/testing/index.d.ts +1 -1
  221. package/dist/tooling/testing/index.js +3 -3
  222. package/dist/tooling/testing/isolation.d.ts +11 -5
  223. package/package.json +8 -3
  224. package/dist/shared/7m88x3e9.js +0 -45
  225. package/dist/shared/7zwwyh0v.js +0 -448
  226. package/dist/shared/aj2qxyhj.js +0 -90
  227. package/dist/shared/caw8b9d8.js +0 -204
  228. package/dist/shared/jwtxdj06.js +0 -76
  229. package/dist/shared/kfjp0gck.js +0 -212
  230. package/dist/shared/q58y17hy.js +0 -21
  231. package/dist/shared/qm7cdtvr.js +0 -912
  232. package/dist/shared/r8x5qhd7.js +0 -86
  233. package/dist/shared/zqrnhwsq.js +0 -31
@@ -1,6 +1,9 @@
1
1
  import {
2
- stack
3
- } from "./672t5vym.js";
2
+ stack2
3
+ } from "./mss6ctgv.js";
4
+ import {
5
+ effectivePredicate
6
+ } from "./n816peh1.js";
4
7
  import {
5
8
  runWrite
6
9
  } from "./n4jqhpqz.js";
@@ -12,10 +15,9 @@ import {
12
15
  indexes,
13
16
  quote,
14
17
  isRecord,
15
- effectivePredicate,
16
18
  registerFailFix,
17
- fail
18
- } from "./gbtcc2d7.js";
19
+ fail2
20
+ } from "./67txcrcw.js";
19
21
  import {
20
22
  safetyInstalled,
21
23
  runSafety
@@ -48,7 +50,7 @@ function explainArchive(schema, op, table, input, options, mods, scope) {
48
50
  function planArchive(schema, op, tableName, input, options, mods, scope) {
49
51
  const table = indexes(schema).get(tableName);
50
52
  if (table === undefined)
51
- fail("OKM1120", `Table ${tableName} is not in the schema.`);
53
+ fail2("OKM1120", `Table ${tableName} is not in the schema.`);
52
54
  const archive = table.model.archive;
53
55
  if (archive === undefined) {
54
56
  throw new OkmError2("OKM1052", `Table ${tableName} is not archivable.`, {
@@ -59,7 +61,7 @@ function planArchive(schema, op, tableName, input, options, mods, scope) {
59
61
  rejectKeys(record, OPTIONS, op);
60
62
  const expect = expectOf(record.expect ?? mods.expect);
61
63
  const read = readTarget(op, table, input, mods);
62
- const target = { ...read, where: stack(read.where, mods.presets) };
64
+ const target = { ...read, where: stack2(read.where, mods.presets) };
63
65
  const view = op === "restore" ? "only" : undefined;
64
66
  noteArchive(table, view);
65
67
  const statement = withRowFilters(scope, view, () => op === "archive" ? archiveSql(schema, table, target.where) : restoreSql(schema, table, target));
@@ -81,25 +83,25 @@ function noteArchive(table, view) {
81
83
  }
82
84
  function readTarget(op, table, input, mods) {
83
85
  if (!isRecord(input))
84
- fail("OKM1121", `${op} expects { where }.`);
86
+ fail2("OKM1121", `${op} expects { where }.`);
85
87
  const keys = Object.keys(input);
86
88
  if (op === "restore" && input.archiveId !== undefined) {
87
89
  if (input.where !== undefined)
88
- fail("OKM1120", "restore takes where or archiveId, not both.");
90
+ fail2("OKM1120", "restore takes where or archiveId, not both.");
89
91
  for (const key of keys) {
90
92
  if (key !== "archiveId") {
91
- fail("OKM1120", "restore accepts where or archiveId.");
93
+ fail2("OKM1120", "restore accepts where or archiveId.");
92
94
  }
93
95
  }
94
96
  if (typeof input.archiveId !== "string" || input.archiveId.length === 0) {
95
- fail("OKM1121", "restore archiveId must be a uuid.");
97
+ fail2("OKM1121", "restore archiveId must be a uuid.");
96
98
  }
97
99
  const idField = table.model.archive?.idField ?? "archiveId";
98
100
  return { where: { [idField]: input.archiveId }, archiveId: input.archiveId };
99
101
  }
100
102
  for (const key of keys) {
101
103
  if (key !== "where")
102
- fail("OKM1120", `${op} accepts where.`);
104
+ fail2("OKM1120", `${op} accepts where.`);
103
105
  }
104
106
  requireFilter(table, input.where, mods, op);
105
107
  return { where: input.where, archiveId: undefined };
@@ -356,12 +358,12 @@ function expectOf(value) {
356
358
  function requireFilter(table, where, mods, op) {
357
359
  if (mods.all !== undefined) {
358
360
  if (mods.all.trim().length === 0) {
359
- fail("OKM1102", `${op} on ${table.model.name} needs a where. .all needs a reason.`);
361
+ fail2("OKM1102", `${op} on ${table.model.name} needs a where. .all needs a reason.`);
360
362
  }
361
363
  return;
362
364
  }
363
365
  if (!effectivePredicate(where)) {
364
- fail("OKM1102", `${op} on ${table.model.name} needs a where. Pass a filter, or .all(reason).`);
366
+ fail2("OKM1102", `${op} on ${table.model.name} needs a where. Pass a filter, or .all(reason).`);
365
367
  }
366
368
  }
367
369
  function callOptions(input) {
@@ -0,0 +1,342 @@
1
+ import {
2
+ DriverError2,
3
+ timedOut2
4
+ } from "./cm3w0ywp.js";
5
+ import {
6
+ PgSession
7
+ } from "./14vx95q4.js";
8
+ import {
9
+ isConnectionLost
10
+ } from "./cnkky55j.js";
11
+ import {
12
+ driverErrors,
13
+ rejectClosed,
14
+ mapFailure,
15
+ runCall,
16
+ rowsFrom,
17
+ resultFrom
18
+ } from "./kb2sdhfb.js";
19
+
20
+ // src/adapters/pg/postgres-extra.ts
21
+ var RESET_ALL = "RESET ALL";
22
+ var UNLOCK = "SELECT pg_advisory_unlock_all()";
23
+ async function checkout2(pool, timeoutMs) {
24
+ if (pool.isClosed())
25
+ return rejectClosed();
26
+ const reserved = await reserveConnection(pool.sql, pool.acquireMs, pool.counters, timeoutMs);
27
+ pool.counters.reserved += 1;
28
+ const local = new Set;
29
+ const session = new PgSession(reserved, pool.counters, "reserved", local, pool.isClosed, pool.poolInflight, pool.notices, pool.named);
30
+ let released = false;
31
+ const lost = { on: false, stop: () => {
32
+ return;
33
+ } };
34
+ arm(reserved, lost);
35
+ let limit = timeoutMs !== undefined && timeoutMs > 0 ? timeoutMs : 0;
36
+ const remember = (timeout) => {
37
+ if (timeout !== undefined && timeout > 0)
38
+ limit = timeout;
39
+ };
40
+ const guard = (run) => lost.on ? rejectClosed() : run().catch((error) => {
41
+ if (isConnectionLost(error))
42
+ lost.on = true;
43
+ throw error;
44
+ });
45
+ return {
46
+ execute: (text, params, options) => {
47
+ remember(options?.timeout);
48
+ return guard(() => runCall(pool.isClosed(), options, (watch) => session.query(text, params, watch)));
49
+ },
50
+ batch: (statements, options) => {
51
+ remember(options?.timeout);
52
+ return guard(() => runCall(pool.isClosed(), options, async (watch) => {
53
+ const { runAtomicBatch } = await import("./b76b8d95.js");
54
+ return runAtomicBatch(session, statements, watch);
55
+ }));
56
+ },
57
+ cancel() {
58
+ session.cancel();
59
+ },
60
+ async release() {
61
+ if (released)
62
+ return;
63
+ released = true;
64
+ const ms = limit;
65
+ try {
66
+ if (!lost.on)
67
+ await deadline(ms, () => resetConnection(reserved, session.depth()), lost);
68
+ } catch {
69
+ lost.on = true;
70
+ } finally {
71
+ pool.counters.reserved = Math.max(0, pool.counters.reserved - 1);
72
+ if (lost.on)
73
+ await deadline(ms, () => pool.sql.unsafe("select 1"), pool.sql).catch(() => {
74
+ return;
75
+ });
76
+ else
77
+ reserved.release();
78
+ }
79
+ }
80
+ };
81
+ }
82
+ async function describe2(sql, text, params) {
83
+ const pending = sql.unsafe(text, asParams(params), { prepare: true });
84
+ const described = await pending.describe();
85
+ return {
86
+ columns: described.columns.map((column) => column.name),
87
+ parameterCount: described.types.length
88
+ };
89
+ }
90
+ async function* stream2(sql, text, params) {
91
+ const pending = sql.unsafe(text, asParams(params));
92
+ for await (const chunk of pending.cursor(64)) {
93
+ yield rowsFrom(chunk);
94
+ }
95
+ }
96
+ function poolWaits(sql) {
97
+ const options = sql.options;
98
+ if (options === undefined || options.q !== undefined)
99
+ return options;
100
+ const q = new Set;
101
+ const n = new Set;
102
+ options.q = q;
103
+ options.n = n;
104
+ options.onclose = (id) => {
105
+ n.add(id);
106
+ if (n.size < options.max) {
107
+ queueMicrotask(() => {
108
+ for (const wait of q)
109
+ wait.a();
110
+ });
111
+ return;
112
+ }
113
+ n.clear();
114
+ for (const wait of q)
115
+ wait.s();
116
+ q.clear();
117
+ };
118
+ return options;
119
+ }
120
+ function deadline(ms, run, link) {
121
+ let timer;
122
+ let off = () => {
123
+ return;
124
+ };
125
+ const slot = link !== undefined && "stop" in link ? link : undefined;
126
+ const pool = slot === undefined && link !== undefined ? poolWaits(link) : undefined;
127
+ let ended = false;
128
+ return new Promise((resolve, reject) => {
129
+ const fail = (error) => {
130
+ if (ended)
131
+ return;
132
+ ended = true;
133
+ reject(error);
134
+ };
135
+ const stop = () => {
136
+ fail(new DriverError2("Connection closed", { sqlstate: "08006" }));
137
+ };
138
+ if (slot !== undefined)
139
+ slot.stop = stop;
140
+ const go = () => {
141
+ const pending = run();
142
+ if (ms > 0 && timer === undefined) {
143
+ timer = setTimeout(() => {
144
+ pending.cancel?.();
145
+ fail(timedOut2());
146
+ }, ms);
147
+ }
148
+ pending.then((value) => {
149
+ if (ended) {
150
+ value.release?.();
151
+ return;
152
+ }
153
+ ended = true;
154
+ pool?.n?.clear();
155
+ resolve(value);
156
+ }, fail);
157
+ };
158
+ if (ms === 0 && pool?.q !== undefined) {
159
+ let used = false;
160
+ const wait = {
161
+ s: stop,
162
+ a() {
163
+ if (used || ended)
164
+ return;
165
+ used = true;
166
+ go();
167
+ }
168
+ };
169
+ pool.q.add(wait);
170
+ off = () => pool.q?.delete(wait);
171
+ }
172
+ go();
173
+ }).finally(() => {
174
+ clearTimeout(timer);
175
+ if (slot !== undefined)
176
+ slot.stop = () => {
177
+ return;
178
+ };
179
+ off();
180
+ });
181
+ }
182
+ async function runBatch2(pool, statements, options) {
183
+ const connection = await checkout2(pool, options?.timeout);
184
+ try {
185
+ return await connection.batch(statements, options);
186
+ } finally {
187
+ await connection.release();
188
+ }
189
+ }
190
+ function reserveConnection(sql, acquireMs, counters, callMs) {
191
+ counters.waiting += 1;
192
+ let settled = false;
193
+ const finish = () => {
194
+ counters.waiting = Math.max(0, counters.waiting - 1);
195
+ };
196
+ return new Promise((resolve, reject) => {
197
+ let timer;
198
+ if (acquireMs !== undefined) {
199
+ timer = setTimeout(() => {
200
+ if (settled)
201
+ return;
202
+ settled = true;
203
+ finish();
204
+ driverErrors().then((errors) => {
205
+ reject(errors.acquireTimeout());
206
+ });
207
+ }, acquireMs);
208
+ }
209
+ const ms = callMs !== undefined && callMs > 0 ? callMs : 0;
210
+ deadline(ms, () => sql.reserve(), sql).then((connection) => {
211
+ if (settled) {
212
+ resetConnection(connection, 0).catch(() => {
213
+ return;
214
+ }).finally(() => {
215
+ connection.release();
216
+ });
217
+ return;
218
+ }
219
+ settled = true;
220
+ if (timer !== undefined)
221
+ clearTimeout(timer);
222
+ finish();
223
+ resolve(connection);
224
+ }, (error) => {
225
+ if (settled)
226
+ return;
227
+ settled = true;
228
+ if (timer !== undefined)
229
+ clearTimeout(timer);
230
+ finish();
231
+ mapFailure(error).then(reject);
232
+ });
233
+ });
234
+ }
235
+ async function resetConnection(sql, depth) {
236
+ if (depth > 0)
237
+ await sql.unsafe("ROLLBACK").catch(() => {
238
+ return;
239
+ });
240
+ try {
241
+ await sql.unsafe(RESET_ALL);
242
+ await sql.unsafe(UNLOCK);
243
+ } catch {
244
+ await sql.unsafe("ROLLBACK").catch(() => {
245
+ return;
246
+ });
247
+ await sql.unsafe(RESET_ALL);
248
+ await sql.unsafe(UNLOCK);
249
+ }
250
+ }
251
+ function asParams(params) {
252
+ if (params === undefined)
253
+ return [];
254
+ return params;
255
+ }
256
+ function watchReserved2(sql, lost) {
257
+ arm(sql, lost);
258
+ }
259
+ function arm(sql, lost) {
260
+ const unsafe = sql.unsafe.bind(sql);
261
+ sql.unsafe = (text, params, options) => unsafe(text, params, {
262
+ ...options,
263
+ onexecute(connection) {
264
+ connection.onclose = () => {
265
+ lost.on = true;
266
+ lost.stop();
267
+ };
268
+ }
269
+ });
270
+ }
271
+ async function holdCheckout2(gate, checkout) {
272
+ try {
273
+ const connection = await checkout();
274
+ let released = false;
275
+ const release = connection.release.bind(connection);
276
+ connection.release = () => {
277
+ if (released)
278
+ return Promise.resolve();
279
+ released = true;
280
+ return release().finally(() => {
281
+ gate.leave();
282
+ });
283
+ };
284
+ return connection;
285
+ } catch (error) {
286
+ gate.leave();
287
+ throw error;
288
+ }
289
+ }
290
+ async function holdListener2(gate, start) {
291
+ try {
292
+ const stop = await start();
293
+ let stopped = false;
294
+ return async () => {
295
+ if (stopped)
296
+ return;
297
+ stopped = true;
298
+ try {
299
+ await stop();
300
+ } finally {
301
+ gate.leave();
302
+ }
303
+ };
304
+ } catch (error) {
305
+ gate.leave();
306
+ throw error;
307
+ }
308
+ }
309
+ function watchQuery2(pending, watch, onAbort, ctx) {
310
+ return pending.then((value) => {
311
+ finishWatch(pending, watch, onAbort, ctx);
312
+ const why = watch.reason();
313
+ if (why === "timeout" || why === "cancelled")
314
+ return classify(why, watch);
315
+ ctx.note(ctx.text);
316
+ return resultFrom(value, ctx.notices.since(ctx.start));
317
+ }, (error) => {
318
+ finishWatch(pending, watch, onAbort, ctx);
319
+ return classify(error, watch);
320
+ });
321
+ }
322
+ function finishWatch(pending, watch, onAbort, ctx) {
323
+ ctx.leave();
324
+ ctx.untrack(pending);
325
+ watch.signal.removeEventListener("abort", onAbort);
326
+ }
327
+ async function classify(error, watch) {
328
+ const errors = await driverErrors();
329
+ if (error instanceof errors.DriverError)
330
+ return Promise.reject(error);
331
+ const why = typeof error === "string" ? error : watch.reason();
332
+ if (why === "timeout") {
333
+ return Promise.reject(errors.timedOut(typeof error === "string" ? undefined : error));
334
+ }
335
+ const code = errors.errorField(error, "code");
336
+ if (why === "cancelled" || code === "57014") {
337
+ return Promise.reject(errors.cancelled(typeof error === "string" ? undefined : error));
338
+ }
339
+ return Promise.reject(errors.mapDriverError(error));
340
+ }
341
+
342
+ export { checkout2, describe2, stream2, runBatch2, watchReserved2, holdCheckout2, holdListener2, watchQuery2 };
@@ -96,22 +96,21 @@ function messageOf(error) {
96
96
  }
97
97
  function driverFields(input) {
98
98
  const fields = {};
99
- if (input.sqlstate !== undefined)
100
- fields.sqlstate = input.sqlstate;
101
- if (input.constraint !== undefined)
102
- fields.constraint = input.constraint;
103
- if (input.table !== undefined)
104
- fields.table = input.table;
105
- if (input.column !== undefined)
106
- fields.column = input.column;
107
- if (input.detail !== undefined)
108
- fields.detail = input.detail;
109
- if (input.cause !== undefined)
110
- fields.cause = input.cause;
111
- if (input.batchIndex !== undefined)
112
- fields.batchIndex = input.batchIndex;
113
- if (input.kind !== undefined)
114
- fields.kind = input.kind;
99
+ const keys = [
100
+ "sqlstate",
101
+ "constraint",
102
+ "table",
103
+ "column",
104
+ "detail",
105
+ "cause",
106
+ "batchIndex",
107
+ "kind"
108
+ ];
109
+ for (const key of keys) {
110
+ const value = input[key];
111
+ if (value !== undefined)
112
+ fields[key] = value;
113
+ }
115
114
  return fields;
116
115
  }
117
116
 
@@ -0,0 +1,179 @@
1
+ import {
2
+ build3
3
+ } from "./pbp1ss85.js";
4
+ import {
5
+ attachHttp
6
+ } from "./tbrvgw6z.js";
7
+ import {
8
+ rejectKeys,
9
+ readLimit,
10
+ indexes,
11
+ isRecord,
12
+ fail2
13
+ } from "./67txcrcw.js";
14
+ import {
15
+ OkmError2
16
+ } from "./52szzakm.js";
17
+
18
+ // src/runtime/read-stream.ts
19
+ var DEFAULT_BATCH = 1000;
20
+ var MAX_BATCH = 2147483647;
21
+ async function* streamRows(session, call, mods, view, batchOptions, log) {
22
+ const stop = (error) => {
23
+ throw log(attachHttp(session.http, error));
24
+ };
25
+ let batch;
26
+ let total;
27
+ let signal;
28
+ let deadline;
29
+ try {
30
+ if (call.op !== "find") {
31
+ fail2("OKM1120", `stream() reads rows from find(). ${call.op}() returns one value.`);
32
+ }
33
+ if (call.search !== undefined) {
34
+ fail2("OKM1120", "stream() does not take search in this version. Use find({ search, limit }) for a ranked list.", "Ranked pages are later. find({ search, limit }) returns one ranked list.");
35
+ }
36
+ if (Array.isArray(call.select) && call.select.some((item) => typeof item !== "string")) {
37
+ fail2("OKM1120", "stream() does not take a window. The keyset cursor has no window value to encode.");
38
+ }
39
+ const indexed = indexes(session.schema).get(call.table);
40
+ if (indexed === undefined)
41
+ fail2("OKM1120", `Table ${call.table} is not in the schema.`);
42
+ if (indexed.model.primary.length === 0) {
43
+ fail2("OKM1120", `stream() needs a primary key on ${indexed.model.name}. A keyset batch orders by that key.`, "Give the table a primary key. stream() orders by it.");
44
+ }
45
+ batch = readBatch(batchOptions);
46
+ if (mods.all !== undefined && mods.all.length === 0) {
47
+ fail2("OKM1101", "find() .all needs a reason.");
48
+ }
49
+ if (call.limit !== undefined)
50
+ total = readLimit(call.limit);
51
+ else if (mods.all === undefined) {
52
+ fail2("OKM1101", "find() needs a limit, or .all(reason) when the full set is intentional.");
53
+ }
54
+ const timeout = call.timeout;
55
+ if (timeout !== undefined && (typeof timeout !== "number" || !(timeout > 0))) {
56
+ fail2("OKM1121", "timeout must be a number of milliseconds above 0.");
57
+ }
58
+ signal = signalOf(call.signal);
59
+ deadline = timeout === undefined ? undefined : Date.now() + timeout;
60
+ } catch (error) {
61
+ if (error instanceof OkmError2)
62
+ stop(error);
63
+ throw error;
64
+ }
65
+ if (total === 0)
66
+ return;
67
+ const cancel = session.pool.capabilities.cancel === true;
68
+ const statement = session.timeouts?.statement;
69
+ const pin = {};
70
+ let after;
71
+ let left = total;
72
+ let pending;
73
+ try {
74
+ while (left === undefined || left > 0) {
75
+ if (signal?.aborted === true) {
76
+ stop(new OkmError2("cancelled", "The stream was cancelled.", { kind: "cancelled" }));
77
+ }
78
+ const remaining = timeLeft(deadline);
79
+ const size = left === undefined ? batch : Math.min(batch, left);
80
+ const handle = build3(session, call.table, pageFields(call, size, after, signal, batchTimeout(remaining, cancel, statement)), mods, view, pin);
81
+ pending = handle;
82
+ const value = asPage(await handle);
83
+ pending = undefined;
84
+ for (const row of value.items) {
85
+ yield row;
86
+ if (left !== undefined) {
87
+ left -= 1;
88
+ if (left === 0)
89
+ return;
90
+ }
91
+ }
92
+ if (value.next === null || value.items.length < size)
93
+ return;
94
+ after = value.next;
95
+ }
96
+ } catch (error) {
97
+ if (error instanceof OkmError2 && error.kind === "timeout" && error.message === "The stream timed out.") {
98
+ stop(error);
99
+ }
100
+ throw error;
101
+ } finally {
102
+ if (pending !== undefined) {
103
+ pending.then(() => {
104
+ return;
105
+ }, () => {
106
+ return;
107
+ });
108
+ }
109
+ }
110
+ }
111
+ function readBatch(options) {
112
+ if (options === undefined)
113
+ return DEFAULT_BATCH;
114
+ if (!isRecord(options))
115
+ fail2("OKM1120", "stream() takes an object. Pass { batch }.");
116
+ rejectKeys(options, ["batch"], "stream");
117
+ const batch = options.batch;
118
+ if (batch === undefined)
119
+ return DEFAULT_BATCH;
120
+ if (typeof batch !== "number" || !Number.isInteger(batch) || batch < 1 || batch > MAX_BATCH) {
121
+ fail2("OKM1121", "stream() batch must be an integer from 1 to 2147483647.");
122
+ }
123
+ return batch;
124
+ }
125
+ function pageFields(call, size, after, signal, timeout) {
126
+ const out = { limit: size };
127
+ if (call.where !== undefined)
128
+ out.where = call.where;
129
+ if (call.select !== undefined)
130
+ out.select = call.select;
131
+ if (call.orderBy !== undefined)
132
+ out.orderBy = call.orderBy;
133
+ if (call.include !== undefined)
134
+ out.include = call.include;
135
+ if (call.route !== undefined)
136
+ out.route = call.route;
137
+ if (after !== undefined)
138
+ out.after = after;
139
+ if (signal !== undefined)
140
+ out.signal = signal;
141
+ if (timeout !== undefined)
142
+ out.timeout = timeout;
143
+ return out;
144
+ }
145
+ function timeLeft(deadline) {
146
+ if (deadline === undefined)
147
+ return;
148
+ const left = deadline - Date.now();
149
+ if (left <= 0)
150
+ throw new OkmError2("timeout", "The stream timed out.", { kind: "timeout" });
151
+ return left;
152
+ }
153
+ function batchTimeout(remaining, cancel, statement) {
154
+ if (!cancel || remaining === undefined)
155
+ return;
156
+ if (statement !== undefined && statement > 0 && statement < remaining)
157
+ return statement;
158
+ return remaining;
159
+ }
160
+ function signalOf(value) {
161
+ if (typeof value === "object" && value !== null && "aborted" in value) {
162
+ return value;
163
+ }
164
+ return;
165
+ }
166
+ function asPage(value) {
167
+ if (typeof value !== "object" || value === null || !("items" in value) || !("next" in value)) {
168
+ throw new OkmError2("OKM1120", "stream() did not get a page of rows.");
169
+ }
170
+ const items = value.items;
171
+ const next = value.next;
172
+ if (!Array.isArray(items) || next !== null && typeof next !== "string") {
173
+ throw new OkmError2("OKM1120", "stream() did not get a page of rows.");
174
+ }
175
+ return { items, next };
176
+ }
177
+ export {
178
+ streamRows
179
+ };