-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add documentation and final touches
- Loading branch information
Showing
5 changed files
with
127 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {LevelDB} from "react-native-leveldb"; | ||
|
||
export function leveldbExample(): boolean { | ||
// 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); // should log: 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. | ||
|
||
return readStringValue == 'value' && readBufferValue.length == 1 && readBufferValue[0] == 654321; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters