@rdlabo/ionic-angular-kit 21.5.6 → 21.5.7

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/README.md CHANGED
@@ -506,16 +506,18 @@ provideOffline({
506
506
  });
507
507
  ```
508
508
 
509
- SQLite entities use an immutable client-generated UUID as `localId` and keep the server's
510
- `AUTO_INCREMENT` id separately as nullable `serverId`. The outbox references only `aggregateLocalId`.
511
- Immediately before each send, the executor receives the latest `{ localId, serverId }` resolved from
512
- SQLite; a successful create adds `serverId` without replacing `localId`. Entity projection and outbox
513
- append/removal are committed in one local transaction.
509
+ Every SQLite entity uses an immutable client-generated UUID as `localId`; the outbox references only
510
+ `aggregateLocalId`. Remote identity is declared per entity:
511
+
512
+ | Remote identity | SQLite representation | Executor target |
513
+ | ------------------------------- | --------------------------------------------- | ----------------------------------------- |
514
+ | `serverId()` | nullable `server_id` | `{ localId, serverId }` |
515
+ | `naturalKey(['a', 'b'])` | mapped `a`/`b` columns; no `server_id` column | `{ localId, serverId: null, naturalKey }` |
516
+ | no remote identity (local-only) | mapped values only | `{ localId, serverId: null }` |
514
517
 
515
- | Identity | SQLite column | Before synchronization | After server acknowledgement |
516
- | ---------- | ------------- | ----------------------- | ----------------------------------- |
517
- | `localId` | `local_id` | client-generated UUID | unchanged UUID |
518
- | `serverId` | `server_id` | `NULL` for a new entity | positive server `AUTO_INCREMENT` id |
518
+ A successful numeric create adds `serverId` without replacing `localId`. A natural key is derived from current
519
+ mapped values in declaration order; it is not duplicated as hidden row metadata. Entity projection and outbox
520
+ append/removal are committed in one local transaction.
519
521
 
520
522
  The write lifecycle is: update the replica immediately → append an outbox command in the same transaction → render
521
523
  the optimistic value → replay in the background → validate the server revision → store the confirmed value and
@@ -528,9 +530,10 @@ may lower these limits with `outboxLimits: { maxCommandsPerUser, maxBytesPerUser
528
530
  existing replica and Outbox remain unchanged and enqueue rejects with `OfflineOutboxCapacityError`, so the UI can ask
529
531
  the user to reconnect or resolve/discard an attention item.
530
532
 
531
- `serverId()` supports positive safe integers only. Products must expose an internal numeric primary key for a
532
- replicated entity; a human-facing string such as a public code, slip number, or SKU remains an ordinary replicated
533
- column. There is intentionally no text-server-id overload.
533
+ Use `serverId()` for a positive safe-integer `AUTO_INCREMENT` key. Use `naturalKey([...])` when the product table's
534
+ remote identity is an existing composite primary key. Natural-key components must be required mapped `text()` or
535
+ `integer()` columns; nullable, ignored, JSON, empty declarations, duplicates, and mixed `serverId()`/`naturalKey()`
536
+ declarations reject. Text components reject NUL, malformed Unicode, and values larger than 1,024 UTF-8 bytes.
534
537
 
535
538
  When the application already knows the numeric server id but the first replica pull has not materialized the row,
536
539
  pass that identity explicitly while adopting the entity. This is required for updates and especially deletes: an
@@ -549,8 +552,9 @@ await offlineSync.enqueue({
549
552
  ```
550
553
 
551
554
  The mapping is immutable and unique inside its effective replica scope. Reassigning one `localId` to another
552
- `serverId`, or assigning the same `serverId` to another `localId`, rejects before persistence. Web storage enforces
553
- the same rule transactionally as SQLite's unique indexes: partition-scoped entities are unique per
555
+ `serverId` or natural key, or assigning the same remote identity to another `localId`, rejects before persistence.
556
+ Web storage enforces the rule transactionally. SQLite uses a scoped partial unique index for `server_id` and a
557
+ scoped composite unique index over the natural-key columns: partition-scoped entities are unique per
554
558
  user/partition/source, and user-scoped entities are unique per user/source across partitions. If an adopted row has
555
559
  no confirmed baseline and
556
560
  its final command is discarded, the local row is removed; the next pull may materialize the authoritative server
@@ -560,7 +564,9 @@ Each synchronization cycle pulls authoritative server deltas before replaying th
560
564
  replica schema version/hash and advances a durable user/partition cursor in the same transaction as its rows. A schema
561
565
  mismatch, malformed row, or non-advancing cursor rejects synchronization without advancing that cursor. If a remote
562
566
  revision changed while a local command is pending, the optimistic row remains visible and both row and command move
563
- to `conflict`; the new server value is retained as the confirmed baseline.
567
+ to `conflict`; the new server value is retained as the confirmed baseline. A remote tombstone follows the same
568
+ retention rule: without pending commands it removes the row, while a pending row and its Outbox remain under
569
+ `remote_deleted` conflict so the user can resolve or discard them.
564
570
 
565
571
  The command adapter must send `commandId` as the server-side idempotency key. The server persists that key with the
566
572
  mutation and returns all keys represented by a delta row as `acknowledgedCommandIds`. This correlation is required:
@@ -577,8 +583,22 @@ shape or `null` to delete a row. Identity and sync metadata (`localId`, `serverI
577
583
  `syncState`) stay outside the callback. The bundle fingerprint hashes `version`, entity layouts, and
578
584
  migration `fromVersion`/`statements` — never function bodies.
579
585
 
586
+ Changing a natural-key component list or its order changes the schema fingerprint and SQLite unique index. For a
587
+ released schema, bump `version`; native migration SQL must `DROP INDEX` and recreate the scoped composite index, and
588
+ `migrateWebRow` must preserve every natural-key value. Keep the transaction journal and schema-migration recovery
589
+ records until the replacement rows, index, and metadata commit together. This natural-key API is currently
590
+ unreleased, so adopting it before the first release needs no compatibility helper or legacy-row migration.
591
+
580
592
  ```typescript
581
- import { defineOfflineReplicaSchema, defineReplicaEntity, provideOffline, serverId, text } from '@rdlabo/ionic-angular-kit/offline';
593
+ import {
594
+ defineOfflineReplicaSchema,
595
+ defineReplicaEntity,
596
+ integer,
597
+ naturalKey,
598
+ provideOffline,
599
+ serverId,
600
+ text,
601
+ } from '@rdlabo/ionic-angular-kit/offline';
582
602
 
583
603
  // This is the Hono package's existing `typeof items.$inferSelect` export.
584
604
  import type { Items as ItemSelect } from '@product/hono/db/schema';
@@ -594,9 +614,28 @@ const itemEntityV2 = defineReplicaEntity<ItemSelect>()({
594
614
  },
595
615
  });
596
616
 
617
+ // Mirrors a product DB TableScheme whose primary key is (favFrom, favTo).
618
+ type FavoriteSelect = {
619
+ favFrom: number;
620
+ favTo: number;
621
+ label: string;
622
+ };
623
+
624
+ const favoriteEntity = defineReplicaEntity<FavoriteSelect>()({
625
+ table: 'favorites',
626
+ sourceKey: 'favorites',
627
+ scope: 'user',
628
+ identity: naturalKey(['favFrom', 'favTo']),
629
+ fields: {
630
+ favFrom: integer(),
631
+ favTo: integer(),
632
+ label: text(),
633
+ },
634
+ });
635
+
597
636
  const replicaSchema = defineOfflineReplicaSchema({
598
637
  version: 2,
599
- entities: [itemEntityV2],
638
+ entities: [itemEntityV2, favoriteEntity],
600
639
  migrations: [
601
640
  {
602
641
  fromVersion: 1,
@@ -623,11 +662,12 @@ not a compatibility option: without strict null checking, TypeScript cannot dist
623
662
  from a required one and the schema lock cannot prove the SQLite mapping.
624
663
 
625
664
  The schema definition must import the Hono package's exported `$inferSelect` type and map every key exactly once as
626
- a SQLite column, `serverId()`, or `ignored(reason)`, with exactly one numeric `serverId()` per replicated entity.
665
+ a SQLite column, `serverId()`, or `ignored(reason)`. A remotely replicated entity declares either one numeric
666
+ `serverId()` or one ordered `naturalKey([...])`; a local-only projection declares neither.
627
667
  Nullable Hono columns require `nullable(...)`; non-null columns reject it. Therefore adding,
628
668
  removing, or changing nullability of a Drizzle column breaks the app build until its replica mapping is updated.
629
- At runtime, `values` contains only the mapped column projection; `localId` and `serverId` remain dedicated replica
630
- fields and ignored server fields are never persisted.
669
+ At runtime, `values` contains only the mapped column projection; `localId` and numeric `serverId` remain dedicated
670
+ replica fields, natural identity is always derived from `values`, and ignored server fields are never persisted.
631
671
 
632
672
  - **Status classification**: `0`→`onNetworkError` (connected only), `429`→`onRateLimited`, `502/503/504`→`onServerBusy`, `400/422/500`+message→`onServerError`, `401`→`onUnauthorized`, `403`→`onForbidden`. Other statuses (e.g. `404`) are left to the caller.
633
673
  - **Universal 60s timeout** — every request fails with a synthetic (retryable) `408` if it hangs for 60s. Deliberately generous (catches a dead server without cutting off a large upload / AI generation; `timeout({ each })` resets per emission, so streaming is unaffected). Not configurable — one fleet-wide behavior.