结构化存储工具
下一个版本 dmb2
第一次使用初始化
import {upgrade} from 'dmb'
upgrade()
运行会进入命令行:
1、输入存储名:名称
2、询问是否更新:选择 是
3、接下来提示是否更新开发文件:暂且选择 否
文件结构为:假设存储名为:test
root ┯ test ┳ backup ┳
┃ ┇ // 备份文件存储目录
┃ ┗
┣ buffer ┳
┃ ┇ // buffer格式或者文本格式的存储目录
┃ ┗
┣ data // 数据存储文件
┣ index // 数据索引文件
┣ index.mjs // 数据开发指导文件(更新开发文件选:是)
┗ struct.json // 数据结构文件
定义存储结构
struct.json
{
"version": 1,
"head": {
"admin": {
"uid": {
"type": "id",
"index": true,
"value": 1,
"step": 1,
"": "这个备注信息"
},
"pwd": {
"type": "string",
"length": 12,
"value": "123456",
"index": true,
"": "index是给加上索引可以使用.indexByPwd('123456')"
},
"remark": 64,
"": "这个集合的备注信息"
},
"log": {
"uid": "id",
"time": "time",
"content": "text"
}
}
}
升级需要增加version重新执行upgrade()
类型:id、uint、int、bool、time、float、string、buffer、text
1、简写: 名称
: 类型
解释为:名称
: { type: 类型
}
特殊情况类型
为id
名称
: id
解释为:名称
: {
type: id,
step: 1,
value: 1,
index: true
}
2、简写: 名称
: 数字
解释为:名称
: {
type: string,
length: 数字
}
3、简写: 名称
: //内容
解释为:名称
: {
type: string,
length: 12,
"": "内容"
}
4、简写: 名称
: 对象
解释为:名称
: {
补充默认配置
}
type
缺省值为string
type: id
缺省值为:step、value、index
type: string
缺省值为:length
注意: string的length为大于1的整数,text、buffer不支持默认值,time可使用now、update默认值。
例如
"admin": {
"uid": "id",
"pwd": {
"value": "123456"
},
"loginTime": {
"type": "time",
"value": "update"
},
"remark": "//备注",
"": "管理员"
},
"log": {
"uid": {
"type": "uint",
"index": true
},
"ip": 32,
"time": {
"type": "time",
"value": "now"
},
"content": "text"
}
解释为:
"admin": {
"uid": {
"type": "id",
"index": true,
"value": 1,
"step": 1
},
"pwd": {
"type": "string",
"length": 12,
"value": "123456"
},
"loginTime": {
"type": "time",
"value": "update"
},
"remark": {
"type": "string",
"length": 12,
"": "备注"
},
"": "管理员"
},
"log": {
"uid": {
"type": "uint",
"index": true
},
"ip": {
"type": "string",
"length": 32
},
"time": {
"type": "time",
"value": "now"
},
"content": {
"type": "text"
}
}
升级
新增:
"admin": {...}, "log": {...}
"admin": {...},
"role": {...}
,
"log": {...}
更新:
"admin": {...},
"log": {旧配置
}
"admin": {...},
"log": {新配置
}
改名:
"admin": {...}, "log": "log2"
删除:
"admin": {...}, "log2": null
打开使用已有存储文件
开发模式
import connect, {close} from "./test/index.mjs";
生产模式
import connect, {close} from "dmb";
连接
const storage = connect({name: 'test'})
使用
查:
storage.admin.find(admin => admin.usn.includes('test')).role
if (storage.admin.indexByUid('admin')[0].pwd === '123456');
增:
storage.admin.push(
{
usn: 'admin',
pwd: '123456'
},
{
usn: 'test',
pwd: '123456'
}
)
改:
storage.admin.find(admin => admin.usn === 'admin').role = 'root'
删:
storage.admin.remove(...storage.admin.filter(admin => admin.usn.startsWith('test')))
保存:
storage[close]()
默认是每5分钟保存一次