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

Binary file not shown.

Binary file not shown.