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

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/package.json +5 -5
  2. package/src/.component/.expand/bridge.js +1 -1
  3. package/src/.component/.expand/entity.js +1 -1
  4. package/src/.component/.expand/events.js +1 -1
  5. package/src/.component/.expand/extensions.js +1 -1
  6. package/src/.component/.expand/operations.js +1 -1
  7. package/src/.component/.expand/properties.js +1 -1
  8. package/src/.component/.expand/receivers.js +3 -2
  9. package/src/.component/.expand/version.js +11 -13
  10. package/src/.component/.normalize/entity.js +7 -0
  11. package/src/.component/.normalize/events.js +4 -1
  12. package/src/.component/.normalize/index.js +1 -0
  13. package/src/.component/.normalize/operations.js +9 -1
  14. package/src/.component/.normalize/receivers.js +4 -1
  15. package/src/.component/collapse.js +14 -6
  16. package/src/.component/defaults.js +3 -4
  17. package/src/.component/dependencies.js +7 -4
  18. package/src/.component/dereference.js +14 -13
  19. package/src/.component/expand.js +11 -2
  20. package/src/.component/extensions.js +2 -1
  21. package/src/.component/index.js +1 -0
  22. package/src/.component/merge.js +3 -5
  23. package/src/.component/migrations.js +57 -0
  24. package/src/.component/normalize.js +9 -1
  25. package/src/.component/schema.yaml +29 -39
  26. package/src/.component/validate.js +23 -3
  27. package/src/.context/.dependencies/connectors.js +3 -3
  28. package/src/.context/.dependencies/describe.js +4 -1
  29. package/src/.context/.dependencies/extensions.js +18 -13
  30. package/src/.context/.dependencies/load.js +6 -4
  31. package/src/.context/.dependencies/resolve.js +1 -1
  32. package/src/.context/dereference.js +3 -1
  33. package/src/.context/normalize.js +5 -0
  34. package/src/.context/schema.yaml +6 -4
  35. package/src/component.js +14 -5
  36. package/src/context.js +9 -5
  37. package/src/entity.js +15 -0
  38. package/src/index.js +1 -0
  39. package/src/shortcuts.js +0 -1
  40. package/test/component/collapse.fixtures.js +28 -34
  41. package/test/component/collapse.test.js +21 -0
  42. package/test/component/dependencies.test.js +6 -6
  43. package/test/component/dereference.fixtures.js +13 -17
  44. package/test/component/dereference.test.js +12 -3
  45. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yaml +2 -0
  46. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yml +2 -0
  47. package/test/component/dummies/migrations/malformed/migrations/0001-one.yaml +1 -0
  48. package/test/component/dummies/migrations/ordered/migrations/0001-first.yml +4 -0
  49. package/test/component/dummies/migrations/ordered/migrations/0002-second.yaml +4 -0
  50. package/test/component/dummies/migrations/ordered/migrations/0010-third.json +1 -0
  51. package/test/component/dummies/migrations/stateless/migrations/0001-one.yaml +2 -0
  52. package/test/component/migrations.test.js +62 -0
  53. package/test/component/normalize.test.js +51 -13
  54. package/test/component/validate.fixtures.js +14 -8
  55. package/test/component/validate.test.js +113 -59
  56. package/test/context/complete.test.js +5 -1
  57. package/test/context/dereference.test.js +3 -2
  58. package/test/context/expand.test.js +4 -2
  59. package/test/context/normalize.test.js +14 -1
  60. package/test/context/validate.test.js +40 -20
  61. package/test/shortcuts.test.js +2 -1
  62. package/types/component.d.ts +14 -3
  63. package/types/context/declaration.d.ts +1 -1
  64. package/types/context.d.ts +10 -9
  65. package/types/entity.d.ts +7 -0
  66. package/types/index.d.ts +1 -0
  67. package/CHANGELOG.md +0 -203
@@ -4,7 +4,9 @@ import { readFileSync } from 'node:fs'
4
4
  import { yaml } from '@toa.io/generic'
5
5
  import * as schemas from '@toa.io/schemas'
6
6
 
7
- const object = yaml.load(readFileSync(path.resolve(import.meta.dirname, 'schema.yaml'), 'utf8'))
7
+ const object = yaml.load(
8
+ readFileSync(path.resolve(import.meta.dirname, 'schema.yaml'), 'utf8')
9
+ )
8
10
  const schema = schemas.schema(object)
