@katn30/trakr 4.0.0 → 4.1.0
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 +58 -4
- package/dist/dev/index.cjs +3 -0
- package/dist/dev/index.cjs.map +1 -1
- package/dist/dev/index.d.cts +6 -5
- package/dist/dev/index.d.ts +6 -5
- package/dist/dev/index.js +3 -0
- package/dist/dev/index.js.map +1 -1
- package/dist/prod/index.cjs +3 -0
- package/dist/prod/index.cjs.map +1 -1
- package/dist/prod/index.js +3 -0
- package/dist/prod/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -610,6 +610,14 @@ tracker.onCommit(keys); // same, plus write real server IDs to @AutoId fie
|
|
|
610
610
|
2. Transitions every tracked object's `state` to `Unchanged` and resets `dirtyCounter`.
|
|
611
611
|
3. Appends the state change into the existing last undo operation — so undo atomically reverts both the user's edits and the committed state together (no spurious extra undo steps).
|
|
612
612
|
|
|
613
|
+
**Lookup by trackingId**
|
|
614
|
+
|
|
615
|
+
```typescript
|
|
616
|
+
const obj = tracker.getByTrackingId(42); // TrackedObject | undefined
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
Returns the tracked object whose `trackingId` matches the given value, or `undefined` if none. Deleted objects are still findable this way (they remain in `trackedObjects` until `destroy()`). Useful for debugging, correlating server responses against known objects, or hydrating a UI selection from a persisted `trackingId`.
|
|
620
|
+
|
|
613
621
|
**Sessions**
|
|
614
622
|
|
|
615
623
|
```typescript
|
|
@@ -886,6 +894,8 @@ tracker.onCommit(response.ids);
|
|
|
886
894
|
|
|
887
895
|
Marks a property as the server-assigned autoincrement primary key for this model. Only one `@AutoId` field is allowed per class. Enables the `onCommit` lifecycle for real-ID assignment.
|
|
888
896
|
|
|
897
|
+
`@AutoId` is a **specialisation of `@Id`**: it also registers the property as part of the model's identity (so `getIdentity(obj)` includes it), but it additionally singles the property out as the one trakr will overwrite from `onCommit(keys)`. Use `@Id` for identity properties you assign yourself; use `@AutoId` for the single property trakr should patch on commit. See [`@Id`](#id) for identity-only properties (composite keys, caller-assigned UUIDs, natural keys).
|
|
898
|
+
|
|
889
899
|
```typescript
|
|
890
900
|
class InvoiceModel extends TrackedObject {
|
|
891
901
|
@AutoId
|
|
@@ -927,6 +937,45 @@ tracker.onCommit(serverIds);
|
|
|
927
937
|
|
|
928
938
|
`trackingId` values are globally unique across the lifetime of the tracker and never reused, so they can safely serve as correlation keys across multiple save cycles.
|
|
929
939
|
|
|
940
|
+
**Reactivity gate.** The `@AutoId` write performed by `onCommit(keys)` is a **library-internal write, not a user edit**. It does **not** fire `TrackedObject.changed` for the `@AutoId` property, does **not** bump `dirtyCounter`, does **not** re-run `@Tracked` validators, and does **not** flicker `tracker.isDirty` back to `true` during commit. After `onCommit` returns, `state === Unchanged` and `isDirty === false` — as if the object were freshly loaded with the real PK. Treat the write as *authoritative baseline update*, not as a change event.
|
|
941
|
+
|
|
942
|
+
---
|
|
943
|
+
|
|
944
|
+
### `@Id`
|
|
945
|
+
|
|
946
|
+
Marks a property as part of the model's **caller-provided identity**. Any type is allowed (string, number, UUID, ULID, tuple-like composite via multiple decorators). Trakr never mutates the value — you set it (typically from the server, or client-generated for UUID schemas), and trakr uses it purely to compute `getIdentity(obj)`, `getIdentityObject(obj)`, and `getIdentityProperties(proto)`.
|
|
947
|
+
|
|
948
|
+
```typescript
|
|
949
|
+
class TenantModel extends TrackedObject {
|
|
950
|
+
@Id
|
|
951
|
+
code: string = ''; // caller-assigned string PK
|
|
952
|
+
|
|
953
|
+
@Tracked()
|
|
954
|
+
accessor name: string = '';
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
class LineItemModel extends TrackedObject {
|
|
958
|
+
@Id
|
|
959
|
+
orderId: number = 0; // composite key part 1
|
|
960
|
+
@Id
|
|
961
|
+
lineNo: number = 0; // composite key part 2
|
|
962
|
+
|
|
963
|
+
@Tracked()
|
|
964
|
+
accessor qty: number = 0;
|
|
965
|
+
}
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
**Relationship to `@AutoId`.**
|
|
969
|
+
|
|
970
|
+
| | `@Id` | `@AutoId` |
|
|
971
|
+
|----------------------------------|-----------------------------------|-------------------------------------------------|
|
|
972
|
+
| Registers property as identity | yes | yes (implicitly) |
|
|
973
|
+
| Value type | any | any (via `IdAssignment<V>`; historically `number`) |
|
|
974
|
+
| Written by `onCommit(keys)` | never | yes, from the matching `IdAssignment.value` |
|
|
975
|
+
| Multiple per class | yes (composite key) | no (at most one) |
|
|
976
|
+
|
|
977
|
+
Use `@Id` for identity you own (client-generated UUIDs, natural keys, composite keys). Use `@AutoId` for the single property whose value only the server can produce and that must be patched back into the model after `onCommit`. A class may combine them: several `@Id` properties for a composite key plus one `@AutoId` for a surrogate PK, if that matches your schema.
|
|
978
|
+
|
|
930
979
|
---
|
|
931
980
|
|
|
932
981
|
### `ITracked`
|
|
@@ -951,19 +1000,24 @@ function isReady(item: ITracked): boolean {
|
|
|
951
1000
|
|
|
952
1001
|
---
|
|
953
1002
|
|
|
954
|
-
### `IdAssignment
|
|
1003
|
+
### `IdAssignment<V>`
|
|
955
1004
|
|
|
956
|
-
The shape of each entry in the `keys` array passed to `tracker.onCommit(keys)`:
|
|
1005
|
+
The shape of each entry in the `keys` array passed to `tracker.onCommit(keys)`. Generic in the PK value type, defaulting to `number`:
|
|
957
1006
|
|
|
958
1007
|
```typescript
|
|
959
1008
|
import type { IdAssignment } from '@katn30/trakr';
|
|
960
|
-
// { trackingId: number; value:
|
|
1009
|
+
// IdAssignment<V = number> = { trackingId: number; value: V }
|
|
1010
|
+
|
|
1011
|
+
const numericKeys: IdAssignment[] = [{ trackingId: 1, value: 42 }];
|
|
1012
|
+
const uuidKeys: IdAssignment<string>[] = [{ trackingId: 1, value: '01HXYZ-ULID' }];
|
|
961
1013
|
```
|
|
962
1014
|
|
|
963
1015
|
| Field | Type | Description |
|
|
964
1016
|
|---|---|---|
|
|
965
1017
|
| `trackingId` | `number` | The `trackingId` of the object that received a new server-assigned PK |
|
|
966
|
-
| `value` | `number` | The real server-assigned ID to write to the `@AutoId` field |
|
|
1018
|
+
| `value` | `V` (default `number`) | The real server-assigned ID to write to the `@AutoId` field |
|
|
1019
|
+
|
|
1020
|
+
`tracker.onCommit<V>(keys)` is generic in `V`: pass `IdAssignment<string>[]` for UUID/ULID schemas, `IdAssignment[]` for the numeric default. Trakr writes `entry.value` straight into the `@AutoId` field — the field's declared type is what enforces the match at the call site (e.g. `@AutoId id: string = ''` with `onCommit<string>(...)`).
|
|
967
1021
|
|
|
968
1022
|
The server returns one `IdAssignment` per item that produced a new database row — both inserted objects and, in temporal tables, updated objects (see [Temporally versioned tables](#temporally-versioned-tables)). `onCommit()` iterates every entry, matches by `trackingId` against every tracked object, and writes `value` to the `@AutoId` field of any match.
|
|
969
1023
|
|
package/dist/dev/index.cjs
CHANGED
|
@@ -838,6 +838,9 @@ var Tracker = class {
|
|
|
838
838
|
this._commitStateOperation = lastOp;
|
|
839
839
|
this.reset();
|
|
840
840
|
}
|
|
841
|
+
getByTrackingId(trackingId) {
|
|
842
|
+
return this.trackedObjects.find((o) => o.trackingId === trackingId);
|
|
843
|
+
}
|
|
841
844
|
/** @internal */
|
|
842
845
|
_isInUndoStack(op) {
|
|
843
846
|
return this._undoOperations.includes(op);
|