anbaric-tsapi 1.8.0 → 1.9.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
@@ -11,12 +11,10 @@ synchronous.
11
11
 
12
12
  ## Value classes
13
13
 
14
- **`Job`** — a unit of work moving through a workflow.
15
- `new Job(id, properties = new Map(), initialState, workflowId?, startedBy = "system", startedAt = new Date(), lastUpdated = startedAt, transitions = [])`.
16
- `stateId` is a getter; state only changes through
17
- `transition(transition, actor = workflowId ?? "state-machine")`, which
18
- records `{from, to, actor}` into `transitions` and bumps `lastUpdated` when
19
- the transition's predicate accepts the job.
14
+ **`Job`** — a unit of work moving through a workflow. Immutable.
15
+ `new Job(id, properties = new Map(), state, workflowId?, startedBy = "system", startedAt = new Date(), lastUpdated = startedAt)`.
16
+ `state` is a readonly string; a job never changes in place — `JobPersistence.save`
17
+ constructs a new `Job` at the target `state` and stamps `lastUpdated`.
20
18
 
21
19
  **`State`** — `new State(id, actions = [], transitions = [])`, plus
22
20
  `subscribe(action)` to add an action later.
@@ -27,7 +25,7 @@ accept) gates whether the action runs, and
27
25
  `run : (job) => Promise<Map<string, any>>` (defaults to an empty map)
28
26
  returns the property changes the action wants. `run` never mutates the job.
29
27
 
30
- **`Actor`** — an interface, pure identity: `{ type : "HUMAN" | "CODE" | "AGENT", id : string, role : string }`.
28
+ **`Actor`** — an interface, pure identity: `{ type : "HUMAN" | "CODE" | "AGENT" | "SYSTEM", id : string, role : string }`.
31
29
  Concrete `Human`, `Code`, `Agent` classes live in `anbaric-state-machine`.
32
30
 
33
31
  **`Transition`** — `new Transition(to, predicate)`; the first accepting
@@ -37,18 +35,18 @@ transition in a state wins.
37
35
  `required : boolean` (default false) and `validation : (value) => boolean`
38
36
  (default accept).
39
37
 
40
- **`JobTransition`** — `{ from : string, to : string, actor : string }`.
41
-
42
38
  **Serialization** — `serializeJob(job) : SerializedJob` and
43
39
  `deserializeJob(serialized) : Job` define the wire shape used by the cloud
44
40
  clients and platform (dates as ISO strings, properties as a plain object).
45
41
 
46
42
  ## Contracts
47
43
 
48
- **`JobPersistence`** — `save(job)`, `retrieve(id)` (throws
49
- `No job found with id "x"`), `delete(id)`, `list(pageSize = 100, page = 0)`,
50
- `updateProperties(id, properties)` (merge; implementations stamp
51
- `lastUpdated`).
44
+ **`JobPersistence`** — an abstract class taking an `Auditor`; every method takes
45
+ the acting `Actor` and audits before deferring to an abstract `…Internal`.
46
+ `create(actor, job)`, `save(actor, changeDescription, job, properties?, state?)`
47
+ (applies the property/state change and stamps `lastUpdated`),
48
+ `retrieve(id, actor)` (throws `No job found with id "x"`), `delete(id, actor)`,
49
+ `list(actor, pageSize?, page?)`.
52
50
 
53
51
  **`Queue`** — `enqueue(jobId, workflowId)`, `schedule(jobId, workflowId, due)`.
54
52
  **`Dequeue extends Queue`** adds `dequeueSome() : Promise<Array<QueueMessage>>`;
@@ -58,10 +56,15 @@ Delivery is at-least-once: consumers must tolerate redelivery.
58
56
  **`Consumer`** — `subscribe(workflowId, processJob)` routes deliveries for
59
57
  one workflow to a callback; `cleanUp()` releases resources.
60
58
 
