Skip to content

Commit

Permalink
feat(db): only update data files if database data has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuallyunknown committed Jul 16, 2024
1 parent 18f8482 commit 4d1da03
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/db/data/site-meta.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"lastUpdate": "2024-07-15T20:43:22.551Z"
"lastUpdate": "2024-07-16T03:39:37.072Z"
}
69 changes: 48 additions & 21 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
import { writeFile } from 'node:fs/promises';
import { readFile, writeFile } from 'node:fs/promises';
import { devvitSourcesQuery, siteTiersQuery, siteUpdatesQuery } from './db-queries.js';
import { db } from './db.js';

const items = [
{ query: siteUpdatesQuery, file: './data/site-updates.json' },
{ query: devvitSourcesQuery, file: './data/devvit-sources.json' },
{ query: siteTiersQuery, file: './data/site-tiers.json' },
];

await Promise.all(items.map(async ({ query, file }) => {
if (query === siteUpdatesQuery) {
const data = (await db.executeQuery(query)).rows;
async function tryReadJson(path: string) {
try {
return JSON.parse(await readFile(path, 'utf-8')) as object;
}
catch (error: unknown) {
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
return null;
}
throw error;
}
}

const queries = {
siteTiers: {
data: (await db.executeQuery(siteTiersQuery)).rows,
path: './data/site-tiers.json',
compareHash: true,
isUpdated: true,
},
siteUpdates: {
/**
* Apply sorting here becuase kysely doesn't support sorting yet
* but will do in a future release
*
* @see https://github.com/kysely-org/kysely/pull/896
*/
const sortedData = data.map(update => ({
data: (await db.executeQuery(siteUpdatesQuery)).rows.map(update => ({
...update,
...update.additions && { additions: update.additions.sort((a, b) => a.name.localeCompare(b.name)) },
...update.removals && { removals: update.removals.sort((a, b) => a.name.localeCompare(b.name)) },
...update.promotions && { promotions: update.promotions.sort((a, b) => a.name.localeCompare(b.name)) },
...update.demotions && { demotions: update.demotions.sort((a, b) => a.name.localeCompare(b.name)) },
}));
})),
path: './data/site-updates.json',
compareHash: true,
isUpdated: true,
},
devvitSources: {
data: (await db.executeQuery(devvitSourcesQuery)).rows,
path: './data/devvit-sources.json',
compareHash: false,
isUpdated: true,
},
};

await writeFile(file, JSON.stringify(sortedData, null, 4));
}
else {
const data = (await db.executeQuery(query)).rows;
await writeFile(file, JSON.stringify(data, null, 4));
}
}));
await Promise.all(Object.entries(queries).map(async ([_, item]) => {
const file = await tryReadJson(item.path);

await writeFile('./data/site-meta.json', JSON.stringify({ lastUpdate: new Date() }, null, 4));
if (file && item.compareHash) {
item.isUpdated = JSON.stringify(file) !== JSON.stringify(item.data);
}

await db.destroy();
await writeFile(item.path, JSON.stringify(item.data, null, 4));
}));

/**
* Only update the site-meta.json file if either site-tiers or site-updates
* have been updated or if they didn't exist before
*/
if (queries.siteTiers.isUpdated || queries.siteUpdates.isUpdated) {
await writeFile('./data/site-meta.json', JSON.stringify({ lastUpdate: new Date() }, null, 4));
}

await db.destroy();

0 comments on commit 4d1da03

Please sign in to comment.