npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dmb2

v0.6.7

Published

storage dmb2

Downloads

3

Readme

结构化存储工具

第一次使用初始化

import open, {close} from 'dmb2'

const storage = open('test', {
    upgrade({appendSet}) {
        // todo: appendSet(/* ... */)
    }
})

storage[close]()

更新升级

import open, {close} from 'dmb2'

const storage = open('test', {
    version: 1.2,
    upgrade(/* ... */) {
        // 当前版本 version 大于指定版本 version: 1.2 就会触发 upgrade 回调
        // todo
    }
})

storage[close]()

文件结构为:假设存储名为:test

test ┯ 1.2[version] ┳ buffer ┳
                    ┃        ┇ // buffer格式或者文本格式的存储目录
                    ┃        ┗
                    ┣ data          // 数据存储文件
                    ┣ index         // 数据索引文件
                    ┗ index.mjs     // 数据开发指导文件(更新开发文件选:是)

数据类型

类型:Id, Uint, Int, Bool, Time, Float, String, Buffer, Text

import open, {Id, Uint, Int, Bool, Time, Float, String, Buffer, Text, close} from "dmb2";
...
    upgrade({appendSet}) {
        appendSet('user', {
            uid: String,
            pwd: String('存MD5指').length(32),
            loginTime: Time().value(Time.update)
        }, '用户表')
    }
...

缺省写法与完整写法

// 在 upgrade() 中 Time.value(Time.update) 和 Time().value(Time.update) 两种写法一样。
/* 例如:
    appendSet('test', {
        id: Id,
    })
    等价
    appendSet('test', {
        id: Id(),
    })
*/

// Id 用法
Id('备注') // 会补全默认值
Id()
    .index(false) // 是否索引,此类型默认为 true
    .value('2') // 默认值,此类型缺省为 1
    .step(2) // 缺省自增值,此类型缺省为 1
    .remark('备注') // 可缺省
    
// Uint 用法
Uint()
    .index(false) // 是否索引,此类型默认为 false
    .value(2) // 默认值,此类型没有缺省值
    .step(2) // 缺省自增值,此类型没有缺省值
    .remark('备注') // 可缺省
    
// Int 用法
Int()
    .index(false) // 是否索引,此类型默认为 false
    .value(2) // 默认值,此类型没有缺省值
    .step(2) // 缺省自增值,此类型没有缺省值
    .remark('备注') // 可缺省
    
// Bool 用法
Bool()
    .index(false) // 是否索引,此类型默认为 false
    .value(false) // 默认值,此类型缺省为 false
    .remark('备注') // 可缺省
    
// Time 用法
Time()
    .index(false) // 是否索引,此类型默认为 false
    .value(Time.update) // 默认值,此类型没有缺省值,可取值:Time.update、Time.now
    .remark('备注') // 可缺省
    
// Float 用法
Float()
    .index(false) // 是否索引,此类型默认为 false
    .value(0.1) // 默认值,此类型没有缺省值
    .step(0.1) // 缺省自增值,此类型没有缺省值
    .remark('备注') // 可缺省
    
// String 用法
String()
    .index(false) // 是否索引,此类型默认为 false
    .value('123456') // 默认值,此类型没有缺省值
    .length(8) // 长度,此类型默认为 12
    .remark('备注') // 可缺省
    
// Buffer 用法
Buffer().remark('备注') // 可缺省
    
// Text 用法
Text().remark('备注') // 可缺省

存储结构更新写法

upgrade({appendSet, appendCol, updateSetRemark, updateCol, renameSet, renameCol, deleteSet, deleteCol}) {
    // 新增集合
    appendSet('user', {
        uid: String,
        pwd: String('存MD5指').length(32)
    }, '用户表')
    
    // 新增列
    appendCol('user', {
        loginTime: Time().value(Time.update)
    })
    
    // 更新集合备注
    updateSetRemark('log', '日志表')
    
    // 更新列
    updateCol('user', {
        loginTime: Time().value(Time.update).remark('登录时间')
    })
    
    // 更名操作
    renameSet('test', 'test2')
    renameCol('user', {
        uid: 'usn',
        test: 'test2',
    })
    
    // 删除操作
    deleteSet('test2')
    deleteCol('user', 'test2')
    
    // 碎片整理,当改变存储结构就会产生有的空间没办法回收,可以通过这种方法回收因为结构变化占用的空间
    open('test', {version: 3, minimum: true})
}

打开使用已有存储文件

开发模式,开发模式可以看到存储有哪些集合,集合内有哪些列,支持哪些操作等提示

import open, {close} from "./test/2/index.mjs";

生产模式

import open, {close} from "dmb2";

连接

let storage = open('test') // 普通使用
storage[close]() // 打开后不会自动释放,按需经行关闭释放

storage = open('test', '123123') // 设置和使用密码
storage[close]()

storage = open('test', {
    newPwd: '新密码',
    password: '旧密码',
    version: 1, // 版本号
    upgrade: () => {}, // 版本号增加后触发的回调函数
    noCache: false, // 是否使用缓存,默认使用
    dev: false, // 开发模式可以更清晰的看到存储结构
    minimum: false // 升级版本号来回收空间,要求不配置upgrade
})
storage[close]()

使用

查:

storage.admin.find(admin => admin.usn.includes('test')).role

if (storage.admin.indexByUid('admin')[0].pwd === '123456');

// 带索引的时间
storage.admin.indexByLoginTime(-Infinity, '2022/01/01')

// 简单分页查询
storage.admin.page(admin => predicate(admin), index, size)

// 结果缓存的分页查询,缓存10秒
const params = {key: 'name', index: 0, size: 1}
storage.admin.page(admin => predicate(admin), 'index', 'size', params)

增:

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')))

更多使用方法参考 test.mjs

保存:

storage[close]()