@toa.io/norm 1.0.0-alpha.284 → 1.0.0-alpha.287

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +1 -196
  2. package/package.json +5 -5
  3. package/src/.component/.expand/bridge.js +1 -1
  4. package/src/.component/.expand/entity.js +1 -1
  5. package/src/.component/.expand/events.js +1 -1
  6. package/src/.component/.expand/extensions.js +1 -1
  7. package/src/.component/.expand/operations.js +1 -1
  8. package/src/.component/.expand/properties.js +1 -1
  9. package/src/.component/.expand/receivers.js +3 -2
  10. package/src/.component/.expand/version.js +11 -13
  11. package/src/.component/.normalize/entity.js +7 -0
  12. package/src/.component/.normalize/events.js +4 -1
  13. package/src/.component/.normalize/index.js +1 -0
  14. package/src/.component/.normalize/operations.js +9 -1
  15. package/src/.component/.normalize/receivers.js +4 -1
  16. package/src/.component/collapse.js +30 -6
  17. package/src/.component/defaults.js +3 -4
  18. package/src/.component/dependencies.js +7 -4
  19. package/src/.component/dereference.js +14 -13
  20. package/src/.component/expand.js +11 -2
  21. package/src/.component/extensions.js +2 -1
  22. package/src/.component/index.js +1 -0
  23. package/src/.component/merge.js +3 -5
  24. package/src/.component/migrations.js +57 -0
  25. package/src/.component/normalize.js +9 -1
  26. package/src/.component/schema.yaml +29 -39
  27. package/src/.component/validate.js +23 -3
  28. package/src/.context/.dependencies/connectors.js +3 -3
  29. package/src/.context/.dependencies/describe.js +4 -1
  30. package/src/.context/.dependencies/extensions.js +18 -13
  31. package/src/.context/.dependencies/load.js +6 -4
  32. package/src/.context/.dependencies/resolve.js +1 -1
  33. package/src/.context/dereference.js +3 -1
  34. package/src/.context/normalize.js +5 -0
  35. package/src/.context/schema.yaml +6 -4
  36. package/src/component.js +14 -5
  37. package/src/context.js +9 -5
  38. package/src/entity.js +15 -0
  39. package/src/index.js +1 -0
  40. package/src/shortcuts.js +0 -1
  41. package/test/component/collapse.fixtures.js +28 -34
  42. package/test/component/collapse.test.js +41 -0
  43. package/test/component/dependencies.test.js +6 -6
  44. package/test/component/dereference.fixtures.js +13 -17
  45. package/test/component/dereference.test.js +12 -3
  46. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yaml +2 -0
  47. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yml +2 -0
  48. package/test/component/dummies/migrations/malformed/migrations/0001-one.yaml +1 -0
  49. package/test/component/dummies/migrations/ordered/migrations/0001-first.yml +4 -0
  50. package/test/component/dummies/migrations/ordered/migrations/0002-second.yaml +4 -0
  51. package/test/component/dummies/migrations/ordered/migrations/0010-third.json +1 -0
  52. package/test/component/dummies/migrations/stateless/migrations/0001-one.yaml +2 -0
  53. package/test/component/migrations.test.js +62 -0
  54. package/test/component/normalize.test.js +61 -13
  55. package/test/component/validate.fixtures.js +14 -8
  56. package/test/component/validate.test.js +113 -59
  57. package/test/context/complete.test.js +5 -1
  58. package/test/context/dereference.test.js +3 -2
  59. package/test/context/expand.test.js +4 -2
  60. package/test/context/normalize.test.js +14 -1
  61. package/test/context/validate.test.js +40 -20
  62. package/test/shortcuts.test.js +2 -1
  63. package/types/component.d.ts +14 -3
  64. package/types/context/declaration.d.ts +1 -1
  65. package/types/context.d.ts +10 -9
  66. package/types/entity.d.ts +7 -0
  67. package/types/index.d.ts +1 -0
@@ -37,6 +37,47 @@ describe('entity', () => {
37
37
  collapse(manifest, samples.entity.prototype)
38
38
  assert.deepStrictEqual(manifest, samples.entity.result)
39
39
  })
