envio 3.0.0-alpha.21 → 3.0.0-alpha.23

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 (220) hide show
  1. package/README.md +3 -3
  2. package/bin.mjs +2 -48
  3. package/evm.schema.json +67 -0
  4. package/fuel.schema.json +67 -0
  5. package/index.d.ts +822 -38
  6. package/index.js +5 -3
  7. package/package.json +10 -8
  8. package/rescript.json +5 -9
  9. package/src/Address.res +4 -5
  10. package/src/Address.res.mjs +9 -12
  11. package/src/Api.res +15 -0
  12. package/src/Api.res.mjs +20 -0
  13. package/src/Batch.res +32 -34
  14. package/src/Batch.res.mjs +172 -187
  15. package/src/Bin.res +89 -0
  16. package/src/Bin.res.mjs +97 -0
  17. package/src/ChainFetcher.res +33 -57
  18. package/src/ChainFetcher.res.mjs +197 -227
  19. package/src/ChainManager.res +6 -14
  20. package/src/ChainManager.res.mjs +74 -85
  21. package/src/ChainMap.res +14 -16
  22. package/src/ChainMap.res.mjs +38 -38
  23. package/src/Config.res +193 -135
  24. package/src/Config.res.mjs +566 -592
  25. package/src/Core.res +182 -0
  26. package/src/Core.res.mjs +207 -0
  27. package/src/Ecosystem.res +25 -4
  28. package/src/Ecosystem.res.mjs +12 -13
  29. package/src/Env.res +20 -13
  30. package/src/Env.res.mjs +124 -113
  31. package/src/EnvSafe.res +269 -0
  32. package/src/EnvSafe.res.mjs +296 -0
  33. package/src/EnvSafe.resi +18 -0
  34. package/src/Envio.res +37 -26
  35. package/src/Envio.res.mjs +59 -60
  36. package/src/ErrorHandling.res +2 -2
  37. package/src/ErrorHandling.res.mjs +15 -15
  38. package/src/EventConfigBuilder.res +219 -81
  39. package/src/EventConfigBuilder.res.mjs +259 -202
  40. package/src/EventProcessing.res +27 -38
  41. package/src/EventProcessing.res.mjs +165 -183
  42. package/src/EventUtils.res +11 -11
  43. package/src/EventUtils.res.mjs +21 -22
  44. package/src/EvmTypes.res +0 -1
  45. package/src/EvmTypes.res.mjs +5 -5
  46. package/src/FetchState.res +360 -256
  47. package/src/FetchState.res.mjs +958 -914
  48. package/src/GlobalState.res +365 -351
  49. package/src/GlobalState.res.mjs +958 -992
  50. package/src/GlobalStateManager.res +1 -2
  51. package/src/GlobalStateManager.res.mjs +36 -44
  52. package/src/HandlerLoader.res +107 -23
  53. package/src/HandlerLoader.res.mjs +128 -38
  54. package/src/HandlerRegister.res +127 -103
  55. package/src/HandlerRegister.res.mjs +164 -164
  56. package/src/HandlerRegister.resi +12 -4
  57. package/src/Hasura.res +35 -22
  58. package/src/Hasura.res.mjs +158 -167
  59. package/src/InMemoryStore.res +20 -27
  60. package/src/InMemoryStore.res.mjs +64 -80
  61. package/src/InMemoryTable.res +34 -39
  62. package/src/InMemoryTable.res.mjs +165 -170
  63. package/src/Internal.res +52 -33
  64. package/src/Internal.res.mjs +84 -81
  65. package/src/LazyLoader.res.mjs +55 -61
  66. package/src/LoadLayer.res +77 -78
  67. package/src/LoadLayer.res.mjs +160 -189
  68. package/src/LoadManager.res +16 -21
  69. package/src/LoadManager.res.mjs +79 -84
  70. package/src/LogSelection.res +236 -68
  71. package/src/LogSelection.res.mjs +211 -141
  72. package/src/Logging.res +13 -9
  73. package/src/Logging.res.mjs +130 -143
  74. package/src/Main.res +430 -51
  75. package/src/Main.res.mjs +530 -271
  76. package/src/Persistence.res +80 -84
  77. package/src/Persistence.res.mjs +131 -132
  78. package/src/PgStorage.res +294 -167
  79. package/src/PgStorage.res.mjs +799 -817
  80. package/src/Prometheus.res +50 -58
  81. package/src/Prometheus.res.mjs +345 -373
  82. package/src/ReorgDetection.res +22 -24
  83. package/src/ReorgDetection.res.mjs +100 -106
  84. package/src/SafeCheckpointTracking.res +7 -7
  85. package/src/SafeCheckpointTracking.res.mjs +40 -43
  86. package/src/SimulateItems.res +41 -49
  87. package/src/SimulateItems.res.mjs +257 -272
  88. package/src/Sink.res +2 -2
  89. package/src/Sink.res.mjs +22 -26
  90. package/src/TableIndices.res +1 -2
  91. package/src/TableIndices.res.mjs +42 -48
  92. package/src/TestIndexer.res +196 -189
  93. package/src/TestIndexer.res.mjs +536 -536
  94. package/src/TestIndexerProxyStorage.res +16 -16
  95. package/src/TestIndexerProxyStorage.res.mjs +99 -122
  96. package/src/TestIndexerWorker.res +4 -0
  97. package/src/TestIndexerWorker.res.mjs +7 -0
  98. package/src/Throttler.res +3 -3
  99. package/src/Throttler.res.mjs +23 -24
  100. package/src/Time.res +1 -1
  101. package/src/Time.res.mjs +18 -21
  102. package/src/TopicFilter.res +3 -3
  103. package/src/TopicFilter.res.mjs +29 -30
  104. package/src/UserContext.res +93 -54
  105. package/src/UserContext.res.mjs +197 -182
  106. package/src/Utils.res +141 -86
  107. package/src/Utils.res.mjs +334 -295
  108. package/src/bindings/BigDecimal.res +0 -2
  109. package/src/bindings/BigDecimal.res.mjs +19 -23
  110. package/src/bindings/ClickHouse.res +28 -27
  111. package/src/bindings/ClickHouse.res.mjs +243 -240
  112. package/src/bindings/DateFns.res +11 -11
  113. package/src/bindings/DateFns.res.mjs +7 -7
  114. package/src/bindings/EventSource.res.mjs +2 -2
  115. package/src/bindings/Express.res +2 -5
  116. package/src/bindings/Hrtime.res +2 -2
  117. package/src/bindings/Hrtime.res.mjs +30 -32
  118. package/src/bindings/Lodash.res.mjs +1 -1
  119. package/src/bindings/NodeJs.res +14 -9
  120. package/src/bindings/NodeJs.res.mjs +20 -20
  121. package/src/bindings/Pino.res +8 -10
  122. package/src/bindings/Pino.res.mjs +40 -43
  123. package/src/bindings/Postgres.res +7 -5
  124. package/src/bindings/Postgres.res.mjs +9 -9
  125. package/src/bindings/PromClient.res +17 -2
  126. package/src/bindings/PromClient.res.mjs +30 -7
  127. package/src/bindings/SDSL.res.mjs +2 -2
  128. package/src/bindings/Viem.res +4 -4
  129. package/src/bindings/Viem.res.mjs +20 -22
  130. package/src/bindings/Vitest.res +1 -1
  131. package/src/bindings/Vitest.res.mjs +2 -2
  132. package/src/bindings/WebSocket.res +1 -1
  133. package/src/db/EntityHistory.res +9 -3
  134. package/src/db/EntityHistory.res.mjs +84 -59
  135. package/src/db/InternalTable.res +62 -60
  136. package/src/db/InternalTable.res.mjs +271 -203
  137. package/src/db/Schema.res +1 -2
  138. package/src/db/Schema.res.mjs +28 -32
  139. package/src/db/Table.res +28 -27
  140. package/src/db/Table.res.mjs +276 -292
  141. package/src/sources/EventRouter.res +21 -16
  142. package/src/sources/EventRouter.res.mjs +55 -57
  143. package/src/sources/Evm.res +17 -1
  144. package/src/sources/Evm.res.mjs +16 -8
  145. package/src/sources/EvmChain.res +15 -17
  146. package/src/sources/EvmChain.res.mjs +40 -42
  147. package/src/sources/Fuel.res +14 -1
  148. package/src/sources/Fuel.res.mjs +16 -8
  149. package/src/sources/FuelSDK.res +1 -1
  150. package/src/sources/FuelSDK.res.mjs +6 -8
  151. package/src/sources/HyperFuel.res +8 -10
  152. package/src/sources/HyperFuel.res.mjs +113 -123
  153. package/src/sources/HyperFuelClient.res.mjs +6 -7
  154. package/src/sources/HyperFuelSource.res +19 -20
  155. package/src/sources/HyperFuelSource.res.mjs +339 -356
  156. package/src/sources/HyperSync.res +11 -13
  157. package/src/sources/HyperSync.res.mjs +206 -220
  158. package/src/sources/HyperSyncClient.res +5 -7
  159. package/src/sources/HyperSyncClient.res.mjs +70 -75
  160. package/src/sources/HyperSyncHeightStream.res +8 -9
  161. package/src/sources/HyperSyncHeightStream.res.mjs +78 -86
  162. package/src/sources/HyperSyncJsonApi.res +18 -15
  163. package/src/sources/HyperSyncJsonApi.res.mjs +201 -231
  164. package/src/sources/HyperSyncSource.res +17 -21
  165. package/src/sources/HyperSyncSource.res.mjs +268 -290
  166. package/src/sources/Rpc.res +5 -5
  167. package/src/sources/Rpc.res.mjs +168 -192
  168. package/src/sources/RpcSource.res +166 -167
  169. package/src/sources/RpcSource.res.mjs +972 -1046
  170. package/src/sources/RpcWebSocketHeightStream.res +10 -11
  171. package/src/sources/RpcWebSocketHeightStream.res.mjs +131 -145
  172. package/src/sources/SimulateSource.res +1 -1
  173. package/src/sources/SimulateSource.res.mjs +35 -38
  174. package/src/sources/Source.res +1 -1
  175. package/src/sources/Source.res.mjs +3 -3
  176. package/src/sources/SourceManager.res +39 -20
  177. package/src/sources/SourceManager.res.mjs +340 -371
  178. package/src/sources/SourceManager.resi +2 -1
  179. package/src/sources/Svm.res +12 -5
  180. package/src/sources/Svm.res.mjs +44 -41
  181. package/src/tui/Tui.res +23 -12
  182. package/src/tui/Tui.res.mjs +292 -290
  183. package/src/tui/bindings/Ink.res +2 -4
  184. package/src/tui/bindings/Ink.res.mjs +35 -41
  185. package/src/tui/components/BufferedProgressBar.res +7 -7
  186. package/src/tui/components/BufferedProgressBar.res.mjs +46 -46
  187. package/src/tui/components/CustomHooks.res +1 -2
  188. package/src/tui/components/CustomHooks.res.mjs +102 -122
  189. package/src/tui/components/Messages.res +1 -2
  190. package/src/tui/components/Messages.res.mjs +38 -42
  191. package/src/tui/components/SyncETA.res +10 -11
  192. package/src/tui/components/SyncETA.res.mjs +178 -196
  193. package/src/tui/components/TuiData.res +1 -1
  194. package/src/tui/components/TuiData.res.mjs +7 -6
  195. package/src/vendored/Rest.res +52 -66
  196. package/src/vendored/Rest.res.mjs +324 -364
  197. package/svm.schema.json +67 -0
  198. package/src/Address.gen.ts +0 -8
  199. package/src/Config.gen.ts +0 -19
  200. package/src/Envio.gen.ts +0 -55
  201. package/src/EvmTypes.gen.ts +0 -6
  202. package/src/InMemoryStore.gen.ts +0 -6
  203. package/src/Internal.gen.ts +0 -64
  204. package/src/PgStorage.gen.ts +0 -10
  205. package/src/PgStorage.res.d.mts +0 -5
  206. package/src/Types.ts +0 -56
  207. package/src/bindings/BigDecimal.gen.ts +0 -14
  208. package/src/bindings/BigDecimal.res.d.mts +0 -5
  209. package/src/bindings/BigInt.gen.ts +0 -10
  210. package/src/bindings/BigInt.res +0 -70
  211. package/src/bindings/BigInt.res.d.mts +0 -5
  212. package/src/bindings/BigInt.res.mjs +0 -154
  213. package/src/bindings/Ethers.res.d.mts +0 -5
  214. package/src/bindings/Pino.gen.ts +0 -17
  215. package/src/bindings/Postgres.gen.ts +0 -8
  216. package/src/bindings/Postgres.res.d.mts +0 -5
  217. package/src/bindings/Promise.res +0 -67
  218. package/src/bindings/Promise.res.mjs +0 -26
  219. package/src/db/InternalTable.gen.ts +0 -36
  220. package/src/sources/HyperSyncClient.gen.ts +0 -19
