Key Value Store

The store module allows you to persist any valid JSON data type across your function runs:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null
import { store } from "napkin";

async store.get(key)

Returns: Object ({ data: ... })

Retrieve the value associated with the key key . The value is returned in a dictionary with a single key, data

await store.put("myKey", 42);
const { data } = await store.get("myKey");
console.log(data);
// 42

async store.has(key)

Returns: Boolean

Return whether or not the key-value store contains a key-value pair with key key

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

await store.delete('myKey')
exists = await store.has('myKey')
console.log(exists)
// false

async store.put(key, value)

Returns: null

Save the given key-value pair in the Store. If the key-value pair already exists in the store, the value is updated accordingly.

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

async store.size()

Returns: Number

Return the current size in bytes of the key-value store. Maximum size is 400 KB.

const size = await store.size();

console.log(size);
// 128

async store.delete(key)

Delete the value stored under the key key

await store.put('myKey', 42)
let exists = await store.has('myKey')
console.log(exists)
// true

await store.delete('myKey')
exists = await store.has('myKey'))
console.log(exists)
// false

const { data } = await store.get('myKey')
console.log(data)
// null