-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-tables.js
72 lines (58 loc) · 1.89 KB
/
create-tables.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import './fetch-polyfill.js';
import * as fs from 'fs';
import { provider, tableland } from "./tableland.js";
import { tables } from './tables.js';
const { name: chainName } = await provider.getNetwork();
const artifactsDirectory = `./artifacts/${chainName}`;
if (!fs.existsSync(artifactsDirectory)) {
fs.mkdirSync(artifactsDirectory);
}
const validateReceipt = async (txnHash) => {
const receipt = await tableland.receipt(txnHash)
if (!receipt.error) return receipt
throw new Error(receipt)
}
const createTableIfDoesNotExist = async (prefix, schema, dryRun = false) => {
if (!prefix) {
throw new Error('Need table prefix to create table');
}
const artifactFilePath = `${artifactsDirectory}/${prefix}.json`;
let tableArtifact;
let tableExists = false;
try {
const rawData = fs.readFileSync(artifactFilePath);
tableArtifact = JSON.parse(rawData);
if (tableArtifact) {
const tables = await tableland.list();
tableExists = !!tables.find(table => table.name === tableArtifact.name);
}
if (tableExists) {
console.log("Table exists!", tableArtifact.name)
return
}
} catch (error) {
if (!error.code === 'ENOENT') {
throw error;
}
}
console.log(`Creating table with prefix ${prefix}....`)
if (dryRun) {
console.log("\tSchema\n\t", schema)
console.log("\tArtifacts path", artifactFilePath)
return
}
try {
const result = await tableland.create(schema, { prefix });
await validateReceipt(result.txnHash)
console.log("Created new table! Table Result:", result.name);
fs.writeFileSync(artifactFilePath, JSON.stringify(result));
console.log("Table artifact saved:", artifactFilePath);
return result;
} catch (error) {
console.log("Error when creating a table\n\t", prefix, schema)
throw error
}
}
for (const { prefix, schema } of tables) {
await createTableIfDoesNotExist(prefix, schema)
}