Farm.js

Storage

Farm.js gives server code two kinds of storage: a key/value store through getStorage(), and SQL tables with typed models through the Farming Labs ORM.

Every store is a named mount with its own driver. A driver can point at a service you run, such as PostgreSQL, Redis, or S3, or at a database that Farm.js Infra provisions for the project. Your server code calls the same functions either way.

Choose the right storage

What you needUseSection
Cache entries, flags, settings, counters, or JSONgetStorage(name) on a KV mountKey/value storage
Files or values addressed by one keygetStorage(name) on s3Storage(...) or a blob driverSupported KV drivers
Models, relations, joins, and filters in your own DB@farming-labs/orm with a PostgreSQL, MySQL, or SQLite driverSQL databases and models
The same models without running a databasefarmStorage({ type: "sql", schema }) and getDatabase(name)Hosted by Farm.js Infra
Rate limiting shared across production instancesAn atomic adapter such as redisRateLimitStorage(...)Atomic rate-limit storage
Models owned by a schema-backed Farm.js integrationFarm.js's integration ORM through ctx.args.dbDatabase and ORM Clients
Better Auth users, accounts, and sessionsBetter Auth's own database adapterDatabase and ORM Clients

A KV store does not become a relational database because its driver is PostgreSQL. postgresStorage({ url }) keeps key/value records in one table. Use the ORM when you need columns, relations, or filters.

At a glance

One config can mix the two kinds and both hosting models:

farm.config.ts
import { defineConfig } from "@farm.js/core";
import { farmStorage, redisStorage, sqliteStorage } from "@farm.js/core/storage";
import { schema } from "./src/lib/schema";

export default defineConfig({
  storage: {
    mounts: {
      settings: sqliteStorage({ path: "./.farm/storage/settings.sqlite" }),
      cache: redisStorage({ url: process.env.REDIS_URL! }),
      app: farmStorage({ type: "sql", schema }),
    },
  },
});
  • settings is KV in a local SQLite file. Good for a single long-running server.
  • cache is KV in a Redis you run.
  • app is a SQL database hosted by Farm.js Infra, with typed models from schema.

Server code reads each mount by name:

src/app/api/projects/route.ts
import { createEndpoint } from "@farm.js/core/api";
import { getDatabase, getStorage } from "@farm.js/core/storage";

export const GET = createEndpoint("/api/projects", { method: "GET" }, async () => {
  const theme = await getStorage("settings").getItem<string>("theme");

  const db = await getDatabase("app");
  const projects = await db.project.findMany({
    select: { id: true, name: true },
  });

  return { theme, projects };
});

Key/value storage

Farm.js's getStorage() is a key/value API. It stores each value under a string key and exposes operations such as getItem, setItem, getKeys, and removeItem.

Use it for caches, feature flags, application settings, idempotency records, checkpoints, and object-backed values. Do not use it for users, accounts, products, or other records that need model fields, relations, joins, or typed filters; use SQL for those. Request-rate enforcement uses a separate atomic counter contract.

Create a KV client

src/lib/storage.ts
import { sqliteStorage } from "@farm.js/core/storage";

export const appStorage = sqliteStorage({
  path: "./.farm/storage/app.sqlite",
  tableName: "app_store",
});

Local SQLite and the other self-configured KV helpers return ready-to-use clients, so application code can import and call them directly. Mounting the clients is useful when you want one central configuration and a stable name that any server-side module can retrieve later.

Register named mounts

farm.config.ts
import { defineConfig } from "@farm.js/core";
import { appStorage } from "./src/lib/storage";

export default defineConfig({
  storage: {
    mounts: {
      app: appStorage,
    },
  },
});

Each property under mounts is a KV namespace:

  • app is an application-defined name. Farm.js does not attach special behavior to it.
  • You can add names such as cache, sessions, uploads, or webhooks when the application needs more stores.

A mount name is not a URL, route, database table, or filesystem directory. It is the lookup name that connects configuration to later server-side calls.

getStorage(name) always returns a namespaced key/value view. When the name matches a configured mount, operations use that mount's driver. When no matching mount exists, Farm.js uses the same namespace on the root store instead. The default root store is in memory, so a missing or misspelled production mount does not throw but may store data only for the lifetime of one process. Rate-limit middleware is an exception: enforcement requires its dedicated atomic storage contract and never treats a generic KV mount as atomic.

