-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync): extra table definitions (#1840)
- Loading branch information
Showing
18 changed files
with
239 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
"@latticexyz/store-sync": minor | ||
--- | ||
|
||
Added an optional `tables` option to `syncToRecs` to allow you to sync from tables that may not be expressed by your MUD config. This will be useful for namespaced tables used by [ERC20](https://github.com/latticexyz/mud/pull/1789) and [ERC721](https://github.com/latticexyz/mud/pull/1844) token modules until the MUD config gains [namespace support](https://github.com/latticexyz/mud/issues/994). | ||
|
||
Here's how we use this in our example project with the `KeysWithValue` module: | ||
|
||
```ts | ||
syncToRecs({ | ||
... | ||
tables: { | ||
KeysWithValue: { | ||
namespace: "keywval", | ||
name: "Inventory", | ||
tableId: resourceToHex({ type: "table", namespace: "keywval", name: "Inventory" }), | ||
keySchema: { | ||
valueHash: { type: "bytes32" }, | ||
}, | ||
valueSchema: { | ||
keysWithValue: { type: "bytes32[]" }, | ||
}, | ||
}, | ||
}, | ||
... | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,10 @@ | ||
import { StoreConfig } from "@latticexyz/store"; | ||
import { Component as RecsComponent, Metadata as RecsMetadata, Type as RecsType } from "@latticexyz/recs"; | ||
import { SchemaAbiTypeToRecsType } from "./schemaAbiTypeToRecsType"; | ||
import { SchemaAbiType } from "@latticexyz/schema-type"; | ||
import { Metadata } from "@latticexyz/recs"; | ||
import { KeySchema, ValueSchema } from "@latticexyz/protocol-parser"; | ||
|
||
export type StoreComponentMetadata = RecsMetadata & { | ||
export type StoreComponentMetadata = Metadata & { | ||
componentName: string; | ||
tableName: string; | ||
// TODO: migrate to store's KeySchema/ValueSchema | ||
keySchema: KeySchema; | ||
valueSchema: ValueSchema; | ||
}; | ||
|
||
export type ConfigToRecsComponents<TConfig extends StoreConfig> = { | ||
[tableName in keyof TConfig["tables"] & string]: RecsComponent< | ||
{ | ||
__staticData: RecsType.OptionalString; | ||
__encodedLengths: RecsType.OptionalString; | ||
__dynamicData: RecsType.OptionalString; | ||
} & { | ||
[fieldName in keyof TConfig["tables"][tableName]["valueSchema"] & string]: RecsType & | ||
SchemaAbiTypeToRecsType<SchemaAbiType & TConfig["tables"][tableName]["valueSchema"][fieldName]>; | ||
}, | ||
StoreComponentMetadata & { | ||
componentName: tableName; | ||
tableName: `${TConfig["namespace"]}:${tableName}`; | ||
keySchema: TConfig["tables"][tableName]["keySchema"]; | ||
valueSchema: TConfig["tables"][tableName]["valueSchema"]; | ||
} | ||
>; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Component, Type, World, defineComponent } from "@latticexyz/recs"; | ||
import { StoreComponentMetadata } from "./common"; | ||
import { SchemaAbiTypeToRecsType, schemaAbiTypeToRecsType } from "./schemaAbiTypeToRecsType"; | ||
import { SchemaAbiType } from "@latticexyz/schema-type"; | ||
import { Table } from "@latticexyz/store"; | ||
import { mapObject } from "@latticexyz/common/utils"; | ||
|
||
export type TableToComponent<table extends Table> = Component< | ||
{ | ||
__staticData: Type.OptionalString; | ||
__encodedLengths: Type.OptionalString; | ||
__dynamicData: Type.OptionalString; | ||
} & { | ||
[fieldName in keyof table["valueSchema"] & string]: Type & | ||
SchemaAbiTypeToRecsType<SchemaAbiType & table["valueSchema"][fieldName]["type"]>; | ||
}, | ||
StoreComponentMetadata & { | ||
componentName: table["name"]; | ||
tableName: `${table["namespace"]}:${table["name"]}`; | ||
keySchema: { [name in keyof table["keySchema"] & string]: table["keySchema"][name]["type"] }; | ||
valueSchema: { [name in keyof table["valueSchema"] & string]: table["valueSchema"][name]["type"] }; | ||
} | ||
>; | ||
|
||
export function tableToComponent<table extends Table>(world: World, table: table): TableToComponent<table> { | ||
return defineComponent( | ||
world, | ||
{ | ||
...Object.fromEntries( | ||
Object.entries(table.valueSchema).map(([fieldName, { type: schemaAbiType }]) => [ | ||
fieldName, | ||
schemaAbiTypeToRecsType[schemaAbiType as SchemaAbiType], | ||
]) | ||
), | ||
__staticData: Type.OptionalString, | ||
__encodedLengths: Type.OptionalString, | ||
__dynamicData: Type.OptionalString, | ||
}, | ||
{ | ||
id: table.tableId, | ||
metadata: { | ||
componentName: table.name, | ||
tableName: `${table.namespace}:${table.name}`, | ||
keySchema: mapObject(table.keySchema, ({ type }) => type), | ||
valueSchema: mapObject(table.valueSchema, ({ type }) => type), | ||
}, | ||
} | ||
) as TableToComponent<table>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Table } from "@latticexyz/store"; | ||
import { TableToComponent, tableToComponent } from "./tableToComponent"; | ||
import { mapObject } from "@latticexyz/common/utils"; | ||
import { World } from "@latticexyz/recs"; | ||
|
||
export type TablesToComponents<tables extends Record<string, Table>> = { | ||
[tableName in keyof tables]: TableToComponent<tables[tableName]>; | ||
}; | ||
|
||
export function tablesToComponents<tables extends Record<string, Table>>( | ||
world: World, | ||
tables: tables | ||
): TablesToComponents<tables> { | ||
return mapObject(tables, (table) => tableToComponent(world, table)); | ||
} |
Oops, something went wrong.