@toa.io/core 1.0.0-alpha.12 → 1.0.0-alpha.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "1.0.0-alpha.12",
3
+ "version": "1.0.0-alpha.14",
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.12",
25
- "@toa.io/generic": "1.0.0-alpha.12",
26
- "@toa.io/yaml": "1.0.0-alpha.12",
24
+ "@toa.io/console": "1.0.0-alpha.14",
25
+ "@toa.io/generic": "1.0.0-alpha.14",
26
+ "@toa.io/yaml": "1.0.0-alpha.14",
27
27
  "error-value": "0.3.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "clone-deep": "4.0.1"
31
31
  },
32
- "gitHead": "897206fbcf724fa88f427b6aee35bff571b2a3a1"
32
+ "gitHead": "8aa52cb97021695885c8dbe64beca26c9665fc8f"
33
33
  }
@@ -1,6 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const { merge, overwrite, newid } = require('@toa.io/generic')
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
- const changeset = this.#state
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
 
@@ -48,7 +48,8 @@ class Entity {
48
48
  #init (id) {
49
49
  const value = {
50
50
  ...this.#schema.defaults({ id }),
51
- _version: 0
51
+ _version: 0,
52
+ _created: Date.now()
52
53
  }
53
54
 
54
55
  this.#set(value)
@@ -61,6 +62,7 @@ class Entity {
61
62
  })
62
63
 
63
64
  if (this.#state !== undefined) {
65
+ value._updated = Date.now()
64
66
  value._version++
65
67
  }
66
68
 
package/src/exceptions.js CHANGED
@@ -89,7 +89,7 @@ for (const [name, code] of Object.entries(codes)) {
89
89
  if (exports[classname] === undefined) {
90
90
  exports[classname] = class extends Exception {
91
91
  constructor (message) {
92
- super(code, message || classname)
92
+ super(code, message ?? classname)
93
93
  }
94
94
  }
95
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) return new Locator(namespace)
43
- else return new Locator(name, namespace)
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/state.js CHANGED
@@ -65,25 +65,18 @@ class State {
65
65
  ok = await this.#storage.store(object)
66
66
 
67
67
  // #20
68
- await this.#emission.emit(event)
68
+ if (ok === true) {
69
+ await this.#emission.emit(event)
70
+ }
69
71
  }
70
72
 
71
73
  return ok
72
74
  }
73
75
 
74
76
  async apply (state) {
75
- const {
76
- changeset,
77
- insert
78
- } = state.export()
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, upsert)
79
+ const result = await this.#storage.upsert(state.query, changeset)
87
80
 
88
81
  if (result === null) {
89
82
  if (state.query.version !== undefined) {
package/src/transition.js CHANGED
@@ -5,9 +5,7 @@ const { retry } = require('@toa.io/generic')
5
5
  const { Operation } = require('./operation')
6
6
  const {
7
7
  StateConcurrencyException,
8
- StateNotFoundException,
9
- DuplicateException,
10
- Exception
8
+ StateNotFoundException
11
9
  } = require('./exceptions')
12
10
 
13
11
  class Transition extends Operation {
@@ -28,7 +26,9 @@ class Transition extends Operation {
28
26
 
29
27
  store.scope = request.query ? await this.query(request.query) : this.scope.init()
30
28
 
31
- if (store.scope === null) throw new StateNotFoundException()
29
+ if (store.scope === null) {
30
+ throw new StateNotFoundException()
31
+ }
32
32
 
33
33
  store.state = store.scope.get()
34
34
  }
@@ -47,16 +47,13 @@ class Transition extends Operation {
47
47
 
48
48
  const result = await this.scope.commit(scope)
49
49
 
50
- if (result === false ||
51
- (result instanceof DuplicateException && result.message.includes('_id'))) {
50
+ if (result === false) {
52
51
  if (this.#concurrency === 'retry') {
53
52
  return retry()
54
53
  } else {
55
54
  throw new StateConcurrencyException()
56
55
  }
57
56
  }
58
-
59
- if (result instanceof Exception) throw result
60
57
  }
61
58
 
62
59
  async #retry (store, retry) {
@@ -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,14 +54,14 @@ 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
- })
63
+ })
64
+ }))
65
65
  })
66
66
 
67
67
  it('should define `id` as readonly', async () => {
@@ -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