40
+
41
+ it('should let a component state its own id', () => {
42
+ const prototype = { entity: { properties: { id: { type: 'string' }, VERSION: {} } } }
43
+ const manifest = { name: 'pot', entity: { properties: { id: { type: 'number' } } } }
44
+
45
+ collapse(manifest, prototype)
46
+
47
+ assert.strictEqual(manifest.entity.custom, true)
48
+ assert.deepStrictEqual(manifest.entity.properties.id, { type: 'number' })
49
+ })
50
+
51
+ it('should refuse a component that states a property the runtime writes', () => {
52
+ const prototype = { entity: { properties: { DELETED: { type: 'integer' } } } }
53
+ const manifest = { name: 'pot', entity: { properties: { DELETED: { type: 'integer' } } } }
54
+
55
+ assert.throws(
56
+ () => collapse(manifest, prototype),
57
+ /System property 'DELETED' cannot be overridden/
58
+ )
59
+ })
60
+
61
+ it('should not inherit migrations', () => {
62
+ const manifest = { entity: { migrations: [{ id: '0002', steps: [] }] } }
63
+ const prototype = { entity: { migrations: [{ id: '0001', steps: [] }] } }
64
+
65
+ collapse(manifest, prototype)
66
+
67
+ assert.deepStrictEqual(
68
+ manifest.entity.migrations.map(({ id }) => id),
69
+ ['0002']
70
+ )
71
+ })
72
+
73
+ it('should leave a component with no migrations without any', () => {
74
+ const manifest = {}
75
+ const prototype = { entity: { migrations: [{ id: '0001', steps: [] }] } }
76
+
77
+ collapse(manifest, prototype)
78
+
79
+ assert.strictEqual(manifest.entity?.migrations, undefined)
80
+ })
40
81
  })
41
82
 
