# Model a Local-First App

Canonical: https://maryk.io/guides/model-a-local-first-app/
Source: https://github.com/marykdb/maryk/blob/main/website/src/content/docs/guides/model-a-local-first-app.mdx

This guide is for an app that writes locally, needs fast reads, and may sync or inspect data later.

The short version:

1. Model stable domain objects as `RootDataModel`s.
2. Embed small data that shares the parent lifecycle.
3. Split large or independently edited data into separate roots.
4. Pick keys for the primary local access path.
5. Use Memory Store in tests, then IndexedDB for browser-local data or RocksDB for durable embedded data.
6. Use the App or CLI to inspect real records early.

## Start with roots

Use a root for anything users search, open, sync, or edit independently.

```kotlin
object Project : RootDataModel<Project>() {
    val title by string(index = 1u)
    val archived by boolean(index = 2u, default = false)
}
```

Property indexes are the stored contract. Keep them stable forever once data exists.

## Embed bounded details

Embed data when it is always read with the parent and stays bounded in size.

```kotlin
object Profile : DataModel<Profile>() {
    val displayName by string(index = 1u)
    val timezone by string(index = 2u, required = false)
}


object Account : RootDataModel<Account>() {
    val email by string(index = 1u)
    val profile by embed(index = 2u, dataModel = { Profile })
}
```

This keeps common reads cheap and updates atomic at the record level.

Use graphs when a screen only needs part of the embedded data:

```kotlin
val listScreen = Account.graph {
    listOf(
        email,
        graph(profile) {
            listOf(displayName)
        }
    )
}
```

## Split growing data

Split when a child collection can grow, has separate permissions, or changes independently.

```kotlin
object Task : RootDataModel<Task>(
    minimumKeyScanByteRange = Project.Meta.keyByteSize.toUInt(),
    keyDefinition = {
        Multiple(
            Task.project.ref(),
            Reversed(Task.createdAt.ref()),
            UUIDv7Key,
        )
    }
) {
    val project by reference(index = 1u, dataModel = { Project }, final = true)
    val createdAt by dateTime(index = 2u, final = true)
    val title by string(index = 3u)
    val done by boolean(index = 4u, default = false)
}
```

The key starts with the project reference, so scans for one project can stay near one key range. `minimumKeyScanByteRange` allows that complete project-key prefix while rejecting unbounded key scans unless the caller explicitly enables table scans. `Reversed(createdAt.ref())` puts recently created tasks first. The trailing `UUIDv7Key` keeps keys unique when multiple tasks share the same timestamp.

Key properties must be required and final. Use an immutable creation time here; an editable “last updated” time belongs in a secondary index, not the key.

## Add only the indexes screens need

Add secondary indexes for access paths that are not covered by the key.

```kotlin
object Task : RootDataModel<Task>(
    minimumKeyScanByteRange = Project.Meta.keyByteSize.toUInt(),
    keyDefinition = {
        Multiple(Task.project.ref(), Reversed(Task.createdAt.ref()), UUIDv7Key)
    },
    indexes = {
        listOf(
            Multiple(Task.project.ref(), Task.done.ref(), Reversed(Task.createdAt.ref()))
        )
    }
) {
    val project by reference(index = 1u, dataModel = { Project }, final = true)
    val createdAt by dateTime(index = 2u, final = true)
    val title by string(index = 3u)
    val done by boolean(index = 4u, default = false)
}
```

Use that index for a project task list filtered by `done` and ordered newest first.

Do not add indexes for every property. Persistent stores treat index additions as model/storage changes.

## Test with Memory Store

Memory Store runs the same request API without persistence. Run this inside a coroutine:

```kotlin
import maryk.core.query.responses.statuses.AddSuccess
InMemoryDataStore.open(
    keepAllVersions = true,
    keepUpdateHistoryIndex = true,
    dataModelsById = mapOf(
        1u to Project,
        2u to Task,
    )
).use {
    val added = execute(Project.add(Project.create { title with "Docs" }))
    val status = added.statuses.single() as? AddSuccess<Project>
        ?: error("Project creation failed: ${added.statuses.single()}")
    val projectKey = status.key


    execute(
        Task.scan(
            where = Equals(Task.ref { project } with projectKey),
            limit = 50u,
        )
    )
}
```

Keep model IDs stable before moving to a persistent store. ID `0u` is reserved.

## Persist in browsers with IndexedDB

Use IndexedDB for durable local storage in JS and WasmJS browser apps:

```kotlin
IndexedDbDataStore.open(
    databaseName = "maryk-local-app",
    keepAllVersions = true,
    keepUpdateHistoryIndex = true,
    dataModelsById = mapOf(
        1u to Project,
        2u to Task,
    )
).use {
    // Same execute(...) calls as Memory Store.
}
```

The IndexedDB store uses the browser’s native IndexedDB database, key ranges, transactions, and cursor scans. It is the browser-local persistence path for web clients.

## Persist with RocksDB

Use RocksDB for local-first durable storage:

```kotlin
RocksDBDataStore.open(
    keepAllVersions = true,
    keepUpdateHistoryIndex = true,
    relativePath = "data/maryk",
    dataModelsById = mapOf(
        1u to Project,
        2u to Task,
    )
).use {
    // Same execute(...) calls as Memory Store.
}
```

The request code stays the same across Memory, IndexedDB, and RocksDB.

## Inspect early

Use the CLI for fast checks after closing the app’s RocksDB connection. A second process cannot open the same RocksDB directory while the app holds its lock. To inspect a running store, expose it through Remote Store and use the App Remote connection or a programmatic client.

```text
maryk --connect rocksdb --dir ./data/maryk --exec "list"
maryk --connect rocksdb --dir ./data/maryk --exec "scan Task --limit 20"
```

Use the desktop App when you need a visual loop: models, records, filters, form-based edits, and history in one place.

## Checklist

- One root per independently edited object.
- Embedded objects are bounded and share the parent lifecycle.
- Key matches the primary scan.
- Key properties are stable.
- Secondary indexes match real screens or workflows.
- `dataModelsById` is stable before persistence.
- Memory Store tests cover add, scan, change, and history.
- RocksDB is inspected through CLI/App with sample data.

Next: [Design Sync-Friendly Keys](https://maryk.io/guides/design-sync-friendly-keys/index.md) and [Changing Models Safely](https://maryk.io/guides/change-models-safely/index.md).
