-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): only update data files if database data has changed
- Loading branch information
1 parent
18f8482
commit 4d1da03
Showing
2 changed files
with
49 additions
and
22 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"lastUpdate": "2024-07-15T20:43:22.551Z" | ||
"lastUpdate": "2024-07-16T03:39:37.072Z" | ||
} |
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,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(); |