nowity 1.4.0 → 1.5.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 CHANGED
@@ -411,11 +411,18 @@ it is the only part written twice:
411
411
 
412
412
  ```ts
413
413
  { name: "scores.add", local: ([id, delta], tx) => {
414
- const row = tx.get("Score", id);
415
- if (row) tx.update("Score", id, { points: row.points + delta });
414
+ const score = tx.of<{ points: number }>("Score").get(id);
415
+ if (score) score.points += delta;
416
416
  } }
417
417
  ```
418
418
 
419
+ `tx.of(entity)` gives `get(id)`, `all()`, `create(row)` and `remove(id)`. A row from `get` is a
420
+ draft: reading it gives the current value, assigning to a field stages the change in the
421
+ overlay (not in the live MobX object, which the next rebase rebuilds). A row that is not
422
+ loaded gives `undefined`. `tx.get/all/update/put/remove(entity, …)` still work and are
423
+ deprecated. Type the entities once with `declare module "nowity/client" { interface
424
+ LocalEntities { Score: { id: string; points: number } } }` and `tx.of("Score")` knows its fields.
425
+
419
426
  Rollback of a refused write and reconciliation with the confirmed one need no
420
427
  code at all — that is what the rebase is.
421
428
 
@@ -1,13 +1,55 @@
1
1
  import type { EntityId, Row } from "../core/types";
2
2
  import type { ConfirmedStore } from "./confirmed-store";
3
+ /**
4
+ * Optionally maps entity names to their row types, so `tx.of("Score")` knows
5
+ * its fields. Augment it from application code:
6
+ *
7
+ * declare module "nowity/client" {
8
+ * interface LocalEntities { Score: { id: string; points: number } }
9
+ * }
10
+ *
11
+ * Left empty, `of` takes a row type as a type argument instead.
12
+ */
13
+ export interface LocalEntities {
14
+ }
15
+ /**
16
+ * A row as a mutator sees it: reads give the row's current value (confirmed data
17
+ * plus whatever earlier mutators staged), assignments stage a change.
18
+ *
19
+ * It is not the live MobX object the UI holds. Those are the *output* of a
20
+ * rebase and are rebuilt from it, so a write there would be gone on the next
21
+ * message; a draft writes into the overlay, which is what the rebase replays.
22
+ */
23
+ export type Draft<T extends Row = Row> = T;
24
+ /** The rows of one entity, as a mutator sees them. */
25
+ export interface EntityTx<T extends Row = Row> {
26
+ /** The row, or undefined if it is not loaded. Assign to its fields to change it. */
27
+ get(id: EntityId): Draft<T> | undefined;
28
+ all(): Draft<T>[];
29
+ /** Insert a row that does not exist yet. An existing id is replaced whole. */
30
+ create(row: T): void;
31
+ remove(id: EntityId): void;
32
+ }
3
33
  /** Write surface a client-side mutator gets. */
4
34
  export interface LocalTx {
35
+ /** The rows of one entity: `tx.of("Score").get(id).points += delta`. */
36
+ of<K extends keyof LocalEntities>(entity: K): EntityTx<LocalEntities[K] & Row>;
37
+ of<T extends Row = Row>(entity: string): EntityTx<T>;
38
+ /** @deprecated Use `tx.of(entity).get(id)`. */
5
39
  get(entity: string, id: EntityId): Row | undefined;
40
+ /** @deprecated Use `tx.of(entity).all()`. */
6
41
  all(entity: string): Row[];
7
- /** Merge fields into an existing row. No-op if the row is not loaded. */
42
+ /**
43
+ * Merge fields into an existing row. No-op if the row is not loaded.
44
+ * @deprecated Assign to the fields of `tx.of(entity).get(id)`.
45
+ */
8
46
  update(entity: string, id: EntityId, fields: Row): void;
9
- /** Insert or replace a whole row. */
47
+ /**
48
+ * Insert or replace a whole row.
49
+ * @deprecated Use `tx.of(entity).create(row)`, or assign to the fields of the row.
50
+ */
10
51
  put(entity: string, row: Row): void;
52
+ /** @deprecated Use `tx.of(entity).remove(id)`. */
11
53
  remove(entity: string, id: EntityId): void;
12
54
  }
13
55
  /**
@@ -25,6 +67,14 @@ export declare class Overlay {
25
67
  private readonly changes;
26
68
  constructor(confirmed: ConfirmedStore, idFieldOf: (entity: string) => string);
27
69
  tx(): LocalTx;
70
+ private entityTx;
71
+ /**
72
+ * A view of one row that reads through to the overlay on every access, so
73
+ * it stays right as later writes land and a row removed meanwhile reads as
74
+ * empty. Writes to a row that is gone are dropped, like `update` on one that
75
+ * is not loaded.
76
+ */
77
+ private draft;
28
78
  get(entity: string, id: EntityId): Row | undefined;
