初提交
This commit is contained in:
209
src/db.ts
Normal file
209
src/db.ts
Normal file
@ -0,0 +1,209 @@
|
||||
import cluster from 'cluster'
|
||||
import { IStorageDataItem, storage, IStorageDocument, dbs as _dbs } from './storage'
|
||||
|
||||
const MESSAGE_TYPE_NAME = 'yz_memdb'
|
||||
const sendable = cluster.isWorker && process.send
|
||||
|
||||
type DataTypes = keyof ReturnType<typeof memdb> | 'dbs'
|
||||
|
||||
let maxId = 1
|
||||
|
||||
//ID消息监听器(用于子进程)
|
||||
const idListeners: { [k: number]: (data: any, error?: string) => void } = {}
|
||||
|
||||
//主进程
|
||||
if (cluster.isMaster) {
|
||||
//监听消息
|
||||
cluster.on('message', async (worker, message) => {
|
||||
let res!: { id: number, data: any, db: string, keyName: string, type: DataTypes, messageType: string }
|
||||
let err: Error = undefined!
|
||||
let result: any = null //处理结果
|
||||
//解码数据得到结果
|
||||
try {
|
||||
res = JSON.parse(message)
|
||||
} catch (_err) {
|
||||
err = _err
|
||||
}
|
||||
//这些不用处理
|
||||
if (err || res.messageType !== MESSAGE_TYPE_NAME) return
|
||||
//针对不同消息进行处理
|
||||
try {
|
||||
const db = memdb(res.db, res.keyName)
|
||||
//获取数据库列表
|
||||
if (res.type == 'dbs') result = dbs()
|
||||
//保存数据
|
||||
else if (res.type == 'save') await db.save(res.data)
|
||||
//清空数据
|
||||
else if (res.type == 'clean') await db.clean()
|
||||
//删除数据
|
||||
else if (res.type == 'delete') await db.delete(res.data)
|
||||
//替换数据
|
||||
else if (res.type == 'replace') await db.replace(res.data)
|
||||
//获取列表
|
||||
else if (res.type == 'getAll') result = await db.getAll()
|
||||
//通过键获取数据
|
||||
else if (res.type == 'getByKeys') result = await db.getByKeys(res.data)
|
||||
//获取键列表
|
||||
else if (res.type == 'keys') result = await db.keys()
|
||||
//数据查询
|
||||
else if (res.type == 'find') result = await db.find(res.data)
|
||||
//反馈消息给客户端
|
||||
worker.send(JSON.stringify({ id: res.id, messageType: MESSAGE_TYPE_NAME, data: result, type: res.type }))
|
||||
}
|
||||
//错误反馈
|
||||
catch (err) {
|
||||
worker.send(JSON.stringify({ id: res.id, messageType: MESSAGE_TYPE_NAME, error: err.message, type: res.type }))
|
||||
}
|
||||
})
|
||||
}
|
||||
//子进程
|
||||
else {
|
||||
process.on('message', message => {
|
||||
let id!: number
|
||||
let data!: any
|
||||
let error!: string
|
||||
let messageType!: string
|
||||
let err: Error = undefined!
|
||||
//解码数据得到结果
|
||||
try {
|
||||
const res = JSON.parse(message)
|
||||
id = res.id
|
||||
data = res.data
|
||||
error = res.error
|
||||
messageType = res.messageType
|
||||
} catch (_err) {
|
||||
err = _err
|
||||
}
|
||||
//过滤无用数据
|
||||
if (err || messageType !== MESSAGE_TYPE_NAME) return
|
||||
//取得消息监听器
|
||||
const listener = idListeners[id]
|
||||
if (!listener) return
|
||||
//调用监听器并删除之
|
||||
listener(data, error)
|
||||
delete idListeners[id]
|
||||
})
|
||||
}
|
||||
|
||||
//发送数据并得到结果
|
||||
function send(type: DataTypes, db: string, keyName: string, data: any): Promise<any> {
|
||||
const id = maxId++
|
||||
return new Promise((resolve, reject) => {
|
||||
//监听消息
|
||||
idListeners[id] = (data, error) => {
|
||||
if (error) reject(new Error(error))
|
||||
else resolve(data)
|
||||
}
|
||||
//发送数据
|
||||
process.send!(JSON.stringify({ id, type, messageType: MESSAGE_TYPE_NAME, data, db, keyName }))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建内存数据库
|
||||
* @param db 数据库名
|
||||
* @param keyName 键名称,如id
|
||||
*/
|
||||
export function memdb(db: string, keyName: string) {
|
||||
//是否允许发送数据
|
||||
const s = storage(db)
|
||||
|
||||
/**
|
||||
* 保存值
|
||||
* @param vals 要保存的值
|
||||
*/
|
||||
async function save(vals: Array<IStorageDataItem> | IStorageDataItem): Promise<void> {
|
||||
if (sendable) return await send('save', db, keyName, vals)
|
||||
else ((vals instanceof Array) ? vals : [vals]).forEach(val => {
|
||||
const key = val[keyName]
|
||||
key && s.save(key, val)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空数据
|
||||
*/
|
||||
async function clean() {
|
||||
if (sendable) return await send('clean', db, keyName, null)
|
||||
else s.clean()
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空原有值,并保存新值
|
||||
* @param vals 要保存的值
|
||||
*/
|
||||
async function replace(vals: Array<IStorageDataItem>) {
|
||||
if (sendable) return await send('replace', db, keyName, vals)
|
||||
else {
|
||||
s.clean()
|
||||
vals.forEach(val => {
|
||||
const key = val[keyName]
|
||||
key && s.save(key, val)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param keys 要删除的键列表
|
||||
*/
|
||||
async function _delete(keys: Array<string>): Promise<void> {
|
||||
if (sendable) return await send('delete', db, keyName, keys)
|
||||
else s.delete(keys)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据
|
||||
*/
|
||||
async function getAll(): Promise<Array<IStorageDataItem> | null> {
|
||||
if (sendable) return await send('getAll', db, keyName, null)
|
||||
else return s.all()
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过指定的键获取数据
|
||||
* @param keys 键列表
|
||||
*/
|
||||
async function getByKeys(keys: Array<string>): Promise<Array<IStorageDataItem> | null> {
|
||||
if (sendable) return await send('getByKeys', db, keyName, keys)
|
||||
else return s.keysof(keys)
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据查询
|
||||
* @param option 查询选项
|
||||
*/
|
||||
async function find(option: { [i: string]: string | number | boolean }): Promise<Array<IStorageDataItem> | null> {
|
||||
if (sendable) return await send('find', db, keyName, option)
|
||||
else return s.find(option)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有键列表
|
||||
*/
|
||||
async function keys(): Promise<Array<string>> {
|
||||
if (sendable) return await send('keys', db, keyName, null)
|
||||
else return Object.keys(s.datas() || {})
|
||||
}
|
||||
|
||||
//返回结果
|
||||
return {
|
||||
save,
|
||||
clean,
|
||||
replace,
|
||||
delete: _delete,
|
||||
getAll,
|
||||
getByKeys,
|
||||
find,
|
||||
keys,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据库列表
|
||||
*/
|
||||
export async function dbs(): Promise<Array<string>> {
|
||||
if (sendable) return await send('delete', null!, null!, null)
|
||||
else return _dbs()
|
||||
}
|
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { memdb } from './db'
|
||||
|
||||
export { memdb }
|
||||
module.exports = { memdb }
|
176
src/storage.ts
Normal file
176
src/storage.ts
Normal file
@ -0,0 +1,176 @@
|
||||
/**
|
||||
* @author 陆益之
|
||||
* 此文件用于处理数据存储,数据存储共分为3个部分,库、文档、数据,每个库下面有多个文档,每个文档下面有多个数据,例如:
|
||||
* [users]
|
||||
* [lee]
|
||||
* [name = lee]
|
||||
* [age = 20]
|
||||
* [gender = male]
|
||||
* [love = [1,2]]
|
||||
* [jane]
|
||||
* [name = jane]
|
||||
* [age=19]
|
||||
* [gender = female]
|
||||
* [love = [2]]
|
||||
* [foods]
|
||||
* [1]
|
||||
* [id = 1]
|
||||
* [name = beef]
|
||||
* [2]
|
||||
* [id = 2]
|
||||
* [name = bread]
|
||||
*/
|
||||
|
||||
/** 数据 */
|
||||
export type IStorageDataItem = { [i: string]: any }
|
||||
|
||||
/** 文档(键=>值) */
|
||||
export type IStorageDocument = { [i: string]: IStorageDataItem }
|
||||
|
||||
/** 库 */
|
||||
export type IStorageDB = { [i: string]: IStorageDocument }
|
||||
|
||||
|
||||
/** 数据存储 */
|
||||
const storages: IStorageDB = {}
|
||||
|
||||
/**
|
||||
* 建立一个库操作器
|
||||
* @param db 库名称
|
||||
*/
|
||||
export function storage(db: string) {
|
||||
/**
|
||||
* 是否存在数据库
|
||||
*/
|
||||
function exists(): boolean {
|
||||
return !!storages[db]
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据库
|
||||
*/
|
||||
function drop() {
|
||||
delete storages[db]
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名数据库并返回新名称的数据库
|
||||
* @param newName 新数据库名称
|
||||
*/
|
||||
function rename(newName: string) {
|
||||
storages[newName] = storages[db]
|
||||
drop()
|
||||
return storage(newName)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空数据
|
||||
*/
|
||||
function clean() {
|
||||
storages[db] = {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据列表
|
||||
* @param init 如果没有是否初始化
|
||||
*/
|
||||
function datas(init?: boolean): IStorageDocument | null {
|
||||
if (init && !storages[db]) storages[db] = {}
|
||||
return storages[db] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键获取数据
|
||||
* @param keys 键列表
|
||||
*/
|
||||
function keysof(keys: Array<string>) {
|
||||
const d = datas()
|
||||
if (!d) return null
|
||||
const buffer: Array<IStorageDataItem> = []
|
||||
keys.forEach(key => {
|
||||
if (d[key] === undefined) return
|
||||
buffer.push(d[key])
|
||||
})
|
||||
return buffer
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据
|
||||
*/
|
||||
function all() {
|
||||
const d = datas()
|
||||
return d ? Object.values(d) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据查询
|
||||
* @param option 查询选项
|
||||
*/
|
||||
function find(option: { [key: string]: string | boolean | number }) {
|
||||
const d = datas()
|
||||
if (!d) return null
|
||||
const okeys = Object.keys(option)
|
||||
//如果没有任何选项则返回所有数据
|
||||
if (!okeys.length) return all()
|
||||
//否则按照选项查询
|
||||
const buffer: Array<IStorageDataItem> = []
|
||||
for (let key in d) {
|
||||
let val = d[key]
|
||||
if (!val) continue
|
||||
let eq = true
|
||||
for (let okey in okeys) {
|
||||
if (option[okey] !== val[okey]) {
|
||||
eq = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!eq) continue
|
||||
buffer.push(val)
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param keys 要删除的数据的键
|
||||
*/
|
||||
function _delete(keys: Array<string>) {
|
||||
const d = datas()
|
||||
if (!d) return
|
||||
keys.forEach(key => {
|
||||
delete d[key]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据,无则添加
|
||||
* @param key 要修改的数据的键
|
||||
* @param value 数据值
|
||||
* @param replace 是否进行替换操作,默认true
|
||||
*/
|
||||
function save(key: string, value: IStorageDataItem, replace: boolean = true) {
|
||||
const d = datas(true)!
|
||||
//没有数据或者说是替换则直接赋值
|
||||
if (d[key] || replace) {
|
||||
d[key] = value
|
||||
return
|
||||
}
|
||||
//按键覆盖
|
||||
const data = d[key]
|
||||
Object.keys(value).forEach(key => data[key] = value[key])
|
||||
}
|
||||
|
||||
//返回操作函数
|
||||
return {
|
||||
exists, drop, rename, clean,
|
||||
datas, keysof, all, find,
|
||||
delete: _delete, save,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有库的键
|
||||
*/
|
||||
export function dbs() {
|
||||
return Object.keys(storages)
|
||||
}
|
Reference in New Issue
Block a user