42
83
  it('should ignore bindings', () => {
@@ -21,15 +21,15 @@ for (const [_, reference] of [
21
21
  ['package id', '@toa.io/norm'],
22
22
  ['relative path', '../../']
23
23
  ])
24
- describe(`${_}`, () => {
25
- it('should resolve storage', async () => {
26
- component.entity = { storage: reference, schema: {} }
24
+ describe(`${_}`, () => {
25
+ it('should resolve storage', async () => {
26
+ component.entity = { storage: reference, schema: {} }
27
27
 
28
- dependencies(component)
28
+ dependencies(component)
29
29
 
30
- assert.deepStrictEqual(component.entity.storage, NORM)
30
+ assert.deepStrictEqual(component.entity.storage, NORM)
31
+ })
31
32
  })
32
- })
33
33
 
34
34
  it('should resolve toa packages', async () => {
35
35
  component.entity = { storage: '@toa.io/core', schema: {} }
@@ -1,14 +1,12 @@
1
1
  export const source = {
2
2
  entity: {
3
- schema: {
4
- properties: {
5
- foo: {
6
- type: 'string'
7
- },
8
- bar: {
9
- type: 'string',
10
- default: '.foo'
11
- }
3
+ properties: {
4
+ foo: {
5
+ type: 'string'
6
+ },
7
+ bar: {
8
+ type: 'string',
9
+ default: '.foo'
12
10
  }
13
11
  }
14
12
  },
@@ -66,14 +64,12 @@ export const source = {
66
64
 
67
65
  export const target = {
68
66
  entity: {
69
- schema: {
70
- properties: {
71
- foo: {
72
- type: 'string'
73
- },
74
- bar: {
75
- type: 'string'
76
- }
67
+ properties: {
68
+ foo: {
69
+ type: 'string'
70
+ },
71
+ bar: {
72
+ type: 'string'
77
73
  }
78
74
  }
79
75
  },
@@ -20,13 +20,22 @@ it('should dereference', () => {
20
20
 
21
21
  it('should throw on invalid schema reference', () => {
22
22
  source.operations.transit.output.properties.baz = { type: 'string', default: '.' }
23
- assert.throws(() => dereference(source), (error) => /is not defined/.test(error.message))
23
+ assert.throws(
24
+ () => dereference(source),
25
+ (error) => /is not defined/.test(error.message)
26
+ )
24
27
 
25
28
  source.operations.transit.output.properties.baz = { type: 'string', default: '.baz' }
26
- assert.throws(() => dereference(source), (error) => /is not defined/.test(error.message))
29
+ assert.throws(
30
+ () => dereference(source),
31
+ (error) => /is not defined/.test(error.message)
32
+ )
27
33
  })
28
34
 
29
35
  it('should throw on invalid forwarding', () => {
30
36
  source.operations.create.forward = 'foo'
31
- assert.throws(() => dereference(source), (error) => /is not defined/.test(error.message))
37
+ assert.throws(
38
+ () => dereference(source),
39
+ (error) => /is not defined/.test(error.message)
40
+ )
32
41
  })
@@ -0,0 +1,4 @@
1
+ - index:
2
+ name: index_a
3
+ keys:
4
+ a: asc
@@ -0,0 +1,4 @@
1
+ - index:
2
+ name: index_b
3
+ keys:
4
+ b: asc
@@ -0,0 +1 @@
1
+ [{ "delete": { "filter": {} } }]
@@ -0,0 +1,62 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { join } from 'node:path'
4
+
5
+ import { migrations } from '../../src/.component/index.js'
6
+
7
+ const DUMMIES = join(import.meta.dirname, 'dummies/migrations')
8
+
9
+ const entity = () => ({ entity: { schema: { properties: {} } } })
10
+
11
+ describe('migrations', () => {
12
+ it('should read a directory of migrations', async () => {
13
+ const manifest = entity()
14
+
15
+ await migrations(join(DUMMIES, 'ordered'), manifest)
16
+
17
+ assert.deepEqual(
18
+ manifest.entity.migrations.map(({ id }) => id),
19
+ ['0001-first', '0002-second', '0010-third']
20
+ )
21
+ })
22
+
23
+ it('should read whatever extension each was written with', async () => {
24
+ const manifest = entity()
25
+
26
+ await migrations(join(DUMMIES, 'ordered'), manifest)
27
+
28
+ assert.deepEqual(manifest.entity.migrations[0].steps, [
29
+ { index: { name: 'index_a', keys: { a: 'asc' } } }
30
+ ])
31
+ assert.deepEqual(manifest.entity.migrations[2].steps, [{ delete: { filter: {} } }])
32
+ })
33
+
34
+ it('should leave a component with no migrations alone', async () => {
35
+ const manifest = entity()
36
+
37
+ await migrations(join(DUMMIES, 'nothing-here'), manifest)
38
+
39
+ assert.equal(manifest.entity.migrations, undefined)
40
+ })
41
+
42
+ it('should refuse two files resolving to one id', async () => {
43
+ await assert.rejects(
44
+ migrations(join(DUMMIES, 'conflicting'), entity()),
45
+ /has more than one migrations\/0001-one/
46
+ )
47
+ })
48
+
49
+ it('should refuse migrations on a component that stores nothing', async () => {
50
+ await assert.rejects(
51
+ migrations(join(DUMMIES, 'stateless'), {}),
52
+ /declares migrations but stores nothing/
53
+ )
54
+ })
55
+
56
+ it('should refuse a migration that is not a list of steps', async () => {
57
+ await assert.rejects(
58
+ migrations(join(DUMMIES, 'malformed'), entity()),
59
+ /is not a list of steps/
60
+ )
61
+ })
62
+ })
@@ -18,6 +18,63 @@ describe('operations', () => {
18
18
 
19
19
  assert.deepStrictEqual(manifest.operations.add.bindings, manifest.bindings)
20
20
  })
21
+
22
+ it('should state an empty output where none is declared', async () => {
23
+ await normalize(manifest)
24
+
25
+ assert.deepStrictEqual(manifest.operations.add.output, {})
26
+ })
27
+
28
+ it('should scope a computation to none', async () => {
29
+ manifest.operations.compute = { type: 'computation' }
30
+
31
+ await normalize(manifest)
32
+
33
+ assert.deepStrictEqual(manifest.operations.compute.scope, 'none')
34
+ assert.deepStrictEqual(manifest.operations.compute.query, false)
35
+ })
36
+
37
+ it('should scope an effect to none unless it says otherwise', async () => {
38
+ manifest.operations.affect = { type: 'effect' }
39
+ manifest.operations.reach = { type: 'effect', scope: 'objects' }
40
+
41
+ await normalize(manifest)
42
+
43
+ assert.deepStrictEqual(manifest.operations.affect.scope, 'none')
44
+ assert.deepStrictEqual(manifest.operations.reach.scope, 'objects')
45
+ })
46
+ })
47
+
48
+ describe('namespace', () => {
49
+ it('should default to `default`', async () => {
50
+ delete manifest.namespace
51
+
52
+ await normalize(manifest)
53
+
54
+ assert.deepStrictEqual(manifest.namespace, 'default')
55
+ })
56
+ })
57
+
58
+ describe('entity', () => {
59
+ it('should default storage and flags', async () => {
60
+ manifest.entity = { properties: {} }
61
+
62
+ await normalize(manifest)
63
+
64
+ assert.deepStrictEqual(manifest.entity.storage, '@toa.io/storages.mongodb')
65
+ assert.deepStrictEqual(manifest.entity.associated, false)
66
+ assert.deepStrictEqual(manifest.entity.custom, false)
67
+ })
68
+
69
+ it('should allow a date-time on the record', async () => {
70
+ manifest.entity = { properties: { settled: { type: 'string', format: 'date-time' } } }
71
+
72
+ await normalize(manifest)
73
+
74
+ assert.deepStrictEqual(manifest.entity.properties.settled.format, 'date-time')
75
+ })
76
+
77
+
21
78
  })
22
79
 
23
80
  describe('extensions', () => {
@@ -44,30 +101,21 @@ describe('extensions', () => {
44
101
  describe('receivers', () => {
45
102
  it('should substitute default namespace', async () => {
46
103
  manifest.receivers = {
47
- 'messages.created': 'add'
104
+ 'messages.created': { operation: 'add' }
48
105
  }
49
106
 
50
107
  await normalize(manifest)
51
108
 
52
- assert.deepStrictEqual(manifest.receivers, {
53
- 'default.messages.created': 'add'
54
- })
109
+ assert.deepStrictEqual(Object.keys(manifest.receivers), ['default.messages.created'])
55
110
  })
56
111
 
57
112
  it('should not substitute default namespace for foreign events', async () => {
58
- const receiver = {
59
- transition: 'add',
60
- source: 'test'
61
- }
62
-
63
113
  manifest.receivers = {
64
- 'messages.created': receiver
114
+ 'messages.created': { transition: 'add', source: 'test' }
65
115
  }
66
116
 
67
117
  await normalize(manifest)
68
118
 
69
- assert.deepStrictEqual(manifest.receivers, {
70
- 'messages.created': receiver
71
- })
119
+ assert.deepStrictEqual(Object.keys(manifest.receivers), ['messages.created'])
72
120
  })
73
121
  })
@@ -3,13 +3,13 @@ export const ok = {
3
3
  name: 'bar',
4
4
  entity: {
5
5
  storage: 'whatever',
6
- schema: {
7
- properties: {
8
- name: {
9
- type: 'string',
10
- minLength: 1,
11
- maxLength: 128
12
- }
6
+ associated: false,
7
+ custom: false,
8
+ properties: {
9
+ name: {
10
+ type: 'string',
11
+ minLength: 1,
12
+ maxLength: 128
13
13
  }
14
14
  }
15
15
  },
@@ -35,11 +35,15 @@ export const ok = {
35
35
  },
36
36
  compute: {
37
37
  type: 'computation',
38
+ scope: 'none',
39
+ query: false,
38
40
  bridge: 'whatever',
39
41
  bindings: ['@toa.io/bindings.amqp']
40
42
  },
41
43
  affect: {
42
44
  type: 'effect',
45
+ scope: 'none',
46
+ query: false,
43
47
  bridge: 'whatever',
44
48
  bindings: ['@toa.io/bindings.amqp']
45
49
  }
@@ -58,7 +62,9 @@ export const ok = {
58
62
  operation: 'add',
59
63
  bridge: 'whatever',
60
64
  binding: 'amqp',
61
- path: '/somewhere'
65
+ path: '/somewhere',
66
+ conditioned: false,
67
+ adaptive: false
62
68
  }
63
69
  }
64
70
  }