@toa.io/core 1.0.0-alpha.4 → 1.0.0-alpha.41
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/package.json +5 -5
- package/src/assignment.js +11 -2
- package/src/cascade.js +2 -3
- package/src/contract/schemas/query.yaml +1 -2
- package/src/effect.js +19 -0
- package/src/entities/changeset.js +3 -12
- package/src/entities/entity.js +16 -11
- package/src/entities/factory.js +6 -0
- package/src/exceptions.js +7 -11
- package/src/index.js +2 -0
- package/src/locator.js +7 -2
- package/src/observation.js +0 -8
- package/src/operation.js +26 -4
- package/src/receiver.js +5 -9
- package/src/state.js +61 -40
- package/src/transition.js +13 -6
- package/test/entities/entity.test.js +5 -22
- package/types/locator.d.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/core",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.41",
|
|
4
4
|
"description": "Toa Core",
|
|
5
5
|
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/toa-io/toa#readme",
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@rsql/parser": "1.2.4",
|
|
24
|
-
"@toa.io/console": "1.0.0-alpha.
|
|
25
|
-
"@toa.io/generic": "1.0.0-alpha.
|
|
26
|
-
"@toa.io/yaml": "1.0.0-alpha.
|
|
24
|
+
"@toa.io/console": "1.0.0-alpha.41",
|
|
25
|
+
"@toa.io/generic": "1.0.0-alpha.41",
|
|
26
|
+
"@toa.io/yaml": "1.0.0-alpha.41",
|
|
27
27
|
"error-value": "0.3.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"clone-deep": "4.0.1"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "3b338a3f21ff9113cefa75d7789d9267917d41e3"
|
|
33
33
|
}
|
package/src/assignment.js
CHANGED
|
@@ -9,13 +9,22 @@ class Assignment extends Operation {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
async commit (store) {
|
|
12
|
-
const {
|
|
12
|
+
const {
|
|
13
|
+
scope,
|
|
14
|
+
state,
|
|
15
|
+
reply
|
|
16
|
+
} = store
|
|
13
17
|
|
|
14
18
|
if (reply.error !== undefined) return
|
|
15
19
|
|
|
16
20
|
scope.set(state)
|
|
17
21
|
|
|
18
|
-
await this.scope.apply(scope)
|
|
22
|
+
const output = await this.scope.apply(scope)
|
|
23
|
+
|
|
24
|
+
// assignment returns new state by default
|
|
25
|
+
if (store.reply.output === undefined) {
|
|
26
|
+
store.reply.output = output
|
|
27
|
+
}
|
|
19
28
|
}
|
|
20
29
|
}
|
|
21
30
|
|
package/src/cascade.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { merge } = require('@toa.io/generic')
|
|
4
3
|
const { Connector } = require('./connector')
|
|
5
4
|
|
|
6
5
|
class Cascade extends Connector {
|
|
7
|
-
#bridges
|
|
6
|
+
// #bridges
|
|
8
7
|
#last
|
|
9
8
|
|
|
10
9
|
constructor (bridges) {
|
|
11
10
|
super()
|
|
12
11
|
|
|
13
|
-
this.#bridges = bridges
|
|
12
|
+
// this.#bridges = bridges
|
|
14
13
|
this.#last = bridges[bridges.length - 1]
|
|
15
14
|
|
|
16
15
|
this.depends(bridges)
|
|
@@ -18,14 +18,13 @@ properties:
|
|
|
18
18
|
minItems: 1
|
|
19
19
|
items:
|
|
20
20
|
type: string
|
|
21
|
-
pattern:
|
|
21
|
+
pattern: ^\w{1,32}(?::(?:asc|desc))?$
|
|
22
22
|
projection:
|
|
23
23
|
type: array
|
|
24
24
|
uniqueItems: true
|
|
25
25
|
minItems: 1
|
|
26
26
|
items:
|
|
27
27
|
type: string
|
|
28
|
-
pattern: ^([a-zA-Z]+([-a-zA-Z0-9]*[a-zA-Z0-9]+)?)$
|
|
29
28
|
not:
|
|
30
29
|
const: id
|
|
31
30
|
additionalProperties: false
|
package/src/effect.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Observation } = require('./observation')
|
|
4
|
+
|
|
5
|
+
class Effect extends Observation {
|
|
6
|
+
|
|
7
|
+
async acquire (store) {
|
|
8
|
+
const { query, entity } = store.request
|
|
9
|
+
|
|
10
|
+
if (entity === undefined)
|
|
11
|
+
return super.acquire(store)
|
|
12
|
+
|
|
13
|
+
store.scope = await this.scope.ensure(query, entity)
|
|
14
|
+
store.state = store.scope.get()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.Effect = Effect
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { merge, overwrite, newid } = require('@toa.io/generic')
|
|
4
3
|
const { EntityContractException } = require('../exceptions')
|
|
5
4
|
|
|
6
5
|
class Changeset {
|
|
@@ -26,21 +25,13 @@ class Changeset {
|
|
|
26
25
|
|
|
27
26
|
if (error !== null) throw new EntityContractException(error)
|
|
28
27
|
|
|
28
|
+
value._updated = Date.now()
|
|
29
|
+
|
|
29
30
|
this.#state = value
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export () {
|
|
33
|
-
|
|
34
|
-
const result = { changeset }
|
|
35
|
-
const insert = merge({ id: newid() }, changeset)
|
|
36
|
-
const error = this.#schema.fit(insert)
|
|
37
|
-
|
|
38
|
-
if (error === null) {
|
|
39
|
-
delete insert.id
|
|
40
|
-
result.insert = overwrite(insert, changeset)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return result
|
|
34
|
+
return this.#state
|
|
44
35
|
}
|
|
45
36
|
}
|
|
46
37
|
|
package/src/entities/entity.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
difference,
|
|
5
|
-
newid
|
|
6
|
-
} = require('@toa.io/generic')
|
|
7
|
-
|
|
3
|
+
const { difference, newid } = require('@toa.io/generic')
|
|
8
4
|
const { EntityContractException } = require('../exceptions')
|
|
9
5
|
|
|
10
6
|
class Entity {
|
|
@@ -32,7 +28,8 @@ class Entity {
|
|
|
32
28
|
set (value) {
|
|
33
29
|
const error = this.#schema.fit(value)
|
|
34
30
|
|
|
35
|
-
if (error !== null)
|
|
31
|
+
if (error !== null)
|
|
32
|
+
throw new EntityContractException(error)
|
|
36
33
|
|
|
37
34
|
this.#set(value)
|
|
38
35
|
}
|
|
@@ -41,7 +38,8 @@ class Entity {
|
|
|
41
38
|
return {
|
|
42
39
|
origin: this.#origin,
|
|
43
40
|
state: this.#state,
|
|
44
|
-
changeset: this.#origin === null ? this.#state : difference(this.#origin, this.#state)
|
|
41
|
+
changeset: this.#origin === null ? this.#state : difference(this.#origin, this.#state),
|
|
42
|
+
trailers: this.#state._trailers
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
|
|
@@ -55,12 +53,19 @@ class Entity {
|
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
#set (value) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
if (!('_trailers' in value))
|
|
57
|
+
Object.defineProperty(value, '_trailers', {
|
|
58
|
+
writable: false,
|
|
59
|
+
configurable: false,
|
|
60
|
+
enumerable: false,
|
|
61
|
+
value: {}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
if (!('_created' in value))
|
|
65
|
+
value._created = Date.now()
|
|
62
66
|
|
|
63
67
|
if (this.#state !== undefined) {
|
|
68
|
+
value._updated = Date.now()
|
|
64
69
|
value._version++
|
|
65
70
|
}
|
|
66
71
|
|
package/src/entities/factory.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { newid } = require('@toa.io/generic')
|
|
4
|
+
|
|
3
5
|
const { Entity } = require('./entity')
|
|
4
6
|
const { EntitySet } = require('./set')
|
|
5
7
|
const { Changeset } = require('./changeset')
|
|
@@ -11,6 +13,10 @@ class Factory {
|
|
|
11
13
|
this.#schema = schema
|
|
12
14
|
}
|
|
13
15
|
|
|
16
|
+
fit (values) {
|
|
17
|
+
this.#schema.validate({ id: newid(), ...values }, 'Entity')
|
|
18
|
+
}
|
|
19
|
+
|
|
14
20
|
init (id) {
|
|
15
21
|
return new Entity(this.#schema, id)
|
|
16
22
|
}
|
package/src/exceptions.js
CHANGED
|
@@ -19,6 +19,7 @@ const codes = {
|
|
|
19
19
|
StatePrecondition: 303,
|
|
20
20
|
StateConcurrency: 304,
|
|
21
21
|
StateInitialization: 305,
|
|
22
|
+
Duplicate: 306,
|
|
22
23
|
|
|
23
24
|
Communication: 400,
|
|
24
25
|
Transmission: 401
|
|
@@ -48,18 +49,13 @@ class SystemException extends Exception {
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
class ContractException extends Exception {
|
|
51
|
-
keyword
|
|
52
|
-
property
|
|
53
|
-
schema
|
|
54
|
-
path
|
|
55
|
-
|
|
56
52
|
constructor (code, error) {
|
|
57
|
-
super(code || codes.Contract, error
|
|
53
|
+
super(code || codes.Contract, typeof error === 'string' ? error : error?.message)
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
if (typeof error === 'object' && error !== null)
|
|
56
|
+
for (const k of ['keyword', 'property', 'schema', 'path'])
|
|
57
|
+
if (k in error)
|
|
58
|
+
this[k] = error[k]
|
|
63
59
|
}
|
|
64
60
|
}
|
|
65
61
|
|
|
@@ -88,7 +84,7 @@ for (const [name, code] of Object.entries(codes)) {
|
|
|
88
84
|
if (exports[classname] === undefined) {
|
|
89
85
|
exports[classname] = class extends Exception {
|
|
90
86
|
constructor (message) {
|
|
91
|
-
super(code, message
|
|
87
|
+
super(code, message ?? classname)
|
|
92
88
|
}
|
|
93
89
|
}
|
|
94
90
|
}
|
package/src/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const { Composition } = require('./composition')
|
|
|
5
5
|
const { Connector } = require('./connector')
|
|
6
6
|
const { Context } = require('./context')
|
|
7
7
|
const { Discovery } = require('./discovery')
|
|
8
|
+
const { Effect } = require('./effect')
|
|
8
9
|
const { Emission } = require('./emission')
|
|
9
10
|
const { Event } = require('./event')
|
|
10
11
|
const { Exposition } = require('./exposition')
|
|
@@ -32,6 +33,7 @@ exports.Composition = Composition
|
|
|
32
33
|
exports.Connector = Connector
|
|
33
34
|
exports.Context = Context
|
|
34
35
|
exports.Discovery = Discovery
|
|
36
|
+
exports.Effect = Effect
|
|
35
37
|
exports.Emission = Emission
|
|
36
38
|
exports.Event = Event
|
|
37
39
|
exports.Exposition = Exposition
|
package/src/locator.js
CHANGED
|
@@ -12,6 +12,7 @@ class Locator {
|
|
|
12
12
|
id
|
|
13
13
|
label
|
|
14
14
|
uppercase
|
|
15
|
+
lowercase
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* @param {string} name
|
|
@@ -26,6 +27,7 @@ class Locator {
|
|
|
26
27
|
this.id = concat(namespace, '.') + name
|
|
27
28
|
this.label = (concat(namespace, '-') + name).toLowerCase()
|
|
28
29
|
this.uppercase = (concat(namespace, '_') + name).toUpperCase()
|
|
30
|
+
this.lowercase = (concat(namespace, '_') + name).toLowerCase()
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
hostname (prefix) {
|
|
@@ -39,8 +41,11 @@ class Locator {
|
|
|
39
41
|
static parse (string) {
|
|
40
42
|
const [namespace, name] = string.split(DOT)
|
|
41
43
|
|
|
42
|
-
if (name === undefined)
|
|
43
|
-
|
|
44
|
+
if (name === undefined) {
|
|
45
|
+
return new Locator(namespace)
|
|
46
|
+
} else {
|
|
47
|
+
return new Locator(name, namespace)
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
|
package/src/observation.js
CHANGED
|
@@ -3,14 +3,6 @@
|
|
|
3
3
|
const { Operation } = require('./operation')
|
|
4
4
|
|
|
5
5
|
class Observation extends Operation {
|
|
6
|
-
async acquire (store) {
|
|
7
|
-
const scope = await this.query(store.request.query)
|
|
8
|
-
const state = scope === null ? null : scope.get()
|
|
9
|
-
|
|
10
|
-
store.scope = scope
|
|
11
|
-
store.state = state
|
|
12
|
-
}
|
|
13
|
-
|
|
14
6
|
async run (store) {
|
|
15
7
|
if (store.scope === null) store.reply = null
|
|
16
8
|
else await super.run(store)
|
package/src/operation.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Connector } = require('./connector')
|
|
4
|
-
const { SystemException } = require('./exceptions')
|
|
4
|
+
const { SystemException, RequestContractException } = require('./exceptions')
|
|
5
|
+
const { Readable } = require('node:stream')
|
|
5
6
|
|
|
6
7
|
class Operation extends Connector {
|
|
7
8
|
scope
|
|
@@ -26,13 +27,22 @@ class Operation extends Connector {
|
|
|
26
27
|
|
|
27
28
|
async invoke (request) {
|
|
28
29
|
try {
|
|
29
|
-
if (request.authentic !== true)
|
|
30
|
-
|
|
30
|
+
if (request.authentic !== true)
|
|
31
|
+
this.#contracts.request.fit(request)
|
|
32
|
+
|
|
33
|
+
if ('query' in request)
|
|
34
|
+
request.query = this.#query.parse(request.query)
|
|
35
|
+
|
|
36
|
+
// validate entity
|
|
37
|
+
if ('entity' in request)
|
|
38
|
+
this.scope.fit(request.entity)
|
|
31
39
|
|
|
32
40
|
const store = { request }
|
|
33
41
|
|
|
34
42
|
return await this.process(store)
|
|
35
43
|
} catch (e) {
|
|
44
|
+
console.error(e)
|
|
45
|
+
|
|
36
46
|
const exception = e instanceof Error ? new SystemException(e) : e
|
|
37
47
|
|
|
38
48
|
return { exception }
|
|
@@ -47,7 +57,16 @@ class Operation extends Connector {
|
|
|
47
57
|
return store.reply
|
|
48
58
|
}
|
|
49
59
|
|
|
50
|
-
async acquire () {
|
|
60
|
+
async acquire (store) {
|
|
61
|
+
if (this.#scope === 'none')
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
const scope = await this.query(store.request.query)
|
|
65
|
+
const raw = scope === null || scope instanceof Readable
|
|
66
|
+
|
|
67
|
+
store.scope = scope
|
|
68
|
+
store.state = raw ? scope : scope.get()
|
|
69
|
+
}
|
|
51
70
|
|
|
52
71
|
async run (store) {
|
|
53
72
|
const { request, state } = store
|
|
@@ -62,6 +81,9 @@ class Operation extends Connector {
|
|
|
62
81
|
async commit () {}
|
|
63
82
|
|
|
64
83
|
async query (query) {
|
|
84
|
+
if (query === undefined)
|
|
85
|
+
throw new RequestContractException('Request query is required')
|
|
86
|
+
|
|
65
87
|
return this.scope[this.#scope](query)
|
|
66
88
|
}
|
|
67
89
|
}
|
package/src/receiver.js
CHANGED
|
@@ -16,18 +16,13 @@ class Receiver extends Connector {
|
|
|
16
16
|
/** @type {string} */
|
|
17
17
|
#endpoint
|
|
18
18
|
|
|
19
|
-
/** @type {
|
|
19
|
+
/** @type {unknown[]} */
|
|
20
|
+
#arguments
|
|
21
|
+
|
|
20
22
|
#local
|
|
21
23
|
|
|
22
|
-
/** @type {toa.core.bridges.Receiver} */
|
|
23
24
|
#bridge
|
|
24
25
|
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
* @param {toa.norm.component.Receiver} definition
|
|
28
|
-
* @param {toa.core.Component} local
|
|
29
|
-
* @param {toa.core.bridges.Receiver} bridge
|
|
30
|
-
*/
|
|
31
26
|
constructor (definition, local, bridge) {
|
|
32
27
|
super()
|
|
33
28
|
|
|
@@ -36,6 +31,7 @@ class Receiver extends Connector {
|
|
|
36
31
|
this.#conditioned = conditioned
|
|
37
32
|
this.#adaptive = adaptive
|
|
38
33
|
this.#endpoint = operation
|
|
34
|
+
this.#arguments = definition.arguments
|
|
39
35
|
|
|
40
36
|
this.#local = local
|
|
41
37
|
this.#bridge = bridge
|
|
@@ -58,7 +54,7 @@ class Receiver extends Connector {
|
|
|
58
54
|
}
|
|
59
55
|
|
|
60
56
|
async #request (payload) {
|
|
61
|
-
return this.#adaptive ? await this.#bridge.request(payload) : { input: payload }
|
|
57
|
+
return this.#adaptive ? await this.#bridge.request(payload, ...(this.#arguments ?? [])) : { input: payload }
|
|
62
58
|
}
|
|
63
59
|
}
|
|
64
60
|
|
package/src/state.js
CHANGED
|
@@ -1,62 +1,79 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { empty } = require('@toa.io/generic')
|
|
3
|
+
const { empty, newid } = require('@toa.io/generic')
|
|
4
|
+
const { StatePreconditionException, StateNotFoundException } = require('./exceptions')
|
|
4
5
|
|
|
5
|
-
const {
|
|
6
|
-
StatePreconditionException,
|
|
7
|
-
StateNotFoundException,
|
|
8
|
-
StateInitializationException
|
|
9
|
-
} = require('./exceptions')
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @implements {toa.core.State}
|
|
13
|
-
*/
|
|
14
6
|
class State {
|
|
15
|
-
|
|
7
|
+
#associated
|
|
16
8
|
#storage
|
|
17
|
-
|
|
18
|
-
/** @type {toa.core.entity.Factory} */
|
|
19
|
-
#entity
|
|
9
|
+
#entities
|
|
20
10
|
#emission
|
|
21
|
-
#dependent
|
|
22
11
|
|
|
23
|
-
constructor (storage, entity, emission,
|
|
12
|
+
constructor (storage, entity, emission, associated) {
|
|
24
13
|
this.#storage = storage
|
|
25
|
-
this.#
|
|
14
|
+
this.#entities = entity
|
|
26
15
|
this.#emission = emission
|
|
27
|
-
this.#
|
|
16
|
+
this.#associated = associated === true
|
|
28
17
|
}
|
|
29
18
|
|
|
30
19
|
init (id) {
|
|
31
|
-
return this.#
|
|
20
|
+
return this.#entities.init(id)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fit (values) {
|
|
24
|
+
return this.#entities.fit(values)
|
|
32
25
|
}
|
|
33
26
|
|
|
34
27
|
async object (query) {
|
|
35
28
|
const record = await this.#storage.get(query)
|
|
36
29
|
|
|
37
30
|
if (record === null) {
|
|
38
|
-
if (this.#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
if (this.#associated && query.id !== undefined && query.version === undefined)
|
|
32
|
+
return this.init(query.id)
|
|
33
|
+
else if (query.version !== undefined)
|
|
34
|
+
throw new StatePreconditionException()
|
|
35
|
+
|
|
36
|
+
return null
|
|
37
|
+
} else
|
|
38
|
+
return this.#entities.object(record)
|
|
44
39
|
}
|
|
45
40
|
|
|
46
41
|
async objects (query) {
|
|
47
42
|
const recordset = await this.#storage.find(query)
|
|
48
43
|
|
|
49
|
-
return this.#
|
|
44
|
+
return this.#entities.objects(recordset)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async stream (query) {
|
|
48
|
+
return this.#storage.stream(query)
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
changeset (query) {
|
|
53
|
-
return this.#
|
|
52
|
+
return this.#entities.changeset(query)
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
none () {
|
|
57
56
|
return null
|
|
58
57
|
}
|
|
59
58
|
|
|
59
|
+
async ensure (query, properties) {
|
|
60
|
+
const object = this.#entities.init()
|
|
61
|
+
const blank = object.get()
|
|
62
|
+
|
|
63
|
+
Object.assign(blank, properties)
|
|
64
|
+
|
|
65
|
+
object.set(blank)
|
|
66
|
+
|
|
67
|
+
const record = await this.#storage.ensure(query, properties, object.get())
|
|
68
|
+
|
|
69
|
+
if (record.id !== blank.id) // exists
|
|
70
|
+
return this.#entities.object(record)
|
|
71
|
+
|
|
72
|
+
await this.#emission.emit(object.event())
|
|
73
|
+
|
|
74
|
+
return object
|
|
75
|
+
}
|
|
76
|
+
|
|
60
77
|
async commit (state) {
|
|
61
78
|
const event = state.event()
|
|
62
79
|
|
|
@@ -68,30 +85,34 @@ class State {
|
|
|
68
85
|
ok = await this.#storage.store(object)
|
|
69
86
|
|
|
70
87
|
// #20
|
|
71
|
-
|
|
88
|
+
if (ok === true) {
|
|
89
|
+
await this.#emission.emit(event)
|
|
90
|
+
}
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
return ok
|
|
75
94
|
}
|
|
76
95
|
|
|
77
96
|
async apply (state) {
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
let upsert
|
|
81
|
-
|
|
82
|
-
if (this.#dependent && state.query.id !== undefined && state.query.version === undefined) {
|
|
83
|
-
upsert = insert
|
|
84
|
-
}
|
|
97
|
+
const changeset = state.export()
|
|
85
98
|
|
|
86
|
-
const result = await this.#storage.upsert(state.query, changeset
|
|
99
|
+
const result = await this.#storage.upsert(state.query, changeset)
|
|
87
100
|
|
|
88
101
|
if (result === null) {
|
|
89
|
-
if (state.query.version !== undefined)
|
|
90
|
-
|
|
102
|
+
if (state.query.version !== undefined) {
|
|
103
|
+
throw new StatePreconditionException()
|
|
104
|
+
} else {
|
|
105
|
+
throw new StateNotFoundException()
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
// same as above
|
|
109
|
+
await this.#emission.emit({
|
|
110
|
+
changeset,
|
|
111
|
+
state: result
|
|
112
|
+
})
|
|
91
113
|
}
|
|
92
114
|
|
|
93
|
-
|
|
94
|
-
await this.#emission.emit({ changeset, state: result })
|
|
115
|
+
return result
|
|
95
116
|
}
|
|
96
117
|
}
|
|
97
118
|
|
package/src/transition.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
const { retry } = require('@toa.io/generic')
|
|
4
4
|
|
|
5
5
|
const { Operation } = require('./operation')
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
StateConcurrencyException,
|
|
8
|
+
StateNotFoundException
|
|
9
|
+
} = require('./exceptions')
|
|
7
10
|
|
|
8
11
|
class Transition extends Operation {
|
|
9
12
|
#concurrency
|
|
@@ -23,7 +26,9 @@ class Transition extends Operation {
|
|
|
23
26
|
|
|
24
27
|
store.scope = request.query ? await this.query(request.query) : this.scope.init()
|
|
25
28
|
|
|
26
|
-
if (store.scope === null)
|
|
29
|
+
if (store.scope === null) {
|
|
30
|
+
throw new StateNotFoundException()
|
|
31
|
+
}
|
|
27
32
|
|
|
28
33
|
store.state = store.scope.get()
|
|
29
34
|
}
|
|
@@ -35,11 +40,13 @@ class Transition extends Operation {
|
|
|
35
40
|
|
|
36
41
|
scope.set(state)
|
|
37
42
|
|
|
38
|
-
const
|
|
43
|
+
const result = await this.scope.commit(scope)
|
|
39
44
|
|
|
40
|
-
if (
|
|
41
|
-
if (this.#concurrency === 'retry')
|
|
42
|
-
|
|
45
|
+
if (result === false) {
|
|
46
|
+
if (this.#concurrency === 'retry')
|
|
47
|
+
return retry()
|
|
48
|
+
else
|
|
49
|
+
throw new StateConcurrencyException()
|
|
43
50
|
}
|
|
44
51
|
}
|
|
45
52
|
|
|
@@ -33,7 +33,7 @@ describe('argument', () => {
|
|
|
33
33
|
_version: 0
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
expect(entity.get()).toStrictEqual(expected)
|
|
36
|
+
expect(entity.get()).toStrictEqual(expect.objectContaining(expected))
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
it('should set state', () => {
|
|
@@ -54,29 +54,12 @@ it('should provide event', () => {
|
|
|
54
54
|
|
|
55
55
|
const event = entity.event()
|
|
56
56
|
|
|
57
|
-
expect(event).toEqual({
|
|
57
|
+
expect(event).toEqual(expect.objectContaining({
|
|
58
58
|
state,
|
|
59
59
|
origin,
|
|
60
|
-
changeset: {
|
|
60
|
+
changeset: expect.objectContaining({
|
|
61
61
|
foo: 'new value',
|
|
62
62
|
_version: 1
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('should define `id` as readonly', async () => {
|
|
68
|
-
const origin = fixtures.state()
|
|
69
|
-
const entity = new Entity(fixtures.schema, origin)
|
|
70
|
-
const state = entity.get()
|
|
71
|
-
|
|
72
|
-
expect(() => (state.id = 1)).toThrow('assign to read only property')
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
it('should seal id', async () => {
|
|
76
|
-
const origin = fixtures.state()
|
|
77
|
-
const entity = new Entity(fixtures.schema, origin)
|
|
78
|
-
const state = entity.get()
|
|
79
|
-
const redefine = () => Object.defineProperty(state, 'id', { writable: true })
|
|
80
|
-
|
|
81
|
-
expect(redefine).toThrow('redefine property')
|
|
63
|
+
})
|
|
64
|
+
}))
|
|
82
65
|
})
|
package/types/locator.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export class Locator
|
|
1
|
+
export class Locator{
|
|
2
2
|
public readonly name: string
|
|
3
3
|
public readonly namespace: string
|
|
4
4
|
|
|
5
5
|
public readonly id: string
|
|
6
6
|
public readonly label: string
|
|
7
7
|
public readonly uppercase: string
|
|
8
|
+
public readonly lowercase: string
|
|
8
9
|
|
|
9
10
|
constructor (name: string, namespace?: string)
|
|
10
11
|
|