Coder Social home page Coder Social logo

ts-indexdb's Introduction

ts-indexdb

Build Status

Install

npm install ts-indexdb
yarn add ts-indexdb

Usage

Typescript

import { init, getInstance } from 'ts-indexdb';
export type Rack =  {
    name: string
    id?: number
}

javascript

import TsIndexDb = require('ts-indexdb');

数据库操作方法

注意

  • 当前类为单例模式只要init一次,后面直接getInstance获取实例来操作数据库
  • 操作返回的均为Promis对象
  • js不用加泛型

数据库与表操作

方法 方法名 参数 属性
open_db 打开数据库 -
close_db 关闭数据库 -
delete_db 删除数据库 String name
delete_table 删除表数据 String tableName

查询操作(query)

方法 方法名 参数 属性
queryAll 查询某张表的所有数据(返回具体数组) Object { tableName }
query 查询(返回具体数组) Object { tableName, condition }
query_by_keyValue 查询数据(更具表具体属性)返回具体某一个 Object { tableName, key, value }
query_by_primaryKey 查询数据(主键值) Object { tableName, value }

更新操作(update)

方法 方法名 参数 属性
update 更具条件修改数据(返回修改的数组) Object { tableName, condition, handle }
update_by_primaryKey 修改某条数据(主键)返回修改的对象 Object { tableName, value, handle }

插入操作(insert)

方法 方法名 参数 属性
insert 增加数据 Object { tableName, data(数组或者单独对象) }

删除操作(delete)

方法 方法名 参数 属性
delete 根据条件删除数据(返回删除数组) Object { tableName, condition }
delete_by_primaryKey 删除数据(主键) Object { tableName, value }

例子:

初始化

await init({
    dbName: "books",        // 数据库名称               
    version: 1,             // 版本号                
    tables: [                               
        {
            tableName: "bookrackList",         // 表名         
            option: { keyPath: "id", autoIncrement: true }, // 指明主键为id
            indexs: [    // 数据库索引
                {
                    key: "id",
                    option: {
                        unique: true
                    }
                },
                {
                    key: "name"
                }
            ]
        }
    ]
})

查询

 /**
   * @method 查询某张表的所有数据(返回具体数组)
   * @param {Object}
   *   @property {String} tableName 表名
   */
 await getInstance().queryAll<Rack>({
   tableName: 'bookrackList'
 });


 /**
   * @method 查询(返回具体数组)
   * @param {Object}
   *   @property {String} tableName 表名
   *   @property {Function} condition 查询的条件
   * */
 await getInstance().query<Rack>({
    tableName: 'bookrackList',
    condition: item => item.id === 3
  });

 /**
   * @method 查询数据(更具表具体属性)返回具体某一个
   * @param {Object}
   *   @property {String} tableName 表名
   *   @property {Number|String} key 名
   *   @property {Number|String} value 值
   *
   * */
 await getInstance().query_by_keyValue<Rack>({
    tableName: 'bookrackList',
    key: 'name',
    value: '我师兄实在太稳健了'
  });

 /**
   * @method 查询数据(主键值)
   * @param {Object}
   *   @property {String} tableName 表名
   *   @property {Number|String} value 主键值
   *
   * */ 
 await getInstance().query_by_primaryKey<Rack>({
    tableName: 'bookrackList',
    value: 3
  });

更新

  /**
     * @method 修改数据(返回修改的数组)
     * @param {Object}
     *   @property {String} tableName 表名
     *   @property {Function} condition 查询的条件,遍历,与filter类似
     *      @arg {Object} 每个元素
     *      @return 条件
     *   @property {Function} handle 处理函数,接收本条数据的引用,对其修改
     * */
  await getInstance().update<Rack>({
        tableName: 'bookrackList',
        condition: item => item.id === 8,
        handle: r => {
          r.name = '测试修改';
        }
  })


  /**
  * @method 修改某条数据(主键)返回修改的对象
  * @param {Object}
  *   @property {String} tableName 表名
  *   @property {String\|Number} value 目标主键值
  *   @property {Function} handle 处理函数,接收本条数据的引用,对其修改
  * */
  await getInstance().update<Rack>({
        tableName: 'bookrackList',
        value: 1,
        handle: r => {
          r.name = '测试修改';
        }
  })

增加

  /**
     * @method 增加数据
     * @param {Object}
     *   @property {String} tableName 表名
     *   @property {Object} data 插入的数据
     * */
  await getInstance().insert<Rack>({
    tableName: 'bookrackList',
    data: {
      name: '测试',
    }
  })

删除

/**
  * @method 删除数据(返回删除数组)
  * @param {Object}
  *   @property {String} tableName 表名
  *   @property {Function} condition 查询的条件,遍历,与filter类似
  *      @arg {Object} 每个元素
  *      @return 条件
  * */
await getInstance().delete<Rack>({
  tableName: 'bookrackList',
  condition: (item)=> item.name === '测试',
})


 /**
  * @method 删除数据(主键)
  * @param {Object}
  *   @property {String} tableName 表名
  *   @property {String\|Number} value 目标主键值
  * */
await getInstance().delete_by_primaryKey<Rack>({
  tableName: 'bookrackList',
  value: 4
})

/**
  * @method 删除表数据
  * @param {String}name 数据库名称
  */
await getInstance().delete_table('bookrackList')


/**
  * @method 删除数据库
  * @param {String}name 数据库名称
  */
await getInstance().delete_db('bookrackList')

ts-indexdb's People

Contributors

jay0815 avatar swcbo avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

yunwisdom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.