From f77936828cafe7b4a4479bb22682b6461197399e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 30 Jan 2024 15:54:24 -0500 Subject: [PATCH] create database --- .envrc | 1 + .gitignore | 1 + db/migrations/20240130203355_user-table.sql | 11 +++++++++ db/migrations/20240130203603_game-table.sql | 16 ++++++++++++ db/migrations/20240130205124_add-price.sql | 8 ++++++ db/schema.sql | 18 ++++++++++++++ package.json | 3 ++- src/fetchCollection.js | 27 ++++++++++++++++++--- 8 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 .envrc create mode 100644 db/migrations/20240130203355_user-table.sql create mode 100644 db/migrations/20240130203603_game-table.sql create mode 100644 db/migrations/20240130205124_add-price.sql create mode 100644 db/schema.sql diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5de6d36 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +export DATABASE_URL=sqlite3:./games.db diff --git a/.gitignore b/.gitignore index c07ea0f..cb04b10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ pnpm-lock.yaml +games.db diff --git a/db/migrations/20240130203355_user-table.sql b/db/migrations/20240130203355_user-table.sql new file mode 100644 index 0000000..e07dee9 --- /dev/null +++ b/db/migrations/20240130203355_user-table.sql @@ -0,0 +1,11 @@ +-- migrate:up + +CREATE TABLE IF NOT EXISTS bgg_user ( + username text primary key +); + + +-- migrate:down + +DROP TABLE bgg_user; + diff --git a/db/migrations/20240130203603_game-table.sql b/db/migrations/20240130203603_game-table.sql new file mode 100644 index 0000000..697ea6c --- /dev/null +++ b/db/migrations/20240130203603_game-table.sql @@ -0,0 +1,16 @@ +-- migrate:up + +CREATE TABLE IF NOT EXISTS game ( + id integer primary key, + username text, + thumbnail text, + bgg_id text, + name text, + notes text, + FOREIGN KEY(username) REFERENCES bgg_user(username) +); + +-- migrate:down + +drop table game; + diff --git a/db/migrations/20240130205124_add-price.sql b/db/migrations/20240130205124_add-price.sql new file mode 100644 index 0000000..5602e5f --- /dev/null +++ b/db/migrations/20240130205124_add-price.sql @@ -0,0 +1,8 @@ +-- migrate:up + +ALTER TABLE game ADD COLUMN price integer; + +-- migrate:down + +ALTER TABLE game DROP COLUMN price integer; + diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..838036f --- /dev/null +++ b/db/schema.sql @@ -0,0 +1,18 @@ +CREATE TABLE IF NOT EXISTS "schema_migrations" (version varchar(255) primary key); +CREATE TABLE bgg_user ( + username text primary key +); +CREATE TABLE game ( + id integer primary key, + username text, + thumbnail text, + bgg_id text, + name text, + notes text, price integer, + FOREIGN KEY(username) REFERENCES bgg_user(username) +); +-- Dbmate schema migrations +INSERT INTO "schema_migrations" (version) VALUES + ('20240130203355'), + ('20240130203603'), + ('20240130205124'); diff --git a/package.json b/package.json index a76f885..304e872 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { - "type":"module", + "type": "module", "dependencies": { + "better-sqlite3": "^9.3.0", "cheerio": "1.0.0-rc.12", "node-fetch": "^3.3.2", "vitest": "^1.2.2" diff --git a/src/fetchCollection.js b/src/fetchCollection.js index af761f9..23a059c 100644 --- a/src/fetchCollection.js +++ b/src/fetchCollection.js @@ -1,5 +1,9 @@ import { load } from 'cheerio'; import fetch from 'node-fetch'; +import DB from 'better-sqlite3'; + +const db = DB('games.db'); +db.pragma('journal_mode = WAL'); export function extract_user_forsale(page) { @@ -9,8 +13,8 @@ export function extract_user_forsale(page) { const data = $('item').map( function (i,item) { const data = {}; - data.game_id = $(this).attr('objectid'); - data.game_name = $(this).find('name').text(); + data.bgg_id = $(this).attr('objectid'); + data.name = $(this).find('name').text(); data.thumbnail = $(this).find('thumbnail').text(); data.notes = $(this).find('conditiontext').text(); const find_price = data.notes.match(/\$(\d+)/); @@ -41,4 +45,21 @@ async function fetch_user_forsale(username,n=1) { throw new Error("couldn't get the collection"); } -console.log( await fetch_user_forsale('yenzie') ); +async function update_user_games(username) { + const games = await fetch_user_forsale(username); + + const insert = db.prepare( 'INSERT into game (username, bgg_id, name, thumbnail, notes) VALUES(@username,@bgg_id,@name,@thumbnail,@notes)' ); + + db.prepare('DELETE FROM game where username = @username').run({username}); + + for ( let game of games ) { + insert.run({ + username, + ...game + }); + } + + +} + +console.log( await update_user_games('yenzie') );