Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
🔨 fix: async prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
whitigol committed Feb 10, 2024
1 parent c4647bf commit 3ea349a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 65 deletions.
4 changes: 2 additions & 2 deletions apps/api/src/sockets/create-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ export default function HandleCreateInstance(socket: Socket) {
socket.emit("create-instance-stderr", data.toString());
});

moveEnvProcess.on("close", (code) => {
moveEnvProcess.on("close", async (code) => {
if (code === 0) {
socket.emit(
"create-instance-stdout",
"Moved env files successfully"
);
ManageDatabase.instances.addInstance({
await ManageDatabase.instances.addInstance({
id,
name,
settings: {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/sockets/delete-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function HandleDeleteInstance(socket: Socket) {
}

try {
ManageDatabase.instances.deleteInstance(data.id);
await ManageDatabase.instances.deleteInstance(data.id);
} catch (err) {
socket.emit(
"error",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/sockets/instance-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function HandleInstanceSettings(socket: Socket) {

socket.emit("client:instance-settings-updated", data);

ManageDatabase.instances.updateInstance({
await ManageDatabase.instances.updateInstance({
...data,
name: instanceName,
});
Expand Down
15 changes: 8 additions & 7 deletions apps/api/src/sockets/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import bcrypt from "bcrypt";

export default function HandleUser(socket: Socket) {
socket.on("server:add-user", async (data: AddUserData) => {
ManageDatabase.users.addUser({
await ManageDatabase.users.addUser({
id: uuid(),
username: data.username,
password: await bcrypt.hash(data.password, 10),
role: data.role,
});
});

socket.on("server:get-users", () => {
socket.emit("client:get-users", ManageDatabase.users.getUsers());
socket.on("server:get-users", async () => {
const users = await ManageDatabase.users.getUsers();
socket.emit("client:get-users", users);
});

socket.on("server:delete-user", (id: string) => {
ManageDatabase.users.deleteUser(id);
socket.on("server:delete-user", async (id: string) => {
await ManageDatabase.users.deleteUser(id);
});

socket.on("server:update-user", (data: any) => {
ManageDatabase.users.updateUser(data);
socket.on("server:update-user", async (data: any) => {
await ManageDatabase.users.updateUser(data);
});

socket.on("server:user-login", async (data: UserLoginData) => {
Expand Down
71 changes: 17 additions & 54 deletions apps/api/src/util/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,6 @@ export const prisma = new PrismaClient();

export default class ManageDatabase {
static async init() {
// console.log(fs.existsSync(dbPath));
// if (!fs.existsSync(dbPath)) {
// await fs.promises.mkdir(path.resolve(process.cwd(), "data"));
// await fs.promises.writeFile(dbPath, "");
// console.log("Created database file.");
// }

// db.exec(`
// CREATE TABLE IF NOT EXISTS users (
// id TEXT,
// username TEXT,
// password TEXT,
// role TEXT
// )
// `);
// console.log("Created users table.");

// db.exec(`
// CREATE TABLE IF NOT EXISTS instances (
// id TEXT,
// name TEXT,
// settings TEXT
// )
// `);

// // Insert into users if the user doesn't already exist, a default admin user.
// const adminUserExists = db.prepare(
// "SELECT * FROM users WHERE role = ?"
// );
// if (!adminUserExists.get("admin")) {
// const adminPassword = uuid();
// const adminPasswordHash = await bcrypt.hash(adminPassword, 10);
// db.prepare(
// "INSERT INTO users (id, username, password, role) VALUES (?, ?, ?, ?)"
// ).run(uuid(), "admin", adminPasswordHash, "admin");
// console.log(
// `Created default admin user with password: ${adminPassword}`
// );
// }

const defaultAdminExists = await prisma.user.findFirst({
where: {
username: "admin",
Expand Down Expand Up @@ -85,40 +45,43 @@ export default class ManageDatabase {
},
});
},
getUser: (username: string) => {
return prisma.user.findFirst({
getUser: async (username: string) => {
const user = await prisma.user.findFirst({
where: {
username,
},
});

return user;
},
getUsers: () => {
return prisma.user.findMany();
getUsers: async () => {
const users = await prisma.user.findMany();
},
deleteUser: (id: string) => {
return prisma.user.delete({
deleteUser: async (id: string) => {
await prisma.user.delete({
where: {
id,
},
});
},
updateUser: async (data: User) => {
return prisma.user.update({
const passwordHash = await bcrypt.hash(data.password, 10);
await prisma.user.update({
where: {
id: data.id,
},
data: {
username: data.username,
password: passwordHash,
role: data.role,
password: await bcrypt.hash(data.password, 10),
},
});
},
};

static instances = {
addInstance: (instance: StorageInstance) => {
prisma.storageInstance.create({
addInstance: async (instance: StorageInstance) => {
await prisma.storageInstance.create({
data: {
id: instance.id,
name: instance.name,
Expand Down Expand Up @@ -153,15 +116,15 @@ export default class ManageDatabase {
};
});
},
deleteInstance: (id: string) => {
return prisma.storageInstance.delete({
deleteInstance: async (id: string) => {
await prisma.storageInstance.delete({
where: {
id,
},
});
},
updateInstance: (instance: StorageInstance) => {
return prisma.storageInstance.update({
updateInstance: async (instance: StorageInstance) => {
await prisma.storageInstance.update({
where: {
id: instance.id,
},
Expand Down

0 comments on commit 3ea349a

Please sign in to comment.