expressServer/repositories/userRepository.js
LingandRX 602c0fff4b refactor(config): 优化错误消息和日志记录
- 更新错误消息格式,使用驼峰命名法
- 添加服务器错误消息
- 用 logger 替代 console.log 记录日志
- 更新 MongoDB 连接日志
- 优化用户控制器中的错误处理
- 移除 SearchQuery 类中的无效代码
- 更新 package.json 中的脚本
- 移除未使用的 StringUtil 工具类
- 优化 hashUtils 中的密码加密和比较函数
2025-01-04 23:25:29 +08:00

104 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const UserModel = require('../models/userModel')
const { SearchResult } = require('../models/search')
exports.startTransaction = async () => {
const session = await UserModel.startSession()
session.startTransaction()
return session
}
exports.commitTransaction = async (session) => {
return session.commitTransaction()
}
exports.rollbackTransaction = async (session) => {
return session.abortTransaction()
}
exports.selectAllUser = async () => {
return UserModel.find()
}
exports.selectUserList = async (search = {}) => {
try {
const { size = 20, page = 1, sort, filters } = search
const skip = (page - 1) * size
const searchResult = new SearchResult()
searchResult.num = page
searchResult.size = size
// 检查 filters确保只有在 filters 存在时才应用
const matchFilters = filters ? { ...filters } : {}
const sortObj = sort && typeof sort === 'object' ? sort : { _id: 1 } // 默认按 _id 升序排序
const result = await UserModel.aggregate([
// 应用过滤条件确保filters存在时才传入
{ $match: matchFilters },
{
$facet: {
metadata: [{ $count: 'total' }, { $addFields: { total: '$total' } }],
data: [{ $sort: sortObj }, { $skip: skip }, { $limit: size }]
}
}
])
// 解构 result 并进行必要的空值检查
const { metadata = [], data = [] } = result[0] || {}
// 提取 metadata 中的 total若不存在则为 0
searchResult.total = metadata.length > 0 ? metadata[0].total : 0
searchResult.list = data
return searchResult
} catch (error) {
console.log(error)
throw error
}
}
exports.selectUserById = async (id) => {
return UserModel.findById(id)
}
exports.selectUserByAccount = async (account) => {
return UserModel.findOne({ account: account })
}
exports.selectUserByEmail = async (email) => {
return UserModel.findOne({ email: email })
}
exports.selectUserByUsername = async (username) => {
return UserModel.findOne({ username: username })
}
exports.selectUserByPhone = async (phone) => {
return UserModel.findOne({ phone: phone })
}
exports.selectUserByAccountExist = async (account) => {
const exist = await UserModel.exists({ account: account })
return exist !== null
}
exports.createUser = async (user) => {
return UserModel.create(user)
}
exports.updateUserById = async (id, user) => {
return UserModel.findByIdAndUpdate(id, user)
}
exports.updateUserByLoginDate = async (id, loginDate) => {
return UserModel.findByIdAndUpdate(id, { last_login_date: loginDate })
}
exports.deleteUserById = async (id) => {
return UserModel.findByIdAndDelete(id)
}
exports.deleteAllUser = async () => {
return UserModel.deleteMany()
}