Reference

API reference

Every export of persistence-save, with its options and errors.

Everything is exported from the package root, under ESM and CommonJS, with types. The two WebAssembly assets are subpath exports; see Web assets.

createPersistence<TState, TSnapshot>(config)

Returns a Persistence<TState>. Synchronous; opens nothing until first use.

PersistenceConfig<TState, TSnapshot extends VersionedSnapshot>

OptionTypeDefaultMeaning
dbNamestringrequiredSQLite database name. Namespace it (com.example.game_v1); a bare app name collides with other apps on the same web origin.
dbVersionnumber1Capacitor SQLite connection version.
snapshotVersionnumberrequiredThe version serialize emits and the migration walker's target.
serialize(state: TState) => TSnapshotrequiredMust return a non-array object with version === snapshotVersion; checked before SQLite opens.
deserialize(snap: TSnapshot) => TStaterequiredTurns a migrated snapshot into live state.
migrationsMigrationTablerequiredN → N+1 steps keyed by source version. See Migrations.
encryptedbooleanfalseSQLCipher on iOS and Android with a per-install key. Ignored on web.
encryptionKeyPreferencestring<dbName>.dbKeyPreferences key for the SQLCipher passphrase; set only to keep an existing key.
seedPhraseOf(state: TState) => string() => ''Value stored in each row's seedPhrase column.
maxSavesnumber100Row cap; the oldest rows past it are pruned after each save.
listLimitnumber50Row cap on list(), clamped to 1–500.
maxSnapshotBytesnumber2 MiBSnapshot JSON cap in UTF-8 bytes, on write and on read (before parsing).
wasmAssetsPathstring/assetsWhere the web build serves the sql.js WebAssembly.
initializeSchema(connection: SQLiteDBConnection) => Promise<void>noneIdempotent setup for your own tables, run on the same connection after the saves table exists.

Persistence<TState>

MethodReturnsBehaviour
save(name, state)Promise<void>UPSERT by name, prune past maxSaves, flush the web store, then resolve. Names are capped at 256 characters.
load(id)Promise<SaveRecord<TState> | null>null when no row; throws CorruptSaveError when the row cannot be parsed, migrated or deserialized.
list()Promise<SaveRecord<TState>[]>Newest first, at most listLimit; corrupt rows are skipped and logged individually.
delete(id)Promise<void>No-op when absent.
withConnection(operation, fallback)Promise<TResult>Runs your operation on the live connection; returns fallback where SQLite is unavailable.
flush()Promise<void>Flushes writes made through withConnection to the web store.
close()Promise<void>Closes the connection; later calls reopen it.

SaveRecord<TState>

{ id: number; name: string; seedPhrase: string; savedAt: string; snapshot: TState }, where savedAt is an ISO timestamp and snapshot is already migrated and deserialized.

CorruptSaveError

Thrown by load(). Has recordId: number and cause: string.

Migrations

  • migrateSnapshot(snapshot, targetVersion, migrations) walks a snapshot to targetVersion and returns the migrated copy; the input is never mutated.
  • type SnapshotMigration = (snap: Record<string, unknown>) => Record<string, unknown>
  • type MigrationTable = Record<number, SnapshotMigration>
  • interface VersionedSnapshot { version: number }
  • SnapshotVersionError has version (where the walk failed) and targetVersion.

createAutoSaveScheduler<TData>(options)

OptionTypeDefaultMeaning
provider() => TData | nullrequiredThe data to write, or null to skip (menu, mid-load).
save(data: TData) => Promise<void>requiredThe write, for example (d) => saves.save('AutoSave', d).
debounceMsnumber400Window that coalesces bursts.
onError(err: unknown) => voidconsole.errorBackground save failures; awaited flush() and flushPending() failures reject instead.

The returned AutoSaveScheduler:

MethodBehaviour
schedule()Queue a debounced save.
scheduleThrottled(key, windowMs)Leading-edge throttle per key for per-frame callers; throws on a non-positive or non-finite window.
flush()Save now, cancelling any pending debounce. Use on pagehide and in tests.
flushPending()Save now only if a debounced or throttled save is pending, cancelling its timer so no duplicate trails; otherwise resolve without calling provider or save. Use to wait for saves already asked for (tests, unmount) without making one. Suppression, a null provider and rejections behave as in flush(). A save whose timer has already fired is not awaited.
pendingRead-only boolean: true while a debounced or throttled save is queued. A suppressed schedule() or a throttled call inside its window leaves it unchanged; cancel(), flush(), flushPending() and the timer firing clear it.
suppress(fn)Run fn with autosave suppressed; re-entrant. Wrap state restores.
cancel()Drop a pending debounced save.
resetThrottle()Clear every throttle window, for example on session reset.

createPreferencesKv(namespace)

Returns a PreferencesKv whose keys are stored as <namespace>.<key>. The namespace must be non-empty.

MethodBehaviour
get(key)The string value, or null.
set(key, value)Write a string.
remove(key)Remove; no-op when absent.
clear()Remove only this namespace's keys.
keys()This namespace's keys, prefix stripped.
getParsed(key, parse, fallback)Parse the value; on a read failure or a parser throw, log and return fallback. Never rejects.