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

@indexeddb-tools/indexeddb

v1.0.0

Published

操作本地数据库的核心方法。

Downloads

77

Readme

indexeddb

操作本地数据库的核心方法。

使用方法

安装

npm install @indexeddb-tools/indexeddb

或者

yarn add @indexeddb-tools/indexeddb

初始化

import { initDatabase } from '@indexeddb-tools/indexeddb';

/**
 * name            连接的数据库名
 * version         数据库版本号
 * callbackObject  数据库创建或连接成功后的回调函数
 */
initDatabase(dbName, dbVersion, {
  success(IDBEvent) {},      // 数据库连接成功的回调函数
  error(IDBEvent) {},        // 数据库连接失败的回调函数
  upgradeneeded(IDBEvent) {} // 数据库首次创建成功的回调函数
});

删除数据库

import { deleteDatabase } from '@indexeddb-tools/indexeddb';

deleteDatabase(dbName);

关闭数据库

initDatabase(dbName, dbVersion, {
  success(event) {
    this.close();
  }
});

创建一个objectStore来存储数据

import { initDatabase } from '@indexeddb-tools/indexeddb';

initDatabase(name, version, {
  upgradeneeded(IDBEvent) {
    this.createObjectStore(objectStoreName, keyPath, [
      {
        name: name,
        index: index,
        // https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex
        options: { unique: false }
      }
    ]);
  }
});

判断是否有objectStore

initDatabase(dbName, dbVersion, {
  success(IDBEvent) {
    this.hasObjectStore(objectStoreName);
  }
});

删除objectStore

initDatabase(dbName, dbVersion, {
  success(IDBEvent) {
    this.deleteObjectStore(objectStoreName);
  }
});

获取objectStore

initDatabase(dbName, dbVersion, {
  success(IDBEvent) {
    const store = this.getObjectStore(objectStoreName, true);
  }
});

添加数据

value的类型可以是Array或者Object。

store.add(value);

更新数据

value的类型可以是Array或者Object。

store.put(value);

查找数据

根据主键的值来查找数据。

initDatabase(dbName, dbVersion, {
  success(IDBEvent) {
    const store = this.getObjectStore(objectStoreName, true);
    
    store.get(query, function(event) {
      const result = event.target.result;
    });
  }
});

删除数据

删除数据是根据主键的值来删除的。value可以是string、number或Array类型。

store.delete(value);

清除objectStore内的数据

store.clear();

通过游标查询

range用来选择范围。如果想要查询所有数据,可以不传range。
value可以是string、number或Array类型。也可以传递一个IDBKeyRange对象。

store.cursor(indexName, callback);

// 或

store.cursor(indexName, range, callback);

使用方法:

initDatabase(dbName, dbVersion, {
  success(IDBEvent) {
    const store = this.getObjectStore(objectStoreName);

    store.cursor('index', function(event) {
      const result = event.target.value;
      const queryResult = [];

      if (result) {
        queryResult.push(result.value);
        result.continue();
      }
    });

    // 或

    store.cursor('index', 'name', function(event) {
      const result = event.target.value;
      const queryResult = [];

      if (result) {
        queryResult.push(result.value);
        result.continue();
      }
    });
  }
});

根据字符串返回游标查询的范围:

  • > 5:大于
  • >= 5:大于等于
  • < 5:小于
  • <= 5:小于等于
  • [5, 8]>= 5 && <= 8:闭区间(5 <= x <= 8)
  • (5, 8)> 5 && < 8:开区间(5 < x < 8)