♻️ 重构本地数据库-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, GroupDB } = require('../utils/db-class')
const { HostListDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance() const hostListDB = new HostListDB().getInstance()
const groupDB = new GroupDB().getInstance()
async function getGroupList({ res }) { async function getGroupList({ res }) {
let data = await readGroupList() let data = await groupDB.findAsync({})
data = data.map(item => { data = data.map(item => ({ ...item, id: item._id }))
return { ...item, id: item._id }
})
data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0)) data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0))
res.success({ data }) res.success({ data })
} }
@ -15,10 +13,8 @@ async function getGroupList({ res }) {
const addGroupList = async ({ res, request }) => { const addGroupList = async ({ res, request }) => {
let { body: { name, index } } = request let { body: { name, index } } = request
if (!name) return res.fail({ data: false, msg: '参数错误' }) if (!name) return res.fail({ data: false, msg: '参数错误' })
let groupList = await readGroupList()
let group = { name, index } let group = { name, index }
groupList.push(group) await groupDB.insertAsync(group)
await writeGroupList(groupList)
res.success({ data: '添加成功' }) res.success({ data: '添加成功' })
} }
@ -26,35 +22,26 @@ const updateGroupList = async ({ res, request }) => {
let { params: { id } } = request let { params: { id } } = request
let { body: { name, index } } = request let { body: { name, index } } = request
if (!id || !name) return res.fail({ data: false, msg: '参数错误' }) if (!id || !name) return res.fail({ data: false, msg: '参数错误' })
let groupList = await readGroupList() let target = await groupDB.findOneAsync({ _id: id })
let idx = groupList.findIndex(item => item._id === id) if (!target) return res.fail({ data: false, msg: `分组ID${ id }不存在` })
if (idx === -1) return res.fail({ data: false, msg: `分组ID${ id }不存在` }) await groupDB.updateAsync({ _id: id }, { name, index: Number(index) || 0 })
const { _id } = groupList[idx]
let group = { _id, name, index: Number(index) || 0 }
groupList.splice(idx, 1, group)
await writeGroupList(groupList)
res.success({ data: '修改成功' }) res.success({ data: '修改成功' })
} }
const removeGroup = async ({ res, request }) => { const removeGroup = async ({ res, request }) => {
let { params: { id } } = request let { params: { id } } = request
if (id === 'default') return res.fail({ data: false, msg: '保留分组, 禁止删除' }) 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中去 // 移除分组将所有该分组下host分配到default中去
let hostList = await hostListDB.findAsync({}) let hostList = await hostListDB.findAsync({})
if (Array.isArray(hostList) && hostList.length > 0) { if (Array.isArray(hostList) && hostList.length > 0) {
for (let item of hostList) { for (let item of hostList) {
if (item.group === groupList[idx]._id) { if (item.group === id) {
item.group = 'default' item.group = 'default'
await hostListDB.updateAsync({ _id: item._id }, item) await hostListDB.updateAsync({ _id: item._id }, item)
} }
} }
} }
groupList.splice(idx, 1) await groupDB.removeAsync({ _id: id })
await writeGroupList(groupList)
res.success({ data: '移除成功' }) 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') const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class')
function initKeyDB() { function initKeyDB() {
@ -26,23 +26,15 @@ function initKeyDB() {
}) })
} }
function initGroupDB() { async function initGroupDB() {
return new Promise((resolve, reject) => { const groupDB = new GroupDB().getInstance()
const groupDB = new GroupDB().getInstance() let count = await groupDB.countAsync({})
groupDB.count({}, async (err, count) => { if (count === 0) {
if (err) { consola.log('初始化groupDB✔')
consola.log('初始化groupDB错误:', err) const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }]
reject(err) return groupDB.insertAsync(defaultData)
} else { }
if (count === 0) { return Promise.resolve()
consola.log('初始化groupDB✔')
const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }]
await writeGroupList(defaultData)
}
}
resolve()
})
})
} }
function initNotifyDB() { function initNotifyDB() {

View File

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