after add new toy for sharing, the new toys still not exist in toys tabl
This commit is contained in:
parent
276db4fa31
commit
c74edf7397
|
|
@ -6,7 +6,8 @@ import { createToy, updateToy, getToyById } from '@/data/operations';
|
||||||
import type { Toy } from '@/types';
|
import type { Toy } from '@/types';
|
||||||
|
|
||||||
// This type represents the data coming from the form.
|
// 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;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
category: string;
|
category: string;
|
||||||
|
|
|
||||||
133
src/lib/db.ts
133
src/lib/db.ts
|
|
@ -15,81 +15,82 @@ db.pragma('journal_mode = WAL');
|
||||||
db.pragma('foreign_keys = ON');
|
db.pragma('foreign_keys = ON');
|
||||||
|
|
||||||
function initDb() {
|
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.
|
if (!table) {
|
||||||
// This is useful for development to easily reset the data.
|
console.log("Database not found. Initializing and seeding...");
|
||||||
db.exec('DROP TABLE IF EXISTS toys');
|
|
||||||
db.exec('DROP TABLE IF EXISTS users');
|
|
||||||
|
|
||||||
|
// 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
|
// Create Toys Table
|
||||||
db.exec(`
|
db.exec(`
|
||||||
CREATE TABLE users (
|
CREATE TABLE toys (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id TEXT PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
nickname TEXT,
|
description TEXT NOT NULL,
|
||||||
email TEXT NOT NULL UNIQUE,
|
category TEXT NOT NULL,
|
||||||
role TEXT,
|
images TEXT,
|
||||||
avatarUrl TEXT,
|
unavailableRanges TEXT,
|
||||||
bio TEXT
|
ownerId INTEGER NOT NULL,
|
||||||
);
|
pricePerDay REAL,
|
||||||
`);
|
location TEXT,
|
||||||
|
FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
// Create Toys Table
|
console.log("Seeding initial data...");
|
||||||
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)
|
||||||
|
`);
|
||||||
|
|
||||||
// Prepare insert statements
|
const insertToy = db.prepare(`
|
||||||
const insertUser = db.prepare(`
|
INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location)
|
||||||
INSERT INTO users (id, name, nickname, email, role, avatarUrl, bio)
|
VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)
|
||||||
VALUES (@id, @name, @nickname, @email, @role, @avatarUrl, @bio)
|
`);
|
||||||
`);
|
|
||||||
|
|
||||||
const insertToy = db.prepare(`
|
// Use a transaction for efficiency
|
||||||
INSERT INTO toys (id, name, description, category, images, unavailableRanges, ownerId, pricePerDay, location)
|
const seedData = db.transaction(() => {
|
||||||
VALUES (@id, @name, @description, @category, @images, @unavailableRanges, @ownerId, @pricePerDay, @location)
|
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
|
seedData();
|
||||||
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();
|
console.log("Database initialization and seeding complete.");
|
||||||
|
} else {
|
||||||
console.log("Database initialization and seeding complete.");
|
console.log("Database already initialized.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize and export db
|
// Initialize and export db
|
||||||
|
|
|
||||||
BIN
toyshare.db
BIN
toyshare.db
Binary file not shown.
BIN
toyshare.db-shm
BIN
toyshare.db-shm
Binary file not shown.
BIN
toyshare.db-wal
BIN
toyshare.db-wal
Binary file not shown.
Loading…
Reference in New Issue