Datastore
datastore Datastore.
A local key–value store for objects. Create instances with createDatastore.
Table of contents
Methods
Methods
clear
clear(): Promise<void>
Deletes this datastore and all of its entries asynchronously. Resolves when the operation finishes.
Returns
Promise<void>
clearSync
clearSync(): void
Deletes this datastore and all of its entries synchronously. Prefer the async clear on the UI thread and anywhere blocking must be avoided.
Returns
void
contains
contains(key: string): Promise<boolean>
Returns whether key exists. The promise resolves to true or false.
Parameters
key: Key to test.
Returns
Promise<boolean>
containsSync
containsSync(key: string): boolean
Same as contains, but synchronous.
Parameters
key: Key to test.
Returns
boolean
true if the key exists, otherwise false.
edit
edit(block: (editor: DatastoreEditor) => void): Promise<void>
Apply several writes in one batch (more efficient than many separate set calls).
Example
const { createDatastore } = require('datastore');
// Create a datastore named example.test
const datastore = createDatastore('example.test');
async function main() {
await datastore.edit(editor => {
editor.set('boolKey', true)
.set('arrayKey', [1, '2', false])
.remove('versions');
});
}
main().catch(console.error);Parameters
block: Callback receiving aDatastoreEditor. Chainset/removeoneditor.
Returns
Promise<void>
editSync
editSync(block: (editor: DatastoreEditor) => void): void
Same as edit, but synchronous.
Parameters
block: Callback receiving aDatastoreEditor.
Returns
void
get
get<T>(key: string): Promise<T | undefined>
Reads key asynchronously. If the key is missing, the promise resolves to undefined.
Type parameters
T: Expected value type.
Parameters
key: Key to read.
Returns
Promise<T | undefined>
getSync
getSync<T>(key: string): T | undefined
Reads key synchronously. Prefer async get on UI threads.
Type parameters
T: Expected value type.
Parameters
key: Key to read.
Returns
T | undefined
remove
remove<T>(key: string): Promise<T | undefined>
Removes key asynchronously and resolves to the previous value, or undefined if it did not exist.
Type parameters
T: Value type for the removed entry.
Parameters
key: Key to remove.
Returns
Promise<T | undefined>
removeSync
removeSync<T>(key: string): T | undefined
Removes key synchronously and returns the previous value, or undefined if it did not exist. Prefer async remove on UI threads.
Type parameters
T: Value type for the removed entry.
Parameters
key: Key to remove.
Returns
T | undefined
set
set<T>(key: string, value: T): Promise<void>
Stores value under key asynchronously. Resolves when persistence completes.
Values are serialized with JSON.stringify, so value must be JSON-serializable.
Type parameters
T: Value type to store.
Parameters
key: Key.value: Value to store.
Returns
Promise<void>
setSync
setSync<T>(key: string, value: T): void
Stores value under key synchronously. Prefer async set on UI threads.
Values are serialized with JSON.stringify, so value must be JSON-serializable.
Type parameters
T: Value type to store.
Parameters
key: Key.value: Value to store.
Returns
void