9
11
 
10
12
  export const validate = async (manifest) => {
@@ -12,10 +14,24 @@ export const validate = async (manifest) => {
12
14
 
13
15
  if (error) throw error
14
16
 
17
+ if (manifest.entity !== undefined) entity(manifest)
15
18
  if (manifest.events !== undefined) await events(manifest)
16
19
  if (manifest.receivers !== undefined) receivers(manifest)
17
20
  }
18
21
 
22
+ /** What only the entity's own declaration can answer: whether a name it uses is one it declares. */
23
+ const entity = (manifest) => {
24
+ const { properties, required, blank } = manifest.entity
25
+
26
+ for (const name of required ?? [])
27
+ if (properties[name] === undefined)
28
+ throw new Error(`Entity requires property '${name}', which is not defined`)
29
+
30
+ for (const name of Object.keys(blank ?? {}))
31
+ if (properties[name] === undefined)
32
+ throw new Error(`Entity blank names property '${name}', which is not defined`)
33
+ }
34
+
19
35
  const events = async (manifest) => {
20
36
  for (const [label, event] of Object.entries(manifest.events)) {
21
37
  const { properties } = await import(event.binding)
@@ -29,11 +45,15 @@ const events = async (manifest) => {
29
45
  const receivers = (manifest) => {
30
46
  for (const [locator, receiver] of Object.entries(manifest.receivers)) {
31
47
  if (manifest.operations?.[receiver.operation] === undefined) {
32
- throw new Error(`Receiver '${locator}' refers to undefined operation '${receiver.operation}'`)
48
+ throw new Error(
49
+ `Receiver '${locator}' refers to undefined operation '${receiver.operation}'`
50
+ )
33
51
  }
34
52
 
35
53
  if (!TYPES.has(manifest.operations[receiver.operation].type)) {
36
- throw new Error(`Receiver '${locator}' must refer to an operation of the allowed types: ${Array.from(TYPES).join(', ')}`)
54
+ throw new Error(
55
+ `Receiver '${locator}' must refer to an operation of the allowed types: ${Array.from(TYPES).join(', ')}`
56
+ )
37
57
  }
38
58
  }
39
59
  }
@@ -3,10 +3,10 @@ import { resolve } from 'node:path'
3
3
  export const connectors = (context, extracted) => {
4
4
  const connectors = {}
5
5
 
6
- const components = (context.components === undefined
6
+ const components =
7
+ (context.components === undefined
7
8
  ? extracted
8
- : context.components.concat(extracted)
9
- ) ?? []
9
+ : context.components.concat(extracted)) ?? []
10
10
 
11
11
  for (const component of components) {
12
12
  if (component.entity !== undefined) {
@@ -3,4 +3,7 @@
3
3
  * @param {Object} manifest
4
4
  * @returns {toa.norm.context.dependencies.Instance}
5
5
  */
6
- export const describe = (component, manifest = undefined) => ({ locator: component.locator, manifest })
6
+ export const describe = (component, manifest = undefined) => ({
7
+ locator: component.locator,
8
+ manifest
9
+ })
@@ -10,7 +10,12 @@ export const extensions = async (context) => {
10
10
  const declared = await extractDeclaredServices(context, extensions)
11
11
 
12
12
  const extracted = declared.concat(
13
- await extractExtensionComponents(components.concat(declared), extensions, context.annotations))
13
+ await extractExtensionComponents(
14
+ components.concat(declared),
15
+ extensions,
16
+ context.annotations
17
+ )
18
+ )
14
19
 
15
20
  components.push(...extracted)
16
21
 
@@ -27,7 +32,7 @@ export const extensions = async (context) => {
27
32
  return { extensions, components: extracted }
28
33
  }
29
34
 
30
- async function extractDeclaredServices (context, extensions) {
35
+ async function extractDeclaredServices(context, extensions) {
31
36
  const extracted = []
32
37
 
33
38
  for (const composition of context.compositions ?? [])
@@ -35,18 +40,20 @@ async function extractDeclaredServices (context, extensions) {
35
40
  if (reference in extensions) continue
36
41
 
37
42
  try {
38
- extracted.push(...await extract(reference, extensions, context.annotations))
43
+ extracted.push(...(await extract(reference, extensions, context.annotations)))
39
44
  } catch (e) {
40
- throw new Error(`Composition '${composition.name}' lists '${reference}', ` +
41
- `which cannot be resolved: ${e.message}`, { cause: e })
45
+ throw new Error(
46
+ `Composition '${composition.name}' lists '${reference}', ` +
47
+ `which cannot be resolved: ${e.message}`,
48
+ { cause: e }
49
+ )
42
50
  }
43
51
  }
44
52
 
45
53
  return extracted
46
54
  }
47
55
 
48
- async function extractExtensionComponents (components, extensions, annotations) {
49
-
56
+ async function extractExtensionComponents(components, extensions, annotations) {
50
57
  const extracted = []
51
58
 
52
59
  for (const component of components) {
@@ -55,12 +62,11 @@ async function extractExtensionComponents (components, extensions, annotations)
55
62
  for (const reference of Object.keys(component.extensions)) {
56
63
  if (reference in extensions) continue
57
64
 
58
- extracted.push(...await extract(reference, extensions, annotations))
65
+ extracted.push(...(await extract(reference, extensions, annotations)))
59
66
  }
60
67
  }
61
68
 
62
- if (extracted.length === 0)
63
- return extracted
69
+ if (extracted.length === 0) return extracted
64
70
 
65
71
  const deeper = await extractExtensionComponents(extracted, extensions, annotations)
66
72
 
@@ -75,7 +81,7 @@ async function extractExtensionComponents (components, extensions, annotations)
75
81
  * @param {object} [annotations]
76
82
  * @returns {Promise<Array<toa.norm.Component>>}
77
83
  */
78
- async function extract (reference, extensions, annotations) {
84
+ async function extract(reference, extensions, annotations) {
79
85
  extensions[reference] = []
80
86
 
81
87
  const { metadata, module: mod } = await loadDependency(reference)
@@ -86,8 +92,7 @@ async function extract (reference, extensions, annotations) {
86
92
  const annotation = annotations?.[metadata?.name ?? reference]
87
93
  const extracted = []
88
94
 
89
- for (const path of mod.components(annotation).paths)
90
- extracted.push(await load(path))
95
+ for (const path of mod.components(annotation).paths) extracted.push(await load(path))
91
96
 
92
97
  return extracted
93
98
  }
@@ -11,16 +11,18 @@ const require = createRequire(import.meta.url)
11
11
  * @param {string} path
12
12
  * @returns {Promise<{ metadata: object, module: object }>}
13
13
  */
14
- export async function load (path) {
14
+ export async function load(path) {
15
15
  const metadata = loadMetadata(path)
16
16
  const module = await loadModule(path)
17
17
 
18
18
  return { metadata, module }
19
19
  }
20
20
 
21
- function loadMetadata (reference) {
21
+ function loadMetadata(reference) {
22
22
  try {
23
- return JSON.parse(readFileSync(require.resolve(join(reference, 'package.json')), 'utf8'))
23
+ return JSON.parse(
24
+ readFileSync(require.resolve(join(reference, 'package.json')), 'utf8')
25
+ )
24
26
  } catch {
25
27
  return null
26
28
  }
@@ -30,7 +32,7 @@ function loadMetadata (reference) {
30
32
  * @param {string} reference
31
33
  * @returns {Promise<object>}
32
34
  */
33
- async function loadModule (reference) {
35
+ async function loadModule(reference) {
34
36
  const path = require.resolve(reference)
35
37
 
36
38
  return await import(pathToFileURL(path).href)
@@ -38,7 +38,7 @@ export const resolve = async (references, annotations = {}) => {
38
38
  * @param {string} reference
39
39
  * @returns {object | null}
40
40
  */
41
- async function optional (reference) {
41
+ async function optional(reference) {
42
42
  try {
43
43
  return (await load(reference)).module
44
44
  } catch {
@@ -12,7 +12,9 @@ export const dereference = (context) => {
12
12
  const component = components[id]
13
13
 
14
14
  if (component === undefined)
15
- throw new Error(`Composition '${composition.name}' lists an unknown component '${id}'.`)
15
+ throw new Error(
16
+ `Composition '${composition.name}' lists an unknown component '${id}'.`
17
+ )
16
18
 
17
19
  return component
18
20
  })
@@ -12,4 +12,9 @@ export const normalize = (context) => {
12
12
  }
13
13
 
14
14
  if (typeof context.registry === 'string') context.registry = { base: context.registry }
15
+
16
+ // what an image is built for, where the Context does not say
17
+ if (context.registry !== undefined) context.registry.platforms ??= PLATFORMS
15
18
  }
19
+
20
+ const PLATFORMS = ['linux/amd64', 'linux/arm/v7', 'linux/arm64']
@@ -40,10 +40,12 @@ properties:
40
40
  minItems: 1
41
41
  items:
42
42
  type: string
43
- default:
44
- - linux/amd64
45
- - linux/arm/v7
46
- - linux/arm64
43
+ services:
44
+ description: >
45
+ Where an extension service's image comes from. `build`, which absence means,
46
+ builds it into `registry.base` the way a composition is built. `published` uses
47
+ the image the extension ships, which is neither built nor pushed.
48
+ enum: [build, published]
47
49
  # required: [base]
48
50
  compositions:
49
51
  type: array
package/src/component.js CHANGED
@@ -5,7 +5,17 @@ import { yaml as jsyaml } from '@toa.io/generic'
5
5
  import { find } from '@toa.io/generic'
6
6
  import { Locator } from '@toa.io/core'
7
7
 
8
- import { expand, merge, validate, collapse, dereference, defaults, normalize, extensions } from './.component/index.js'
8
+ import {
9
+ expand,
10
+ merge,
11
+ migrations,
12
+ validate,
13
+ collapse,
14
+ dereference,
15
+ defaults,
16
+ normalize,
17
+ extensions
18
+ } from './.component/index.js'
9
19
 
10
20
  export const component = async (path) => {
11
21
  const manifest = await load(path)
@@ -23,7 +33,7 @@ const load = async (path, base, proto = false) => {
23
33
  if (base !== undefined) path = find(path, base, MANIFEST)
24
34
 
25
35
  const file = join(path, MANIFEST)
26
- const manifest = await read(file) ?? {}
36
+ const manifest = (await read(file)) ?? {}
27
37
 
28
38
  manifest.path = path
29
39
 
@@ -31,6 +41,7 @@ const load = async (path, base, proto = false) => {
31
41
  await expand(manifest)
32
42
 
33
43
  await merge(path, manifest)
44
+ await migrations(path, manifest)
34
45
 
35
46
  if (manifest.prototype !== null) {
36
47
  const prototype = await load(manifest.prototype, path, true)
@@ -46,8 +57,6 @@ const load = async (path, base, proto = false) => {
46
57
 
47
58
  const MANIFEST = 'manifest.toa.yaml'
48
59
 
49
-
50
-
51
60
  /**
52
61
  * Reads a YAML file, resolving anchors into distinct objects so that
53
62
  * mutating one node cannot reach another.
@@ -55,7 +64,7 @@ const MANIFEST = 'manifest.toa.yaml'
55
64
  * @param {string} path
56
65
  * @return {Promise<object>}
57
66
  */
58
- async function read (path) {
67
+ async function read(path) {
59
68
  const object = jsyaml.load(await readFile(path, 'utf8'))
60
69
 
61
70
  return jsyaml.load(jsyaml.dump(object, { noRefs: true, lineWidth: -1 }))
package/src/context.js CHANGED
@@ -6,7 +6,14 @@ import { yaml as jsyaml } from '@toa.io/generic'
6
6
 
7
7
  import { component } from './component.js'
8
8
 
9
- import { dependencies, normalize, complete, dereference, expand, validate } from './.context/index.js'
9
+ import {
10
+ dependencies,
11
+ normalize,
12
+ complete,
13
+ dereference,
14
+ expand,
15
+ validate
16
+ } from './.context/index.js'
10
17
 
11
18
  export const context = async (root, environment = process.env.TOA_ENV) => {
12
19
  const path = resolve(root, CONTEXT)
@@ -34,11 +41,8 @@ export const context = async (root, environment = process.env.TOA_ENV) => {
34
41
  const CONTEXT = 'context.toa.yaml'
35
42
  const COMPONENTS = 'components/*'
36
43
 
37
-
38
44
  const GLOB = { onlyDirectories: true, absolute: true }
39
45
 
40
-
41
-
42
46
  /**
43
47
  * Reads a YAML file, resolving anchors into distinct objects so that
44
48
  * mutating one node cannot reach another.
@@ -46,7 +50,7 @@ const GLOB = { onlyDirectories: true, absolute: true }
46
50
  * @param {string} path
47
51
  * @return {Promise<object>}
48
52
  */
49
- async function read (path) {
53
+ async function read(path) {
50
54
  const object = jsyaml.load(await readFile(path, 'utf8'))
51
55
 
52
56
  return jsyaml.load(jsyaml.dump(object, { noRefs: true, lineWidth: -1 }))
package/src/entity.js ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * What a component declares about its records is a property map and a list of the properties a
3
+ * whole one has. A validator wants a schema, so it is assembled here rather than declared: one
4
+ * that a stored record must fit, and one for a changeset, which is whatever subset of the same
5
+ * properties an assignment writes.
6
+ */
7
+ export const schema = (entity) =>
8
+ entity.required === undefined
9
+ ? changeset(entity)
10
+ : { type: 'object', properties: entity.properties, required: entity.required }
11
+
12
+ export const changeset = (entity) => ({
13
+ type: 'object',
14
+ properties: entity.properties
15
+ })
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { context } from './context.js'
2
2
  export { component } from './component.js'
3
3
  export * as shortcuts from './shortcuts.js'
4
+ export * as entity from './entity.js'
package/src/shortcuts.js CHANGED
@@ -41,7 +41,6 @@ export const SHORTCUTS = {
41
41
  node: '@toa.io/bridges.node',
42
42
  bash: '@toa.io/bridges.bash',
43
43
  mongodb: '@toa.io/storages.mongodb',
44
- sql: '@toa.io/storages.sql',
45
44
  queues: '@toa.io/storages.queues',
46
45
  exposition: '@toa.io/extensions.exposition',
47
46
  realtime: '@toa.io/extensions.realtime',
@@ -3,51 +3,45 @@ import { generate } from 'randomstring'
3
3
  const entity = {
4
4
  manifest: {
5
5
  entity: {
6
- schema: {
7
- properties: {
8
- foo: {
9
- type: 'string'
10
- },
11
- bar: {
12
- type: 'integer'
13
- }
6
+ properties: {
7
+ foo: {
8
+ type: 'string'
14
9
  },
15
- required: ['foo']
16
- }
10
+ bar: {
11
+ type: 'integer'
12
+ }
13
+ },
14
+ required: ['foo']
17
15
  }
18
16
  },
19
17
  prototype: {
20
18
  entity: {
21
- schema: {
22
- properties: {
23
- foo: {
24
- default: 'ok'
25
- },
26
- baz: {
27
- type: 'boolean'
28
- }
19
+ properties: {
20
+ foo: {
21
+ minLength: 1
29
22
  },
30
- required: ['baz']
31
- }
23
+ baz: {
24
+ type: 'boolean'
25
+ }
26
+ },
27
+ required: ['baz']
32
28
  }
33
29
  },
34
30
  result: {
35
31
  entity: {
36
- schema: {
37
- properties: {
38
- foo: {
39
- type: 'string',
40
- default: 'ok'
41
- },
42
- bar: {
43
- type: 'integer'
44
- },
45
- baz: {
46
- type: 'boolean'
47
- }
32
+ properties: {
33
+ foo: {
34
+ type: 'string',
35
+ minLength: 1
48
36
  },
49
- required: ['foo', 'baz']
50
- }
37
+ bar: {
38
+ type: 'integer'
39
+ },
40
+ baz: {
41
+ type: 'boolean'
42
+ }
43
+ },
44
+ required: ['foo', 'baz']
51
45
  }
52
46
  }
53
47
  }
@@ -37,6 +37,27 @@ describe('entity', () => {
37
37
  collapse(manifest, samples.entity.prototype)
38
38
  assert.deepStrictEqual(manifest, samples.entity.result)
39
39
  })
40
+
41
+ it('should not inherit migrations', () => {
42
+ const manifest = { entity: { migrations: [{ id: '0002', steps: [] }] } }
43
+ const prototype = { entity: { migrations: [{ id: '0001', steps: [] }] } }
44
+
45
+ collapse(manifest, prototype)
46
+
47
+ assert.deepStrictEqual(
48
+ manifest.entity.migrations.map(({ id }) => id),
49
+ ['0002']
50
+ )
51
+ })
52
+
53
+ it('should leave a component with no migrations without any', () => {
54
+ const manifest = {}
55
+ const prototype = { entity: { migrations: [{ id: '0001', steps: [] }] } }
56
+
57
+ collapse(manifest, prototype)
58
+
59
+ assert.strictEqual(manifest.entity?.migrations, undefined)
60
+ })
40
61
  })
41
62
 
42
63
  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
+ })