put
Writes a value at the end of a chain. It must be called after get(), and optionally next().
Parameter | Description |
|---|---|
| The value to store: a string, number, object, |
| Optional. When |
| Optional function that receives |
Without a callback, put() returns a promise that resolves on success and rejects with an Error on failure. You don't have to wait for a response, a put() that's never awaited still writes the value, and a failure won't cause an unhandled rejection.
Examples
// Put a string.
await holster.get("key").put("value")
// Put an object, which is merged with any data already there.
await holster.get("item").put({property: true})
// Put a nested value.
await holster.get("item").next("child").next("name").put("nested")
// With a callback.
holster.get("key").put("value", err => {
if (err) console.error(err)
})
// Handle a failure with await, such as a relay that only accepts signed data.
try {
await holster.get("key").put("value")
} catch (err) {
console.error(err.message)
}
// Or with catch.
holster
.get("key")
.put("value")
.catch(err => console.error(err.message))Objects can contain other objects, and each is stored as its own node. Property names can't be "_", which is reserved for the graph's own metadata, and values can't be undefined, arrays, or functions. To store a list of unique strings, see Arrays.
Sets
Passing true as the set parameter adds the value under a new random key, which works like set() in GunDB. Unlike Arrays, the values don't need to be unique strings, but each one gets a random key instead of a key you choose:
await holster.get("todo").put("Buy milk", true)
await holster.get("todo").put("Walk the dog", true)
const todo = await holster.get("todo")
for (const item of Object.values(todo)) {
console.log(item)
}
// With a callback.
holster.get("todo").put("Water the plants", true, err => {
if (err) console.error(err)
})Since get() returns the full node, iterate over its entries to read the values back.