A name made only of separators or spaces, such as " ", "/", or "::", names no namespace, so getStorage() throws for it instead of returning the root store. Call getStorage() without an argument for the root store.

The development server and a built app both apply storage from farm.config before they serve the first request. A production build bundles the drivers the config names (such as local or sqlite), and the server disposes the configured drivers when it shuts down.

Use a mounted store

Call getStorage("app") from server-side code to retrieve the client registered under the app mount.

It is not limited to API routes. You can call it from any module that executes on the server after Farm.js initializes storage. Prefer resolving the store inside the request handler, action, job, or lifecycle function that uses it so the configured storage is ready before lookup.

src/app/api/settings/route.ts
import { createEndpoint } from "@farm.js/core/api";
import { getStorage } from "@farm.js/core/storage";
import { z } from "zod";

type AppSettings = {
  theme: "light" | "dark";
  productName: string;
};

const settingsSchema = z.object({
  theme: z.enum(["light", "dark"]),
  productName: z.string().min(1),
});

const defaultSettings: AppSettings = {
  theme: "dark",
  productName: "Farm.js App",
};

export const GET = createEndpoint("/api/settings", { method: "GET" }, async () => {
  const appStore = getStorage("app");
  const settings = await appStore.getItem<AppSettings>("settings");

  return {
    settings: settings ?? defaultSettings,
  };
});

export const POST = createEndpoint(
  "/api/settings",
  {
    method: "POST",
    body: settingsSchema,
  },
  async (ctx) => {
    const appStore = getStorage("app");
    await appStore.setItem("settings", ctx.body);

    return {
      saved: true,
      settings: ctx.body,
    };
  },
);

The same mount can be used from other server-only application surfaces:

SurfaceExample use
API routesPersist settings, idempotency keys, webhook state, or cached provider responses.
MiddlewareRead feature flags, request policy, tenant configuration, or custom counters.
Server components and pagesLoad data needed during server rendering.
Server actions and server functionsSave form state or invalidate application-owned cached values.
Integration server handlersRead application key/value data that is separate from an integration schema.
Jobs and workflowsStore checkpoints, deduplication markers, or lightweight progress state.

Do not call getStorage() from a client component or browser bundle. Expose the required operation through an API route, server action, or server function instead.

KV operations

Mounted stores expose the standard Farm.js KV API:

const appStore = getStorage("app");

await appStore.setItem("feature:checkout", { enabled: true });

const feature = await appStore.getItem<{ enabled: boolean }>("feature:checkout");
const exists = await appStore.hasItem("feature:checkout");
const keys = await appStore.getKeys("feature:");

await appStore.removeItem("feature:checkout");
await appStore.clear();

clear() only clears the selected namespace. Calling getStorage("app").clear() does not clear ratelimit, cache, or another mounted store.

Use descriptive keys such as settings:global, tenant:acme:flags, or webhook:event_123. Namespaced keys make inspection and targeted cleanup easier.

Choosing mounts and drivers

Mount names describe the responsibility; drivers decide where the values live:

Use caseSuggested mountTypical driver
Application settings and durable JSON stateapp or settingsSQLite, Postgres, MySQL, libSQL, or farmStorage({ type: "kv" })
Shared cache entriescacheRedis, Upstash Redis, or memory for local development
Rate-limit countersDedicated rate-limit adapterredisRateLimitStorage(...) or another atomic store
Idempotency and webhook deduplicationwebhooks or idempotencyRedis or a durable SQL store
Bucket-backed objects or metadatauploadsS3 or Vercel Blob
Tests and disposable local stateAny descriptive nameMemory or local filesystem

The mount names are conventions chosen by the application. Two mounts may use the same driver type while remaining isolated, or use different drivers based on durability and latency requirements. A mount named ratelimit is still generic KV storage and is not accepted as proof of atomic increment support.

The full list of KV drivers is in Supported KV drivers.

SQL databases and models

Use SQL when records have named fields, relations, unique constraints, or filters. Farm.js uses @farming-labs/orm for this: you write one schema, and the same typed query API runs on PostgreSQL, MySQL, SQLite, or a database hosted by Farm.js Infra. Only the driver line changes.

pnpm add @farming-labs/orm

Define a schema

A schema is plain TypeScript: models, fields, and relations.

src/lib/schema.ts
import { belongsTo, datetime, defineSchema, hasMany, id, model, string } from "@farming-labs/orm";

export const schema = defineSchema({
  user: model({
    table: "users",
    fields: {
      id: id(),
      email: string().unique(),
      name: string(),
    },
    relations: {
      projects: hasMany("project", { foreignKey: "ownerId" }),
    },
  }),
  project: model({
    table: "projects",
    fields: {
      id: id(),
      name: string(),
      ownerId: string().map("owner_id").references("user.id"),
      createdAt: datetime().map("created_at").defaultNow(),
    },
    relations: {
      owner: belongsTo("user", { foreignKey: "ownerId" }),
    },
  }),
});
  • Each key under defineSchema becomes a model on the client: db.user, db.project.
  • table and .map() set the physical table and column names. Field keys stay camelCase in your code.
  • Other field types include integer(), boolean(), json(), decimal(), bigint(), and enumeration([...]). Chain .nullable(), .default(value), or .unique() on any of them.

Pick a driver

DatabaseInstallDriver
PostgreSQL through a pg pool@farming-labs/orm-sql pgcreatePgPoolDriver(pool)
SQLite through node:sqlite or better-sqlite3@farming-labs/orm-sqlcreateSqliteDriver(db)
MySQL through a mysql2 pool@farming-labs/orm-runtime mysql2createOrmFromRuntime
Prisma, Drizzle, Kysely, D1, MongoDB, Redis@farming-labs/orm-runtimecreateOrmFromRuntime
Hosted by Farm.js InfraNothing extra; @farm.js/core includes itgetDatabase(name)

Pass the first two to createOrm({ schema, driver }). createOrmFromRuntime and getDatabase return the client directly; await both.

createOrmFromRuntime inspects the client you pass and picks the matching driver, so you can keep the client your app already has.

Connect your own PostgreSQL

pnpm add @farming-labs/orm @farming-labs/orm-sql pg
src/lib/db.ts
import { createOrm } from "@farming-labs/orm";
import { createPgPoolDriver } from "@farming-labs/orm-sql";
import { Pool } from "pg";
import { schema } from "./schema";

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

export const db = createOrm({ schema, driver: createPgPoolDriver(pool) });

The module creates one pool per server process. Import db only from server code so the connection string stays off the client.

Switching databases changes only the driver:

src/lib/db.ts (SQLite)
import { createOrm } from "@farming-labs/orm";
import { createSqliteDriver } from "@farming-labs/orm-sql";
import { DatabaseSync } from "node:sqlite";
import { schema } from "./schema";

export const db = createOrm({
  schema,
  driver: createSqliteDriver(new DatabaseSync("./.farm/app.sqlite")),
});
src/lib/db.ts (MySQL)
import { createOrmFromRuntime } from "@farming-labs/orm-runtime";
import { createPool } from "mysql2/promise";
import { schema } from "./schema";

export const db = await createOrmFromRuntime({
  schema,
  client: createPool(process.env.DATABASE_URL!),
});

Query models

The query API is the same on every driver, including the hosted one:

src/app/api/signup/route.ts
import { createEndpoint } from "@farm.js/core/api";
import { z } from "zod";
import { db } from "../../../lib/db";

const signup = z.object({
  email: z.string().email(),
  name: z.string().min(1),
});

export const POST = createEndpoint("/api/signup", { method: "POST", body: signup }, async (ctx) => {
  const user = await db.transaction(async (tx) => {
    const user = await tx.user.create({ data: ctx.body });
    await tx.project.create({
      data: { name: "My first project", ownerId: user.id },
    });
    return user;
  });

  const projects = await db.project.findMany({
    where: { ownerId: user.id },
    select: { id: true, name: true, owner: { select: { email: true } } },
  });

  return { user, projects };
});
  • findMany, findFirst, findUnique, and count read. findUnique takes a unique field such as email.
  • create, createMany, update, updateMany, upsert, delete, and deleteMany write.
  • select picks columns and follows relations, as owner does above. The result type follows the select.
  • transaction commits every write inside it together, or none of them. A second signup with the same email fails on the unique email column and creates no project.

Create the tables

Defining a schema does not create tables. renderSafeSql turns the schema into create table if not exists statements for a dialect:

import { renderSafeSql } from "@farming-labs/orm";
import { schema } from "./src/lib/schema";

const sql = renderSafeSql(schema, { dialect: "postgres" });

Use "postgres", "mysql", or "sqlite". Run the output with your migration tool, or wire that tool into farm migrate as shown in Schema generation and migrations. The ORM can also generate Prisma and Drizzle schemas from the same declaration. For a hosted mount, see Migrate a hosted database.

Hosted by Farm.js Infra

Don't want to run a database? Farm.js Infra provisions one for each mount you declare, per project and environment. You get:

  • One database per mount, holding one kind of data. A key-value mount serves getStorage(name); a SQL mount serves getDatabase(name).
  • Typed models. Pass your Farming Labs ORM schema to the mount and getDatabase(name) returns db.<model> with the same API as above.
  • No credentials in your app config. Infra issues short-lived database credentials and Farm.js refreshes them.
  • A Storage view in the dashboard. Browse keys, tables, rows, and foreign keys.

Hosted databases run on libSQL and speak SQLite. Setting managed: true on postgresStorage or redisStorage provisions a hosted SQLite database; it does not start a PostgreSQL server or Redis. Use your own connection when you need PostgreSQL-specific SQL, Redis commands, or provider options such as Redis TTL.

Declare hosted mounts

Declare a mount in farm.config.ts. @farm.js/core includes the client it uses, so there is nothing else to install. Each named resource belongs to one project and environment.

farm.config.ts
import { defineConfig } from "@farm.js/core";
import { farmStorage, postgresStorage, redisStorage, sqliteStorage } from "@farm.js/core/storage";

export default defineConfig({
  storage: {
    mounts: {
      drafts: farmStorage({ type: "sql" }),
      settings: farmStorage({ type: "kv" }),
      orders: postgresStorage({ managed: true }),
      cache: redisStorage({ managed: true }),
      scratch: sqliteStorage({ path: "./.farm/scratch.sqlite" }),
    },
  },
});

Every hosted mount holds one kind of data:

MountKindRead it with
farmStorage({ type: "kv" })Key-valuegetStorage(name)
farmStorage({ type: "sql" }), optionally with schemaSQLgetDatabase(name)
A database driver with managed: true: sqliteStorage, postgresStorage, mysqlStorage, libsqlStorage, pgliteStorage, planetscaleStorageSQLgetDatabase(name)
Any other driver with managed: true: redisStorage, upstashStorage, vercelKVStorage, s3Storage, { driver: "cloudflare-kv-binding", managed: true }, and so onKey-valuegetStorage(name)

A driver helper takes its kind from the driver, so orders above is SQL and cache is key-value. Calling the other API throws and names the right one: getDatabase("cache") says to use getStorage("cache"). A schema needs a SQL mount, so redisStorage({ managed: true, schema }) throws. In this example scratch has no managed flag, so it stays a local SQLite file.

farmStorage() without type is deprecated. It still works and accepts both APIs, and farm storage plan warns about it. Pick the kind by how you read the mount:

BeforeAfter
farmStorage() read with getStorage()farmStorage({ type: "kv" })
farmStorage() or farmStorage({ schema }) read with getDatabase()farmStorage({ type: "sql", schema })

A mount that used both APIs needs two mounts: give the key-value one its own namespace and copy its existing keys into it.

The mount name is the default resource name. Set namespace to reuse a resource, for example redisStorage({ managed: true, namespace: "sessions" }). Mounts that share a resource must hold the same kind: farm storage plan refuses a key-value mount and a SQL mount on one database. Omit managed or set it to false on a provider helper to keep using its own driver. When switching an existing external store to Farm.js hosting, migrate its data separately. Pass native connection options only when using the native driver. For a custom driver instance or an existing database client, keep using driverStorage(driver) or databaseStorage(db).

Provision and connect

  1. List the resources your config declares. This runs locally:

    pnpm exec farm storage plan
  2. In app.farmjs.dev, create a Farm.js API key scoped to the project and environment, with the databases:provision permission. Set it as FARMJS_API_KEY in your shell or the app's .env.local.

  3. Create the databases:

    pnpm exec farm storage apply

    Reapplying reuses each database and records the mount names and drivers for each namespace.

  4. Give the deployed server a separate key with the databases:connect permission.

The deployed server reads these variables:

VariableWhen to set it
FARMJS_API_KEYAlways. A databases:connect key on the server, a databases:provision key for farm storage apply.
FARMJS_PROJECT_IDWhen the key covers several projects.
FARMJS_ENVIRONMENTWhen the key covers several environments: development, staging, or production.
FARMJS_API_URLWhen you run your own Infra instance. Defaults to https://app.farmjs.dev.

Infra returns the database's HTTPS address and a short-lived credential. The application sends SQL directly to that address.

Query a hosted database

Use getDatabase from server-side code after Farm.js initializes storage. The mount owns its optional Farming Labs ORM schema. With a schema on the mount, getDatabase(name) returns its typed models. Without a schema it returns the raw SQL client. Use getDatabase(name, { raw: true }) for migrations on either mount.

farm.config.ts
import { defineConfig } from "@farm.js/core";
import { farmStorage } from "@farm.js/core/storage";
import { defineSchema, id, model, string } from "@farming-labs/orm";

const schema = defineSchema({
  draft: model({ table: "drafts", fields: { id: id(), title: string() } }),
});

export default defineConfig({
  storage: { mounts: { drafts: farmStorage({ type: "sql", schema }) } },
});
// In a server handler, after migrating the drafts table.
import { getDatabase } from "@farm.js/core/storage";

export async function listDrafts() {
  const db = await getDatabase("drafts");
  return db.draft.findMany({ select: { id: true, title: true } });
}

Install @farming-labs/orm in the app for the schema helpers. Dev, build, and pnpm exec farm generate infer model types from the config into src/farm.d.ts. Managed driver helpers also accept the schema, e.g. postgresStorage({ managed: true, schema }). Each mount owns its model view, even when two mounts share a hosted namespace. Existing calls to getDatabase(name, { schema }) can still select a schema for one call.

Farm.js opens the connection on first use and refreshes credentials. Farm.js owns the returned client's lifetime and closes it at shutdown. Use db.transaction(async (tx) => …) for ORM writes that must commit together. Nested ORM transactions are unsupported. A key-value mount keeps its keys in the reserved table _farm_kv. getDatabase(name) throws for a misspelled name, a key-value mount, and a mount that Farm.js Infra does not host; for your own database, use the ORM client from Connect your own PostgreSQL.

Migrate a hosted database

Provisioning creates an empty database, and farm schema migrate covers plugin-owned tables, not storage mounts. Create your tables with the mount's raw SQL client, from server code that runs after Farm.js initializes storage, such as an admin-only API route:

src/lib/migrate.ts
import { renderSafeSql } from "@farming-labs/orm";
import { getDatabase } from "@farm.js/core/storage";
import { schema } from "./schema";

export async function migrateApp() {
  const sql = await getDatabase("app", { raw: true });
  await sql.executeMultiple(renderSafeSql(schema, { dialect: "sqlite" }));
}

renderSafeSql emits create table if not exists, so running it again is harmless. It does not alter existing tables. The raw client is a libSQL client, so execute, batch, and executeMultiple are available.

Rename or remove a hosted mount

To rename the mount and keep its data, declare editor: farmStorage({ type: "sql", namespace: "drafts" }) and call getDatabase("editor"). A resource name accepts 1–63 lowercase letters, digits, and hyphens, starting with a letter. Removing a mount from config keeps the database. Disconnecting its Farm.js project schedules deletion.

Browse storage in Farm.js Infra

Open Storage in the Farm.js Infra sidebar. On a phone, open the navigation menu first. Choose a project and environment. If you open Storage from a project, the dashboard carries that project and environment into the list. You can bookmark your selection.

If the environment is empty, choose Set up a database. Once it has a database, choose Set up another database below the list. Both show a mount declaration, provisioning key instructions, and the CLI commands to apply it.

Members can inspect mount details. Organization owners and admins can open a Ready resource to browse its data.

  • KV shows keys and values. Filter by key prefix and open a key to inspect its value.
  • Tables shows rows, column definitions, and foreign keys. Select a related table to inspect its rows.
  • Details lists the mount names and configured drivers and shows how to connect from your app.

A key-value mount shows KV. A SQL mount, from farmStorage({ type: "sql" }) or a database driver, shows Tables. Only a mount from a deprecated untyped farmStorage() shows both. Back to storage returns to the selected project and environment.

Existing SQLite mounts

Existing sqliteStorage({ managed: true }) mounts use the same hosted resources. Switch to farmStorage({ type: "sql" }) with the same mount name to keep the data. For a mount with managed: { name: "drafts" }, use farmStorage({ type: "sql", namespace: "drafts" }).

Atomic rate-limit storage

The built-in rate limiter uses an atomic increment(key, windowMs) contract. Its default adapter is atomic inside one process, which is useful for local development and a single long-running server. Production deployments with multiple processes or regions should pass a shared atomic adapter explicitly:

