Key/Value
Nitric provides a simple way to work with key/value stores in your applications. Key/value stores are perfect for storing and retrieving data using unique keys, with fast access times and simple data structures.
Quick Start
Here's a minimal example to get you started:
import { kv } from '@nitric/sdk'const store = kv('my-store').allow('get', 'set')// Store a valueawait store.set('user-1', { name: 'John', age: 30 })// Retrieve a valueconst user = await store.get('user-1')
Core Concepts
Key/Value Store
A key/value store is a database that stores data as key-value pairs. Each value is accessed using a unique key, making lookups fast and efficient.
Permissions
Key/value stores require explicit permissions for operations:
get
: Read values or list keysset
: Create or update valuesdelete
: Remove values
Keys and Values
- Keys: Unique strings that identify values
- Values: Any JSON-serializable data (objects, arrays, strings, numbers, etc.)
Common Operations
Setting Values
const store = kv('my-store').allow('set')// Create or update a valueawait store.set('user-1', { name: 'John', age: 30 })
Getting Values
const store = kv('my-store').allow('get')const user = await store.get('user-1')
Deleting Values
const store = kv('my-store').allow('delete')await store.delete('user-1')
Listing Keys
const store = kv('my-store').allow('get')// List all keysfor await (const key of store.keys()) {console.log(key)}// List keys with prefixfor await (const key of store.keys('user-')) {console.log(key)}
Cloud Provider Support
Each cloud provider comes with a set of default services used when deploying resources. You can find the default services for each cloud provider below.
Key value stores are mapped to DynamoDB in AWS. For more details about the AWS implementation, including configuration options and limitations, see the AWS Key/Value documentation.
When running locally, key value stores use a local implementation for development and testing.