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 🙏

© 2025 – Pkg Stats / Ryan Hefner

state-db.js

v0.2.2

Published

state management

Downloads

7

Readme

state-db解决了哪些问题?

API 文档

安装
npm i state-db.js
创建数据库实例
import DB from 'state-db.js';
const db = new DB();
创建一张表

可以通过schema字段限制字段的类型和是否必填

db.createTable({
    name: 'articals', //required
  	schema: {
        id: {type: 'Number', reuqired: true}
      	title: {type: 'String', required: true},
        content: {type: 'String', required: false},
    },  //options
  	initValue:[{id: 1, title: "我的奋斗", content: "你好。。。"}], //options
    pramaryKey: "id" //options
})
获取表
const articalTable = db.table('artical');
删除表

需要注意的是,drop和clear只是在库中删除表,但是如果表对象依然被应用引用,表对象实际在内存中并未被清空

db.drop('artical'); //删除名为artical的表
db.clear();         //清除全部表
监听库变化(增删表时)
db.bindFn((changeInfo) => {
    console.log(changeInfo);
})
监听表变化(增删改数据时)
articalTable.bindFn((changeInfo) => {
    console.log(changeInfo);
})
articalTable.insert({ id: 2, name: "hi,你好"});

articalTable.insert([
    {id: 3, name: '21天精通C++'},
    {id: 4,name: '21天精通Java'}
])
articalTable.where('line.name=="我的奋斗"').delete()

将查到的第一个值update

articalTable.where('line.id==1').update({name: "你的奋斗"});

将查到的全部值update成同一个值

articalTable.where('line.id==1').updateAll({name: "你的奋斗"});

传入一个数组 和 一个key值,当遇到key值相等的行时进行update

articalTable.updateByKey([{ id: 2, name: '奥特曼大战变形金刚' }, {id: 3, name: 'lee',}], 'id');
//指定条件的值
//getValues()保证每次取出的都是全新的对象,values则会使用缓存,和上次同一个查询使用同样的对象,使用values性能更好,使用getValues()更安全。
//只要进行过增删改操作,缓存都会清空。
articalTable.where('line.name=="我的奋斗 && index !== 1"').getValues(); //值是原值,后续操作不安全,仅能用于展示
articalTable.where('line.name=="我的奋斗 && index !== 1"').values; //值是原值,后续操作不安全,仅能用于展示

articalTable.where('line.name=="我的奋斗 && index !== 1"').getValues('safe'); //值是深copy出来的,后续操作是安全的。



//查前三个
articalTable.first(3).getValues();
//查后三个
articalTable.last(3).getValues();

辅助工具

因为DB是非常结构化的并且能够反映全局的,可以有一个完整的视图来告知我们页面当前的状态,方便我们开发和debug。

import DB from 'state-db.js';
import devtool from 'state-db.js/build/devtool.bundle.esm.js'; //引入devtool
const db = new DB();
devtool(db, 'html', {hide: true}); //第二个参数默认为console
export default db;

如何与框架结合

结合模板使用

const getArticals = articalTable.getValues();
const render = () => {
    str = `<ul>
        ${getArticals().map(artical => `<li>${artical.title}/</li>`)}
    </ul>`
    $('#app').innerHTML = str;
}

articalTable.bindFn(render);

结合react使用

数据表和组件进行绑定(表变化触发组件render)
const getArticals = articalTable.getValues()

@db.dbconnectReact('artical')
class Artical extends Component {
    render() {
        return (<ul>
            {getArticals().map(artical => <li>{artical.title}/</li>)
        </ul>)
    }
}

结合vue使用

const getArticals = articalTable.getValues()

new Vue({
  mixins: [db.dbconnectVue('artical')],
  //... your own logic
})

//全局模式,
//不传参数表示监听所有表(在mixin之前)
Vue.mixin(db.dbconnectVue())

推荐开发模式

step1: 创建一个库,建议每个应用只有一个库, 并加入devtool;
import DB from 'state-db.js';
import devtool from 'state-db.js/build/devtool.bundle.esm.js';
const db = new DB();
devtool(db, 'html'); //第二个参数默认为console
export default db;
step2: 分析我们的单页APP,哪些状态是某个路由独有的,哪些是页面生命周期内持久存在的
step3.1: 对于持久存在的状态,比如我们从服务端拉一个配置列表下来, 这时我们的建表语句和操作方法定义要放在组件的定义阶段,然后就可以在组件中调用这些方法了,比如:
import db from '../db.js' //刚才已经new好的DB实例
db.createTable({name: 'configList'});
const configTable = db.table('configList');

const fetchConfigList = () => {
    $.ajax({
        url: url,
        success: (res) => {configTable.init(res.data); }
    })
}

const getConfig = (confName) => {
    var arr = configTable.where('line.name == "'+ confName +'"').getValues;
    if (arr.length) {
        return arr[0].value;
    }
    else{
        return none;
    }
}
 export {fetchConfigList, getConfig}
step3.2: 对于某个路由或组件下独有的状态,比如一块独立的业务逻辑, 这时我们的建表语句和操作方法定义要放在组件的执行阶段 ,可以把model包装为一个函数,在组件入口执行处进行调用。db.dbconnectReact('todos')高阶组件会帮你进行组件和表之间的绑定和解绑。
import db from '../db.js'
const model = () => {
    db.todoTable({name: 'todos' });
    const todoTable = db.table('todos');
    const fetchTodos = () => {
        $.ajax({
             url: url,
             success: (res) => {todoTable.init(res.data);}
        })
    }
    const getTodos = () => todoTable.getValues();
    return { fetchTodos,  getTodos}
}

@db.dbconnectReact('todos')
class Todo extends Component {
    constructor() {
      this.model = model();
    }
    componentDidMount(){
        this.model.fetchTodos();
    }
    render() {
        return(
            return (<ul>
                {this.model.getTodos().map(todo => <li>{todo.content}/</li>)
            </ul>)
        )
    }
}