Schema versions
Telling the server which schema a client was built with, and what happens when client and document disagree.
A web application is deployed in pieces: some users run the new build while others still have yesterday's tab open, and the collection's schema may be migrated in between. This page explains how a client states which schema it was built with, and how that keeps an outdated client from damaging a document.
Schema versions
A collection's schema is versioned. It is created at version 1, and each migration that becomes active adds the next version. Every document is at one version, and the server reports it with the document's state and with every change.
Stating the client's schema
Two options of createSyncClientDocument identify the schema:
import { schemaFingerprint } from "@voidhash/mimic-core";
const document = createSyncClientDocument({
primitive: BoardDocument,
url,
database: databaseId,
collection: collectionId,
document: documentId,
credential,
schemaFingerprint: schemaFingerprint(BoardDocument.schema),
schemaVersion: 3,
});schemaFingerprintis a short hash of the schema. Two schemas that accept the same documents have the same fingerprint, whether they come from your primitives or from the JSON the server stores.schemaVersionis the collection's schema version this schema is: 1 for a collection's first schema, and one more for each migration. Build it into your client next to the schema, and raise it in the release that ships a migration's new schema.
Without schemaFingerprint, no comparison is made, and the client is always
writable.
When the schemas differ
The client compares its fingerprint with that of the document's schema
version, when the session opens and whenever the document's version changes.
When they differ, it calls onSchemaMismatch with a SchemaMismatch:
| Field | Meaning |
|---|---|
server | The document's schema: { fingerprint, migrationVersion } |
client | This client's: { fingerprint, migrationVersion? } |
readOnly | Whether the document is read-only for this client |
clientIsOutdated | Whether the client is behind (or its version is unknown) |
migrationVersion is the schema version. The three cases:
| Case | When | Editing |
|---|---|---|
| Compatible | readOnly is false: same version, schemas that differ in ways that change nothing | Allowed; the server validates every write against its schema |
| Server behind | readOnly and not clientIsOutdated: the document is at a lower version | Read-only until the document is migrated to the client's version |
| Client outdated | readOnly and clientIsOutdated: the client is behind, or did not say its version | Read-only until the client is updated |
const document = createSyncClientDocument({
// …
schemaFingerprint: schemaFingerprint(BoardDocument.schema),
schemaVersion: 3,
onSchemaMismatch: (mismatch) => {
if (mismatch.readOnly && mismatch.clientIsOutdated) {
askToReload();
}
},
});While a mismatch makes the document read-only:
isReadOnly()istrueand writes throwReadOnlyErrorwith the reasonschema_mismatch.- Pending edits stay queued instead of being sent.
- When the server is behind, the client needs no reconnect: the document's migration reaches it as a change, and it becomes writable then.
getSchemaMismatch() returns the current mismatch, or undefined. A
document that does not exist is not compared: the client may create it at
its own version.
When a document is migrated
Every edit names the schema version of the state it was made on, and the
server refuses an edit made on another version than the document's. An edit
made on the old document shape is therefore never applied to a migrated
document. Such edits are reported to onRejection with the reason
"migration_version" (code conflict):
- when the server rejects them;
- when the document's migration arrives while they are still unsent;
- when a client adopts persisted edits made on a version the document has left.
When the migration arrives, the client also discards open
drafts, the undo history
clears both its stacks, and listeners receive onSchemaVersionChange with
the new version. A client built for the new version continues; others become
read-only as the table above says.
While a migration of the collection is being applied, an edit whose result
fails it is rejected with the reason "migration_blocked", and its retry
gets the same answer. The user's change has to be made again, differently.
A state the client cannot read
If a state from the server does not satisfy the client's schema at all, the
client reports a SnapshotValidationError through onSyncError and stops
sending until a valid state arrives. See
Connection states and errors.
Next
- Migrations: how a collection's schema changes on the server.