delete current toyshare.db and regenerate example data
This commit is contained in:
parent
dad13c9cfb
commit
bcaebf0494
|
|
@ -15,11 +15,17 @@ db.pragma('journal_mode = WAL');
|
|||
db.pragma('foreign_keys = ON');
|
||||
|
||||
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(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
nickname TEXT,
|
||||
|
|
@ -29,22 +35,10 @@ function initDb() {
|
|||
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 if it doesn't exist
|
||||
// Create Toys Table
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS toys (
|
||||
CREATE TABLE toys (
|
||||
id TEXT PRIMARY KEY,
|
||||
name 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.
|
||||
// This prevents errors on subsequent runs and adds missing users/toys without overwriting.
|
||||
// Prepare insert statements
|
||||
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)
|
||||
`);
|
||||
|
||||
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)
|
||||
`);
|
||||
|
||||
const insertManyUsers = db.transaction((users: User[]) => {
|
||||
for (const user of users) {
|
||||
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
|
||||
});
|
||||
// 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
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const insertManyToys = db.transaction((toys) => {
|
||||
for (const toy of toys) {
|
||||
insertToy.run({
|
||||
...toy,
|
||||
images: JSON.stringify(toy.images),
|
||||
unavailableRanges: JSON.stringify(toy.unavailableRanges),
|
||||
});
|
||||
for (const toy of rawToys) {
|
||||
insertToy.run({
|
||||
...toy,
|
||||
images: JSON.stringify(toy.images),
|
||||
unavailableRanges: JSON.stringify(toy.unavailableRanges),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
insertManyUsers(rawUsers);
|
||||
insertManyToys(rawToys);
|
||||
seedData();
|
||||
|
||||
console.log("Database initialization and seeding complete.");
|
||||
}
|
||||
|
|
|
|||
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