Skip to main content

Persist

@klotho::persist

Specified on a resource to persist it to the cloud.

We support several kinds of persistence, each shown below.

Key-Value

When specified on a Map, persist the object in a key-value store:

/**
* @klotho::persist {
* id = "my_store"
* }
*/
const store = new Map();

When using the map, you must treat all calls as async and await the results (this will no-op when running locally).

await store.set("mykey", "myvalue");
const value = await store.get("mykey");

Directives

DirectiveTypeDescription
idstring(Required) Specify the map id to use when generating a cloud resource.
secretboolean(Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets
environment_variablesObject(Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page.

Supported operations

OperationNotes1
Map.clear()Fully supported
Map.delete()Fully supported
Map.entries()Unlike standard Maps, the iteration order is undefined
Map.get()Fully supported
Map.has()Fully supported
Map.keys()Unlike standard Maps, the iteration order is undefined
Map.set()Fully supported
Map.values()Unlike standard Maps, the iteration order is undefined

1: As noted above, all operations are async and must be awaited.

caution

Attempting to invoke any unsupported methods or access properties on a Map annotated with @klotho::persist will fail at run-time in the Klotho-compiled version of your application.

Providers

Uses DynamoDB as the backing store.

AWS Directives

DirectiveTypeDescription
ttlstringConfigure the entries with a TTL and clean up expired entries.

Filesystem

When specified on an fs import, persist the object to a blob or filesystem-like store. Must use the promise interface.

/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
const fs = require('fs/promises')

// or

/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
const fs = require('fs').promises

Directives

DirectiveTypeDescription
idstring(Required) Specify the map id to use when generating a cloud resource.
secretboolean(Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets
environment_variablesObject(Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page.

Supported operations

OperationNotes
fsPromises.readFile()Fully supported
fsPromises.writeFile()Fully supported. If flag is w+, the underlying S3 object will be publicly visible, and this call will return a URL for it.
fsPromises.readdir()Fully supported
fsPromises.access()Fully supported
caution

Attempting to invoke any unsupported methods or access properties on an fsPromises annotated with @klotho::persist will fail at run-time in the Klotho-compiled version of your application.

Providers

Uses S3 as the backing store.

AWS Directives

note

This capability does not currently have any AWS-specific directives.

Secret

Similar to Filesystem, but use a store suitable for secrets. Set the secret = true to enable. Only supports fs.readFile with static string paths.

/**
* @klotho::persist {
* secret = true
* }
*/
const fs = require('fs/promises')
// or
const fs = require('fs').promises
// or
const {fs: promises} = require('fs')

async function readKey() {
return await fs.readFile("mysecret.key")
}

Directives

DirectiveTypeDescription
idstring(Required) Specify the map id to use when generating a cloud resource.
secretboolean(Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets
environment_variablesObject(Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page.
note

All filesystem-persist objects use the same underlying storage; they are not separated by id. This mirrors how fs/promises works when run locally (without being Klotho-compiled), where it would simply be backed by your local filesystem.

note

If you do not specify secret = true, this is just a standard filesystem object as described above.

Supported operations

OperationNotes
fsPromises.readFile()No options supported (only path). Always returns a Buffer
caution

Attempting to invoke any unsupported methods or access properties on an fsPromises annotated with @klotho::persist will fail at run-time in the Klotho-compiled version of your application.

Providers

Uses Secrets Manager as the backing store.

After compilation, you may copy your secrets into the compiled output root to have your pulumi up upload the secret.

AWS Directives

note

This capability does not currently have any AWS-specific directives.

ORM

Similar to Key-Value, but persists data to a relational database.

Library: Sequelize

const { Sequelize, Model, DataTypes } = require('sequelize');

/**
* @klotho::persist {
* id = "sequelize"
* }
*/
const sequelize = new Sequelize(`sqlite::memory:`);

Library: typeorm

const { DataSource } = require("typeorm")

/** @klotho::persist {
* id = "typeormDB"
* }
*/
const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
entities: [User],
synchronize: true,
logging: false,
})

Supported operations

All operations are supported.

Directives

DirectiveTypeDescription
idstring(Required) Specify the map id to use when generating a cloud resource.
secretboolean(Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets
environment_variablesObject(Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page.

Providers

Uses RDS as the backing store.

AWS Directives

note

This capability does not currently have any AWS-specific directives.

Redis

Persist data to a redis node.

Library: Node Redis

Interact with a redis node:
const redis = require("redis");

/**
* @klotho::persist {
* id = "redis"
* }
*/
const client = redis.createClient();

Interact with a redis cluster:

const redis = require("redis");

/**
* @klotho::persist {
* id = "redis"
* }
*/
const client = redis.createCluster({
rootNodes:[
{
url: 'redis://127.0.0.1:8001'
},
{
url: 'redis://127.0.0.1:8002'
},
{
url: 'redis://127.0.0.1:8003'
}
],
})

Directives

DirectiveTypeDescription
idstring(Required) Specify the map id to use when generating a cloud resource.
secretboolean(Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets
environment_variablesObject(Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page.

Supported operations

All operations are supported.

Providers

  • Uses Elasticache as the backing store for single nodes. Elasticache is only compatible with the Redis client.
  • Uses MemoryDB as the backing store for clusters. MemoryDB is only compatible with the RedisCluster client.
note

The createCluster client targeting MemoryDB is currently not supported in us-east-1 or us-west-2

AWS Directives

note

This capability does not currently have any AWS-specific directives.