61
- **`JsonStore`** — `save(id, document)`, `retrieve(id)`, `delete(id)`,
62
- `list()`; implementations validate against a `JsonSchema` when one is given.
59
+ **`JsonStore`** — abstract, `Auditor`-backed and actor-audited like
60
+ `JobPersistence`: `create(actor, id, document)`,
61
+ `save(actor, changeDescription, id, document)`, `retrieve(id, actor)`,
62
+ `delete(id, actor)`, `list(actor, pageSize?, page?)` (returns the document
63
+ values, not ids); implementations validate against a `JsonSchema` when one is given.
63
64
 
64
- **`SecretStore`** — `save(name, value)`, `retrieve(name)`, `list()`.
65
+ **`SecretStore`** — abstract, `Auditor`-backed: `create(actor, name, value)`,
66
+ `save(actor, changeDescription, name, value)`, `retrieve(name, actor)`,
67
+ `delete(name, actor)`, `list(actor)`. Secret values never appear in audit details.
65
68
 
66
69
  **`QueueMessage`** — `{ jobId, workflowId }`; part of the platform's private
67
70
  wire protocol (in `api/cloud/`), exported because `Dequeue` returns it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anbaric-tsapi",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "These are the shared libraries - types, classes, etc - that both the public state-machine and hosting services share",
5
5
  "license": "MIT",
6
6
  "author": "chris@anbaric.ai",
@@ -1,19 +1,13 @@
1
1
  import {Actor} from "../actors/Actor";
2
2
 