pnpm add @farm.js/cache-redis ioredis
src/app/api/middleware.ts
import { redisRateLimitStorage } from "@farm.js/cache-redis";
import { middleware } from "@farm.js/core/middleware";
import Redis from "ioredis";

const rateLimits = redisRateLimitStorage({
  client: () => new Redis(process.env.REDIS_URL!),
  prefix: "storefront-ratelimit",
});

export default middleware().rateLimit({
  requests: 100,
  window: "1m",
  storage: rateLimits,
});

The default bucket is the limit's policy (requests and window), the caller's address, and the route the router serves: the path decoded once, without the base path, locale prefix, or a trailing slash, so /api/login/ and /api/%6Cogin count against the same bucket as /api/login. Two limits on one path keep their own counts and windows; two with the same policy count a request once. A built app takes that address from the server's own connection, as the development server does. Behind a reverse proxy or a platform load balancer that connection belongs to the proxy, so set server.trustProxy to key on the last x-forwarded-for hop, the address the proxy appended, instead. When the runtime reports no address at all, the limiter refuses rather than putting every caller in one bucket, so set trustProxy or supply a keyGenerator that uses an identity you do have. Do not fall back to a constant such as "unknown": that is the shared bucket the limiter refuses, and one caller then exhausts the window for everyone.

The Redis adapter uses one Lua operation to increment the counter and establish its expiry. A generic get() followed by set() adapter is rejected because concurrent requests can read the same count and overwrite each other. Limited responses include Retry-After; all responses include the RateLimit and RateLimit-Policy fields from the current IETF draft.

Choose a trusted rate-limit identity when possible. An authenticated user or tenant ID is usually more useful than an IP address for application-level limits:

export default middleware().rateLimit({
  requests: 20,
  window: "1m",
  keyGenerator: (ctx) => {
    const userId = ctx.data.get("userId") as string | undefined;
    if (userId) return `user:${userId}`;
    const address = ctx.request.socket?.remoteAddress;
    // No identity and no address means this request cannot be bucketed on its
    // own, so reject it rather than sharing a bucket with every other caller.
    if (!address) throw new Error("Rate limit identity is unavailable for this request.");
    return `ip:${address}`;
  },
});

Adapters may implement get(key) so getRateLimitStatus() can inspect a counter. Enforcement itself only depends on atomic increment().

memoryRateLimitStorage({ maxEntries }) caps active keys (100,000 by default) and rejects a new key when that capacity is full; it never evicts an active counter to admit another key. Expired windows are reclaimed when needed, and repeated capacity rejections skip cleanup scans until a window could expire. Its increment() and get() results are snapshots: editing a returned counter does not change the stored count or expiry. Use a shared adapter for enforcement across processes or regions.

Supported KV drivers

Farm.js supports KV storage at three levels:

  1. Farm.js convenience helpers for common databases, caches, and object stores.
  2. Direct driver configuration through driver: "name".
  3. Existing or custom unstorage driver instances through databaseStorage(), driverStorage(), or defineStorageClient().

The root store and every mount can use a different supported driver.

Farm.js KV helpers

Import these helpers from @farm.js/core/storage:

HelperDriverCommon use
farmStorage({ type })Hosted by Farm.js InfraA key-value ("kv") or SQL ("sql") mount, with no database to run.
memoryStorage()memoryTests, local development, and disposable process-local state.
localStorage({ base })Farm.js alias for fs-liteLocal persistent files, development caches, and self-hosted single-instance apps.
sqliteStorage({ path, tableName })sqlite / node-sqliteDurable local application state with no external service.
postgresStorage(...) / pgStorage(...)postgresShared durable key/value state backed by Postgres.
mysqlStorage(...) / mysql2Storage(...)mysqlShared durable key/value state backed by MySQL.
pgliteStorage(...)pgliteEmbedded Postgres-compatible storage.
planetscaleStorage(...)planetscalePlanetScale-backed durable storage.
libsqlStorage(...)libsqlLocal or remote libSQL/Turso-compatible storage.
redisStorage(...)redisShared caches, sessions, counters, and short-lived state.
upstashStorage(...)upstashHTTP-based Redis storage for serverless and edge-style deployments.
mongodbStorage(...)mongodbDurable document-backed key/value storage.
s3Storage(...)s3S3-compatible object-backed values.
netlifyBlobsStorage(...)netlify-blobsNamed or deploy-scoped Netlify Blob stores.
vercelKVStorage(...)vercel-kvVercel KV/Redis-backed shared state.
vercelBlobStorage(...)vercel-blobPublic Vercel Blob-backed values.
createStorageClient({ driver, ...options })Any supported nameCreate a reusable client from direct driver configuration.
databaseStorage(database, { tableName })db0Reuse an existing db0 database instance.
driverStorage(driver)CustomWrap an existing unstorage driver or driver factory.
defineStorageClient(factory)CustomLazily create a custom synchronous or asynchronous driver.

