diff --git a/src/app/actions/toy.ts b/src/app/actions/toy.ts index 836950f..85f13fb 100644 --- a/src/app/actions/toy.ts +++ b/src/app/actions/toy.ts @@ -6,7 +6,8 @@ import { createToy, updateToy, getToyById } from '@/data/operations'; import type { Toy } from '@/types'; // This type represents the data coming from the form. -export type ToyFormData = Omit & { +// Simplified for clarity and maintainability. +export type ToyFormData = { name: string; description: string; category: string; diff --git a/src/lib/db.ts b/src/lib/db.ts index 2373437..e1bde8d 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -15,81 +15,82 @@ db.pragma('journal_mode = WAL'); db.pragma('foreign_keys = ON'); function initDb() { - console.log("Resetting database and re-seeding data..."); + // Check if the users table exists. If it does, we assume the DB is initialized. + const table = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'users'").get(); - // 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'); + if (!table) { + console.log("Database not found. Initializing and seeding..."); + // Create Users Table + db.exec(` + CREATE TABLE users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + nickname TEXT, + email TEXT NOT NULL UNIQUE, + role TEXT, + avatarUrl TEXT, + bio TEXT + ); + `); - // Create Users Table - db.exec(` - CREATE TABLE users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - nickname TEXT, - email TEXT NOT NULL UNIQUE, - role TEXT, - avatarUrl TEXT, - bio TEXT - ); - `); + // Create Toys Table + db.exec(` + CREATE TABLE toys ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + description TEXT NOT NULL, + category TEXT NOT NULL, + images TEXT, + unavailableRanges TEXT, + ownerId INTEGER NOT NULL, + pricePerDay REAL, + location TEXT, + FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE + ); + `); + + console.log("Seeding initial data..."); - // Create Toys Table - db.exec(` - CREATE TABLE toys ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - description TEXT NOT NULL, - category TEXT NOT NULL, - images TEXT, - unavailableRanges TEXT, - ownerId INTEGER NOT NULL, - pricePerDay REAL, - location TEXT, - FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE - ); - `); - - console.log("Seeding initial data..."); + // Prepare insert statements + const insertUser = db.prepare(` + INSERT INTO users (id, name, nickname, email, role, avatarUrl, bio) + VALUES (@id, @name, @nickname, @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) + `); - // Prepare insert statements - const insertUser = db.prepare(` - INSERT INTO users (id, name, nickname, email, role, avatarUrl, bio) - VALUES (@id, @name, @nickname, @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 a transaction for efficiency + const seedData = db.transaction(() => { + for (const user of rawUsers) { + insertUser.run({ + id: user.id, + name: user.name, + nickname: user.nickname ?? null, + email: user.email, + role: user.role ?? 'User', + avatarUrl: user.avatarUrl ?? null, + bio: user.bio ?? null + }); + } + for (const toy of rawToys) { + insertToy.run({ + ...toy, + images: JSON.stringify(toy.images), + unavailableRanges: JSON.stringify(toy.unavailableRanges), + }); + } + }); - // Use a transaction for efficiency - const seedData = db.transaction(() => { - for (const user of rawUsers) { - insertUser.run({ - id: user.id, - name: user.name, - nickname: user.nickname ?? null, - email: user.email, - role: user.role ?? 'User', - avatarUrl: user.avatarUrl ?? null, - bio: user.bio ?? null - }); - } - for (const toy of rawToys) { - insertToy.run({ - ...toy, - images: JSON.stringify(toy.images), - unavailableRanges: JSON.stringify(toy.unavailableRanges), - }); - } - }); + seedData(); - seedData(); - - console.log("Database initialization and seeding complete."); + console.log("Database initialization and seeding complete."); + } else { + console.log("Database already initialized."); + } } // Initialize and export db diff --git a/toyshare.db b/toyshare.db index 80c2414..d9d59f6 100644 Binary files a/toyshare.db and b/toyshare.db differ diff --git a/toyshare.db-shm b/toyshare.db-shm index 91a6282..b766a6f 100644 Binary files a/toyshare.db-shm and b/toyshare.db-shm differ diff --git a/toyshare.db-wal b/toyshare.db-wal index b7855fb..b19f49b 100644 Binary files a/toyshare.db-wal and b/toyshare.db-wal differ