29
79
  /**
30
80
  * Rows a pending mutation created, per entity.
@@ -1 +1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GAAG,GAAG,EAAE,CAAC;IAC3B;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC;IACxD;;;OAGG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IACpC,kDAAkD;IAClD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;GAQG;AACH,qBAAa,OAAO;IAIZ,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ9B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgD;gBAGnD,SAAS,EAAE,cAAc,EACzB,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM;IAG1D,EAAE,IAAI,OAAO;IAkBb,OAAO,CAAC,QAAQ;IAShB;;;;;OAKG;IACH,OAAO,CAAC,KAAK;IA2Bb,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,GAAG,GAAG,SAAS;IAMlD;;;;;;;OAOG;IACH,QAAQ,IAAI,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC;IAYnC,yEAAyE;IACzE,QAAQ,IAAI,MAAM,EAAE;IAIpB,mDAAmD;IACnD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC;IAY1C,OAAO,CAAC,KAAK;CAQhB"}
@@ -18,6 +18,7 @@ class Overlay {
18
18
  }
19
19
  tx() {
20
20
  return {
21
+ of: ((entity) => this.entityTx(entity)),
21
22
  get: (entity, id) => this.get(entity, id),
22
23
  all: (entity) => [...this.merged(entity).values()],
23
24
  update: (entity, id, fields) => {
@@ -33,6 +34,48 @@ class Overlay {
33
34
  remove: (entity, id) => this.stage(entity, id, null),
34
35
  };
35
36
  }
37
+ entityTx(entity) {
38
+ return {
39
+ get: (id) => (this.get(entity, id) ? this.draft(entity, id) : undefined),
40
+ all: () => [...this.merged(entity).keys()].map((id) => this.draft(entity, id)),
41
+ create: (row) => this.stage(entity, String(row[this.idFieldOf(entity)]), { ...row }),
42
+ remove: (id) => this.stage(entity, id, null),
43
+ };
44
+ }
45
+ /**
46
+ * A view of one row that reads through to the overlay on every access, so
47
+ * it stays right as later writes land and a row removed meanwhile reads as
48
+ * empty. Writes to a row that is gone are dropped, like `update` on one that
49
+ * is not loaded.
50
+ */
51
+ draft(entity, id) {
52
+ const current = () => this.get(entity, id);
53
+ return new Proxy({}, {
54
+ get: (_, key) => (typeof key === "string" ? current()?.[key] : undefined),
55
+ has: (_, key) => typeof key === "string" && current() !== undefined && key in current(),
56
+ set: (_, key, value) => {
57
+ const row = current();
58
+ if (row && typeof key === "string")
59
+ this.stage(entity, id, { ...row, [key]: value });
60
+ return true;
61
+ },
62
+ deleteProperty: (_, key) => {
63
+ const row = current();
64
+ if (row && typeof key === "string") {
65
+ const { [key]: _removed, ...rest } = row;
66
+ this.stage(entity, id, rest);
67
+ }
68
+ return true;
69
+ },
70
+ ownKeys: () => Object.keys(current() ?? {}),
71
+ getOwnPropertyDescriptor: (_, key) => {
72
+ const row = current();
73
+ if (!row || typeof key !== "string" || !(key in row))
74
+ return undefined;
75
+ return { value: row[key], writable: true, enumerable: true, configurable: true };
76
+ },
77
+ });
78
+ }
36
79
  get(entity, id) {
37
80
  const staged = this.changes.get(entity);
38
81
  if (staged?.has(id))
package/dist/client.d.ts CHANGED
@@ -38,7 +38,7 @@ export type { QueryEntry, QueryError, QuerySource } from "./client/query-source"
38
38
  * has answered states its effect here.
39
39
  */
40
40
  export type { ClientMutatorDef } from "./client/nowity-client";
41
- export type { LocalTx } from "./client/overlay";
41
+ export type { Draft, EntityTx, LocalEntities, LocalTx } from "./client/overlay";
42
42
  export { WebSocketTransport, type WebSocketTransportOptions } from "./client/ws-transport";
43
43
  export { configureNowityClient, type NowityClientContainerConfig } from "./ironbean/client";
44
44
  export { NowityClientToken } from "./ironbean/tokens";
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EACH,GAAG,EACH,QAAQ,EACR,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,SAAS,EACT,UAAU,EACV,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,YAAY,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAQ5E,OAAO,EACH,QAAQ,EACR,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,WAAW,EACX,cAAc;AACd;;;;;GAKG;AACH,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,MAAM,EACX,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,mBAAmB,GAC3B,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,KAAK,uBAAuB,GAC/B,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACjF;;;;;GAKG;AACH,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB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,GAC3B,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,KAAK,uBAAuB,GAC/B,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACjF;;;;;GAKG;AACH,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,YAAY,EAAE,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,KAAK,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAE,qBAAqB,EAAE,KAAK,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAC5F,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGtD,YAAY,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nowity",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Realtime object model over PostgreSQL: logical change stream, query-scoped subscriptions, MobX identity map",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {