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'); db.pragma('foreign_keys = ON');
function initDb() { 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(` db.exec(`
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, name TEXT NOT NULL,
nickname TEXT, nickname TEXT,
@ -29,22 +35,10 @@ function initDb() {
bio TEXT bio TEXT
); );
`); `);
// 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
// Create Toys Table if it doesn't exist
db.exec(` db.exec(`
CREATE TABLE IF NOT EXISTS toys ( CREATE TABLE toys (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NOT NULL, description TEXT NOT NULL,
@ -58,46 +52,42 @@ 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. // Prepare insert statements
// This prevents errors on subsequent runs and adds missing users/toys without overwriting.
const insertUser = db.prepare(` 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) VALUES (@id, @name, @nickname, @email, @role, @avatarUrl, @bio)
`); `);
const insertToy = db.prepare(` 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) VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)
`); `);
const insertManyUsers = db.transaction((users: User[]) => { // Use a transaction for efficiency
for (const user of users) { const seedData = db.transaction(() => {
insertUser.run({ for (const user of rawUsers) {
id: user.id, insertUser.run({
name: user.name, id: user.id,
nickname: user.nickname ?? null, name: user.name,
email: user.email, nickname: user.nickname ?? null,
role: user.role ?? 'User', email: user.email,
avatarUrl: user.avatarUrl ?? null, role: user.role ?? 'User',
bio: user.bio ?? null avatarUrl: user.avatarUrl ?? null,
}); bio: user.bio ?? null
});
} }
}); for (const toy of rawToys) {
insertToy.run({
const insertManyToys = db.transaction((toys) => { ...toy,
for (const toy of toys) { images: JSON.stringify(toy.images),
insertToy.run({ unavailableRanges: JSON.stringify(toy.unavailableRanges),
...toy, });
images: JSON.stringify(toy.images),
unavailableRanges: JSON.stringify(toy.unavailableRanges),
});
} }
}); });
insertManyUsers(rawUsers); seedData();
insertManyToys(rawToys);
console.log("Database initialization and seeding complete."); console.log("Database initialization and seeding complete.");
} }

Binary file not shown.

Binary file not shown.