@rdlabo/ionic-angular-kit 21.5.8 → 21.5.10

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,18 +506,24 @@ provideOffline({
506
506
  });
507
507
  ```
508
508
 
509
- Every SQLite entity uses an immutable client-generated UUID as `localId`; the outbox references only
510
- `aggregateLocalId`. Remote identity is declared per entity:
509
+ Replica identity follows the product database:
511
510
 
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 }` |
511
+ | Identity declaration | SQLite primary key | Row / executor identity |
512
+ | ---------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------- |
513
+ | `generatedId('integer' \| 'text')` | scope columns + immutable `local_id`; scoped nullable unique `server_id` | `{ kind: 'generated', localId, remoteId }` |
514
+ | `naturalKey(['a', 'b'])` | scope columns plus mapped `a`, `b` columns | `{ kind: 'natural', naturalKey: { a, b } }` |
515
+ | `identity: localOnly()` | scope columns + immutable `local_id` | `{ kind: 'local', localId }` |
517
516
 
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.
517
+ Natural-key tables contain neither `local_id` nor `server_id`; their scoped key columns are the SQLite composite
518
+ `PRIMARY KEY`. Generated remote ids may be positive safe integers or non-empty text ids such as server-generated
519
+ UUIDs. A successful create adds `remoteId` without replacing `localId`. Local-only rows cannot enter the Outbox.
520
+ All TEXT identity components are opaque and compared byte-for-byte with SQLite's `BINARY` collation. The server
521
+ database must use the same binary equality, or the Hono/API converter must return one canonical form before the
522
+ value reaches `naturalKey(...)` or `generatedId('text')`. Do not pass case- or accent-insensitive primary keys
523
+ through unchanged: values that the server considers equal must first be canonicalized to identical TEXT.
524
+
525
+ `OfflineScope.userId` accepts `number | string`. A type-tagged TEXT codec keeps numeric `7` and text `"7"` in
526
+ different metadata, manifest, cursor, Outbox, and entity boundaries.
521
527
 
522
528
  The write lifecycle is: update the replica immediately → append an outbox command in the same transaction → render
523
529
  the optimistic value → replay in the background → validate the server revision → store the confirmed value and
@@ -525,8 +531,8 @@ revision. The server remains authoritative; SQLite is the durable local working
525
531
 
526
532
  For a DB row whose deletion is represented by absence, enqueue the same full row values with
527
533
  `replicaMutation: 'delete'`. The runtime stores a library-owned durable tombstone: normal `getReplicaRow()` and
528
- `getReplicaRows()` reads stop returning the row immediately, while synchronization retains its immutable `localId`,
529
- remote identity, confirmed baseline, and Outbox command. A successful tombstone acknowledgement removes the row
534
+ `getReplicaRows()` reads stop returning the row immediately, while synchronization retains its immutable generated
535
+ or natural identity, confirmed baseline, and Outbox command. A successful tombstone acknowledgement removes the row
530
536
  physically; retry, authorization failure, and conflict keep it hidden; discarding restores the latest confirmed
531
537
  baseline unless the server has also deleted it.
532
538
 
