Socket
Book a DemoInstallSign in
Socket

ts-indexdb

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-indexdb

typescript indexDb封装

latest
Source
npmnpm
Version
0.0.8
Version published
Maintainers
2
Created
Source

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删除数据库Stringname
delete_table删除表数据StringtableName

查询操作(query)

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

更新操作(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 查询满足key条件的个数(返回满足条件的数字个数)
    * @param {Object}
    *   @property {String} tableName 表名
    *   @property {Number|String} key 查询的key
    *   @property {Object} countCondition 查询条件
  * */
 /** countCondition传入方式 key 必须为已经简历索引的字段
  *  key ≥ x	            {key: 'gt' rangeValue: [x]}
     key > x	            {key: 'gt' rangeValue: [x, true]}
     key ≤ y	            {key: 'lt' rangeValue: [y]}
     key < y	            {key: 'lt' rangeValue: [y, true]}
     key ≥ x && ≤ y	    {key: 'between' rangeValue: [x, y]}
     key > x &&< y	    {key: 'between' rangeValue: [x, y, true, true]}
     key > x && ≤ y	    {key: 'between' rangeValue: [x, y, true, false]}
     key ≥ x &&< y	    {key: 'between' rangeValue: [x, y, false, true]}
     key = z	            {key: 'equal' rangeValue: [z]}
  */
 await getInstance().count<Rack>({
   tableName: 'bookrackList',
   key: 'createdTime',
   countCondition: {
     type: 'between',
     rangeValue:[1676627113088,new Date().getTime()]
   }
 })

更新

  /**
     * @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 = '测试修改';
          return r;
        }
  })


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

增加

  /**
     * @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')

Keywords

indexdb

FAQs

Package last updated on 12 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.