Skip to content

Merging

Each copy of a database works on its own, with or without a connection. When two copies merge, they end up with the same records. You can merge copies in any order, and as many times as you like. The result is the same.

  • Copy: one database file that holds your data. A user with a laptop and a phone has two copies.
  • Device name: the name you give a copy when you open it.
  • Stamp: a note stored with each field of a record. It holds a counter and the device name of the copy that last wrote the field.
  • Bookmark: a named marker that records what a copy has already sent to another copy.
  • Root: a short code that sums up everything in a database. Two databases with the same records have the same root.

starling-db applies three rules, in this order:

  1. A deletion always wins. If any copy deletes a record, the record is deleted on every copy after they merge. This is true even if another copy changed the record later.
  2. Different fields do not conflict. If one copy changes a record’s title and another copy changes its due date, the merged record has both changes.
  3. For the same field, the higher stamp wins. The stamp with the higher counter wins. If the counters are equal, the device name that sorts later wins.

Each time a copy updates a record, it sets the counter of the changed fields to one more than the highest counter in that record.

  • Put values that change together in one field. A field that holds an object or an array merges as one value. One copy’s whole value wins.
  • Put values that change separately in separate fields. Then two copies can change them without a conflict.
  • Use null to clear a field. You cannot remove a field from a record.
  • Do not rely on rules between records. The only rule that survives a merge is that IDs are unique. For example, a record can refer to another record that a different copy has deleted.
  • Use random IDs for ordinary records. If two copies insert a record with the same ID, the records merge into one, field by field. Use a fixed ID only when you want that.

You give each copy a device name when you open it. Follow these rules:

  • Give each copy a different name. A random ID that you create once and store with the copy works well.
  • Use the same name every time you open that copy.
  • In a browser, use one name for all tabs of your app. They share one copy.

If two copies use the same name, they still end up with the same records after a merge. But when their stamps are equal, you cannot predict which change wins.

To bring another copy up to date, follow these steps. The examples send from a laptop to a phone.

  1. On the laptop, get the changes. Ask for the changes since the bookmark named for the phone. The first time, you get every record.
  2. Send the changes. They are plain data. Send them any way you like.
  3. On the laptop, set the bookmark. Do this after the phone has received the changes.
// On the laptop: get the changes the phone has not received yet.
const changes = await db.changesSinceBookmark("phone");
await send(changes); // plain JSON: send it any way you like
await db.bookmark("phone"); // after the phone has received them
  1. On the phone, merge the changes. The merge returns the records that it changed. Read those records again to update what the user sees.
// On the phone: merge the changes, then update what the user sees.
async function receive(phone: SharedDb<{ todos: Todo }>, changes: Change[]) {
const changed = await phone.merge(changes);
for (const { collection, id } of changed) refresh(collection, id);
}
  1. Repeat in the other direction, from the phone to the laptop. The two copies then hold the same records.

If your Rust program has both databases open, you can merge them directly.

// Merge once in each direction. The order does not matter.
phone.merge_from(&mut laptop)?;
laptop.merge_from(&mut phone)?;
assert_eq!(phone.root(), laptop.root());

Compare the roots of the two copies. If the roots are equal, the copies hold the same records.

If the roots differ after you have sent changes in both directions, some changes were lost on the way. To fix this, remove the bookmark. The next time you ask for changes, you get every record.

async function sendEverything(theirRoot: string | undefined) {
if ((await db.root()) === theirRoot) return; // both hold the same records
await db.forgetBookmark("phone");
await send(await db.changesSinceBookmark("phone")); // now every record
}

Also remove a bookmark when you stop sending to a copy. A bookmark uses space in the file.

  • starling-db does not send data. Your app sends the changes between devices.
  • Deleted records are never fully removed. The ID of a deleted record stays in the file.
  • Tabs are not told about writes. Tabs share one database, but a tab must read again to see what another tab wrote.