user management page still not list registered users, for example admin@
This commit is contained in:
parent
4f73382105
commit
b7a6061fff
|
|
@ -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
|
||||
);
|
||||
`;
|
||||
`);
|
||||
|
||||
db.exec(createUsersTable);
|
||||
db.exec(createToysTable);
|
||||
console.log("Tables created.");
|
||||
console.log("Seeding initial data if missing...");
|
||||
|
||||
// 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
|
||||
|
|
|
|||
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