@@ -18,10 +18,23 @@ type metricValue = {
18
18
  type metricInstance = {get: unit => promise<{"values": array<metricValue>}>}
19
19
  @send external getSingleMetric: (registry, string) => option<metricInstance> = "getSingleMetric"
20
20
  @send external resetMetrics: registry => unit = "resetMetrics"
21
+ @send external clear: registry => unit = "clear"
22
+
23
+ // Idempotent metric creation: if the metric already exists in the
24
+ // registry (e.g., module loaded twice via different pnpm paths),
25
+ // return the existing one instead of throwing.
26
+ let getOrCreate = (name: string, create: unit => 'a): 'a => {
27
+ switch defaultRegister->getSingleMetric(name) {
28
+ | Some(existing) => existing->(Utils.magic: metricInstance => 'a)
29
+ | None => create()
30
+ }
31
+ }
21
32
 
22
33
  module Counter = {
23
34
  type counter
24
- @new @module("prom-client") external makeCounter: customMetric<'a> => counter = "Counter"
35
+ @new @module("prom-client") external makeCounterUnsafe: customMetric<'a> => counter = "Counter"
36
+ let makeCounter = (config: customMetric<'a>): counter =>
37
+ getOrCreate(config["name"], () => makeCounterUnsafe(config))
25
38
 
26
39
  @send external inc: counter => unit = "inc"
27
40
  @send external incMany: (counter, int) => unit = "inc"
@@ -31,7 +44,9 @@ module Counter = {
31
44
 
32
45
  module Gauge = {
33
46
  type gauge
34
- @new @module("prom-client") external makeGauge: customMetric<'a> => gauge = "Gauge"
47
+ @new @module("prom-client") external makeGaugeUnsafe: customMetric<'a> => gauge = "Gauge"
48
+ let makeGauge = (config: customMetric<'a>): gauge =>
49
+ getOrCreate(config["name"], () => makeGaugeUnsafe(config))
35
50
 
36
51
  @send external inc: gauge => unit = "inc"
37
52
  @send external incMany: (gauge, int) => unit = "inc"
@@ -1,15 +1,38 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
+ import * as PromClient from "prom-client";
3
4
 
4
- var Counter = {};
5
+ function getOrCreate(name, create) {
6
+ let existing = PromClient.register.getSingleMetric(name);
7
+ if (existing !== undefined) {
8
+ return existing;
9
+ } else {
10
+ return create();
11
+ }
12
+ }
13
+
14
+ function makeCounter(config) {
15
+ return getOrCreate(config.name, () => new PromClient.Counter(config));
16
+ }
17
+
18
+ let Counter = {
19
+ makeCounter: makeCounter
20
+ };
21
+
22
+ function makeGauge(config) {
23
+ return getOrCreate(config.name, () => new PromClient.Gauge(config));
24
+ }
5
25
 
6
- var Gauge = {};
26
+ let Gauge = {
27
+ makeGauge: makeGauge
28
+ };
7
29
 
8
- var Summary = {};
30
+ let Summary = {};
9
31
 
10
32
  export {
11
- Counter ,
12
- Gauge ,
13
- Summary ,
33
+ getOrCreate,
34
+ Counter,
35
+ Gauge,
36
+ Summary,
14
37
  }
15
- /* No side effect */
38
+ /* prom-client Not a pure module */
@@ -1,9 +1,9 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
3
 
4
- var Queue = {};
4
+ let Queue = {};
5
5
 
6
6
  export {
7
- Queue ,
7
+ Queue,
8
8
  }
9
9
  /* No side effect */
@@ -14,7 +14,7 @@ type decodedEvent<'a> = {
14
14
  type hex = EvmTypes.Hex.t
15
15
  @module("viem") external toHex: 'a => hex = "toHex"
16
16
  @module("viem") external keccak256: hex => hex = "keccak256"
17
- @module("viem") external keccak256Bytes: bytes => hex = "keccak256"
17
+ @module("viem") external keccak256Bytes: Uint8Array.t => hex = "keccak256"
18
18
  @module("viem") external pad: hex => hex = "pad"
19
19
  @module("viem")
20
20
  external encodePacked: (~types: array<string>, ~values: array<'a>) => hex = "encodePacked"
@@ -24,7 +24,7 @@ type sizeOptions = {size: int}
24
24
  @module("viem") external bigintToHex: (bigint, ~options: sizeOptions=?) => hex = "numberToHex"
25
25
  @module("viem") external stringToHex: (string, ~options: sizeOptions=?) => hex = "stringToHex"
26
26
  @module("viem") external boolToHex: (bool, ~options: sizeOptions=?) => hex = "boolToHex"
27
- @module("viem") external bytesToHex: (bytes, ~options: sizeOptions=?) => hex = "bytesToHex"
27
+ @module("viem") external bytesToHex: (Uint8Array.t, ~options: sizeOptions=?) => hex = "bytesToHex"
28
28
  @module("viem") external concat: array<hex> => hex = "concat"
29
29
 
30
30
  exception ParseError(exn)
@@ -37,7 +37,7 @@ let parseLogOrThrow = (
37
37
  ~data,
38
38
  ) => {
39
39
  switch contractNameAbiMapping->Utils.Dict.dangerouslyGetNonOption(contractName) {
40
- | None => raise(UnknownContractName({contractName: contractName}))
40
+ | None => throw(UnknownContractName({contractName: contractName}))
41
41
  | Some(abi) =>
42
42
  let viemLog: eventLog = {
43
43
  abi,
@@ -46,7 +46,7 @@ let parseLogOrThrow = (
46
46
  }
47
47
 
48
48
  try viemLog->decodeEventLogOrThrow catch {
49
- | exn => raise(ParseError(exn))
49
+ | exn => throw(ParseError(exn))
50
50
  }
51
51
  }
52
52
  }
@@ -1,46 +1,44 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
3
  import * as Viem from "viem";
4
- import * as Caml_option from "rescript/lib/es6/caml_option.js";
5
- import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";
6
- import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
4
+ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
5
+ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
7
6
 
8
- var ParseError = /* @__PURE__ */Caml_exceptions.create("Viem.ParseError");
7
+ let ParseError = /* @__PURE__ */Primitive_exceptions.create("Viem.ParseError");
9
8
 
10
- var UnknownContractName = /* @__PURE__ */Caml_exceptions.create("Viem.UnknownContractName");
9
+ let UnknownContractName = /* @__PURE__ */Primitive_exceptions.create("Viem.UnknownContractName");
11
10
 
12
11
  function parseLogOrThrow(contractNameAbiMapping, contractName, topics, data) {
13
- var abi = contractNameAbiMapping[contractName];
12
+ let abi = contractNameAbiMapping[contractName];
14
13
  if (abi !== undefined) {
15
- var viemLog_abi = Caml_option.valFromOption(abi);
16
- var viemLog = {
14
+ let viemLog_abi = Primitive_option.valFromOption(abi);
15
+ let viemLog = {
17
16
  abi: viemLog_abi,
18
17
  data: data,
19
18
  topics: topics
20
19
  };
21
20
  try {
22
21
  return Viem.decodeEventLog(viemLog);
23
- }
24
- catch (raw_exn){
25
- var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
22
+ } catch (raw_exn) {
23
+ let exn = Primitive_exceptions.internalToException(raw_exn);
26
24
  throw {
27
- RE_EXN_ID: ParseError,
28
- _1: exn,
29
- Error: new Error()
30
- };
25
+ RE_EXN_ID: ParseError,
26
+ _1: exn,
27
+ Error: new Error()
28
+ };
31
29
  }
32
30
  } else {
33
31
  throw {
34
- RE_EXN_ID: UnknownContractName,
35
- contractName: contractName,
36
- Error: new Error()
37
- };
32
+ RE_EXN_ID: UnknownContractName,
33
+ contractName: contractName,
34
+ Error: new Error()
35
+ };
38
36
  }
39
37
  }
40
38
 
41
39
  export {
42
- ParseError ,
43
- UnknownContractName ,
44
- parseLogOrThrow ,
40
+ ParseError,
41
+ UnknownContractName,
42
+ parseLogOrThrow,
45
43
  }
46
44
  /* viem Not a pure module */
@@ -24,7 +24,7 @@ type rec expectation<'a> = {
24
24
  toBeNaN: unit => unit,
25
25
  // String matchers
26
26
  toContain: string => unit,
27
- toMatch: Js.Re.t => unit,
27
+ toMatch: RegExp.t => unit,
28
28
  toMatchString: string => unit,
29
29
  // Array matchers
30
30
  toContainItem: 'a => unit,
@@ -1,9 +1,9 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
3
 
4
- var Async = {};
4
+ let Async = {};
5
5
 
6
6
  export {
7
- Async ,
7
+ Async,
8
8
  }
9
9
  /* No side effect */
@@ -17,7 +17,7 @@ type readyState =
17
17
  @get external readyState: t => readyState = "readyState"
18
18
 
19
19
  @set external onopen: (t, unit => unit) => unit = "onopen"
20
- @set external onerror: (t, Js.Exn.t => unit) => unit = "onerror"
20
+ @set external onerror: (t, JsExn.t => unit) => unit = "onerror"
21
21
  @set external onclose: (t, unit => unit) => unit = "onclose"
22
22
 
23
23
  type messageEvent = {data: string}
@@ -141,10 +141,16 @@ let backfillHistory = (sql, ~pgSchema, ~entityName, ~entityIndex, ~ids: array<st
141
141
  makeBackfillHistoryQuery(~entityName, ~entityIndex, ~pgSchema),
142
142
  [ids]->Obj.magic,
143
143
  )
144
- ->Promise.ignoreValue
144
+ ->Utils.Promise.ignoreValue
145
145
  }
146
146
 
147
- let rollback = (sql, ~pgSchema, ~entityName, ~entityIndex, ~rollbackTargetCheckpointId: Internal.checkpointId) => {
147
+ let rollback = (
148
+ sql,
149
+ ~pgSchema,
150
+ ~entityName,
151
+ ~entityIndex,
152
+ ~rollbackTargetCheckpointId: Internal.checkpointId,
153
+ ) => {
148
154
  sql
149
155
  ->Postgres.preparedUnsafe(
150
156
  `DELETE FROM "${pgSchema}"."${historyTableName(
@@ -153,5 +159,5 @@ let rollback = (sql, ~pgSchema, ~entityName, ~entityIndex, ~rollbackTargetCheckp
153
159
  )}" WHERE "${checkpointIdFieldName}" > $1;`,
154
160
  [rollbackTargetCheckpointId->BigInt.toString]->(Utils.magic: array<string> => unknown),
155
161
  )
156
- ->Promise.ignoreValue
162
+ ->Utils.Promise.ignoreValue
157
163
  }
@@ -1,83 +1,96 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
3
  import * as Table from "./Table.res.mjs";
4
- import * as $$BigInt from "../bindings/BigInt.res.mjs";
5
- import * as Js_string from "rescript/lib/es6/js_string.js";
4
+ import * as Js_string from "@rescript/runtime/lib/es6/Js_string.js";
5
+ import * as Stdlib_BigInt from "@rescript/runtime/lib/es6/Stdlib_BigInt.js";
6
6
  import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
7
7
 
8
- var variants = [
8
+ let variants = [
9
9
  "SET",
10
10
  "DELETE"
11
11
  ];
12
12
 
13
- var name = "ENVIO_HISTORY_CHANGE";
13
+ let name = "ENVIO_HISTORY_CHANGE";
14
14
 
15
- var schema = S$RescriptSchema.$$enum(variants);
15
+ let schema = S$RescriptSchema.$$enum(variants);
16
16
 
17
- var config = {
17
+ let config = {
18
18
  name: name,
19
19
  variants: variants,
20
20
  schema: schema
21
21
  };
22
22
 
23
- var RowAction = {
23
+ let RowAction = {
24
24
  variants: variants,
25
25
  name: name,
26
26
  schema: schema,
27
27
  config: config
28
28
  };
29
29
 
30
- var changeFieldName = "envio_change";
30
+ let changeFieldName = "envio_change";
31
31
 
32
- var checkpointIdFieldName = "envio_checkpoint_id";
32
+ let checkpointIdFieldName = "envio_checkpoint_id";
33
33
 
34
- var changeFieldType = {
34
+ let changeFieldType = {
35
35
  type: "Enum",
36
36
  config: config
37
37
  };
38
38
 
39
- var unsafeCheckpointIdSchema = S$RescriptSchema.transform(S$RescriptSchema.setName(S$RescriptSchema.string, "CheckpointId"), (function (s) {
40
- return {
41
- p: (function (string) {
42
- var v = $$BigInt.fromString(string);
43
- if (v !== undefined) {
44
- return v;
45
- } else {
46
- return s.fail("The string is not valid CheckpointId", undefined);
47
- }
48
- }),
49
- s: (function (bigint) {
50
- return bigint.toString();
51
- })
52
- };
53
- }));
39
+ let unsafeCheckpointIdSchema = S$RescriptSchema.transform(S$RescriptSchema.setName(S$RescriptSchema.string, "CheckpointId"), s => ({
40
+ p: string => {
41
+ let v = Stdlib_BigInt.fromString(string);
42
+ if (v !== undefined) {
43
+ return v;
44
+ } else {
45
+ return s.fail("The string is not valid CheckpointId", undefined);
46
+ }
47
+ },
48
+ s: bigint => bigint.toString()
49
+ }));
54
50
 
55
51
  function makeSetUpdateSchema(entitySchema) {
56
- return S$RescriptSchema.object(function (s) {
57
- s.tag(changeFieldName, "SET");
58
- return {
59
- type: "SET",
60
- entityId: s.f(Table.idFieldName, S$RescriptSchema.string),
61
- entity: s.flatten(entitySchema),
62
- checkpointId: s.f(checkpointIdFieldName, unsafeCheckpointIdSchema)
63
- };
64
- });
52
+ return S$RescriptSchema.object(s => {
53
+ s.tag(changeFieldName, "SET");
54
+ return {
55
+ type: "SET",
56
+ entityId: s.f(Table.idFieldName, S$RescriptSchema.string),
57
+ entity: s.flatten(entitySchema),
58
+ checkpointId: s.f(checkpointIdFieldName, unsafeCheckpointIdSchema)
59
+ };
60
+ });
65
61
  }
66
62
 
67
- var historyTablePrefix = "envio_history_";
63
+ let historyTablePrefix = "envio_history_";
68
64
 
69
65
  function historyTableName(entityName, entityIndex) {
70
- var fullName = historyTablePrefix + entityName;
66
+ let fullName = historyTablePrefix + entityName;
71
67
  if (fullName.length <= 63) {
72
68
  return fullName;
73
69
  }
74
- var entityIndexStr = String(entityIndex);
70
+ let entityIndexStr = String(entityIndex);
75
71
  return Js_string.slice(0, 63 - entityIndexStr.length | 0, fullName) + entityIndexStr;
76
72
  }
77
73
 
78
74
  function makePruneStaleEntityHistoryQuery(entityName, entityIndex, pgSchema) {
79
- var historyTableRef = "\"" + pgSchema + "\".\"" + historyTableName(entityName, entityIndex) + "\"";
80
- return "WITH anchors AS (\n SELECT t.id, MAX(t." + checkpointIdFieldName + ") AS keep_checkpoint_id\n FROM " + historyTableRef + " t WHERE t." + checkpointIdFieldName + " <= $1\n GROUP BY t.id\n)\nDELETE FROM " + historyTableRef + " d\nUSING anchors a\nWHERE d.id = a.id\n AND (\n d." + checkpointIdFieldName + " < a.keep_checkpoint_id\n OR (\n d." + checkpointIdFieldName + " = a.keep_checkpoint_id AND\n NOT EXISTS (\n SELECT 1 FROM " + historyTableRef + " ps \n WHERE ps.id = d.id AND ps." + checkpointIdFieldName + " > $1\n ) \n )\n );";
75
+ let historyTableRef = `"` + pgSchema + `"."` + historyTableName(entityName, entityIndex) + `"`;
76
+ return `WITH anchors AS (
77
+ SELECT t.id, MAX(t.` + checkpointIdFieldName + `) AS keep_checkpoint_id
78
+ FROM ` + historyTableRef + ` t WHERE t.` + checkpointIdFieldName + ` <= $1
79
+ GROUP BY t.id
80
+ )
81
+ DELETE FROM ` + historyTableRef + ` d
82
+ USING anchors a
83
+ WHERE d.id = a.id
84
+ AND (
85
+ d.` + checkpointIdFieldName + ` < a.keep_checkpoint_id
86
+ OR (
87
+ d.` + checkpointIdFieldName + ` = a.keep_checkpoint_id AND
88
+ NOT EXISTS (
89
+ SELECT 1 FROM ` + historyTableRef + ` ps
90
+ WHERE ps.id = d.id AND ps.` + checkpointIdFieldName + ` > $1
91
+ )
92
+ )
93
+ );`;
81
94
  }
82
95
 
83
96
  function pruneStaleEntityHistory(sql, entityName, entityIndex, pgSchema, safeCheckpointId) {
@@ -85,8 +98,20 @@ function pruneStaleEntityHistory(sql, entityName, entityIndex, pgSchema, safeChe
85
98
  }
86
99
 
87
100
  function makeBackfillHistoryQuery(pgSchema, entityName, entityIndex) {
88
- var historyTableRef = "\"" + pgSchema + "\".\"" + historyTableName(entityName, entityIndex) + "\"";
89
- return "WITH target_ids AS (\n SELECT UNNEST($1::TEXT[]) AS id\n),\nmissing_history AS (\n SELECT e.*\n FROM \"" + pgSchema + "\".\"" + entityName + "\" e\n JOIN target_ids t ON e.id = t.id\n LEFT JOIN " + historyTableRef + " h ON h.id = e.id\n WHERE h.id IS NULL\n)\nINSERT INTO " + historyTableRef + "\nSELECT *, 0 AS " + checkpointIdFieldName + ", '" + "SET" + "' as " + changeFieldName + "\nFROM missing_history;";
101
+ let historyTableRef = `"` + pgSchema + `"."` + historyTableName(entityName, entityIndex) + `"`;
102
+ return `WITH target_ids AS (
103
+ SELECT UNNEST($1::` + "TEXT" + `[]) AS id
104
+ ),
105
+ missing_history AS (
106
+ SELECT e.*
107
+ FROM "` + pgSchema + `"."` + entityName + `" e
108
+ JOIN target_ids t ON e.id = t.id
109
+ LEFT JOIN ` + historyTableRef + ` h ON h.id = e.id
110
+ WHERE h.id IS NULL
111
+ )
112
+ INSERT INTO ` + historyTableRef + `
113
+ SELECT *, 0 AS ` + checkpointIdFieldName + `, '` + "SET" + `' as ` + changeFieldName + `
114
+ FROM missing_history;`;
90
115
  }
91
116
 
92
117
  function backfillHistory(sql, pgSchema, entityName, entityIndex, ids) {
@@ -94,28 +119,28 @@ function backfillHistory(sql, pgSchema, entityName, entityIndex, ids) {
94
119
  }
95
120
 
96
121
  function rollback(sql, pgSchema, entityName, entityIndex, rollbackTargetCheckpointId) {
97
- return sql.unsafe("DELETE FROM \"" + pgSchema + "\".\"" + historyTableName(entityName, entityIndex) + "\" WHERE \"" + checkpointIdFieldName + "\" > $1;", [rollbackTargetCheckpointId.toString()], {prepare: true});
122
+ return sql.unsafe(`DELETE FROM "` + pgSchema + `"."` + historyTableName(entityName, entityIndex) + `" WHERE "` + checkpointIdFieldName + `" > $1;`, [rollbackTargetCheckpointId.toString()], {prepare: true});
98
123
  }
99
124
 
100
- var checkpointIdFieldType = "UInt64";
125
+ let checkpointIdFieldType = "UInt64";
101
126
 
102
- var maxPgTableNameLength = 63;
127
+ let maxPgTableNameLength = 63;
103
128
 
104
129
  export {
105
- RowAction ,
106
- changeFieldName ,
107
- checkpointIdFieldName ,
108
- checkpointIdFieldType ,
109
- changeFieldType ,
110
- unsafeCheckpointIdSchema ,
111
- makeSetUpdateSchema ,
112
- maxPgTableNameLength ,
113
- historyTablePrefix ,
114
- historyTableName ,
115
- makePruneStaleEntityHistoryQuery ,
116
- pruneStaleEntityHistory ,
117
- makeBackfillHistoryQuery ,
118
- backfillHistory ,
119
- rollback ,
130
+ RowAction,
131
+ changeFieldName,
132
+ checkpointIdFieldName,
133
+ checkpointIdFieldType,
134
+ changeFieldType,
135
+ unsafeCheckpointIdSchema,
136
+ makeSetUpdateSchema,
137
+ maxPgTableNameLength,
138
+ historyTablePrefix,
139
+ historyTableName,
140
+ makePruneStaleEntityHistoryQuery,
141
+ pruneStaleEntityHistory,
142
+ makeBackfillHistoryQuery,
143
+ backfillHistory,
144
+ rollback,
120
145
  }
121
146
  /* schema Not a pure module */