create database

This commit is contained in:
Yanick Champoux 2024-01-30 15:54:24 -05:00
parent a0596cef25
commit f77936828c
8 changed files with 81 additions and 4 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
export DATABASE_URL=sqlite3:./games.db

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules/ node_modules/
pnpm-lock.yaml pnpm-lock.yaml
games.db

View File

@ -0,0 +1,11 @@
-- migrate:up
CREATE TABLE IF NOT EXISTS bgg_user (
username text primary key
);
-- migrate:down
DROP TABLE bgg_user;

View File

@ -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;

View File

@ -0,0 +1,8 @@
-- migrate:up
ALTER TABLE game ADD COLUMN price integer;
-- migrate:down
ALTER TABLE game DROP COLUMN price integer;

18
db/schema.sql Normal file
View File

@ -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');

View File

@ -1,6 +1,7 @@
{ {
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"better-sqlite3": "^9.3.0",
"cheerio": "1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
"node-fetch": "^3.3.2", "node-fetch": "^3.3.2",
"vitest": "^1.2.2" "vitest": "^1.2.2"

View File

@ -1,5 +1,9 @@
import { load } from 'cheerio'; import { load } from 'cheerio';
import fetch from 'node-fetch'; 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) { 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 = $('item').map( function (i,item) {
const data = {}; const data = {};
data.game_id = $(this).attr('objectid'); data.bgg_id = $(this).attr('objectid');
data.game_name = $(this).find('name').text(); data.name = $(this).find('name').text();
data.thumbnail = $(this).find('thumbnail').text(); data.thumbnail = $(this).find('thumbnail').text();
data.notes = $(this).find('conditiontext').text(); data.notes = $(this).find('conditiontext').text();
const find_price = data.notes.match(/\$(\d+)/); 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"); 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') );