Add database initialization
now, when the project is ran without configured database, it will prompt for the first user to configure the database and add the first user
This commit is contained in:
parent
cbfc4e9317
commit
90932a49c8
6 changed files with 286 additions and 0 deletions
15
server/api/firstRun.get.ts
Normal file
15
server/api/firstRun.get.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* global defineEventHandler */
|
||||
import { data, database } from "../utils/database";
|
||||
|
||||
export async function isFirstRun() {
|
||||
const [tables] = await database.query({ sql: "SHOW TABLES", rowsAsArray: true }, []) as data<[string]>;
|
||||
if (tables.length === 0) return true;
|
||||
if (!tables.find(a => a[0] === "users")) return true;
|
||||
const [[users]] = await database.query("SELECT COUNT(*) as `count` FROM `users`") as data<{count: number}>;
|
||||
if (users.count === 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
return isFirstRun();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue