envio 2.26.0-alpha.9 → 2.26.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -13,6 +13,7 @@ import type {
13
13
  } from "./src/Envio.gen.ts";
14
14
 
15
15
  import { schema as bigDecimalSchema } from "./src/bindings/BigDecimal.gen.ts";
16
+ import { schema as bigintSchema } from "./src/bindings/BigInt.gen.ts";
16
17
  import * as Sury from "rescript-schema";
17
18
 
18
19
  type UnknownToOutput<T> = T extends Sury.Schema<unknown>
@@ -35,26 +36,6 @@ type UnknownToOutput<T> = T extends Sury.Schema<unknown>
35
36
  >
36
37
  : T;
37
38
 
38
- type UnknownToInput<T> = T extends Sury.Schema<unknown>
39
- ? Sury.Input<T>
40
- : T extends (...args: any[]) => any
41
- ? T
42
- : T extends unknown[]
43
- ? { [K in keyof T]: UnknownToInput<T[K]> }
44
- : T extends { [k in keyof T]: unknown }
45
- ? Flatten<
46
- {
47
- [k in keyof T as HasUndefined<UnknownToInput<T[k]>> extends true
48
- ? k
49
- : never]?: UnknownToInput<T[k]>;
50
- } & {
51
- [k in keyof T as HasUndefined<UnknownToInput<T[k]>> extends true
52
- ? never
53
- : k]: UnknownToInput<T[k]>;
54
- }
55
- >
56
- : T;
57
-
58
39
  type HasUndefined<T> = [T] extends [undefined]
59
40
  ? true
60
41
  : undefined extends T
@@ -92,7 +73,7 @@ type Flatten<T> = T extends object
92
73
  export function experimental_createEffect<
93
74
  IS,
94
75
  OS,
95
- I = UnknownToInput<IS>,
76
+ I = UnknownToOutput<IS>,
96
77
  O = UnknownToOutput<OS>,
97
78
  // A hack to enforce that the inferred return type
98
79
  // matches the output schema type
