@toa.io/core 1.0.0-alpha.36 → 1.0.0-alpha.38
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/effect.js +19 -0
- package/src/entities/entity.js +4 -2
- package/src/entities/factory.js +6 -0
- package/src/exceptions.js +5 -10
- package/src/index.js +2 -0
- package/src/operation.js +15 -4
- package/src/state.js +34 -18
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.38",
|
|
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.38",
|
|
25
|
+
"@toa.io/generic": "1.0.0-alpha.38",
|
|
26
|
+
"@toa.io/yaml": "1.0.0-alpha.38",
|
|
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": "fcb0693848f04244383cfa638855724aff75e41f"
|
|
33
33
|
}
|
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
|
package/src/entities/entity.js
CHANGED
|
@@ -46,8 +46,7 @@ class Entity {
|
|
|
46
46
|
#init (id) {
|
|
47
47
|
const value = {
|
|
48
48
|
...this.#schema.defaults({ id }),
|
|
49
|
-
_version: 0
|
|
50
|
-
_created: Date.now()
|
|
49
|
+
_version: 0
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
this.#set(value)
|
|
@@ -62,6 +61,9 @@ class Entity {
|
|
|
62
61
|
value: {}
|
|
63
62
|
})
|
|
64
63
|
|
|
64
|
+
if (!('_created' in value))
|
|
65
|
+
value._created = Date.now()
|
|
66
|
+
|
|
65
67
|
if (this.#state !== undefined) {
|
|
66
68
|
value._updated = Date.now()
|
|
67
69
|
value._version++
|
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
|
@@ -49,18 +49,13 @@ class SystemException extends Exception {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
class ContractException extends Exception {
|
|
52
|
-
keyword
|
|
53
|
-
property
|
|
54
|
-
schema
|
|
55
|
-
path
|
|
56
|
-
|
|
57
52
|
constructor (code, error) {
|
|
58
|
-
super(code || codes.Contract, error
|
|
53
|
+
super(code || codes.Contract, typeof error === 'string' ? error : error?.message)
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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]
|
|
64
59
|
}
|
|
65
60
|
}
|
|
66
61
|
|
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/operation.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Connector } = require('./connector')
|
|
4
|
-
const { SystemException } = require('./exceptions')
|
|
5
|
-
const { Entity } = require('./entities/entity')
|
|
4
|
+
const { SystemException, RequestContractException } = require('./exceptions')
|
|
6
5
|
const { Readable } = require('node:stream')
|
|
7
6
|
|
|
8
7
|
class Operation extends Connector {
|
|
@@ -28,13 +27,22 @@ class Operation extends Connector {
|
|
|
28
27
|
|
|
29
28
|
async invoke (request) {
|
|
30
29
|
try {
|
|
31
|
-
if (request.authentic !== true)
|
|
32
|
-
|
|
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)
|
|
33
39
|
|
|
34
40
|
const store = { request }
|
|
35
41
|
|
|
36
42
|
return await this.process(store)
|
|
37
43
|
} catch (e) {
|
|
44
|
+
console.error(e)
|
|
45
|
+
|
|
38
46
|
const exception = e instanceof Error ? new SystemException(e) : e
|
|
39
47
|
|
|
40
48
|
return { exception }
|
|
@@ -73,6 +81,9 @@ class Operation extends Connector {
|
|
|
73
81
|
async commit () {}
|
|
74
82
|
|
|
75
83
|
async query (query) {
|
|
84
|
+
if (query === undefined)
|
|
85
|
+
throw new RequestContractException('Request query is required')
|
|
86
|
+
|
|
76
87
|
return this.scope[this.#scope](query)
|
|
77
88
|
}
|
|
78
89
|
}
|
package/src/state.js
CHANGED
|
@@ -1,49 +1,47 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { empty } = require('@toa.io/generic')
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
StatePreconditionException,
|
|
7
|
-
StateNotFoundException
|
|
8
|
-
} = require('./exceptions')
|
|
3
|
+
const { empty, newid } = require('@toa.io/generic')
|
|
4
|
+
const { StatePreconditionException, StateNotFoundException } = require('./exceptions')
|
|
9
5
|
|
|
10
6
|
class State {
|
|
11
7
|
#associated
|
|
12
8
|
#storage
|
|
13
|
-
#
|
|
9
|
+
#entities
|
|
14
10
|
#emission
|
|
15
11
|
|
|
16
12
|
constructor (storage, entity, emission, associated) {
|
|
17
13
|
this.#storage = storage
|
|
18
|
-
this.#
|
|
14
|
+
this.#entities = entity
|
|
19
15
|
this.#emission = emission
|
|
20
16
|
this.#associated = associated === true
|
|
21
17
|
}
|
|
22
18
|
|
|
23
19
|
init (id) {
|
|
24
|
-
return this.#
|
|
20
|
+
return this.#entities.init(id)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fit (values) {
|
|
24
|
+
return this.#entities.fit(values)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
async object (query) {
|
|
28
28
|
const record = await this.#storage.get(query)
|
|
29
29
|
|
|
30
30
|
if (record === null) {
|
|
31
|
-
if (this.#associated && query.id !== undefined && query.version === undefined)
|
|
31
|
+
if (this.#associated && query.id !== undefined && query.version === undefined)
|
|
32
32
|
return this.init(query.id)
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
else if (query.version !== undefined)
|
|
34
|
+
throw new StatePreconditionException()
|
|
35
35
|
|
|
36
|
-
if (record === null) {
|
|
37
36
|
return null
|
|
38
|
-
} else
|
|
39
|
-
return this.#
|
|
40
|
-
}
|
|
37
|
+
} else
|
|
38
|
+
return this.#entities.object(record)
|
|
41
39
|
}
|
|
42
40
|
|
|
43
41
|
async objects (query) {
|
|
44
42
|
const recordset = await this.#storage.find(query)
|
|
45
43
|
|
|
46
|
-
return this.#
|
|
44
|
+
return this.#entities.objects(recordset)
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
async stream (query) {
|
|
@@ -51,13 +49,31 @@ class State {
|
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
changeset (query) {
|
|
54
|
-
return this.#
|
|
52
|
+
return this.#entities.changeset(query)
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
none () {
|
|
58
56
|
return null
|
|
59
57
|
}
|
|
60
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
|
+
|
|
61
77
|
async commit (state) {
|
|
62
78
|
const event = state.event()
|
|
63
79
|
|