0.0.2 • Published 3 years ago

@styleofpicasso/indexdb v0.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago
1. 安装 与 引入
// 安装 
npm install @styleofpicasso/indexdb --save-dev
或
yarn add @styleofpicasso/indexdb

// 引入
import IdxDB from '@styleofpicasso/client';
2. 使用案例
// 使用案例
import IdxDB from '@styleofpicasso/client';

const indexdb = new IdxDB(databaseName, storeName, keyPath, [{name, prop, {unique: false}}])

indexbd.create(() => {
  console.log('创建成功')
})
3. 详细说明
  • 安装
npm install @styleofpicasso/indexdb --save
  • 引用
import IdxDB from '@styleofpicasso/indexdb'

/**
 * @description 创建(获取)服务的实例 
 * @param databasename  数据库名称
 * @param storename  仓库表名称
 * @param keyPath  主键
 * @param conf 生成索引的配置信息 包含: {name, prop, conf}  name: 索引名称, prop: 索引对应保存的属性 (name 与 prop 一致), conf: 是否可重复 {unique: false}, 不可
 * /

const indexdb = new IdxDB(databasename, storename, keyPath, conf)
// 例如:
const indexdb = new IdxDB('ithinkdt, 'jt', 'id', {name: 'name', prop: 'name', conf: {unique: false}})
  • 功能
|—— 创建 或 打开数据库  // create()
|—— 新增数据  // add()
|—— 更新数据  // update()
|—— 读取数据  // read()
|—— 遍历所有数据  // readAll()
|—— 删除数据  // remove()
|—— 清空数据  // clear()
|—— 根据索引读取数据  // readByIndex()
|—— 关闭数据连接  // close()
|—— 删除仓库表  // deleteObjectStore()
|—— 删除数据库  // deleteDatabase()
  • API 具体的使用
  1. create(succFn, failFn)
/**
 * @description 新建 或 打开数据库 (后续对表的操作都是再打开数据库之后)
 * @param succFn 成功之后的回调
 * @param failFn 失败的回调
 * /

// 打开数据库
indexdb.create(() => {
    console.log('打开成功')
    // 新增
    indexdb.add({id: 1, name: '张三', ageL 10})
    // 更新
    indexdb.update({id: 1, name: 'lisi', age: 10}, {id: 2, name: 'wangwu', age: 18})
    // 读取
    indexdb.read([1, 2], (id, data) => {
        console.log(id, data)
    }, (result) => {
        console.log(result)
    })
    // 遍历读取
    indexdb.readAll((result) => {
        console.log(result)
    })
    // 通过索引读取数据
    indexdb.readByIndex('name', ['lisi', 'wangwu'], (name, data) => {
        console.log(name, data)
    }, (result) => {
        console.log(result)
    })
    // 删除数据
    indexdb.remove(1)
    // 清空表数据
    indexdb.clear(() => {
        console.log('清空成功')
    })
    // 断开连接
    indexdb.close()
})

// 删除仓库表
indexdb.deleteObjectStore(() => {
    console.log('删除成功')
})

// 删除数据库
indexdb.deleteDatabase(() => {
    console.log('删除成功')
})
  1. add(data, cb)
/**
 * @description 新增数据,如果主键相同会报错
 * @param data: array||object,
 * @param cb: 回调 (当data不存在时调用)
 * /
// 使用见 1
  1. update(data, cb)
/**
 * @description 更新数据
 * @param data: array || object
 * @param cb: 回调 (当data不存在时调用)
 * /
使用见 1
  1. read(list, progressFn, complateFn)
/**
 * @description 读取数据
 * @param list: 需要读取的数据的主键值的集合
 * @param progressFn: 每个主键值读取成功的回调 参数:主键值, 读取的结果
 * @param complateFn: 当所有的都读取完的回调  参数: 读取的结果的集合
 * /
使用见 1
  1. readAll(complateFn)
/**
 * @description 遍历读取数据
 * @parame complateFn: 读取完数据的回调
 * /
使用见 1
  1. readByIndex(indexName, list, progressFn, complateFn)
/**
 * @description 通过索引读取数据
 * @param indexName: 当前使用的索引名称
 * @param list: 索引名称值的集合
 * @param progressFn: 每个索引值读取数据成功的回调 参数:索引值, 读取的数据
 * @param complateFn: 所有索引读取完的回调  参数: 读取完数据的结果集合
 * /
使用见 1
  1. remove(data, cb)
/**
 * @description 删除数据
 * @param data: Array || object  需要删除的主键值的集合
 * @param cb: 回调  (当data不存在时调用)
 * /
使用见 1
  1. clear(cb)
/**
 * @description 清空表数据
 * @parame cb: 当清除成功之后的回调
 * /
使用见 1
  1. close()
/**
 * @description 断开连接
 * /
使用见 1
  1. deleteObjectStore(cb) (关系: 他与create() 方法平级)
/ **
  * @description 删除仓库表
  * param cb 删除成功的回到函数
  * /
indexdb.deleteObjectStore(() => {
    console.log('删除成功')
})

也可参阅 1
  1. deleteDatabase(succ, fail) (关系: 它与create() 方法平级)
/**
 * @description 删除数据库
 * @param succ: 成功的回到函数
 * @param dail: 失败的回到
 * /
indexdb.deleteDatabase(() => {
    console.log('删除成功')
})
也可参阅 1