Merge branch 'smarter-updates'
This commit is contained in:
commit
03cd2113cc
5
.changeset/quick-beans-fail.md
Normal file
5
.changeset/quick-beans-fail.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"ottawa-boardgame-sell-bgg": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: be smarter when updating games
|
@ -77,11 +77,46 @@ export async function update_user_games(db, username) {
|
|||||||
'INSERT into game (username, bgg_id, name, thumbnail, notes,price,updated_at) VALUES(@username,@bgg_id,@name,@thumbnail,@notes,@price,@updated_at)',
|
'INSERT into game (username, bgg_id, name, thumbnail, notes,price,updated_at) VALUES(@username,@bgg_id,@name,@thumbnail,@notes,@price,@updated_at)',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const update = db.prepare(
|
||||||
|
'UPDATE game set notes=@notes, price=@price, updated_at=@updated_at where username=@username and bgg_id=@bgg_id',
|
||||||
|
);
|
||||||
|
|
||||||
|
const game_present = db.prepare(
|
||||||
|
'SELECT notes, price from game where username = ? AND bgg_id = ?',
|
||||||
|
);
|
||||||
|
|
||||||
|
const already_there = games
|
||||||
|
.map(({ bgg_id }) => bgg_id)
|
||||||
|
.map((id) => `'${id}'`)
|
||||||
|
.join(',');
|
||||||
await db
|
await db
|
||||||
.prepare('DELETE FROM game where username = @username')
|
.prepare(
|
||||||
.run({ username });
|
`DELETE FROM game where username = @username AND bgg_id not in (${already_there})`,
|
||||||
|
)
|
||||||
|
.run({
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
|
||||||
for (let game of games) {
|
for (let game of games) {
|
||||||
|
console.log(game.name);
|
||||||
|
const existing_game = game_present.get(username, game.bgg_id);
|
||||||
|
if (existing_game) {
|
||||||
|
if (
|
||||||
|
['notes', 'price'].some(
|
||||||
|
(attr) => existing_game[attr] != game[attr],
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
console.log('updating game');
|
||||||
|
update.run({
|
||||||
|
...game,
|
||||||
|
updated_at,
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("didn't change");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('new game!');
|
||||||
insert.run({
|
insert.run({
|
||||||
username,
|
username,
|
||||||
updated_at,
|
updated_at,
|
||||||
@ -89,3 +124,4 @@ export async function update_user_games(db, username) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ const users = db.prepare('SELECT username from bgg_user').all();
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
users.map(({ username }) => {
|
users.map(({ username }) => {
|
||||||
|
console.log(username);
|
||||||
return update_user_games(db, username)
|
return update_user_games(db, username)
|
||||||
.then(() => console.log(username, 'is done'))
|
.then(() => console.log(username, 'is done'))
|
||||||
.catch((e) => console.error(e));
|
.catch((e) => console.error(e));
|
||||||
|
Loading…
Reference in New Issue
Block a user