@@ -121,7 +102,7 @@ export declare namespace S {
121
102
  export const boolean: typeof Sury.boolean;
122
103
  export const int32: typeof Sury.int32;
123
104
  export const number: typeof Sury.number;
124
- export const bigint: typeof Sury.bigint;
105
+ export const bigint: typeof bigintSchema;
125
106
  export const never: typeof Sury.never;
126
107
  export const union: typeof Sury.union;
127
108
  export const object: typeof Sury.object;
@@ -130,6 +111,7 @@ export declare namespace S {
130
111
  // Don't expose recursive for now, since it's too advanced
131
112
  // export const recursive: typeof Sury.recursive;
132
113
  export const transform: typeof Sury.transform;
114
+ export const shape: typeof Sury.shape;
133
115
  export const refine: typeof Sury.refine;
134
116
  export const schema: typeof Sury.schema;
135
117
  export const record: typeof Sury.record;
package/index.js CHANGED
@@ -13,7 +13,7 @@ exports.S = {
13
13
  boolean: Sury.boolean,
14
14
  int32: Sury.int32,
15
15
  number: Sury.number,
16
- bigint: Sury.bigint,
16
+ bigint: require("./src/bindings/BigInt.res.js").schema,
17
17
  never: Sury.never,
18
18
  union: Sury.union,
19
19
  object: Sury.object,
@@ -22,6 +22,7 @@ exports.S = {
22
22
  // Don't expose recursive for now, since it's too advanced
23
23
  // recursive: Sury.recursive,
24
24
  transform: Sury.transform,
25
+ shape: Sury.shape,
25
26
  refine: Sury.refine,
26
27
  schema: Sury.schema,
27
28
  record: Sury.record,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "envio",
3
- "version": "v2.26.0-alpha.9",
3
+ "version": "v2.26.0-rc.1",
4
4
  "description": "A latency and sync speed optimized, developer friendly blockchain data indexer.",
5
5
  "bin": "./bin.js",
6
6
  "main": "./index.js",
@@ -25,10 +25,10 @@
25
25
  },
26
26
  "homepage": "https://envio.dev",
27
27
  "optionalDependencies": {
28
- "envio-linux-x64": "v2.26.0-alpha.9",
29
- "envio-linux-arm64": "v2.26.0-alpha.9",
30
- "envio-darwin-x64": "v2.26.0-alpha.9",
31
- "envio-darwin-arm64": "v2.26.0-alpha.9"
28
+ "envio-linux-x64": "v2.26.0-rc.1",
29
+ "envio-linux-arm64": "v2.26.0-rc.1",
30
+ "envio-darwin-x64": "v2.26.0-rc.1",
31
+ "envio-darwin-arm64": "v2.26.0-rc.1"
32
32
  },
33
33
  "dependencies": {
34
34
  "@envio-dev/hypersync-client": "0.6.5",
package/src/Envio.res CHANGED
@@ -50,6 +50,16 @@ let experimental_createEffect = (
50
50
  >
51
51
  ),
52
52
  callsCount: 0,
53
+ // This is the way to make the createEffect API
54
+ // work without the need for users to call S.schema themselves,
55
+ // but simply pass the desired object/tuple/etc.
56
+ // If they pass a schem, it'll also work.
57
+ input: S.schema(_ => options.input)->(
58
+ Utils.magic: S.t<S.t<'input>> => S.t<Internal.effectInput>
59
+ ),
60
+ output: S.schema(_ => options.output)->(
61
+ Utils.magic: S.t<S.t<'output>> => S.t<Internal.effectOutput>
62
+ ),
53
63
  cache: options.cache->Belt.Option.getWithDefault(false),
54
64
  }->(Utils.magic: Internal.effect => effect<'input, 'output>)
55
65
  }
package/src/Envio.res.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  var Prometheus = require("./Prometheus.res.js");
5
5
  var Belt_Option = require("rescript/lib/js/belt_Option.js");
6
+ var S$RescriptSchema = require("rescript-schema/src/S.res.js");
6
7
 
7
8
  function experimental_createEffect(options, handler) {
8
9
  Prometheus.EffectCallsCount.set(0, options.name);
@@ -10,6 +11,12 @@ function experimental_createEffect(options, handler) {
10
11
  name: options.name,
11
12
  handler: handler,
12
13
  cache: Belt_Option.getWithDefault(options.cache, false),
14
+ output: S$RescriptSchema.schema(function (param) {
15
+ return options.output;
16
+ }),
17
+ input: S$RescriptSchema.schema(function (param) {
18
+ return options.input;
19
+ }),
13
20
  callsCount: 0
14
21
  };
15
22
  }
package/src/Internal.res CHANGED
@@ -198,6 +198,8 @@ type effect = {
198
198
  name: string,
199
199
  handler: effectArgs => promise<effectOutput>,
200
200
  cache: bool,
201
+ output: S.t<effectOutput>,
202
+ input: S.t<effectInput>,
201
203
  mutable callsCount: int,
202
204
  }
203
205
 
@@ -5,14 +5,12 @@
5
5
  // Currently there are quite many code spread across
6
6
  // DbFunctions, Db, Migrations, InMemoryStore modules which use codegen code directly.
7
7
 
8
- // The type reflects an effect cache table in the db
8
+ // The type reflects an cache table in the db
9
9
  // It might be present even if the effect is not used in the application
10
- type effectCache = {
11
- name: string,
10
+ type effectCacheRecord = {
11
+ effectName: string,
12
12
  // Number of rows in the table
13
- mutable size: int,
14
- // Lazily attached table definition when effect is used in the application
15
- mutable table: option<Table.table>,
13
+ mutable count: int,
16
14
  }
17
15
 
18
16
  type operator = [#">" | #"="]
@@ -28,7 +26,6 @@ type storage = {
28
26
  ~generalTables: array<Table.table>=?,
29
27
  ~enums: array<Internal.enumConfig<Internal.enum>>=?,
30
28
  ) => promise<unit>,
31
- loadEffectCaches: unit => promise<array<effectCache>>,
32
29
  @raises("StorageError")
33
30
  loadByIdsOrThrow: 'item. (
34
31
  ~ids: array<string>,
@@ -50,6 +47,16 @@ type storage = {
50
47
  ~table: Table.table,
51
48
  ~itemSchema: S.t<'item>,
52
49
  ) => promise<unit>,
50
+ @raises("StorageError")
51
+ setEffectCacheOrThrow: (
52
+ ~effectName: string,
53
+ ~ids: array<string>,
54
+ ~outputs: array<Internal.effectOutput>,
55
+ ~outputSchema: S.t<Internal.effectOutput>,
56
+ ~initialize: bool,
57
+ ) => promise<unit>,
58
+ dumpEffectCache: unit => promise<unit>,
59
+ restoreEffectCache: (~withUpload: bool) => promise<array<effectCacheRecord>>,
53
60
  }
54
61
 
55
62
  exception StorageError({message: string, reason: exn})
@@ -57,7 +64,7 @@ exception StorageError({message: string, reason: exn})
57
64
  type storageStatus =
58
65
  | Unknown
59
66
  | Initializing(promise<unit>)
60
- | Ready({cleanRun: bool, effectCaches: dict<effectCache>})
67
+ | Ready({cleanRun: bool, cache: dict<effectCacheRecord>})
61
68
 
62
69
  type t = {
63
70
  userEntities: array<Internal.entityConfig>,
@@ -66,7 +73,6 @@ type t = {
66
73
  allEnums: array<Internal.enumConfig<Internal.enum>>,
67
74
  mutable storageStatus: storageStatus,
68
75
  storage: storage,
69
- onStorageInitialize: option<unit => promise<unit>>,
70
76
  }
71
77
 
72
78
  let entityHistoryActionEnumConfig: Internal.enumConfig<EntityHistory.RowAction.t> = {
@@ -83,7 +89,6 @@ let make = (
83
89
  ~allEnums,
84
90
  ~staticTables,
85
91
  ~storage,
86
- ~onStorageInitialize=?,
87
92
  ) => {
88
93
  let allEntities = userEntities->Js.Array2.concat([dcRegistryEntityConfig])
89
94
  let allEnums =
@@ -95,62 +100,70 @@ let make = (
95
100
  allEnums,
96
101
  storageStatus: Unknown,
97
102
  storage,
98
- onStorageInitialize,
99
103
  }
100
104
  }
101
105
 
102
- let init = async (persistence, ~reset=false) => {
103
- try {
104
- let shouldRun = switch persistence.storageStatus {
105
- | Unknown => true
106
- | Initializing(promise) => {
107
- await promise
108
- reset
109
- }
110
- | Ready(_) => reset
111
- }
112
- if shouldRun {
113
- let resolveRef = ref(%raw(`null`))
114
- let promise = Promise.make((resolve, _) => {
115
- resolveRef := resolve
116
- })
117
- persistence.storageStatus = Initializing(promise)
118
- if reset || !(await persistence.storage.isInitialized()) {
119
- let _ = await persistence.storage.initialize(
120
- ~entities=persistence.allEntities,
121
- ~generalTables=persistence.staticTables,
122
- ~enums=persistence.allEnums,
123
- )
106
+ let init = {
107
+ let loadInitialCache = async (persistence, ~withUpload) => {
108
+ let effectCacheRecords = await persistence.storage.restoreEffectCache(~withUpload)
109
+ let cache = Js.Dict.empty()
110
+ effectCacheRecords->Js.Array2.forEach(record => {
111
+ Prometheus.EffectCacheCount.set(~count=record.count, ~effectName=record.effectName)
112
+ cache->Js.Dict.set(record.effectName, record)
113
+ })
114
+ cache
115
+ }
124
116
 
125
- persistence.storageStatus = Ready({
126
- cleanRun: true,
127
- effectCaches: Js.Dict.empty(),
128
- })
129
- switch persistence.onStorageInitialize {
130
- | Some(onStorageInitialize) => await onStorageInitialize()
131
- | None => ()
132
- }
133
- } else if (
134
- // In case of a race condition,
135
- // we want to set the initial status to Ready only once.
136
- switch persistence.storageStatus {
137
- | Initializing(_) => true
138
- | _ => false
117
+ async (persistence, ~reset=false) => {
118
+ try {
119
+ let shouldRun = switch persistence.storageStatus {
120
+ | Unknown => true
121
+ | Initializing(promise) => {
122
+ await promise
123
+ reset
139
124
  }
140
- ) {
141
- let effectCaches = Js.Dict.empty()
142
- (await persistence.storage.loadEffectCaches())->Js.Array2.forEach(effectCache => {
143
- effectCaches->Js.Dict.set(effectCache.name, effectCache)
144
- })
145
- persistence.storageStatus = Ready({
146
- cleanRun: false,
147
- effectCaches,
125
+ | Ready(_) => reset
126
+ }
127
+ if shouldRun {
128
+ let resolveRef = ref(%raw(`null`))
129
+ let promise = Promise.make((resolve, _) => {
130
+ resolveRef := resolve
148
131
  })
132
+ persistence.storageStatus = Initializing(promise)
133
+ if reset || !(await persistence.storage.isInitialized()) {
134
+ Logging.info(`Initializing the indexer storage...`)
135
+
136
+ await persistence.storage.initialize(
137
+ ~entities=persistence.allEntities,
138
+ ~generalTables=persistence.staticTables,
139
+ ~enums=persistence.allEnums,
140
+ )
141
+
142
+ Logging.info(`The indexer storage is ready. Uploading cache...`)
143
+ persistence.storageStatus = Ready({
144
+ cleanRun: true,
145
+ cache: await loadInitialCache(persistence, ~withUpload=true),
146
+ })
147
+ } else if (
148
+ // In case of a race condition,
149
+ // we want to set the initial status to Ready only once.
150
+ switch persistence.storageStatus {
151
+ | Initializing(_) => true
152
+ | _ => false
153
+ }
154
+ ) {
155
+ Logging.info(`The indexer storage is ready.`)
156
+ persistence.storageStatus = Ready({
157
+ cleanRun: false,
158
+ cache: await loadInitialCache(persistence, ~withUpload=false),
159
+ })
160
+ }
161
+ resolveRef.contents()
149
162
  }
150
- resolveRef.contents()
163
+ } catch {
164
+ | exn =>
165
+ exn->ErrorHandling.mkLogAndRaise(~msg=`EE800: Failed to initialize the indexer storage.`)
151
166
  }
152
- } catch {
153
- | exn => exn->ErrorHandling.mkLogAndRaise(~msg=`EE800: Failed to initialize the indexer storage.`)
154
167
  }
155
168
  }
156
169
 
@@ -162,3 +175,26 @@ let getInitializedStorageOrThrow = persistence => {
162
175
  | Ready(_) => persistence.storage
163
176
  }
164
177
  }
178
+
179
+ let setEffectCacheOrThrow = async (persistence, ~effectName, ~ids, ~outputs, ~outputSchema) => {
180
+ switch persistence.storageStatus {
181
+ | Unknown
182
+ | Initializing(_) =>
183
+ Js.Exn.raiseError(`Failed to access the indexer storage. The Persistence layer is not initialized.`)
184
+ | Ready({cache}) => {
185
+ let storage = persistence.storage
186
+ let effectCacheRecord = switch cache->Utils.Dict.dangerouslyGetNonOption(effectName) {
187
+ | Some(c) => c
188
+ | None => {
189
+ let c = {effectName, count: 0}
190
+ cache->Js.Dict.set(effectName, c)
191
+ c
192
+ }
193
+ }
194
+ let initialize = effectCacheRecord.count === 0
195
+ await storage.setEffectCacheOrThrow(~effectName, ~ids, ~outputs, ~outputSchema, ~initialize)
196
+ effectCacheRecord.count = effectCacheRecord.count + ids->Js.Array2.length
197
+ Prometheus.EffectCacheCount.set(~count=effectCacheRecord.count, ~effectName)
198
+ }
199
+ }
200
+ }
@@ -2,6 +2,8 @@
2
2
  'use strict';
3
3
 
4
4
  var Js_exn = require("rescript/lib/js/js_exn.js");
5
+ var Logging = require("./Logging.res.js");
6
+ var Prometheus = require("./Prometheus.res.js");
5
7
  var EntityHistory = require("./db/EntityHistory.res.js");
6
8
  var ErrorHandling = require("./ErrorHandling.res.js");
7
9
  var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js");
@@ -22,7 +24,7 @@ var entityHistoryActionEnumConfig = {
22
24
  default: "SET"
23
25
  };
24
26
 
25
- function make(userEntities, dcRegistryEntityConfig, allEnums, staticTables, storage, onStorageInitialize) {
27
+ function make(userEntities, dcRegistryEntityConfig, allEnums, staticTables, storage) {
26
28
  var allEntities = userEntities.concat([dcRegistryEntityConfig]);
27
29
  var allEnums$1 = allEnums.concat([entityHistoryActionEnumConfig]);
28
30
  return {
@@ -31,11 +33,20 @@ function make(userEntities, dcRegistryEntityConfig, allEnums, staticTables, stor
31
33
  allEntities: allEntities,
32
34
  allEnums: allEnums$1,
33
35
  storageStatus: "Unknown",
34
- storage: storage,
35
- onStorageInitialize: onStorageInitialize
36
+ storage: storage
36
37
  };
37
38
  }
38
39
 
40
+ async function loadInitialCache(persistence, withUpload) {
41
+ var effectCacheRecords = await persistence.storage.restoreEffectCache(withUpload);
42
+ var cache = {};
43
+ effectCacheRecords.forEach(function (record) {
44
+ Prometheus.EffectCacheCount.set(record.count, record.effectName);
45
+ cache[record.effectName] = record;
46
+ });
47
+ return cache;
48
+ }
49
+
39
50
  async function init(persistence, resetOpt) {
40
51
  var reset = resetOpt !== undefined ? resetOpt : false;
41
52
  try {
@@ -63,30 +74,24 @@ async function init(persistence, resetOpt) {
63
74
  _0: promise$1
64
75
  };
65
76
  if (reset || !await persistence.storage.isInitialized()) {
77
+ Logging.info("Initializing the indexer storage...");
66
78
  await persistence.storage.initialize(persistence.allEntities, persistence.staticTables, persistence.allEnums);
79
+ Logging.info("The indexer storage is ready. Uploading cache...");
67
80
  persistence.storageStatus = {
68
81
  TAG: "Ready",
69
82
  cleanRun: true,
70
- effectCaches: {}
83
+ cache: await loadInitialCache(persistence, true)
71
84
  };
72
- var onStorageInitialize = persistence.onStorageInitialize;
73
- if (onStorageInitialize !== undefined) {
74
- await onStorageInitialize();
75
- }
76
-
77
85
  } else {
78
86
  var match = persistence.storageStatus;
79
87
  var tmp;
80
88
  tmp = typeof match !== "object" || match.TAG !== "Initializing" ? false : true;
81
89
  if (tmp) {
82
- var effectCaches = {};
83
- (await persistence.storage.loadEffectCaches()).forEach(function (effectCache) {
84
- effectCaches[effectCache.name] = effectCache;
85
- });
90
+ Logging.info("The indexer storage is ready.");
86
91
  persistence.storageStatus = {
87
92
  TAG: "Ready",
88
93
  cleanRun: false,
89
- effectCaches: effectCaches
94
+ cache: await loadInitialCache(persistence, false)
90
95
  };
91
96
  }
92
97
 
@@ -108,9 +113,38 @@ function getInitializedStorageOrThrow(persistence) {
108
113
  }
109
114
  }
110
115
 
116
+ async function setEffectCacheOrThrow(persistence, effectName, ids, outputs, outputSchema) {
117
+ var match = persistence.storageStatus;
118
+ if (typeof match !== "object") {
119
+ return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
120
+ }
121
+ if (match.TAG === "Initializing") {
122
+ return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
123
+ }
124
+ var cache = match.cache;
125
+ var storage = persistence.storage;
126
+ var c = cache[effectName];
127
+ var effectCacheRecord;
128
+ if (c !== undefined) {
129
+ effectCacheRecord = c;
130
+ } else {
131
+ var c$1 = {
132
+ effectName: effectName,
133
+ count: 0
134
+ };
135
+ cache[effectName] = c$1;
136
+ effectCacheRecord = c$1;
137
+ }
138
+ var initialize = effectCacheRecord.count === 0;
139
+ await storage.setEffectCacheOrThrow(effectName, ids, outputs, outputSchema, initialize);
140
+ effectCacheRecord.count = effectCacheRecord.count + ids.length | 0;
141
+ return Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName);
142
+ }
143
+
111
144
  exports.StorageError = StorageError;
112
145
  exports.entityHistoryActionEnumConfig = entityHistoryActionEnumConfig;
113
146
  exports.make = make;
114
147
  exports.init = init;
115
148
  exports.getInitializedStorageOrThrow = getInitializedStorageOrThrow;
116
- /* EntityHistory Not a pure module */
149
+ exports.setEffectCacheOrThrow = setEffectCacheOrThrow;
150
+ /* Logging Not a pure module */