3
- enum AuditInteraction {
4
- CREATE = "CREATE",
5
- UPDATE_PROPERTIES = "UPDATE_PROPERTIES",
6
- CHANGE_STATE = "CHANGE_STATE",
7
- DELETE = "DELETE",
8
- READ = "READ",
9
- LIST = "LIST",
10
- }
11
-
3
+ /* Records an interaction with a resource. The interaction is one or more plain
4
+ strings chosen by the persistence class that calls it - the auditor itself
5
+ defines no vocabulary. */
12
6
  interface Auditor {
13
7
 
14
- audit(resourceType : string, resourceId : string, actor : Actor, interaction : AuditInteraction[],
8
+ audit(resourceType : string, resourceId : string, actor : Actor, interaction : Array<string>,
15
9
  description : string, details : any) : Promise<void>;
16
10
 
17
11
  }
18
12
 
19
- export { AuditInteraction, Auditor }
13
+ export { Auditor }
@@ -1,12 +1,12 @@
1
1
  import {Actor} from "../actors/Actor";
2
- import {AuditInteraction, Auditor} from "./Auditor";
2
+ import {Auditor} from "./Auditor";
3
3
 
4
4
  /* An auditor that records nothing. Used by the persistence instances that sit
5
5
  behind the HTTP API, where the app-facing store on the other side has
6
6
  already audited the interaction. */
7
7
  class NoOpAuditor implements Auditor {
8
8
 
9
- async audit(_resourceType : string, _resourceId : string, _actor : Actor, _interaction : AuditInteraction[],
9
+ async audit(_resourceType : string, _resourceId : string, _actor : Actor, _interaction : Array<string>,
10
10
  _description : string, _details : any) : Promise<void> {
11
11
  }
12
12
 
@@ -1,12 +1,10 @@
1
- import {AuditInteraction} from "../auditing/Auditor";
2
-
3
1
  type AuditRecord = {
4
2
  id? : string,
5
3
  resourceType : string,
6
4
  resourceId : string,
7
5
  actorId : string,
8
6
  actorType : string,
9
- interaction : AuditInteraction[],
7
+ interaction : Array<string>,
10
8
  description : string,
11
9
  details : any,
12
10
  at? : string,
@@ -1,5 +1,5 @@
1
1
  import {Actor} from "../actors/Actor";
2
- import {AuditInteraction, Auditor} from "../auditing/Auditor";
2
+ import {Auditor} from "../auditing/Auditor";
3
3
 
4
4
  /* A schema-validated JSON document store that audits every interaction, keyed
5
5
  by collection and id. Public methods audit then defer to ...Internal. */
@@ -9,27 +9,27 @@ abstract class JsonStore {
9
9
  }
10
10
 
11
11
  async create(actor : Actor, id : string, document : any) : Promise<void> {
12
- await this.auditor.audit("document", `${this.collection}/${id}`, actor, [AuditInteraction.CREATE], "Document created", document);
12
+ await this.auditor.audit("document", `${this.collection}/${id}`, actor, [JsonStore.Interaction.CREATE], "Document created", document);
13
13
  await this.saveInternal(id, document);
14
14
  }
15
15
 
16
16
  async save(actor : Actor, changeDescription : string, id : string, document : any) : Promise<void> {
17
- await this.auditor.audit("document", `${this.collection}/${id}`, actor, [AuditInteraction.UPDATE_PROPERTIES], changeDescription, document);
17
+ await this.auditor.audit("document", `${this.collection}/${id}`, actor, [JsonStore.Interaction.SAVE], changeDescription, document);
18
18
  await this.saveInternal(id, document);
19
19
  }
20
20
 
21
21
  async retrieve(id : string, actor : Actor) : Promise<any> {
22
- await this.auditor.audit("document", `${this.collection}/${id}`, actor, [AuditInteraction.READ], "", null);
22
+ await this.auditor.audit("document", `${this.collection}/${id}`, actor, [JsonStore.Interaction.READ], "", null);
23
23
  return this.retrieveInternal(id);
24
24
  }
25
25
 
26
26
  async delete(id : string, actor : Actor) : Promise<void> {
27
- await this.auditor.audit("document", `${this.collection}/${id}`, actor, [AuditInteraction.DELETE], "", null);
27
+ await this.auditor.audit("document", `${this.collection}/${id}`, actor, [JsonStore.Interaction.DELETE], "", null);
28
28
  await this.deleteInternal(id);
29
29
  }
30
30
 
31
31
  async list(actor : Actor, pageSize? : number, page? : number) : Promise<Array<any>> {
32
- await this.auditor.audit("document", `${this.collection}/*`, actor, [AuditInteraction.LIST], "", null);
32
+ await this.auditor.audit("document", `${this.collection}/*`, actor, [JsonStore.Interaction.LIST], "", null);
33
33
  return this.listInternal(pageSize, page);
34
34
  }
35
35
 
@@ -40,4 +40,16 @@ abstract class JsonStore {
40
40
 
41
41
  }
42
42
 
43
+ namespace JsonStore {
44
+
45
+ export enum Interaction {
46
+ CREATE = "CREATE",
47
+ SAVE = "SAVE",
48
+ READ = "READ",
49
+ DELETE = "DELETE",
50
+ LIST = "LIST",
51
+ }
52
+
53
+ }
54
+
43
55
  export { JsonStore }
@@ -1,7 +1,6 @@
1
1
  import {Actor} from "../actors/Actor";
2
- import {AuditInteraction, Auditor} from "../auditing/Auditor";
2
+ import {Auditor} from "../auditing/Auditor";
3
3
  import {Job} from "./Job";
4
- import {SystemActor} from "../actors/SystemActor";
5
4
 
6
5
  /* Persists jobs and audits every interaction. Public methods record the
7
6
  interaction against the injected auditor and then defer to the abstract
@@ -14,7 +13,7 @@ abstract class JobPersistence {
14
13
  async create(actor : Actor,
15
14
  job : Job) : Promise<void> {
16
15
 
17
- await this.auditor.audit("job", job.id, actor, [AuditInteraction.CREATE], "Job created", job);
16
+ await this.auditor.audit("job", job.id, actor, [JobPersistence.Interaction.CREATE], "Job created", job);
18
17
 
19
18
  this.saveInternal(job);
20
19
  }
@@ -30,9 +29,9 @@ abstract class JobPersistence {
30
29
  state: state ? {from : job.state, to : state} : undefined
31
30
  }
32
31
 
33
- const interaction = [];
34
- if (properties) interaction.push(AuditInteraction.UPDATE_PROPERTIES);
35
- if (state) interaction.push(AuditInteraction.CHANGE_STATE);
32
+ const interaction : Array<string> = [];
33
+ if (properties) interaction.push(JobPersistence.Interaction.UPDATE_PROPERTIES);
34
+ if (state) interaction.push(JobPersistence.Interaction.CHANGE_STATE);
36
35
 
37
36
  await this.auditor.audit("job", job.id, actor, interaction, changeDescription, change);
38
37
 
@@ -67,17 +66,17 @@ abstract class JobPersistence {
67
66
  }
68
67
 
69
68
  async retrieve(id : string, actor : Actor) : Promise<Job> {
70
- await this.auditor.audit("job", id, actor, [AuditInteraction.READ], "", null);
69
+ await this.auditor.audit("job", id, actor, [JobPersistence.Interaction.READ], "", null);
71
70
  return this.retrieveInternal(id);
72
71
  }
73
72
 
74
73
  async delete(id : string, actor : Actor) : Promise<void> {
75
- await this.auditor.audit("job", id, actor, [AuditInteraction.DELETE], "", null);
74
+ await this.auditor.audit("job", id, actor, [JobPersistence.Interaction.DELETE], "", null);
76
75
  await this.deleteInternal(id);
77
76
  }
78
77
 
79
78
  async list(actor : Actor, pageSize? : number, page? : number) : Promise<Array<Job>> {
80
- await this.auditor.audit("job", "*", actor, [AuditInteraction.LIST], "", null);
79
+ await this.auditor.audit("job", "*", actor, [JobPersistence.Interaction.LIST], "", null);
81
80
  return this.listInternal(pageSize, page);
82
81
  }
83
82
 
@@ -88,4 +87,17 @@ abstract class JobPersistence {
88
87
 
89
88
  }
90
89
 
90
+ namespace JobPersistence {
91
+
92
+ export enum Interaction {
93
+ CREATE = "CREATE",
94
+ UPDATE_PROPERTIES = "UPDATE_PROPERTIES",
95
+ CHANGE_STATE = "CHANGE_STATE",
96
+ DELETE = "DELETE",
97
+ READ = "READ",
98
+ LIST = "LIST",
99
+ }
100
+
101
+ }
102
+
91
103
  export { JobPersistence }
@@ -1,5 +1,5 @@
1
1
  import {Actor} from "../actors/Actor";
2
- import {AuditInteraction, Auditor} from "../auditing/Auditor";
2
+ import {Auditor} from "../auditing/Auditor";
3
3
 
4
4
  /* Stores named secrets and audits every interaction. The secret value is
5
5
  never included in an audit record. Public methods audit then defer to the
@@ -10,27 +10,27 @@ abstract class SecretStore {
10
10
  }
11
11
 
12
12
  async create(actor : Actor, name : string, value : string) : Promise<void> {
13
- await this.auditor.audit("secret", name, actor, [AuditInteraction.CREATE], "Secret created", null);
13
+ await this.auditor.audit("secret", name, actor, [SecretStore.Interaction.CREATE], "Secret created", null);
14
14
  await this.saveInternal(name, value);
15
15
  }
16
16
 
17
17
  async save(actor : Actor, changeDescription : string, name : string, value : string) : Promise<void> {
18
- await this.auditor.audit("secret", name, actor, [AuditInteraction.UPDATE_PROPERTIES], changeDescription, null);
18
+ await this.auditor.audit("secret", name, actor, [SecretStore.Interaction.SAVE], changeDescription, null);
19
19
  await this.saveInternal(name, value);
20
20
  }
21
21
 
22
22
  async retrieve(name : string, actor : Actor) : Promise<string> {
23
- await this.auditor.audit("secret", name, actor, [AuditInteraction.READ], "", null);
23
+ await this.auditor.audit("secret", name, actor, [SecretStore.Interaction.READ], "", null);
24
24
  return this.retrieveInternal(name);
25
25
  }
26
26
 
27
27
  async delete(name : string, actor : Actor) : Promise<void> {
28
- await this.auditor.audit("secret", name, actor, [AuditInteraction.DELETE], "", null);
28
+ await this.auditor.audit("secret", name, actor, [SecretStore.Interaction.DELETE], "", null);
29
29
  await this.deleteInternal(name);
30
30
  }
31
31
 
32
32
  async list(actor : Actor) : Promise<Array<string>> {
33
- await this.auditor.audit("secret", "*", actor, [AuditInteraction.LIST], "", null);
33
+ await this.auditor.audit("secret", "*", actor, [SecretStore.Interaction.LIST], "", null);
34
34
  return this.listInternal();
35
35
  }
36
36
 
@@ -41,4 +41,16 @@ abstract class SecretStore {
41
41
 
42
42
  }
43
43
 
44
+ namespace SecretStore {
45
+
46
+ export enum Interaction {
47
+ CREATE = "CREATE",
48
+ SAVE = "SAVE",
49
+ READ = "READ",
50
+ DELETE = "DELETE",
51
+ LIST = "LIST",
52
+ }
53
+
54
+ }
55
+
44
56
  export { SecretStore }
@@ -0,0 +1,40 @@
1
+ import {Actor} from "../actors/Actor";
2
+ import {Auditor} from "../auditing/Auditor";
3
+
4
+ /* A relational (SQL) store for application data. Public methods audit the
5
+ interaction then defer to the abstract ...Internal. `query` returns rows;
6
+ `execute` runs a statement and returns the number of affected rows. The
7
+ backing engine (SQLite locally, PostgreSQL in the cloud) is chosen by the
8
+ factory; SQL placeholder syntax differs between them (see the docs). */
9
+ abstract class SqlStore {
10
+
11
+ constructor(protected auditor : Auditor) {
12
+ }
13
+
14
+ async query(actor : Actor, sql : string, parameters : Array<any> = []) : Promise<Array<Record<string, any>>> {
15
+ await this.auditor.audit("sql", "*", actor, [SqlStore.Interaction.QUERY], sql, null);
16
+ return this.queryInternal(sql, parameters);
17
+ }
18
+
19
+ async execute(actor : Actor, sql : string, parameters : Array<any> = []) : Promise<number> {
20
+ await this.auditor.audit("sql", "*", actor, [SqlStore.Interaction.EXECUTE], sql, null);
21
+ return this.executeInternal(sql, parameters);
22
+ }
23
+
24
+ abstract close() : Promise<void>;
25
+
26
+ protected abstract queryInternal(sql : string, parameters : Array<any>) : Promise<Array<Record<string, any>>>;
27
+ protected abstract executeInternal(sql : string, parameters : Array<any>) : Promise<number>;
28
+
29
+ }
30
+
31
+ namespace SqlStore {
32
+
33
+ export enum Interaction {
34
+ QUERY = "QUERY",
35
+ EXECUTE = "EXECUTE",
36
+ }
37
+
38
+ }
39
+
40
+ export { SqlStore }
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export * from "./api/actors/Actor"
15
15
  export * from "./api/actors/SystemActor"
16
16
  export * from "./api/documents/JsonStore"
17
17
  export * from "./api/secrets/SecretStore"
18
+ export * from "./api/sql/SqlStore"
18
19
  export * from "./api/documents/JsonSchema"
19
20
  export * from "./api/state-machine/Queue"
20
21
  export * from "./api/state-machine/Consumer"