@lunora/db 1.0.0-alpha.2 → 1.0.0-alpha.200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -103,3 +103,9 @@ Unless required by applicable law or agreed to in writing, software distributed
103
103
  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
104
  CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
105
  specific language governing permissions and limitations under the License.
106
+
107
+ <!-- DEPENDENCIES -->
108
+ <!-- /DEPENDENCIES -->
109
+
110
+ <!-- TYPE_DEPENDENCIES -->
111
+ <!-- /TYPE_DEPENDENCIES -->
package/README.md CHANGED
@@ -65,7 +65,7 @@ export const createCollections = (client: LunoraClient) =>
65
65
  defineCollections(client, {
66
66
  messages: {
67
67
  list: api.messages.list,
68
- scopeBy: "channelId", // sharded — re-point with db.scope.messages({ channelId })
68
+ scopeBy: "channelId", // the .shardBy column — db.scope.messages({ channelId }) syncs that channel's shard
69
69
  insert: {
70
70
  mutation: api.messages.send,
71
71
  optimistic: (input: Omit<Doc<"messages">, "_id" | "_creationTime">, id) => ({
@@ -85,7 +85,55 @@ export const createCollections = (client: LunoraClient) =>
85
85
 
86
86
  > `vis generate lunora-collections` scaffolds this from your `schema.ts` + functions.
87
87
 
88
- > This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/addons/db)**.
88
+ Keep a **single `db` instance**, and treat its collections as the one source of
89
+ truth per table — don't mirror rows into your own store. A derived index (a tree,
90
+ a search index, an undo capture) built from a copy of the rows can silently read
91
+ stale data while the UI renders through `useLiveQuery`. See [One source of truth
92
+ per table](https://lunora.sh/docs/packages/db#one-source-of-truth-per-table).
93
+
94
+ ### Local-first sync engine
95
+
96
+ Beyond whole-table collections, the `@lunora/db/collections` and
97
+ `@lunora/db/mutators` subpaths expose the local-first sync engine:
98
+ `lunoraCollectionOptions({ shape })` syncs a **partial replication shape** (only
99
+ the rows a client needs, scoped by a server-resolved predicate) over the poke
100
+ diff protocol, and `defineMutator` + `bindMutators` run **optimistic custom
101
+ mutators** (a local body first, a server-authoritative impl second, rebased on
102
+ every sync tick). The framework adapters add a `useMutator` / `createMutator` /
103
+ `mutator` hook over a bound handle. See the
104
+ **[local-first guide](https://lunora.sh/docs/concepts/local-first)**.
105
+
106
+ On a `scopeBy` shape collection, give `bindMutators` the same `scopeBy` so each
107
+ push reaches the shard its args name — the shard the collection syncs:
108
+
109
+ ```ts
110
+ const { checkpoints, config, scope } = lunoraCollectionOptions({ client, shape: { name: "messagesByChannel" }, scopeBy: "channelId" });
111
+ const send = bindMutators(client, { checkpoints, collections, scopeBy: "channelId" }, mutators);
112
+ ```
113
+
114
+ ## Upgrading: scoped collections now use their shard
115
+
116
+ Earlier alphas sent every subscription and insert of a `scopeBy` collection with
117
+ no `shardKey` to the default shard (`__root__`); they now go to the shard the
118
+ scoped value names.
119
+
120
+ - **Authorize shard access.** Configure `authorizeShard` on the worker (or
121
+ `allowUnauthenticatedShardAccess: true` when every table has row-level
122
+ security); otherwise scoped subscriptions and writes fail with `403`
123
+ `FORBIDDEN_SHARD`.
124
+ - **Move existing rows.** Rows written before the upgrade are still in the
125
+ default shard, and scoped reads do not look there. Before the upgraded client
126
+ writes to any channel shard, run `lunora export --tables <table>` (it reaches
127
+ the default shard while the table has no registered shard keys) and then
128
+ `lunora import` the file: import re-buckets each row by the table's `.shardBy`
129
+ field and keeps its `_id`, skipping any `_id` the target shard already holds.
130
+ Import deletes nothing, so then remove the default shard's copies with a
131
+ one-off mutation run via `lunora run <fn> --shard __root__`.
132
+
133
+ See [Upgrading](https://lunora.sh/docs/packages/db#upgrading-scoped-collections-now-use-their-shard)
134
+ for the full recipe.
135
+
136
+ > This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/packages/db)**.
89
137
 
90
138
  ## Related
91
139