user management page still not list registered users, for example admin@

This commit is contained in:
Indigo Tang 2025-07-06 12:28:55 +00:00
parent 4f73382105
commit b7a6061fff
3 changed files with 25 additions and 24 deletions

View File

@ -15,18 +15,11 @@ db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON'); db.pragma('foreign_keys = ON');
function initDb() { function initDb() {
// Check if tables exist console.log("Initializing database schema if needed...");
const tableCheck = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'users'").get();
if (tableCheck) { // Create Users Table if it doesn't exist
return; db.exec(`
} CREATE TABLE IF NOT EXISTS users (
console.log("Initializing database...");
// Create Tables
const createUsersTable = `
CREATE TABLE users (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE,
@ -34,9 +27,11 @@ function initDb() {
avatarUrl TEXT, avatarUrl TEXT,
bio 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, id TEXT PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NOT NULL, description TEXT NOT NULL,
@ -48,15 +43,21 @@ function initDb() {
location TEXT, location TEXT,
FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE FOREIGN KEY (ownerId) REFERENCES users(id) ON DELETE CASCADE
); );
`; `);
console.log("Seeding initial data if missing...");
db.exec(createUsersTable); // Use INSERT OR IGNORE to only add data if the primary key doesn't exist.
db.exec(createToysTable); // This prevents errors on subsequent runs and adds missing users/toys without overwriting.
console.log("Tables created."); const insertUser = db.prepare(`
INSERT OR IGNORE INTO users (id, name, email, role, avatarUrl, bio)
// Seed Data VALUES (@id, @name, @email, @role, @avatarUrl, @bio)
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)');
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[]) => { const insertManyUsers = db.transaction((users: User[]) => {
for (const user of users) { for (const user of users) {
@ -64,7 +65,7 @@ function initDb() {
id: user.id, id: user.id,
name: user.name, name: user.name,
email: user.email, email: user.email,
role: user.role, role: user.role ?? 'User',
avatarUrl: user.avatarUrl ?? null, avatarUrl: user.avatarUrl ?? null,
bio: user.bio ?? null bio: user.bio ?? null
}); });
@ -84,7 +85,7 @@ function initDb() {
insertManyUsers(rawUsers); insertManyUsers(rawUsers);
insertManyToys(rawToys); insertManyToys(rawToys);
console.log("Database seeded with users and toys."); console.log("Database initialization and seeding complete.");
} }
// Initialize and export db // Initialize and export db

Binary file not shown.

Binary file not shown.