frpc-desktop/electron/storage/proxy.ts

92 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-11-27 15:03:25 +08:00
import Datastore from "nedb";
import path from "path";
import { app } from "electron";
2024-08-16 23:59:40 +08:00
const log = require("electron-log");
2023-11-27 15:03:25 +08:00
const proxyDB = new Datastore({
autoload: true,
filename: path.join(app.getPath("userData"), "proxy.db")
});
/**
*
* @param proxy
* @param cb
*/
export const insertProxy = (
proxy: Proxy,
cb?: (err: Error | null, document: Proxy) => void
) => {
2024-08-06 00:17:39 +08:00
log.debug(`新增代理:${JSON.stringify(proxy)}`);
2023-11-27 15:03:25 +08:00
proxyDB.insert(proxy, cb);
};
/**
*
* @param _id
* @param cb
*/
export const deleteProxyById = (
_id: string,
cb?: (err: Error | null, n: number) => void
) => {
2024-08-06 00:17:39 +08:00
log.debug(`删除代理:${_id}`);
2023-11-27 15:03:25 +08:00
proxyDB.remove({ _id: _id }, cb);
};
/**
*
*/
export const updateProxyById = (
proxy: Proxy,
cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void
) => {
2024-08-06 00:17:39 +08:00
log.debug(`修改代理:${proxy}`);
2023-11-27 15:03:25 +08:00
proxyDB.update({ _id: proxy._id }, proxy, {}, cb);
};
/**
*
* @param cb
*/
export const listProxy = (
callback: (err: Error | null, documents: Proxy[]) => void
) => {
proxyDB.find({}, callback);
};
/**
* id查询
* @param id
* @param callback
*/
export const getProxyById = (
id: string,
callback: (err: Error | null, document: Proxy) => void
) => {
proxyDB.findOne({ _id: id }, callback);
};
2024-08-22 14:19:19 +08:00
2024-09-05 10:28:55 +08:00
/**
*
* @param cb
*/
2024-08-22 14:19:19 +08:00
export const clearProxy = (cb?: (err: Error | null, n: number) => void) => {
proxyDB.remove({}, { multi: true }, cb);
};
2024-09-05 10:28:55 +08:00
/**
*
* @param id id
* @param st
* @param cb
*/
export const updateProxyStatus = (
id: string,
st: boolean,
cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void
) => {
proxyDB.update({ _id: id }, { $set: { status: st } }, {}, cb);
};