diff --git a/src/lib/db.ts b/src/lib/db.ts index 0540606..388769c 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -15,18 +15,11 @@ db.pragma('journal_mode = WAL'); db.pragma('foreign_keys = ON'); function initDb() { - // Check if tables exist - const tableCheck = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'users'").get(); + console.log("Initializing database schema if needed..."); - if (tableCheck) { - return; - } - - console.log("Initializing database..."); - - // Create Tables - const createUsersTable = ` - CREATE TABLE users ( + // Create Users Table if it doesn't exist + db.exec(` + CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, @@ -34,9 +27,11 @@ function initDb() { avatarUrl TEXT, bio TEXT ); - `; - const createToysTable = ` - CREATE TABLE toys ( + `); + + // Create Toys Table if it doesn't exist + db.exec(` + CREATE TABLE IF NOT EXISTS toys ( id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, @@ -48,15 +43,21 @@ function initDb() { location TEXT, FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE ); - `; + `); + + console.log("Seeding initial data if missing..."); - db.exec(createUsersTable); - db.exec(createToysTable); - console.log("Tables created."); - - // Seed Data - const insertUser = db.prepare('INSERT INTO users (id, name, email, role, avatarUrl, bio) VALUES (@id, @name, @email, @role, @avatarUrl, @bio)'); - const insertToy = db.prepare('INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location) VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)'); + // 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. + const insertUser = db.prepare(` + INSERT OR IGNORE INTO users (id, name, email, role, avatarUrl, bio) + VALUES (@id, @name, @email, @role, @avatarUrl, @bio) + `); + + const insertToy = db.prepare(` + INSERT OR IGNORE 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) { @@ -64,7 +65,7 @@ function initDb() { id: user.id, name: user.name, email: user.email, - role: user.role, + role: user.role ?? 'User', avatarUrl: user.avatarUrl ?? null, bio: user.bio ?? null }); @@ -84,7 +85,7 @@ function initDb() { insertManyUsers(rawUsers); insertManyToys(rawToys); - console.log("Database seeded with users and toys."); + console.log("Database initialization and seeding complete."); } // Initialize and export db diff --git a/toyshare.db-shm b/toyshare.db-shm index f0197b0..db17920 100644 Binary files a/toyshare.db-shm and b/toyshare.db-shm differ diff --git a/toyshare.db-wal b/toyshare.db-wal index 53cd19f..39b9561 100644 Binary files a/toyshare.db-wal and b/toyshare.db-wal differ