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

@michaelray/indexdb

v0.0.3

Published

typescript indexDb封装

Downloads

2

Readme

@michaelray/indexdb

Install

npm install @michaelray/indexdb
yarn add @michaelray/indexdb

Usage

Typescript

import {
	init,
	getInstance
} from '@michaelray/indexdb';

export type Rack = {
	name: string
	id?: number
}

数据库操作方法

注意

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

数据库与表操作

方法|方法名|参数|属性 --|:--|:--:|:-- openDb|打开数据库|无|- closeDb|关闭数据库|无|- deleteDb|删除数据库|String|name deleteTable|删除表数据|String|tableName

查询操作(query)

方法|方法名|参数|属性 --|:--|:--:|:-- queryAll|查询某张表的所有数据(返回具体数组)|Object|{ tableName } query|查询(返回具体数组)|Object|{ tableName, condition } queryByKeyValue|查询数据(更具表具体属性)返回具体某一个|Object|{ tableName, key, value } queryByPrimaryKey|查询数据(主键值)|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 } deleteByPrimaryKey|删除数据(主键) |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().queryByKeyValue<Rack>({
   tableName: 'bookrackList',
   key: 'name',
   value: '我师兄实在太稳健了'
});

/**
* @method 查询数据(主键值)
* @param {Object}
*   @property {String} tableName 表名
*   @property {Number|String} value 主键值
*
* */
await getInstance().queryByPrimaryKey<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_by_primaryKey<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().deleteByPrimaryKey<Rack>({
	tableName: 'bookrackList',
	value: 4
})

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


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