expressServer/repositories/userRepository.js
LingandRX 8741389ef5 refactor: 优化项目结构和代码
- 更新 .gitignore 文件,排除 Idea 项目配置
- 移除 vuepress 缓存目录
- 优化 app.js 中的错误处理和状态码导入- 调整 CORS 配置,移除未使用的 allowdHeaders
- 更新 hashUtils.js 中的密码处理函数
- 优化 index.js 中的路由定义
- 更新 mongodbConfig.js 中的数据库连接逻辑
- 升级 express到 5.0.0- 更新 mongoose 到 8.7.1
2025-01-01 12:21:43 +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()
}