/
renderRecsV1Tables.ts
37 lines (32 loc) · 1.18 KB
/
renderRecsV1Tables.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { TableId } from "@latticexyz/common";
import { RecsV1TableOptions } from "./types";
export function renderRecsV1Tables(options: RecsV1TableOptions) {
const { tables } = options;
return `/* Autogenerated file. Do not edit manually. */
import { defineComponent, Type as RecsType, type World } from "@latticexyz/recs";
export function defineContractComponents(world: World) {
return {
${tables.map((table) => `${table.tableName}: ${renderDefineComponent(table)},`).join("")}
}
}
`;
}
function renderDefineComponent(table: RecsV1TableOptions["tables"][number]) {
const { namespace, name } = table.staticResourceData;
const tableId = new TableId(namespace, name);
return `
(() => {
return defineComponent(world, {
${table.fields.map(({ name, recsTypeString }) => `${name}: ${recsTypeString}`).join(",")}
}, {
id: ${JSON.stringify(tableId.toHex())},
metadata: {
componentName: ${JSON.stringify(name)},
tableName: ${JSON.stringify([namespace, name].join(":"))},
keySchema: ${JSON.stringify(table.keySchema)},
valueSchema: ${JSON.stringify(table.valueSchema)},
},
} as const);
})()
`;
}