delete current toyshare.db and regenerate example data

This commit is contained in:
Indigo Tang 2025-07-06 13:37:24 +00:00
parent dad13c9cfb
commit bcaebf0494
3 changed files with 34 additions and 44 deletions

View File

@ -15,11 +15,17 @@ db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
function initDb() {
console.log("Initializing database schema if needed...");
console.log("Resetting database and re-seeding data...");
// Create Users Table if it doesn't exist
// Drop existing tables to ensure a clean slate on every run.
// This is useful for development to easily reset the data.
db.exec('DROP TABLE IF EXISTS toys');
db.exec('DROP TABLE IF EXISTS users');
// Create Users Table
db.exec(`
CREATE TABLE IF NOT EXISTS users (
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
nickname TEXT,
@ -30,21 +36,9 @@ function initDb() {
);
`);
// Migration: Add nickname column to users table if it doesn't exist
try {
const columns = db.prepare("PRAGMA table_info(users)").all();
if (!columns.some((col: any) => col.name === 'nickname')) {
console.log("Adding 'nickname' column to 'users' table...");
db.exec('ALTER TABLE users ADD COLUMN nickname TEXT');
}
} catch (error) {
console.error("Error during 'users' table migration:", error);
}
// Create Toys Table if it doesn't exist
// Create Toys Table
db.exec(`
CREATE TABLE IF NOT EXISTS toys (
CREATE TABLE toys (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
@ -58,22 +52,22 @@ function initDb() {
);
`);
console.log("Seeding initial data if missing...");
console.log("Seeding initial data...");
// Use INSERT OR IGNORE to only add data if the primary key doesn't exist.
// This prevents errors on subsequent runs and adds missing users/toys without overwriting.
// Prepare insert statements
const insertUser = db.prepare(`
INSERT OR IGNORE INTO users (id, name, nickname, email, role, avatarUrl, bio)
INSERT INTO users (id, name, nickname, email, role, avatarUrl, bio)
VALUES (@id, @name, @nickname, @email, @role, @avatarUrl, @bio)
`);
const insertToy = db.prepare(`
INSERT OR IGNORE INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location)
INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location)
VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)
`);
const insertManyUsers = db.transaction((users: User[]) => {
for (const user of users) {
// Use a transaction for efficiency
const seedData = db.transaction(() => {
for (const user of rawUsers) {
insertUser.run({
id: user.id,
name: user.name,
@ -84,10 +78,7 @@ function initDb() {
bio: user.bio ?? null
});
}
});
const insertManyToys = db.transaction((toys) => {
for (const toy of toys) {
for (const toy of rawToys) {
insertToy.run({
...toy,
images: JSON.stringify(toy.images),
@ -96,8 +87,7 @@ function initDb() {
}
});
insertManyUsers(rawUsers);
insertManyToys(rawToys);
seedData();
console.log("Database initialization and seeding complete.");
}

Binary file not shown.

Binary file not shown.