Storage
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.
| Backend | name | Writes |
|---|---|---|
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 for a backend named 'fs', such as fsStorage(), because Kubb treats only that name as writing to disk. An S3 or other remote backend skips them.createStorage
createStorage turns a builder (options: TOptions) => Storage into a factory (options?: TOptions) => Storage. This is a minimal in-memory backend:
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()]
if (!base) return keys
const dir = `${base.replace(/\/+$/, '')}/`
return keys.filter((k) => k.startsWith(dir)).map((k) => k.slice(dir.length))
},
async empty(base) {
const dir = base && `${base.replace(/\/+$/, '')}/`
for (const k of store.keys()) if (!dir || k.startsWith(dir)) store.delete(k)
},
}
})Storage interface
Every member is required. Keys are the files' absolute paths. The build result exposes the backend as storage.
| Member | Params | Returns | Does |
|---|---|---|---|
name | string | Names the backend in logs and diagnostics. | |
readItem() | key: string | Promise<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: string | Promise<void> | Deletes an item. Does nothing for a missing key. |
readKeys() | base?: string | Promise<string[]> | Lists keys. With base, lists the keys inside that folder as posix paths relative to it. See Storage keys. |
empty() | base?: string | Promise<void> | Deletes every item, or only the items inside the folder base. Without base, the backend decides. See Storage keys. |
writeItem for a file whose content did not change, so do not expect one call per generated file.Less common cases
Storage keys
readKeys(base) returns the keys inside the folder base as posix paths relative to it, such as models/pet.ts, on every backend. A key under a sibling folder, such as ${base}2/, is not inside base. empty(base) removes the same keys. A custom backend must return the same shape.
Without base, the result depends on the backend.
| Backend | readKeys() returns | empty() |
|---|---|---|
fsStorage() | Paths relative to the working directory | Does nothing |
memoryStorage() | The stored keys (absolute paths) | Deletes every entry |
In writeItem(), stored is the previous content when Kubb already read it, so the backend can skip a second read.
