0.1.3 • Published 10 months ago

minordb v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

MinorDB

介绍

MinorDB 是基于 IndexDB 封装的 web 前端数据库,轻量,Promise语法使用简单,支持 jQuery , Vue , React等,支持typescript

安装与配置

npm 安装

npm install minordb

或者 browser 引入

//cdn
<script src="https://cdn.jsdelivr.net/npm/minordb/dist/minordb.min.js"></script>

配置

const dbConfig = {
    name: 'minorDbTest', //数据库名字
    version: 1,         //数据库版本
    //数据库的表配置
    schemas: {  
        user: '++id,username, &passsword, age',
        friends: 'name,age'
        message:'++id,from,to,msg'
    },
};

关于schemas

  • key : user,friends,message 分别对应一个 table(Store) 名字
  • value : 第一个字段是主键(值唯一),其他的是索引(非主键,非索引字段不用配置

    • ++ ++代表是自增的主键(非必须,比如 friends.name非自增)
    • & 代表唯一索引,unique唯一不可重复

动态创建

创建,打开 minorDb

let minorDb = new MinorDB(dbConfig.name, dbConfig.version);
minorDb.open(dbConfig.schemas).then((event) => {}).catch(err => {});

使用说明

下面的语句若没有特殊注明,默认都是自动使用了事务,支持promise(async/await)

  1. insert

    可以直接使用 insert 方法插入一条或多条记录,支持promise

    //一条
    let user={ username: "1", password: '123', age: '1' }
    const result = await minorDb.user.insert(user)
    
    //多条
    const users = [
     { username: "2", password: '123', age: '1' },
     { username: "3", password: '123', age: '1' },
     { username: "4", password: '123', age: '1' }
    ];
    const result1 = await minorDb.user.insert(users)
  2. Find

    可以直接使用 getfind 方法获取一条或多条记录。

   //默认查询所有
   let users= await minorDb.user.find()
    

   //查询id>1的2条数据,升序排序
   //where 条件只能是 主键或索引
   let lUsers =minorDb.user.where({ id: { '>': 1 }).limit(2).sort(asc).find()

   //查询username=2的数据
   let lUsers =minorDb.user.where({ username: { '=': '3' }).find()
  1. update

    可以直接使用 update 方法更新数据库记录。

    const user = { id: 1, username: "1", password: '1234', age: '1' };
    //user数据需要包含主键
    let result = minorDb.user.update(user)

    //如果不包含主键,需要手动设置索引where
    let result = minorDb.user.where({password:{ =:"1"} }).update(user)
  1. remove

    可以直接使用 remove 方法删除数据库记录。

   //根据删除所有
   let result = minorDb.user.remove()
   //复杂的删除
   let result = minorDb.user.where({id:{'>':1,'<':3}}).remove()

遗留问题

where条件只支持 一个主键 或者 一个索引key查询

对于查询< > =条件受到indexDB官方IDBKeyRange 限制 只支持以下的组合形式

   All keys ≥ x	IDBKeyRange.lowerBound(x)
   All keys > x	IDBKeyRange.lowerBound(x, true)
   All keys ≤ y	IDBKeyRange.upperBound(y)
   All keys < y	IDBKeyRange.upperBound(y, true)
   All keys ≥ x && ≤ y	IDBKeyRange.bound(x, y)
   All keys > x &&< y	IDBKeyRange.bound(x, y, true, true)
   All keys > x && ≤ y	IDBKeyRange.bound(x, y, true, false)
   All keys ≥ x &&< y	IDBKeyRange.bound(x, y, false, true)
   The key = z
0.1.2

10 months ago

0.1.3

10 months ago

0.1.0

1 year ago

0.1.1

1 year ago

0.0.15

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago