Skip to content

Commit 40da641

Browse files
authored
Librarify InMemoryStore (#816)
* Move resource to npm package * fix tests
1 parent b8c2d45 commit 40da641

11 files changed

Lines changed: 20 additions & 19 deletions

File tree

codegenerator/cli/templates/static/codegen/src/InMemoryStore.res renamed to codegenerator/cli/npm/envio/src/InMemoryStore.res

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let hashRawEventsKey = (key: rawEventsKey) =>
88
EventUtils.getEventIdKeyString(~chainId=key.chainId, ~eventId=key.eventId)
99

1010
module EntityTables = {
11-
type t = dict<InMemoryTable.Entity.t<Entities.internalEntity>>
11+
type t = dict<InMemoryTable.Entity.t<Internal.entity>>
1212
exception UndefinedEntity({entityName: string})
1313
let make = (entities: array<Internal.entityConfig>): t => {
1414
let init = Js.Dict.empty()
@@ -22,7 +22,7 @@ module EntityTables = {
2222
switch self->Utils.Dict.dangerouslyGetNonOption(entityName) {
2323
| Some(table) =>
2424
table->(
25-
Utils.magic: InMemoryTable.Entity.t<Entities.internalEntity> => InMemoryTable.Entity.t<
25+
Utils.magic: InMemoryTable.Entity.t<Internal.entity> => InMemoryTable.Entity.t<
2626
entity,
2727
>
2828
)
@@ -51,13 +51,13 @@ type effectCacheInMemTable = {
5151

5252
type t = {
5353
rawEvents: InMemoryTable.t<rawEventsKey, InternalTable.RawEvents.t>,
54-
entities: dict<InMemoryTable.Entity.t<Entities.internalEntity>>,
54+
entities: dict<InMemoryTable.Entity.t<Internal.entity>>,
5555
effects: dict<effectCacheInMemTable>,
5656
rollbackTargetCheckpointId: option<int>,
5757
}
5858

5959
let make = (
60-
~entities: array<Internal.entityConfig>=Entities.allEntities,
60+
~entities: array<Internal.entityConfig>,
6161
~rollbackTargetCheckpointId=?,
6262
): t => {
6363
rawEvents: InMemoryTable.make(~hash=hashRawEventsKey),
@@ -106,7 +106,7 @@ let isRollingBack = (inMemoryStore: t) => inMemoryStore.rollbackTargetCheckpoint
106106
let setBatchDcs = (inMemoryStore: t, ~batch: Batch.t, ~shouldSaveHistory) => {
107107
let inMemTable =
108108
inMemoryStore->getInMemTable(
109-
~entityConfig=module(InternalTable.DynamicContractRegistry)->Entities.entityModToInternal,
109+
~entityConfig=InternalTable.DynamicContractRegistry.config,
110110
)
111111

112112
let itemIdx = ref(0)
@@ -154,3 +154,4 @@ let setBatchDcs = (inMemoryStore: t, ~batch: Batch.t, ~shouldSaveHistory) => {
154154
itemIdx := itemIdx.contents + checkpointEventsProcessed
155155
}
156156
}
157+

codegenerator/cli/templates/dynamic/codegen/src/TestHelpers_MockDb.res.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ let rec makeWithInMemoryStore: InMemoryStore.t => t = (inMemoryStore: InMemorySt
272272
~getStore=db =>
273273
db
274274
->InMemoryStore.getInMemTable(
275-
~entityConfig=module(InternalTable.DynamicContractRegistry)->Entities.entityModToInternal,
275+
~entityConfig=InternalTable.DynamicContractRegistry.config,
276276
)
277277
->(
278278
Utils.magic: InMemoryTable.Entity.t<Internal.entity> => InMemoryTable.Entity.t<
@@ -403,7 +403,7 @@ and makeProcessEvents = (mockDb: t, ~chainId=?) => async (
403403
let mockDbClone = mockDb->cloneMockDb
404404

405405
//Construct a new instance of an in memory store to run for the given event
406-
let inMemoryStore = InMemoryStore.make()
406+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
407407
let loadManager = LoadManager.make()
408408
let persistence = {
409409
...indexer.persistence,
@@ -626,4 +626,4 @@ new state so you can compare states before and after.
626626
@genType
627627
let //Note: It's called createMockDb over "make" to make it more intuitive in JS and TS
628628

629-
createMockDb = () => makeWithInMemoryStore(InMemoryStore.make())
629+
createMockDb = () => makeWithInMemoryStore(InMemoryStore.make(~entities=Entities.allEntities))

codegenerator/cli/templates/static/codegen/src/IO.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ let executeBatch = async (
327327
}
328328

329329
let prepareRollbackDiff = async (~persistence: Persistence.t, ~rollbackTargetCheckpointId) => {
330-
let inMemStore = InMemoryStore.make(~rollbackTargetCheckpointId)
330+
let inMemStore = InMemoryStore.make(~entities=Entities.allEntities, ~rollbackTargetCheckpointId)
331331

332332
let deletedEntities = Js.Dict.empty()
333333
let setEntities = Js.Dict.empty()

codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ let injectedTaskReducer = (
955955
| _ => None
956956
}
957957

958-
let inMemoryStore = rollbackInMemStore->Option.getWithDefault(InMemoryStore.make())
958+
let inMemoryStore = rollbackInMemStore->Option.getWithDefault(InMemoryStore.make(~entities=Entities.allEntities))
959959

960960
inMemoryStore->InMemoryStore.setBatchDcs(~batch, ~shouldSaveHistory)
961961

scenarios/erc20_multichain_factory/pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scenarios/test_codegen/pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scenarios/test_codegen/test/EventOrigin_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("Chains State", () => {
3939
async () => {
4040
// This test verifies that the chains field is accessible
4141
// The actual integration test is in EventHandlers.res with the EmptyEvent handler
42-
let inMemoryStore = InMemoryStore.make()
42+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
4343
let loadManager = LoadManager.make()
4444

4545
let item = MockEvents.newGravatarLog1->MockEvents.newGravatarEventToBatchItem

scenarios/test_codegen/test/LoadLayer_test.res

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open RescriptMocha
33
describe("LoadLayer", () => {
44
Async.it("Trys to load non existing entity from db", async () => {
55
let storageMock = Mock.Storage.make([#loadByIdsOrThrow])
6-
let inMemoryStore = InMemoryStore.make()
6+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
77
let loadManager = LoadManager.make()
88

99
let getUser = entityId =>
@@ -36,7 +36,7 @@ describe("LoadLayer", () => {
3636
async () => {
3737
let storageMock = Mock.Storage.make([#loadByIdsOrThrow])
3838
let loadManager = LoadManager.make()
39-
let inMemoryStore = InMemoryStore.make()
39+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
4040

4141
let getUser = entityId =>
4242
LoadLayer.loadById(
@@ -75,7 +75,7 @@ describe("LoadLayer", () => {
7575
async () => {
7676
let storageMock = Mock.Storage.make([#loadByIdsOrThrow])
7777
let loadManager = LoadManager.make()
78-
let inMemoryStore = InMemoryStore.make()
78+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
7979
let getUser = entityId =>
8080
LoadLayer.loadById(
8181
~loadManager,

scenarios/test_codegen/test/helpers/Mock.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module InMemoryStore = {
2020
}
2121

2222
let make = (~entities=[]) => {
23-
let inMemoryStore = InMemoryStore.make()
23+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
2424
entities->Js.Array2.forEach(((entityMod, items)) => {
2525
items->Js.Array2.forEach(entity => {
2626
inMemoryStore->setEntity(~entityMod, entity)

scenarios/test_codegen/test/schema_types/BigDecimal_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("Load and save an entity with a BigDecimal from DB", () => {
3434
~pgSchema=Generated.storagePgSchema,
3535
)
3636

37-
let inMemoryStore = InMemoryStore.make()
37+
let inMemoryStore = InMemoryStore.make(~entities=Entities.allEntities)
3838
let loadManager = LoadManager.make()
3939

4040
let item = MockEvents.newGravatarLog1->MockEvents.newGravatarEventToBatchItem

0 commit comments

Comments
 (0)