Skip to main content

Data persistency

By default data is stored in memory. This means the data is flushed when the server restarts. To persist your data, provide the connectionString config setting for your JSON file(s) or MongoDB database.

connectionString accepts either a string shorthand (simple, great for quick setups) or a DataSourceConfig object (structured, full TypeScript autocomplete).

How Temba determines the storage type from a string:

Temba inspects the connectionString value to decide which adapter to use:

  1. MongoDB: Starts with "mongodb" → MongoDB database
  2. Single JSON file: Ends with ".json" → Single file for all resources
  3. Directory of JSON files: Matches /^[a-zA-Z0-9_-]+$/ → Folder with one file per resource
  4. Fallback: Any other value → In-memory storage

Both the JSON file and folder paths are resolved relative to the directory from which you start the server.

JSON file

All resources are stored in a single JSON file with the structure:

{
"movies": [{ "id": "1", "title": "..." }, ...],
"actors": [{ "id": "2", "name": "..." }, ...]
}

The file is created automatically when the first resource is added (POST).

String shorthand
const server = await create({
connectionString: 'data.json',
})
Object form
const server = await create({
connectionString: { type: 'file', filename: 'data.json' },
})

Folder of JSON files

To store each resource in its own JSON file, use a folder name. Each resource will be saved in a separate JSON file inside the folder, created on demand when data for that resource is first added. For example:

  • data/movies.json — Contains an array of movie objects
  • data/actors.json — Contains an array of actor objects

Valid folder names for the string shorthand: Only alphanumeric characters, hyphens, and underscores are allowed (e.g., "data", "my_data", "api-db").

String shorthand
const server = await create({
connectionString: 'data',
})
Object form
const server = await create({
connectionString: { type: 'folder', folder: 'data' },
})

MongoDB

String shorthand
const server = await create({
connectionString: 'mongodb://localhost:27017/myDatabase',
})
Object form
const server = await create({
connectionString: { type: 'mongodb', uri: 'mongodb://localhost:27017/myDatabase' },
})

For every resource you use in your requests, a collection is created in the database. However, not until you actually create a resource with a POST.

The object form also lets you configure additional MongoDB connection options without embedding them in the URI:

MongoDB with extra options
const server = await create({
connectionString: {
type: 'mongodb',
uri: 'mongodb://localhost:27017/myDatabase',
username: 'alice',
password: 's3cr3t',
authSource: 'admin',
tls: true,
tlsCAFile: '/certs/ca.pem',
maxPoolSize: 10,
serverSelectionTimeoutMS: 5000,
},
})

The full set of available MongoDB options:

OptionTypeDescription
uristringMongoDB connection URI (required)
usernamestringUsername for authentication
passwordstringPassword for authentication
authSourcestringAuthentication database (defaults to 'admin')
tlsbooleanEnable TLS/SSL
tlsCAFilestringPath to the CA certificate file
tlsCertificateKeyFilestringPath to the client certificate/key file
tlsAllowInvalidCertificatesbooleanAllow invalid TLS certificates (not recommended for production)
maxPoolSizenumberMaximum connections in the connection pool
minPoolSizenumberMinimum connections in the connection pool
serverSelectionTimeoutMSnumberTimeout (ms) for server selection
connectTimeoutMSnumberTimeout (ms) for initial connection
replicaSetstringReplica set name
readPreferencestringRead preference (e.g. 'primary', 'secondary', 'nearest')
writeConcernstringWrite concern (e.g. 'majority')