impress-2020/scripts/build-cached-data.js
Matchu 47d22ad25c Build cached zones, stop querying on server
In this change, we cache the zones table as part of the JS build process. This keeps the database as our source of truth, while aggressively caching the data at deploy time.

See the new README for some rationale!

I tested this by pulling up dev Honeycomb, and observing that we no longer run db queries to `zones` in the new traces for the wardrobe page. (It's a good thing we did it this way, because I noticed some code in the server that was still loading the zone anyway, and fixed it here!)
2020-08-19 17:50:05 -07:00

41 lines
1.1 KiB
JavaScript

// We run this on build to cache some stable database tables into the JS
// bundle!
const fs = require("fs").promises;
const path = require("path");
const connectToDb = require("../src/server/db");
const { normalizeRow } = require("../src/server/util");
const cachedDataPath = path.join(__dirname, "..", "src", "app", "cached-data");
async function buildZonesCache(db) {
const [rows] = await db.query(
`SELECT z.id, z.depth, zt.label FROM zones z ` +
`INNER JOIN zone_translations zt ON z.id = zt.zone_id ` +
`WHERE locale = "en" ORDER BY z.id;`
);
const entities = rows.map(normalizeRow);
const filePath = path.join(cachedDataPath, "zones.json");
fs.writeFile(filePath, JSON.stringify(entities, null, 4), "utf8");
console.log(`📚 Wrote zones to ${path.relative(process.cwd(), filePath)}`);
}
async function main() {
const db = await connectToDb();
await fs.mkdir(cachedDataPath, { recursive: true });
try {
await buildZonesCache(db);
} catch (e) {
db.close();
throw e;
}
db.close();
}
main().catch((e) => {
console.error(e);
process.exitCode = 1;
});