Skip to content
Kit

Storage

Storage backends decide where Kubb writes generated files. Covers createStorage, the Storage interface, and the fsStorage and memoryStorage backends.

A storage backend decides where Kubb writes generated files: to disk, to memory, or to your own destination. Set it with the config's storage field.

Built-in backends

Both ship from kubb/kit and go straight into storage.

BackendnameWrites
fsStorage()'fs'Each file to its path on disk, creating folders. Skips a file with the same content. The default.
memoryStorage()'memory'A Map in memory, so nothing reaches disk. Suits plugin tests, CI checks, and dry runs.
output.format, output.lint, and output.postGenerate run only when the backend's name is 'fs'.

createStorage

createStorage turns a builder (options: TOptions) => Storage into a factory (options?: TOptions) => Storage. This is the built-in memoryStorage:

memory-storage.ts
import { createStorage } from 'kubb/kit'

export const memoryStorage = createStorage(() => {
  const store = new Map<string, string>()
  return {
    name: 'memory',
    async readItem(key) {
      return store.get(key) ?? null
    },
    async writeItem(key, value) {
      store.set(key, value)
    },
    async removeItem(key) {
      store.delete(key)
    },
    async readKeys(base) {
      const keys = [...store.keys()]
      return base ? keys.filter((k) => k.startsWith(base)) : keys
    },
    async empty(base) {
      for (const k of store.keys()) if (!base || k.startsWith(base)) store.delete(k)
    },
  }
})

Storage interface

Every member is required. Keys are the files' absolute paths. The build result exposes the backend as storage.

MemberParamsReturnsDoes
namestringNames the backend in logs and diagnostics.
readItem()key: stringPromise<string | null>Returns an item's content, or null for a missing key.
writeItem()key: string, value: string, options?: { stored?: string | null }Promise<void>Writes an item. stored is the previous content when Kubb already read it.
removeItem()key: stringPromise<void>Deletes an item. Does nothing for a missing key.
readKeys()base?: stringPromise<string[]>Lists keys, optionally under a prefix.
empty()base?: stringPromise<void>Deletes every item, optionally under a prefix. Without base, the backend decides. See Storage keys.
Kubb skips writeItem for a file whose content did not change, so do not expect one call per generated file.

Less common cases

Storage keys

When you call readKeys() or empty() on a built-in backend, the result depends on the backend.

BackendreadKeys(base?) returnsempty() without base
fsStorage()Paths relative to base, or to the working directoryDoes nothing
memoryStorage()The stored keys (absolute paths) that start with baseDeletes every entry

In writeItem(), stored is the previous content when Kubb already read it, so the backend can skip a second read.