@@ -539,7 +545,10 @@ tombstone for replay, lost-ACK reconciliation, conflict handling, and discard.
539
545
  await offlineSync.enqueue({
540
546
  scopeId,
541
547
  aggregateType: 'favorites',
542
- aggregateLocalId: favorite.localId,
548
+ identity: {
549
+ kind: 'natural',
550
+ naturalKey: { favFrom: favorite.values.favFrom, favTo: favorite.values.favTo },
551
+ },
543
552
  operation: 'favorite.delete',
544
553
  payload: { favTo: favorite.values.favTo },
545
554
  optimisticValue: favorite.values,
@@ -555,10 +564,11 @@ may lower these limits with `outboxLimits: { maxCommandsPerUser, maxBytesPerUser
555
564
  existing replica and Outbox remain unchanged and enqueue rejects with `OfflineOutboxCapacityError`, so the UI can ask
556
565
  the user to reconnect or resolve/discard an attention item.
557
566
 
558
- Use `serverId()` for a positive safe-integer `AUTO_INCREMENT` key. Use `naturalKey([...])` when the product table's
559
- remote identity is an existing composite primary key. Natural-key components must be required mapped `text()` or
560
- `integer()` columns; nullable, ignored, JSON, empty declarations, duplicates, and mixed `serverId()`/`naturalKey()`
561
- declarations reject. Text components reject NUL, malformed Unicode, and values larger than 1,024 UTF-8 bytes.
567
+ Use `generatedId('integer')` for a positive safe-integer `AUTO_INCREMENT` key and `generatedId('text')` for a
568
+ server-generated text key. Use `naturalKey([...])` for an existing single or composite natural primary key.
569
+ Natural-key components must be required mapped `text()` or `integer()` columns; nullable, ignored, JSON, empty
570
+ declarations, duplicates, and mixed generated/natural declarations reject. Text identities reject NUL, malformed
571
+ Unicode, and values larger than 1,024 UTF-8 bytes.
562
572
 
563
573
  When the application already knows the numeric server id but the first replica pull has not materialized the row,
564
574
  pass that identity explicitly while adopting the entity. This is required for updates and especially deletes: an
@@ -568,19 +578,18 @@ omitted id would otherwise make the executor interpret the row as a not-yet-crea
568
578
  await offlineSync.enqueue({
569
579
  scopeId,
570
580
  aggregateType: 'items',
571
- aggregateLocalId: localId,
572
- serverId: existingApiItem.id,
581
+ identity: { kind: 'generated', localId, remoteId: existingApiItem.id },
573
582
  operation: 'items.delete',
574
583
  payload: { method: 'DELETE' },
575
584
  optimisticValue: existingApiItem,
576
585
  });
577
586
  ```
578
587
 
579
- The mapping is immutable and unique inside its effective replica scope. Reassigning one `localId` to another
580
- `serverId` or natural key, or assigning the same remote identity to another `localId`, rejects before persistence.
581
- Web storage enforces the rule transactionally. SQLite uses a scoped partial unique index for `server_id` and a
582
- scoped composite unique index over the natural-key columns: partition-scoped entities are unique per
583
- user/partition/source, and user-scoped entities are unique per user/source across partitions. If an adopted row has
588
+ The mapping is immutable and unique inside its effective replica scope. Reassigning one generated `localId` to
589
+ another `remoteId`, or assigning the same remote identity to another `localId`, rejects before persistence. A
590
+ natural-key change is a delete of the old key plus an insert of the new key, never an update of the primary key.
591
+ Web storage enforces the same rule transactionally. SQLite uses a scoped partial unique index for generated
592
+ `server_id`; natural identity is enforced by the table's scoped composite primary key. If an adopted row has
584
593
  no confirmed baseline and
585
594
  its final command is discarded, the local row is removed; the next pull may materialize the authoritative server
586
595
  row again. A row with a confirmed baseline rolls back to that baseline instead.
@@ -596,20 +605,20 @@ retention rule: without pending commands it removes the row, while a pending row
596
605
  The command adapter must send `commandId` as the server-side idempotency key. The server persists that key with the
597
606
  mutation and returns all keys represented by a delta row as `acknowledgedCommandIds`. This correlation is required:
598
607
  if the server commits a create/update/delete but its HTTP acknowledgement is lost, the next pull reconciles the
599
- server result into the original `localId`, removes the acknowledged outbox prefix, and rebases later commands without
600
- creating a second local identity.
608
+ server result into the same generated or natural identity, removes the acknowledged Outbox prefix, and rebases later
609
+ commands without creating a second local identity.
601
610
 
602
611
  Versioned replica schemas lock web and native storage. Web metadata stores
603
612
  `replicaSchemaVersion` and `replicaSchemaHash`; native stores the same pair in
604
613
  `offline_replica_schema_metadata`. Bump `version` for every intentional shape change and supply a
605
614
  complete one-step migration chain. Native runs each step's SQL `statements`; web runs
606
615
  `migrateWebRow`, which receives only `{ sourceKey, values, confirmedValues }` and may return the same
607
- shape or `null` to delete a row. Identity and sync metadata (`localId`, `serverId`, scope, revision,
616
+ shape or `null` to delete a row. Identity and sync metadata (`identity`, scope, revision,
608
617
  `syncState`) stay outside the callback. The bundle fingerprint hashes `version`, entity layouts, and
609
618
  migration `fromVersion`/`statements` — never function bodies.
610
619
 
611
- Changing a natural-key component list or its order changes the schema fingerprint and SQLite unique index. For a
612
- released schema, bump `version`; native migration SQL must `DROP INDEX` and recreate the scoped composite index, and
620
+ Changing a natural-key component list or its order changes the schema fingerprint and SQLite primary key. For a
621
+ released schema, bump `version`; native migration SQL must rebuild the table with the new composite primary key, and
613
622
  `migrateWebRow` must preserve every natural-key value. Keep the transaction journal and schema-migration recovery
614
623
  records until the replacement rows, index, and metadata commit together. This natural-key API is currently
615
624
  unreleased, so adopting it before the first release needs no compatibility helper or legacy-row migration.
@@ -618,10 +627,11 @@ unreleased, so adopting it before the first release needs no compatibility helpe
618
627
  import {
619
628
  defineOfflineReplicaSchema,
620
629
  defineReplicaEntity,
630
+ generatedId,
621
631
  integer,
632
+ localOnly,
622
633
  naturalKey,
623
634
  provideOffline,
624
- serverId,
625
635
  text,
626
636
  } from '@rdlabo/ionic-angular-kit/offline';
627
637
 
@@ -633,7 +643,7 @@ const itemEntityV2 = defineReplicaEntity<ItemSelect>()({
633
643
  sourceKey: 'items',
634
644
  scope: 'partition',
635
645
  fields: {
636
- id: serverId(),
646
+ id: generatedId('integer'),
637
647
  title: text(),
638
648
  subtitle: text(),
639
649
  },
@@ -687,12 +697,14 @@ not a compatibility option: without strict null checking, TypeScript cannot dist
687
697
  from a required one and the schema lock cannot prove the SQLite mapping.
688
698
 
689
699
  The schema definition must import the Hono package's exported `$inferSelect` type and map every key exactly once as
690
- a SQLite column, `serverId()`, or `ignored(reason)`. A remotely replicated entity declares either one numeric
691
- `serverId()` or one ordered `naturalKey([...])`; a local-only projection declares neither.
700
+ a SQLite column, `generatedId('integer' | 'text')`, or `ignored(reason)`. A remotely replicated entity declares
701
+ either one generated-id field or one ordered `naturalKey([...])`; a local-only projection explicitly declares
702
+ `identity: localOnly()`.
692
703
  Nullable Hono columns require `nullable(...)`; non-null columns reject it. Therefore adding,
693
704
  removing, or changing nullability of a Drizzle column breaks the app build until its replica mapping is updated.
694
- At runtime, `values` contains only the mapped column projection; `localId` and numeric `serverId` remain dedicated
695
- replica fields, natural identity is always derived from `values`, and ignored server fields are never persisted.
705
+ At runtime, `values` contains only the mapped column projection. Identity is a discriminated union: generated rows
706
+ carry `localId`/`remoteId`, natural rows carry only `naturalKey`, and local-only rows carry only `localId`. Ignored
707
+ server fields are never persisted.
696
708
 
697
709
  - **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.
698
710
  - **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.