Schema Migrations
Sooner or later the shape of your items changes: a field is added, renamed or split up. SignalDB has no built-in versioning or migration API. A Collection always holds items of the type your current code expects, and migrating stored data to that type happens in your application. Which approach fits depends on where the source of truth for your data lives.
| Your data… | Recommended approach |
|---|---|
| is synced from a server | Re-fetch under a versioned name |
| lives only on the device | Migrate on startup |
| needs a different storage layout (stores, indices) | Upgrade the storage layout |
Re-fetch synced data under a versioned name
If your data comes from a server through the SyncManager, migrate it on the server and let every client fetch the new shape. Put the schema version into the collection's name, so a new version starts with an empty collection instead of reading items in the old shape:
import { Collection } from '@signaldb/core'
const Todos = new Collection<TodoV2>('todos-v2', dataAdapter)
syncManager.addCollection(Todos, { name: 'todos-v2' })After an update, todos-v2 is empty, and the first sync pulls every item in the new shape. Your code only ever knows the current item type, and every device ends up with the same model. Delete the old data once it is no longer needed. With @signaldb/indexeddb, replace the old store name with the new one in schema and raise version: stores missing from schema are dropped during the upgrade.
The cost is one full re-fetch per schema change. Changes made with the previous version that haven't been pushed yet live under the old name and aren't pushed by the new one. Keep that in mind when you plan the release, for example by keeping the old collection registered until it has pushed its pending changes.
Migrate local data on startup
If the data exists only on the device, nothing can re-fetch it, so migrate it in place. Keep a schema version next to your data and run every migration step the stored version hasn't seen yet, once the collection is ready:
const CURRENT_VERSION = 3
await Notes.ready()
const version = Number(localStorage.getItem('notes-version') ?? 1)
if (version < 2) {
await Notes.updateMany({ title: { $exists: false } }, { $set: { title: '' } })
}
if (version < 3) {
await Notes.updateMany({ archived: true }, { $set: { status: 'archived' }, $unset: { archived: true } })
}
localStorage.setItem('notes-version', String(CURRENT_VERSION))Each step brings the data one version further, so a device that skipped several releases runs all of them in order. You keep a single Collection with the current item type and never need the old collection definitions in your code. Store the version wherever it fits your application, for example in localStorage or in a small collection of your own.
If the collection is also synced, run the migration before you call syncManager.addCollection(). Otherwise the sync manager records the migration's writes as local changes and pushes them to your server.
Upgrade the storage layout
Some changes concern the storage rather than the items: a new collection, a removed one, or a new index. With @signaldb/indexeddb, describe the new layout in schema and raise version. SignalDB reconciles the stores and indices during the upgrade:
import createIndexedDBAdapter from '@signaldb/indexeddb'
const storage = createIndexedDBAdapter({
databaseName: 'my-app',
version: 2,
schema: {
notes: ['status'],
},
async onUpgrade(database, transaction, oldVersion, newVersion) {
// transform raw records inside the upgrade transaction, if needed
},
})onUpgrade runs inside IndexedDB's upgrade transaction, before the stores are reconciled. Use it for changes that have to happen at that level. Changes to the items themselves are usually easier to write and to test with Migrate on startup, which uses the regular collection API.