@toa.io/core 1.0.0-alpha.2 → 1.0.0-alpha.20
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/contract/schemas/query.yaml +1 -2
- package/src/entities/changeset.js +8 -12
- package/src/entities/entity.js +18 -3
- package/src/exceptions.js +2 -1
- package/src/locator.js +7 -2
- package/src/query/criteria.js +3 -3
- package/src/state.js +29 -28
- package/src/transition.js +20 -7
- package/test/entities/entity.fixtures.js +5 -2
- package/test/entities/entity.test.js +11 -5
- 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.20",
|
|
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.20",
|
|
25
|
+
"@toa.io/generic": "1.0.0-alpha.20",
|
|
26
|
+
"@toa.io/yaml": "1.0.0-alpha.20",
|
|
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": "dcde3389a86ab38e377ebc5ab18e5d5a512b85bc"
|
|
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
|
|
|
@@ -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
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
merge,
|
|
5
|
+
overwrite,
|
|
6
|
+
newid
|
|
7
|
+
} = require('@toa.io/generic')
|
|
4
8
|
const { EntityContractException } = require('../exceptions')
|
|
5
9
|
|
|
6
10
|
class Changeset {
|
|
@@ -26,21 +30,13 @@ class Changeset {
|
|
|
26
30
|
|
|
27
31
|
if (error !== null) throw new EntityContractException(error)
|
|
28
32
|
|
|
33
|
+
value._updated = Date.now()
|
|
34
|
+
|
|
29
35
|
this.#state = value
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
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
|
|
39
|
+
return this.#state
|
|
44
40
|
}
|
|
45
41
|
}
|
|
46
42
|
|
package/src/entities/entity.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
difference,
|
|
5
|
+
newid
|
|
6
|
+
} = require('@toa.io/generic')
|
|
4
7
|
|
|
5
8
|
const { EntityContractException } = require('../exceptions')
|
|
6
9
|
|
|
@@ -43,13 +46,25 @@ class Entity {
|
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
#init (id) {
|
|
46
|
-
const value = {
|
|
49
|
+
const value = {
|
|
50
|
+
...this.#schema.defaults({ id }),
|
|
51
|
+
_version: 0,
|
|
52
|
+
_created: Date.now()
|
|
53
|
+
}
|
|
47
54
|
|
|
48
55
|
this.#set(value)
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
#set (value) {
|
|
52
|
-
Object.defineProperty(value, 'id', {
|
|
59
|
+
Object.defineProperty(value, 'id', {
|
|
60
|
+
writable: false,
|
|
61
|
+
configurable: false
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
if (this.#state !== undefined) {
|
|
65
|
+
value._updated = Date.now()
|
|
66
|
+
value._version++
|
|
67
|
+
}
|
|
53
68
|
|
|
54
69
|
this.#state = value
|
|
55
70
|
}
|
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
|
|
@@ -88,7 +89,7 @@ for (const [name, code] of Object.entries(codes)) {
|
|
|
88
89
|
if (exports[classname] === undefined) {
|
|
89
90
|
exports[classname] = class extends Exception {
|
|
90
91
|
constructor (message) {
|
|
91
|
-
super(code, message
|
|
92
|
+
super(code, message ?? classname)
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
}
|
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/query/criteria.js
CHANGED
|
@@ -33,9 +33,9 @@ const coerce = (node, properties) => {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const COERCE = {
|
|
36
|
-
number: Number,
|
|
37
|
-
integer: parseInt,
|
|
38
|
-
boolean:
|
|
36
|
+
number: Number.parseFloat,
|
|
37
|
+
integer: Number.parseInt,
|
|
38
|
+
boolean: (value) => value === 'true'
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
exports.criteria = criteria
|
package/src/state.js
CHANGED
|
@@ -4,27 +4,20 @@ const { empty } = require('@toa.io/generic')
|
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
6
|
StatePreconditionException,
|
|
7
|
-
StateNotFoundException
|
|
8
|
-
StateInitializationException
|
|
7
|
+
StateNotFoundException
|
|
9
8
|
} = require('./exceptions')
|
|
10
9
|
|
|
11
|
-
/**
|
|
12
|
-
* @implements {toa.core.State}
|
|
13
|
-
*/
|
|
14
10
|
class State {
|
|
15
|
-
|
|
11
|
+
#associated
|
|
16
12
|
#storage
|
|
17
|
-
|
|
18
|
-
/** @type {toa.core.entity.Factory} */
|
|
19
13
|
#entity
|
|
20
14
|
#emission
|
|
21
|
-
#dependent
|
|
22
15
|
|
|
23
|
-
constructor (storage, entity, emission,
|
|
16
|
+
constructor (storage, entity, emission, associated) {
|
|
24
17
|
this.#storage = storage
|
|
25
18
|
this.#entity = entity
|
|
26
19
|
this.#emission = emission
|
|
27
|
-
this.#
|
|
20
|
+
this.#associated = associated === true
|
|
28
21
|
}
|
|
29
22
|
|
|
30
23
|
init (id) {
|
|
@@ -35,12 +28,16 @@ class State {
|
|
|
35
28
|
const record = await this.#storage.get(query)
|
|
36
29
|
|
|
37
30
|
if (record === null) {
|
|
38
|
-
if (this.#
|
|
39
|
-
|
|
31
|
+
if (this.#associated && query.id !== undefined && query.version === undefined) {
|
|
32
|
+
return this.init(query.id)
|
|
33
|
+
} else if (query.version !== undefined) throw new StatePreconditionException()
|
|
40
34
|
}
|
|
41
35
|
|
|
42
|
-
if (record === null)
|
|
43
|
-
|
|
36
|
+
if (record === null) {
|
|
37
|
+
return null
|
|
38
|
+
} else {
|
|
39
|
+
return this.#entity.object(record)
|
|
40
|
+
}
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
async objects (query) {
|
|
@@ -68,30 +65,34 @@ class State {
|
|
|
68
65
|
ok = await this.#storage.store(object)
|
|
69
66
|
|
|
70
67
|
// #20
|
|
71
|
-
|
|
68
|
+
if (ok === true) {
|
|
69
|
+
await this.#emission.emit(event)
|
|
70
|
+
}
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
return ok
|
|
75
74
|
}
|
|
76
75
|
|
|
77
76
|
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
|
-
}
|
|
77
|
+
const changeset = state.export()
|
|
85
78
|
|
|
86
|
-
const result = await this.#storage.upsert(state.query, changeset
|
|
79
|
+
const result = await this.#storage.upsert(state.query, changeset)
|
|
87
80
|
|
|
88
81
|
if (result === null) {
|
|
89
|
-
if (state.query.version !== undefined)
|
|
90
|
-
|
|
82
|
+
if (state.query.version !== undefined) {
|
|
83
|
+
throw new StatePreconditionException()
|
|
84
|
+
} else {
|
|
85
|
+
throw new StateNotFoundException()
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
// same as above
|
|
89
|
+
await this.#emission.emit({
|
|
90
|
+
changeset,
|
|
91
|
+
state: result
|
|
92
|
+
})
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
|
|
94
|
-
await this.#emission.emit({ changeset, state: result })
|
|
95
|
+
return result
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
98
|
|
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,23 +26,33 @@ 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
|
}
|
|
30
35
|
|
|
31
36
|
async commit (store) {
|
|
32
|
-
const {
|
|
37
|
+
const {
|
|
38
|
+
scope,
|
|
39
|
+
state,
|
|
40
|
+
reply,
|
|
41
|
+
retry
|
|
42
|
+
} = store
|
|
33
43
|
|
|
34
44
|
if (reply.error !== undefined) return
|
|
35
45
|
|
|
36
46
|
scope.set(state)
|
|
37
47
|
|
|
38
|
-
const
|
|
48
|
+
const result = await this.scope.commit(scope)
|
|
39
49
|
|
|
40
|
-
if (
|
|
41
|
-
if (this.#concurrency === 'retry')
|
|
42
|
-
|
|
50
|
+
if (result === false) {
|
|
51
|
+
if (this.#concurrency === 'retry') {
|
|
52
|
+
return retry()
|
|
53
|
+
} else {
|
|
54
|
+
throw new StateConcurrencyException()
|
|
55
|
+
}
|
|
43
56
|
}
|
|
44
57
|
}
|
|
45
58
|
|
|
@@ -16,10 +16,13 @@ const state = () => ({
|
|
|
16
16
|
_created: generate(),
|
|
17
17
|
_updated: generate(),
|
|
18
18
|
_deleted: generate(),
|
|
19
|
-
_version:
|
|
19
|
+
_version: 0
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
const failed = () => ({
|
|
22
|
+
const failed = () => ({
|
|
23
|
+
...state(),
|
|
24
|
+
fail: true
|
|
25
|
+
})
|
|
23
26
|
|
|
24
27
|
exports.schema = schema
|
|
25
28
|
exports.state = state
|
|
@@ -28,9 +28,12 @@ describe('argument', () => {
|
|
|
28
28
|
it('should provide initial state if no argument passed', () => {
|
|
29
29
|
const entity = new Entity(fixtures.schema)
|
|
30
30
|
const defaults = fixtures.schema.defaults.mock.results[0].value
|
|
31
|
-
const expected = {
|
|
31
|
+
const expected = {
|
|
32
|
+
...defaults,
|
|
33
|
+
_version: 0
|
|
34
|
+
}
|
|
32
35
|
|
|
33
|
-
expect(entity.get()).toStrictEqual(expected)
|
|
36
|
+
expect(entity.get()).toStrictEqual(expect.objectContaining(expected))
|
|
34
37
|
})
|
|
35
38
|
|
|
36
39
|
it('should set state', () => {
|
|
@@ -51,11 +54,14 @@ it('should provide event', () => {
|
|
|
51
54
|
|
|
52
55
|
const event = entity.event()
|
|
53
56
|
|
|
54
|
-
expect(event).toEqual({
|
|
57
|
+
expect(event).toEqual(expect.objectContaining({
|
|
55
58
|
state,
|
|
56
59
|
origin,
|
|
57
|
-
changeset: {
|
|
58
|
-
|
|
60
|
+
changeset: expect.objectContaining({
|
|
61
|
+
foo: 'new value',
|
|
62
|
+
_version: 1
|
|
63
|
+
})
|
|
64
|
+
}))
|
|
59
65
|
})
|
|
60
66
|
|
|
61
67
|
it('should define `id` as readonly', async () => {
|
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
|
|