Passing managed: true to a provider helper switches it to the hosted database described in Hosted by Farm.js Infra. Database drivers become SQL mounts; every other driver becomes a key-value mount.

Configure drivers directly

Helpers and direct configuration produce the same Farm.js storage client behavior. Driver options can be written inline or inside options. If both are present, values inside options take precedence.

farm.config.ts
import { defineConfig } from "@farm.js/core";

export default defineConfig({
  storage: {
    driver: "sqlite",
    path: "./.farm/storage/root.sqlite",
    tableName: "farm_root",
    mounts: {
      cache: {
        driver: "redis",
        url: process.env.REDIS_URL!,
        ttl: 300,
      },
      uploads: {
        driver: "s3",
        endpoint: "https://s3.us-east-1.amazonaws.com",
        region: "us-east-1",
        bucket: process.env.S3_BUCKET!,
        accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
      },
    },
  },
});

In this example, getStorage() uses SQLite, getStorage("cache") uses Redis, and getStorage("uploads") uses S3.

Database-backed KV drivers

Farm.js resolves these names through db0 and exposes the result as key/value storage:

Accepted driver namesDatabase connectorImportant configuration
sqlite, node-sqliteNode SQLitepath or name, plus optional tableName.
sqlite3sqlite3path or name, plus optional tableName.
better-sqlite3better-sqlite3path or name, plus optional tableName.
postgres, postgresql, pgPostgreSQLurl or standard pg client options, plus optional tableName.
mysql, mysql2MySQLStandard mysql2 connection options, plus optional tableName.
pglitePGlitePGlite connector options, plus optional tableName.
planetscalePlanetScalePlanetScale client options, plus optional tableName.
libsql, libsql-nodelibSQL Node clienturl, optional authToken, and optional tableName.
libsql-httplibSQL HTTP clienturl, optional authToken, and optional tableName.
db0Existing db0 databasePass { database, tableName? } inside options.

These drivers create or use a key/value table for Farm.js KV storage. They do not expose tables, models, joins, or arbitrary SQL through getStorage(). For models on the same database, connect the ORM as shown in SQL databases and models.

Complete built-in driver set

Farm.js also accepts the built-in driver names exported by its installed unstorage version. Kebab-case and camel-case names shown on the same row are aliases.

CategoryAccepted driver namesIntended use
Process memorymemoryFast, process-local, non-durable state.
Disabled storagenullDiscard writes and always read empty state. Useful for explicitly disabling a store.
Layered storageoverlayCombine multiple driver layers. Requires configured driver instances in layers.
Bounded memorylru-cache, lruCacheProcess-local cache with size and TTL controls.
Filesystemlocal, fs-lite, fsLite, fsPersistent files. local is Farm.js's shorthand for fs-lite; fs adds watcher support.
Remote HTTPhttpRead and write through an HTTP storage endpoint.
GitHub contentgithubRead-only, cached repository content access.
RedisredisShared cache, counters, sessions, and short-lived state.
Upstash RedisupstashREST-based Redis for serverless runtimes.
MongoDBmongodbMongoDB collection-backed values.
S3-compatible objectss3AWS S3, Cloudflare R2's S3 API, and compatible providers.
UploadThinguploadthingUploadThing-backed object values.
Netlifynetlify-blobs, netlifyBlobsNamed or deploy-scoped Netlify Blob storage.
Vercel KVvercel-kv, vercelKVVercel-managed Redis-compatible key/value storage.
Vercel Blobvercel-blob, vercelBlobVercel Blob object storage.
Vercel Runtime Cachevercel-runtime-cache, vercelRuntimeCacheEphemeral regional runtime cache with TTL and tags.
Cloudflare KV bindingcloudflare-kv-binding, cloudflareKVBindingA KV namespace bound to a Cloudflare runtime.
Cloudflare KV HTTPcloudflare-kv-http, cloudflareKVHttpCloudflare KV through the REST API.
Cloudflare R2 bindingcloudflare-r2-binding, cloudflareR2BindingAn R2 bucket bound to a Cloudflare runtime.
Azure App Configurationazure-app-configuration, azureAppConfigurationAzure App Configuration key/value data.
Azure Cosmos DBazure-cosmos, azureCosmosCosmos DB container-backed values.
Azure Key Vaultazure-key-vault, azureKeyVaultSecret-backed values in Azure Key Vault.
Azure Blob Storageazure-storage-blob, azureStorageBlobAzure container-backed object values.
Azure Table Storageazure-storage-table, azureStorageTableAzure table-backed key/value state.
Deno KVdeno-kv, denoKVDeno runtime KV storage.
Deno KV from Nodedeno-kv-node, denoKVNodeDeno KV through the Node-compatible client.
IndexedDBindexedbIndexedDB-backed values in a compatible browser runtime.
Web Storagelocalstorage, session-storage, sessionStorageBrowser local or session storage.
Capacitorcapacitor-preferences, capacitorPreferencesCapacitor Preferences-backed mobile storage.

