<commit_before># idb-kv-store [![Build Status](https://travis-ci.org/xuset/idb-kv-store.svg?branch=master)](https://travis-ci.org/xuset/idb-kv-store) [![npm](https://img.shields.io/npm/v/idb-kv-store.svg)](https://npmjs.org/package/idb-kv-store)

Persistent key-value store for web browsers backed by IndexDB

[![Sauce Test Status](https://saucelabs.com/browser-matrix/xuset.svg)](https://saucelabs.com/u/xuset)

idb-kv-store uses asynchronous get/set operations to persist everything in IndexDB. Sometimes IndexDB is needed over something like localStorage due to storage size constraints or simply, localStorage is not available within web workers. Since IndexDB presents a complex api, storing simple key-value pairs can be complicated which this project greatly simplifies. Since everything is persisted to IndexDB, the data you store is available across multiple web sessions and within web workers.

This module can be used with [browserify](http://browserify.org/) or the `idbkvstore.min.js` script can be included.

## Usage

```js
var Store = require('idb-kv-store')
var store = new Store('your stores name')

// Store the value 'def' at key 'abc'
store.set('abc', 'def', function (err) {
  store.get('abc', function (err, value) {
    console.log('key=abc  value=' + value)
  })
})
```

Promises are also supported!

```js
var Store = require('idb-kv-store')
var store = new Store('your stores name')

// Store the value 'def' at key 'abc'
store.set('abc', 'def')
.then(() => store.get('abc'))
.then((value) => console.log('key=abc  value=' + value))
```

## API

### `store = new Store(name, [opts])`

Instantiates a new key-value store. `name` is the name of the database used to persist the data. So multiple Store instances with the same name will be sharing the same data.

`opts` can take the following options:
 * `opts.onready` - A zero argument function to call when the IndexDB database is open
 * `opts.onerror` - This function is called when IndexDB experiences an error. It accepts one error argument. If this is undefined, the error is thrown instead.

### `store.set(key, value, [cb])`

Stores the `value` at `key`; the value can be retrieved through `store.get(key)`. When the store operation completes, `cb` is called with `cb(err)`. `err` is null if the store was successful. If `cb` is undefined then a promise is returned instead. If the key already exists then the old value is replaced with the new one.

### `store.get(key, [cb])`

retrieves the value at `key`. When the value is retrieved, `cb` is called with `cb(err, value)`. If the retrieval was successful then `err` will be null. If `cb` is undefined then a promise is returned instead. If the key does not exist then undefined is returned as the `value`; no error is raised.

### `store.keys([cb])`

Retrieves the list of keys stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned.

### `store.json([cb])`

retrieves the entire key-value store as a json object. When the json representation has been retrieved, `cb` is called with `cb(err, json)`. If `cb` is undefined, then a promise is returned.

## License

MIT. Copyright (c) <NAME>.
<commit_msg>Updated README with new methods<commit_after># idb-kv-store [![Build Status](https://travis-ci.org/xuset/idb-kv-store.svg?branch=master)](https://travis-ci.org/xuset/idb-kv-store) [![npm](https://img.shields.io/npm/v/idb-kv-store.svg)](https://npmjs.org/package/idb-kv-store)

Persistent key-value store for web browsers backed by IndexDB

[![Sauce Test Status](https://saucelabs.com/browser-matrix/xuset.svg)](https://saucelabs.com/u/xuset)

idb-kv-store uses asynchronous get/set operations to persist everything in IndexDB. Sometimes IndexDB is needed over something like localStorage due to storage size constraints or simply, localStorage is not available within web workers. Since IndexDB presents a complex api, storing simple key-value pairs can be complicated which this project greatly simplifies. Since everything is persisted to IndexDB, the data you store is available across multiple web sessions and within web workers.

This module can be used with [browserify](http://browserify.org/) or the `idbkvstore.min.js` script can be included.

## Usage

```js
var Store = require('idb-kv-store')
var store = new Store('your stores name')

// Store the value 'def' at key 'abc'
store.set('abc', 'def', function (err) {
  store.get('abc', function (err, value) {
    console.log('key=abc  value=' + value)
  })
})
```

Promises are also supported!

```js
var Store = require('idb-kv-store')
var store = new Store('your stores name')

// Store the value 'def' at key 'abc'
store.set('abc', 'def')
.then(() => store.get('abc'))
.then((value) => console.log('key=abc  value=' + value))
```

## API

### `store = new Store(name, [opts])`

Instantiates a new key-value store. `name` is the name of the database used to persist the data. So multiple Store instances with the same name will be sharing the same data.

`opts` can take the following options:
 * `opts.onready` - A zero argument function to call when the IndexDB database is open
 * `opts.onerror` - This function is called when IndexDB experiences an error. It accepts one error argument. If this is undefined, the error is thrown instead.

### `store.set(key, value, [cb])`

Stores the `value` at `key`; the value can be retrieved through `store.get(key)`. When the store operation completes, `cb` is called with `cb(err)`. `err` is null if the store was successful. If `cb` is undefined then a promise is returned instead. If the key already exists then the old value is replaced with the new one.

### `store.add(key, value, [cb])`

The same as `store.set(...)` except if the key already exists, an error is returned in the callback.

### `store.get(key, [cb])`

Retrieves the value at `key`. When the value is retrieved, `cb` is called with `cb(err, value)`. If the retrieval was successful then `err` will be null. If `cb` is undefined then a promise is returned instead. If the key does not exist then undefined is returned as the `value`; no error is raised.

### `store.remove(key, [cb])`

Removes the given key from the store and calls `cb(err)` upon completion. `err` is null if the removal was successful. If the key did not exist before the removal, the removal is still considered successful. If `cb` is undefined then a promise is returned.

### `store.clear([cb])`

Removes all entries from the store, and calls `cb(err)` upon completion. `err` is null the clear was successful. If `cb` is undefined then a promise is returned.

### `store.keys([cb])`

Retrieves the list of keys stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned.

### `store.json([cb])`

Retrieves the entire key-value store as a json object. When the json representation has been retrieved, `cb` is called with `cb(err, json)`. If `cb` is undefined, then a promise is returned.

### `store.count([cb])`

Retrieves the number of entries in the store, and calls `cb(err, count)` upon retrieval. `err` is null if the count was successful, in which case `count` will hold the value. If `cb` is undefined, then a promise is returned.

## License

MIT. Copyright (c) <NAME>.

{'fraction_non_alphanumeric': 0.12100863368507606, 'fraction_numerical': 0.0, 'mean_word_length': 4.366176470588235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1384430', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2141, 'n_words': 968}