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
Directive | Type | Description |
---|---|---|
id | string | (Required) Specify the map id to use when generating a cloud resource. |
secret | boolean | (Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets |
environment_variables | Object | (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
Operation | Notes1 |
---|---|
Map.clear() | Fully supported |
Map.delete() | Fully supported |
Map.entries() | Unlike standard Map s, the iteration order is undefined |
Map.get() | Fully supported |
Map.has() | Fully supported |
Map.keys() | Unlike standard Map s, the iteration order is undefined |
Map.set() | Fully supported |
Map.values() | Unlike standard Map s, 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
- AWS
- GCP
Filesystem
When specified on an fs
import, persist the object to a blob or filesystem-like store. Must use the promise interface.
- Javascript
- Typescript
/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
const fs = require('fs/promises')
// or
/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
const fs = require('fs').promises
/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
import fs = require('fs/promises');
// or
/**
* @klotho::persist {
* id = "my_cloudfs"
* }
*/
import * as fs from 'fs/promises';
Directives
Directive | Type | Description |
---|---|---|
id | string | (Required) Specify the map id to use when generating a cloud resource. |
secret | boolean | (Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets |
environment_variables | Object | (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
Operation | Notes |
---|---|
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
- AWS
- GCP
Uses S3 as the backing store.
AWS Directives
note
This capability does not currently have any AWS-specific directives.
caution
GCP hasn't been updated to v1 yet, so this is just a projection of what it may be.
Uses Cloud Storage as the backing store.
GCP Directives
note
This capability does not currently have any GCP-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.
- Javascript
- Typescript
/**
* @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")
}
/**
* @klotho::persist {
* secret = true
* }
*/
import fs = require('fs/promises');
async function readKey() {
return await fs.readFile("mysecret.key")
}
Directives
Directive | Type | Description |
---|---|---|
id | string | (Required) Specify the map id to use when generating a cloud resource. |
secret | boolean | (Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets |
environment_variables | Object | (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
Operation | Notes |
---|---|
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
- AWS
- GCP
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.
GCP secrets has not been implemented.
GCP Directives
note
This capability does not currently have any GCP-specific directives.
ORM
Similar to Key-Value, but persists data to a relational database.
Library: Sequelize
- Javascript
- Typescript
const { Sequelize, Model, DataTypes } = require('sequelize');
/**
* @klotho::persist {
* id = "sequelize"
* }
*/
const sequelize = new Sequelize(`sqlite::memory:`);
import { Sequelize, Model, DataTypes } from 'sequelize';
/**
* @klotho::persist {
* id = "sequelize"
* }
*/
const sequelize = new Sequelize(`sqlite::memory:`);
Library: typeorm
- Javascript
- Typescript
const { DataSource } = require("typeorm")
/** @klotho::persist {
* id = "typeormDB"
* }
*/
const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
entities: [User],
synchronize: true,
logging: false,
})
import { DataSource } from "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
Directive | Type | Description |
---|---|---|
id | string | (Required) Specify the map id to use when generating a cloud resource. |
secret | boolean | (Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets |
environment_variables | Object | (Optional) Specified on an annotation to generate infrastructure and inject its characteristics as environment variables. For more information, see the persist concepts page. |
Providers
- AWS
- GCP
Redis
Persist data to a redis node.
Library: Node Redis
- Javascript
- Typescript
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'
}
],
})
import { createClient } from 'redis';
/**
* @klotho::persist {
* id = "redis"
* }
*/
const client = createClient();
Interact with a redis cluster:
import { createCluster } from 'redis';
/**
* @klotho::persist {
* id = "redis"
* }
*/
const client = createCluster({
rootNodes:[
{
url: 'redis://127.0.0.1:8001'
},
{
url: 'redis://127.0.0.1:8002'
},
{
url: 'redis://127.0.0.1:8003'
}
],
})
Directives
Directive | Type | Description |
---|---|---|
id | string | (Required) Specify the map id to use when generating a cloud resource. |
secret | boolean | (Optional) Only available on persist kind filesystem, default = false. Set to true to enable storing secrets |
environment_variables | Object | (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
- AWS
- GCP
- 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.
GCP secrets has not been implemented.
GCP Directives
note
This capability does not currently have any GCP-specific directives.