Farm.js KV storage normally initializes on the server. Browser-only drivers require a compatible custom runtime and should not be used as a reason to call getStorage() from client components.

Most remote and platform drivers load an optional provider SDK. Install the package required by the selected driver, such as ioredis, mongodb, @upstash/redis, @vercel/kv, @vercel/blob, @netlify/blobs, aws4fetch, uploadthing, the relevant Azure SDK, @deno/kv, idb-keyval, or lru-cache. Database aliases may likewise require sqlite3, better-sqlite3, mysql2, @electric-sql/pglite, @planetscale/database, or @libsql/client; the sqlite alias uses Node's built-in node:sqlite. Cloudflare binding drivers instead require the corresponding runtime binding.

Custom drivers

Use driverStorage() when a compatible driver already exists:

import { driverStorage } from "@farm.js/core/storage";
import createCustomDriver from "my-unstorage-driver";

export const customStorage = driverStorage(() =>
  createCustomDriver({
    endpoint: process.env.CUSTOM_STORAGE_URL!,
  }),
);

The wrapped driver can be mounted or used as the root Farm.js storage client just like a built-in helper.

Integration database clients

The current beta config groups two different inputs under the storage key:

  • storage.driver, storage.mounts, and a Farm.js storage client configure the KV system used by getStorage().
  • A raw database or ORM object passed to storage.client is the runtime client for schema-backed integrations. That object is not returned by getStorage(name) or getDatabase(name).
farm.config.ts
import { defineConfig } from "@farm.js/core";
import { DatabaseSync } from "node:sqlite";

const db = new DatabaseSync("farm.sqlite");

export default defineConfig({
  storage: {
    client: db,
  },
});

In this example, the raw SQLite database is available only to integrations. Because no KV driver or Farm.js storage client is configured, getStorage() continues to use the default in-memory KV store. A raw PostgreSQL pool passed the same way does not make the KV store durable either.

When an integration also defines a schema, Farm.js exposes a typed ORM layer at ctx.args.db. The integration does not need to know whether the app passed SQLite, PostgreSQL, or another supported runtime client.

ConfigUse it for
sqliteStorage(...), redisStorage(...)Farm.js KV storage read with getStorage(name).
farmStorage({ schema })Hosted KV and SQL models read with getDatabase(name).
storage.mountsMultiple named stores.
storage.client with a Farm.js storage clientReuse a created Farm.js storage client as the root store.
storage.client with a DB/runtime objectGive integrations a runtime database client.

Inside an integration, use ctx.args.db for ORM-style queries on the integration's own models, and ctx.args.storage.getClient() only when provider-specific operations need the raw configured client. See Database and ORM Clients for integration schemas, PostgreSQL pools, Better Auth ownership, and migrations.

Production notes

  • Use a shared durable KV driver for production state that must survive restarts or be visible across instances. A local SQLite file or the filesystem is durable only on a single long-running server.
  • Keep memory KV storage for tests and explicitly disposable local state.
  • Create one ORM client per process and reuse it. Do not open a new pool per request.
  • Create tables before code that queries them. Provisioning a hosted database does not run migrations.
  • Keep DATABASE_URL, provider tokens, and FARMJS_API_KEY in server-only environment variables.
  • Do not assume that a PostgreSQL-backed KV helper provides relational database access.
  • Configure KV mounts, ORM clients, and integration database clients independently when an application needs more than one.