mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-09 00:10:10 +01:00
085745c5e4
resolves #367
80 lines
1.9 KiB
JavaScript
80 lines
1.9 KiB
JavaScript
const prisma = require("../utils/prisma");
|
|
const bcrypt = require("bcrypt");
|
|
|
|
const User = {
|
|
create: async function ({ username, password, role = "default" }) {
|
|
try {
|
|
const hashedPassword = bcrypt.hashSync(password, 10);
|
|
const user = await prisma.users.create({
|
|
data: {
|
|
username,
|
|
password: hashedPassword,
|
|
role,
|
|
},
|
|
});
|
|
return { user, error: null };
|
|
} catch (error) {
|
|
console.error("FAILED TO CREATE USER.", error.message);
|
|
return { user: null, error: error.message };
|
|
}
|
|
},
|
|
|
|
update: async function (userId, updates = {}) {
|
|
try {
|
|
await prisma.users.update({
|
|
where: { id: parseInt(userId) },
|
|
data: updates,
|
|
});
|
|
return { success: true, error: null };
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return { success: false, error: error.message };
|
|
}
|
|
},
|
|
|
|
get: async function (clause = {}) {
|
|
try {
|
|
const user = await prisma.users.findFirst({ where: clause });
|
|
return user ? { ...user } : null;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
count: async function (clause = {}) {
|
|
try {
|
|
const count = await prisma.users.count({ where: clause });
|
|
return count;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return 0;
|
|
}
|
|
},
|
|
|
|
delete: async function (clause = {}) {
|
|
try {
|
|
await prisma.users.deleteMany({ where: clause });
|
|
return true;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
where: async function (clause = {}, limit = null) {
|
|
try {
|
|
const users = await prisma.users.findMany({
|
|
where: clause,
|
|
...(limit !== null ? { take: limit } : {}),
|
|
});
|
|
return users;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = { User };
|