rn-leveldb
v3.11.0
Published
Superfast React Native bindings for LevelDB
Readme
rn-leveldb
Sponsored by GotChoices.org
Superfast React Native bindings for LevelDB:
- 2-7x faster than AsyncStorage or react-native-sqlite-storage - try the benchmarks under example/!
- completely synchronous, blocking API (even on slow devices, a single read or write takes 0.1ms)
- use it with Flatbuffers to turbo charge your app - support for binary data via ArrayBuffers
What this package is
rn-leveldb is a fork of react-native-leveldb published under a new npm name.
Key fork feature:
- Atomic multi-key writes via native LevelDB
WriteBatchexposed to JS/TS asLevelDBWriteBatch+db.write(batch).
Installation
yarn add rn-leveldb
cd ios && pod installUsage
import {LevelDB} from "rn-leveldb";
// Open a potentially new database.
const name = 'example.db';
const createIfMissing = true;
const errorIfExists = false;
const db = new LevelDB(name, createIfMissing, errorIfExists);
// Insert something into the database. Note that the key and the
// value can either be strings or ArrayBuffers.
// Strings are read & written in utf8.
db.put('key', 'value');
// You can also use ArrayBuffers as input, containing binary data.
const key = new Uint8Array([1, 2, 3]);
const value = new Uint32Array([654321]);
db.put(key.buffer, value.buffer);
// Get values as string or as an ArrayBuffer (useful for binary data).
const readStringValue = db.getStr('key');
const readBufferValue = new Uint32Array(db.getBuf(key.buffer)!);
console.log(readStringValue, readBufferValue); // logs: value [654321]
// Iterate over a range of values (here, from key "key" to the end.)
let iter = db.newIterator();
for (iter.seek('key'); iter.valid(); iter.next()) {
// There are also *Buf version to access iterators' keys & values.
console.log(`iterating: "${iter.keyStr()}" / "${iter.valueStr()}"`);
}
// You need to close iterators when you are done with them.
// Iterators will throw an error if used after this.
iter.close();
db.close(); // Same for databases.
Atomic batch writes
import {LevelDB, LevelDBWriteBatch} from "rn-leveldb";
const db = new LevelDB('example.db', true, false);
const batch = new LevelDBWriteBatch();
batch.put('k1', 'v1');
batch.put('k2', 'v2');
db.write(batch); // atomic at the LevelDB layer
batch.close();
db.close();Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
Example app / smoke tests
The included example app runs basic smoke tests (and benchmarks) on startup.
See CONTRIBUTING.md for the exact commands to run the example on iOS/Android.
License
MIT
