♻️ 重构本地数据库-group模块

This commit is contained in:
chaos-zhu 2024-10-22 21:48:29 +08:00
parent 7aefa410dc
commit 5437486eba
3 changed files with 56 additions and 77 deletions

View File

@ -1,13 +1,11 @@
const { readGroupList, writeGroupList } = require('../utils/storage')
const { HostListDB } = require('../utils/db-class')
const { HostListDB, GroupDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance()
const groupDB = new GroupDB().getInstance()
async function getGroupList({ res }) {
let data = await readGroupList()
data = data.map(item => {
return { ...item, id: item._id }
})
let data = await groupDB.findAsync({})
data = data.map(item => ({ ...item, id: item._id }))
data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0))
res.success({ data })
}
@ -15,10 +13,8 @@ async function getGroupList({ res }) {
const addGroupList = async ({ res, request }) => {
let { body: { name, index } } = request
if (!name) return res.fail({ data: false, msg: '参数错误' })
let groupList = await readGroupList()
let group = { name, index }
groupList.push(group)
await writeGroupList(groupList)
await groupDB.insertAsync(group)
res.success({ data: '添加成功' })
}
@ -26,35 +22,26 @@ const updateGroupList = async ({ res, request }) => {
let { params: { id } } = request
let { body: { name, index } } = request
if (!id || !name) return res.fail({ data: false, msg: '参数错误' })
let groupList = await readGroupList()
let idx = groupList.findIndex(item => item._id === id)
if (idx === -1) return res.fail({ data: false, msg: `分组ID${ id }不存在` })
const { _id } = groupList[idx]
let group = { _id, name, index: Number(index) || 0 }
groupList.splice(idx, 1, group)
await writeGroupList(groupList)
let target = await groupDB.findOneAsync({ _id: id })
if (!target) return res.fail({ data: false, msg: `分组ID${ id }不存在` })
await groupDB.updateAsync({ _id: id }, { name, index: Number(index) || 0 })
res.success({ data: '修改成功' })
}
const removeGroup = async ({ res, request }) => {
let { params: { id } } = request
if (id === 'default') return res.fail({ data: false, msg: '保留分组, 禁止删除' })
let groupList = await readGroupList()
let idx = groupList.findIndex(item => item._id === id)
if (idx === -1) return res.fail({ msg: '分组不存在' })
// 移除分组将所有该分组下host分配到default中去
let hostList = await hostListDB.findAsync({})
if (Array.isArray(hostList) && hostList.length > 0) {
for (let item of hostList) {
if (item.group === groupList[idx]._id) {
if (item.group === id) {
item.group = 'default'
await hostListDB.updateAsync({ _id: item._id }, item)
}
}
}
groupList.splice(idx, 1)
await writeGroupList(groupList)
await groupDB.removeAsync({ _id: id })
res.success({ data: '移除成功' })
}

View File

@ -1,4 +1,4 @@
const { writeKey, writeGroupList, writeNotifyList, writeNotifyConfig } = require('./utils/storage')
const { writeKey, writeNotifyList, writeNotifyConfig } = require('./utils/storage')
const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class')
function initKeyDB() {
@ -26,23 +26,15 @@ function initKeyDB() {
})
}
function initGroupDB() {
return new Promise((resolve, reject) => {
async function initGroupDB() {
const groupDB = new GroupDB().getInstance()
groupDB.count({}, async (err, count) => {
if (err) {
consola.log('初始化groupDB错误:', err)
reject(err)
} else {
let count = await groupDB.countAsync({})
if (count === 0) {
consola.log('初始化groupDB✔')
const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }]
await writeGroupList(defaultData)
return groupDB.insertAsync(defaultData)
}
}
resolve()
})
})
return Promise.resolve()
}
function initNotifyDB() {

View File

@ -178,42 +178,42 @@ const writeNotifyList = async (notifyList) => {
})
}
const readGroupList = async () => {
return new Promise((resolve, reject) => {
const groupDB = new GroupDB().getInstance()
groupDB.find({}, (err, docs) => {
if (err) {
consola.error('读取group list错误: ', err)
reject(err)
} else {
resolve(docs)
}
})
})
}
// const readGroupList = async () => {
// return new Promise((resolve, reject) => {
// const groupDB = new GroupDB().getInstance()
// groupDB.find({}, (err, docs) => {
// if (err) {
// consola.error('读取group list错误: ', err)
// reject(err)
// } else {
// resolve(docs)
// }
// })
// })
// }
const writeGroupList = async (list = []) => {
return new Promise((resolve, reject) => {
const groupDB = new GroupDB().getInstance()
groupDB.remove({}, { multi: true }, (err) => {
if (err) {
consola.error('清空group list出错:', err)
reject(err)
} else {
groupDB.compactDatafile()
groupDB.insert(list, (err, newDocs) => {
if (err) {
consola.error('写入新的group list出错:', err)
reject(err)
} else {
groupDB.compactDatafile()
resolve(newDocs)
}
})
}
})
})
}
// const writeGroupList = async (list = []) => {
// return new Promise((resolve, reject) => {
// const groupDB = new GroupDB().getInstance()
// groupDB.remove({}, { multi: true }, (err) => {
// if (err) {
// consola.error('清空group list出错:', err)
// reject(err)
// } else {
// groupDB.compactDatafile()
// groupDB.insert(list, (err, newDocs) => {
// if (err) {
// consola.error('写入新的group list出错:', err)
// reject(err)
// } else {
// groupDB.compactDatafile()
// resolve(newDocs)
// }
// })
// }
// })
// })
// }
const readScriptList = async () => {
return new Promise((resolve, reject) => {
@ -333,7 +333,7 @@ module.exports = {
readKey, writeKey,
readNotifyList, writeNotifyList,
readNotifyConfig, writeNotifyConfig, getNotifySwByType,
readGroupList, writeGroupList,
// readGroupList, writeGroupList,
readScriptList, writeScriptList,
readOneKeyRecord, writeOneKeyRecord, deleteOneKeyRecord,
readLog, writeLog