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";

Note: Make sure you await any async function calls to ensure they complete!

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.getAll()

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

Retrieve all key-value pairs from the store, along with the store's total size in bytes. The return value is a mapping with two keys, data and size.

await store.put("candy", "yum")
const { data, size } = await store.getAll()
console.log(data)
// { candy: 'yum' } 
console.log(size)
// 338

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