after add new toy for sharing, the new toys still not exist in toys tabl

This commit is contained in:
Indigo Tang 2025-07-06 13:59:18 +00:00
parent 276db4fa31
commit c74edf7397
5 changed files with 71 additions and 69 deletions

View File

@ -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<Toy, 'id' | 'ownerId' | 'name' | 'description' | 'category' | 'images' | 'unavailableRanges' | 'pricePerDay' | 'location' | 'ownerName' | 'ownerAvatarUrl' | 'dataAiHint'> & {
// Simplified for clarity and maintainability.
export type ToyFormData = {
name: string;
description: string;
category: string;

View File

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

Binary file not shown.

Binary file not shown.

Binary file not shown.