@servable/tools 2.1.0 → 2.3.0
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 +1 -1
- package/src/domain/protocolLoader/v1.0.0.js +11 -0
- package/src/domain/protocolLoader/v1.1.0.js +11 -0
- package/src/index.js +2 -0
- package/src/schema/artifact/index.js +15 -1
- package/src/schema/build/buildProtocol/classStruct/protocolInClassClassSchema.js +9 -0
- package/src/schema/ensuredIndexes.js +81 -0
- package/src/schema/plan/index.js +21 -0
- package/src/schema/types/index.js +17 -5
- package/types/domain/protocolLoader/v1.0.0.d.ts +1 -0
- package/types/domain/protocolLoader/v1.1.0.d.ts +1 -0
- package/types/index.d.ts +2 -1
- package/types/schema/ensuredIndexes.d.ts +5 -0
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"source": "src/index.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
7
|
-
"version": "2.
|
|
7
|
+
"version": "2.3.0",
|
|
8
8
|
"description": "Servable tools is a utility that builds a protocol's manifest and documents it.",
|
|
9
9
|
"author": "Servable Community",
|
|
10
10
|
"license": "ISC",
|
|
@@ -525,6 +525,17 @@ export default class ProtocolLoaderV1_0_0 extends BaseClass {
|
|
|
525
525
|
return null
|
|
526
526
|
}
|
|
527
527
|
|
|
528
|
+
// The indexes an engine makes itself, after boot (`ensuredIndexes`: unique, partial, on a dotted path...), that
|
|
529
|
+
// this protocol gives every class it is attached to. Its own classes declare theirs in `managed.classes[]`.
|
|
530
|
+
// See schema/ensuredIndexes.js.
|
|
531
|
+
async schemaEnsuredIndexes(props) {
|
|
532
|
+
const schema = await this.schemaRaw(props)
|
|
533
|
+
if (schema && schema.target) {
|
|
534
|
+
return schema.target.ensuredIndexes
|
|
535
|
+
}
|
|
536
|
+
return null
|
|
537
|
+
}
|
|
538
|
+
|
|
528
539
|
async schemaRaw(props = {}) {
|
|
529
540
|
// Memoized for the same reason v1.1.0.js's does - schemaFields()/schemaIndexes()/
|
|
530
541
|
// schemaClassLevelPermissions() each call this independently for every protocol attached to
|
|
@@ -520,6 +520,17 @@ export default class ProtocolLoaderV1_1_0 extends BaseClass {
|
|
|
520
520
|
return null
|
|
521
521
|
}
|
|
522
522
|
|
|
523
|
+
// The indexes an engine makes itself, after boot (`ensuredIndexes`: unique, partial, on a dotted path...), that
|
|
524
|
+
// this protocol gives every class it is attached to. Its own classes declare theirs in `managed.classes[]`.
|
|
525
|
+
// See schema/ensuredIndexes.js.
|
|
526
|
+
async schemaEnsuredIndexes(props) {
|
|
527
|
+
const schema = await this.schemaRaw(props)
|
|
528
|
+
if (schema && schema.target) {
|
|
529
|
+
return schema.target.ensuredIndexes
|
|
530
|
+
}
|
|
531
|
+
return null
|
|
532
|
+
}
|
|
533
|
+
|
|
523
534
|
async schemaRaw(props = {}) {
|
|
524
535
|
// Memoized: schemaFields()/schemaIndexes()/schemaClassLevelPermissions() each call this
|
|
525
536
|
// independently for every protocol attached to every class, so without caching the same
|
package/src/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import plan from './schema/plan/index.js'
|
|
|
10
10
|
import generateSchemaTypes from './schema/types/index.js'
|
|
11
11
|
import buildProtocolResources from './schema/protocolResources/index.js'
|
|
12
12
|
import generateProtocolResourceTypes from './schema/protocolResources/generateTypes.js'
|
|
13
|
+
import ensuredIndexesProblemsOf from './schema/ensuredIndexes.js'
|
|
13
14
|
// import validateProtocol from './lib/config/validate/protocol/index.js'
|
|
14
15
|
import cleanProtocols from './lib/cleanProtocols.js'
|
|
15
16
|
|
|
@@ -36,6 +37,7 @@ export {
|
|
|
36
37
|
generateSchemaTypes,
|
|
37
38
|
buildProtocolResources,
|
|
38
39
|
generateProtocolResourceTypes,
|
|
40
|
+
ensuredIndexesProblemsOf,
|
|
39
41
|
cleanProtocols,
|
|
40
42
|
generateGithubReadme,
|
|
41
43
|
defineConfig,
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import buildSchema from '../build/index.js'
|
|
12
12
|
import stableStringify from '../../lib/stableStringify.js'
|
|
13
13
|
import crypto from 'crypto'
|
|
14
|
+
import ensuredIndexesProblemsOf from '../ensuredIndexes.js'
|
|
14
15
|
|
|
15
16
|
const ARTIFACT_VERSION = 1
|
|
16
17
|
|
|
@@ -20,6 +21,7 @@ const normalizeClass = (classSchema) => {
|
|
|
20
21
|
fields = {},
|
|
21
22
|
indexes = {},
|
|
22
23
|
classLevelPermissions = {},
|
|
24
|
+
ensuredIndexes,
|
|
23
25
|
} = classSchema || {}
|
|
24
26
|
|
|
25
27
|
return {
|
|
@@ -27,6 +29,9 @@ const normalizeClass = (classSchema) => {
|
|
|
27
29
|
fields,
|
|
28
30
|
indexes,
|
|
29
31
|
classLevelPermissions,
|
|
32
|
+
// Only when the class declares some (schema/ensuredIndexes.js): an artifact, and its hash, stay exactly what they
|
|
33
|
+
// were for every class that declares none.
|
|
34
|
+
...(ensuredIndexes && Object.keys(ensuredIndexes).length ? { ensuredIndexes } : {}),
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
|
|
@@ -93,9 +98,18 @@ export const normalizeArtifact = ({ appProtocol, protocols }) => {
|
|
|
93
98
|
// drift at boot (decision #4): the server calls normalizeArtifact() on the result of its own
|
|
94
99
|
// buildSchema() run to hash "what the sources say right now," rather than calling this and
|
|
95
100
|
// re-running buildSchema() a second time.
|
|
101
|
+
//
|
|
102
|
+
// It refuses (throws) an artifact whose `ensuredIndexes` are malformed, so `servable schema build`/`plan` stop on
|
|
103
|
+
// them before a commit; the boot, which calls normalizeArtifact(), never does (the engine logs such an index and
|
|
104
|
+
// carries on without it).
|
|
96
105
|
export const compileArtifact = async ({ servableConfig }) => {
|
|
97
106
|
const result = await buildSchema({ servableConfig })
|
|
98
|
-
|
|
107
|
+
const artifact = normalizeArtifact(result)
|
|
108
|
+
const problems = ensuredIndexesProblemsOf({ classes: artifact.classes })
|
|
109
|
+
if (problems.length > 0) {
|
|
110
|
+
throw new Error(`The schema's ensuredIndexes cannot be made:\n→ ${problems.join('\n→ ')}`)
|
|
111
|
+
}
|
|
112
|
+
return artifact
|
|
99
113
|
}
|
|
100
114
|
|
|
101
115
|
// Content-only equality - ignores generatedAt, so re-running the build with no real schema
|
|
@@ -22,6 +22,15 @@ export default async (props) => {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// Guarded: a loader written before `ensuredIndexes` existed has no such method.
|
|
26
|
+
const _ensuredIndexes = await protocol.loader.schemaEnsuredIndexes?.()
|
|
27
|
+
if (_ensuredIndexes && Object.keys(_ensuredIndexes).length) {
|
|
28
|
+
_classSchema.ensuredIndexes = {
|
|
29
|
+
..._classSchema.ensuredIndexes,
|
|
30
|
+
..._ensuredIndexes
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
25
34
|
//#TODO: protocol.loader
|
|
26
35
|
const _classLevelPermissions = await protocol.loader.schemaClassLevelPermissions()
|
|
27
36
|
let __classLevelPermissions
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// `ensuredIndexes`: indexes a class needs that the engine's own schema indexes (`indexes`) cannot express, declared
|
|
2
|
+
// beside them in a schema.json and made by the engine after boot, in the background (never awaited by the boot,
|
|
3
|
+
// never dropped). Engine-agnostic: fields are named as the app names them (`objectId`, a pointer by its own name, a
|
|
4
|
+
// dotted path into an object); the engine maps them to its storage.
|
|
5
|
+
//
|
|
6
|
+
// "ensuredIndexes": {
|
|
7
|
+
// "<name in the database>": {
|
|
8
|
+
// "keys": { "<field or dotted path>": 1 | -1, ... }, required, in order
|
|
9
|
+
// "isUnique": true, optional, false by default
|
|
10
|
+
// "isSparse": false, optional, true by default (rows without the field are left out),
|
|
11
|
+
// never with a partialFilter
|
|
12
|
+
// "partialFilter": { "<field>": { "$exists": true } }, optional, only the rows matching it
|
|
13
|
+
// "fieldTypes": { "<field>": { "type": "Pointer", "targetClass": "X" } }
|
|
14
|
+
// optional, the type of a field the class does not declare
|
|
15
|
+
// }
|
|
16
|
+
// }
|
|
17
|
+
//
|
|
18
|
+
// A protocol declares them on its own classes (`managed.classes[].ensuredIndexes`) or on every class it is attached
|
|
19
|
+
// to (`target.ensuredIndexes`), exactly like `indexes`, so a protocol never names another's class. They ride in the
|
|
20
|
+
// compiled artifact (`servable.schema.json`), so `schema plan` shows them and the boot's drift check covers them; a
|
|
21
|
+
// class that declares none has no `ensuredIndexes` key there, so an app that uses none keeps its artifact's hash.
|
|
22
|
+
|
|
23
|
+
const INDEX_DIRECTIONS = [1, -1]
|
|
24
|
+
const KNOWN_OPTIONS = ['keys', 'isUnique', 'isSparse', 'partialFilter', 'fieldTypes']
|
|
25
|
+
|
|
26
|
+
const isPlainObject = (value) => !!value && typeof value === 'object' && !Array.isArray(value)
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The problems of one declaration, in words; none when it is well formed.
|
|
30
|
+
* @param {any} declaration
|
|
31
|
+
* @returns {string[]}
|
|
32
|
+
*/
|
|
33
|
+
export const problemsOfAnEnsuredIndex = (declaration) => {
|
|
34
|
+
if (!isPlainObject(declaration)) {
|
|
35
|
+
return ['the declaration is not an object']
|
|
36
|
+
}
|
|
37
|
+
const problems = []
|
|
38
|
+
const { keys, isUnique, isSparse, partialFilter, fieldTypes } = declaration
|
|
39
|
+
if (!isPlainObject(keys) || Object.keys(keys).length === 0) {
|
|
40
|
+
problems.push('`keys` names no field')
|
|
41
|
+
} else {
|
|
42
|
+
Object.entries(keys)
|
|
43
|
+
.filter(([, direction]) => !INDEX_DIRECTIONS.includes(direction))
|
|
44
|
+
.forEach(([fieldName]) => problems.push(`the direction of \`${fieldName}\` is neither 1 nor -1`))
|
|
45
|
+
}
|
|
46
|
+
if (isUnique !== undefined && typeof isUnique !== 'boolean') {
|
|
47
|
+
problems.push('`isUnique` is not a boolean')
|
|
48
|
+
}
|
|
49
|
+
if (isSparse !== undefined && typeof isSparse !== 'boolean') {
|
|
50
|
+
problems.push('`isSparse` is not a boolean')
|
|
51
|
+
}
|
|
52
|
+
if (partialFilter !== undefined && !isPlainObject(partialFilter)) {
|
|
53
|
+
problems.push('`partialFilter` is not an object')
|
|
54
|
+
}
|
|
55
|
+
if (partialFilter !== undefined && isSparse === true) {
|
|
56
|
+
problems.push('an index is either sparse or partial, not both')
|
|
57
|
+
}
|
|
58
|
+
if (fieldTypes !== undefined && !isPlainObject(fieldTypes)) {
|
|
59
|
+
problems.push('`fieldTypes` is not an object')
|
|
60
|
+
}
|
|
61
|
+
Object.keys(declaration).filter((name) => !KNOWN_OPTIONS.includes(name)).forEach((name) => problems.push(`\`${name}\` is not an option of an ensured index`))
|
|
62
|
+
return problems
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Every problem of every class's `ensuredIndexes`, each naming its class and index; none when all are well formed.
|
|
67
|
+
* @param {{ classes: any[] }} props
|
|
68
|
+
* @returns {string[]}
|
|
69
|
+
*/
|
|
70
|
+
export const ensuredIndexesProblemsOf = ({ classes }) => (classes || []).flatMap((classSchema) => {
|
|
71
|
+
const { className, ensuredIndexes } = classSchema || {}
|
|
72
|
+
if (ensuredIndexes === undefined) {
|
|
73
|
+
return []
|
|
74
|
+
}
|
|
75
|
+
if (!isPlainObject(ensuredIndexes)) {
|
|
76
|
+
return [`${className}: \`ensuredIndexes\` is not an object`]
|
|
77
|
+
}
|
|
78
|
+
return Object.entries(ensuredIndexes).flatMap(([name, declaration]) => problemsOfAnEnsuredIndex(declaration).map((problem) => `${className}.${name}: ${problem}`))
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
export default ensuredIndexesProblemsOf
|
package/src/schema/plan/index.js
CHANGED
|
@@ -62,6 +62,27 @@ const diffClass = ({ before, after }) => {
|
|
|
62
62
|
}
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
+
// ensuredIndexes (schema/ensuredIndexes.js), the same way: one added is SAFE (the engine makes it after boot, in the
|
|
66
|
+
// background); one removed or changed is BREAKING. A removed one is never dropped by the engine, but a running pod
|
|
67
|
+
// may rely on its uniqueness; a changed one clashes with the index already there under its name until someone
|
|
68
|
+
// drops it on purpose.
|
|
69
|
+
const ensuredIndexNames = new Set([
|
|
70
|
+
...Object.keys(before.ensuredIndexes || {}),
|
|
71
|
+
...Object.keys(after.ensuredIndexes || {}),
|
|
72
|
+
])
|
|
73
|
+
ensuredIndexNames.forEach(indexName => {
|
|
74
|
+
const beforeIndex = before.ensuredIndexes?.[indexName]
|
|
75
|
+
const afterIndex = after.ensuredIndexes?.[indexName]
|
|
76
|
+
if (!beforeIndex && afterIndex) {
|
|
77
|
+
changes.push({ kind: 'ensuredIndexAdded', safe: true, className: before.className, indexName })
|
|
78
|
+
} else if (beforeIndex && !afterIndex) {
|
|
79
|
+
changes.push({ kind: 'ensuredIndexRemoved', safe: false, deprecated: false, className: before.className, indexName })
|
|
80
|
+
} else if (beforeIndex && afterIndex && JSON.stringify(beforeIndex) !== JSON.stringify(afterIndex)) {
|
|
81
|
+
// Order-sensitive on purpose, like `indexes` above: the order of an index's keys is part of the index.
|
|
82
|
+
changes.push({ kind: 'ensuredIndexChanged', safe: false, deprecated: false, className: before.className, indexName })
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
|
|
65
86
|
// classLevelPermissions changes are reported but always SAFE: narrowing a CLP is an access
|
|
66
87
|
// decision, not a data-integrity one - it can't produce the "old pod can't read what's there"
|
|
67
88
|
// failure mode this whole plan/apply split exists to prevent. Flagged, not gated.
|
|
@@ -65,7 +65,11 @@ export interface ServableRelation<T> {
|
|
|
65
65
|
/**
|
|
66
66
|
* Generates one TypeScript interface per class in a `servable.schema.json` artifact, each
|
|
67
67
|
* field typed from its Parse field type (`Pointer`/`Relation` narrowed to the target class
|
|
68
|
-
* when that class is also present in the artifact)
|
|
68
|
+
* when that class is also present in the artifact) - plus a merge into
|
|
69
|
+
* @servable/parse-server-engine's `ServableClassMap`, keyed by class name, powering
|
|
70
|
+
* `Servable.App.Typed.object('ClassName')`/`.query('ClassName')` (see that package's own
|
|
71
|
+
* global.d.ts for why that's a separate, opt-in surface rather than a change to `Object`/`Query`
|
|
72
|
+
* themselves).
|
|
69
73
|
*
|
|
70
74
|
* @param {{ classes: Array<{ className: string, fields: Record<string, { type: string, targetClass?: string }> }> }} artifact
|
|
71
75
|
* - a normalized `servable.schema.json` (or anything with the same `classes` shape).
|
|
@@ -75,9 +79,9 @@ export default function generateSchemaTypes(artifact) {
|
|
|
75
79
|
const classes = artifact?.classes || []
|
|
76
80
|
const knownClassNames = new Set(classes.map(c => c.className))
|
|
77
81
|
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
const sortedClasses = classes.slice().sort((a, b) => a.className.localeCompare(b.className))
|
|
83
|
+
|
|
84
|
+
const interfaces = sortedClasses
|
|
81
85
|
.map(({ className, fields = {} }) => {
|
|
82
86
|
const fieldLines = Object.entries(fields)
|
|
83
87
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
@@ -93,5 +97,13 @@ export default function generateSchemaTypes(artifact) {
|
|
|
93
97
|
})
|
|
94
98
|
.join('\n\n')
|
|
95
99
|
|
|
96
|
-
|
|
100
|
+
// Omitted entirely (rather than merged as an empty interface) when there are no classes at
|
|
101
|
+
// all - matches generateProtocolResourceTypes()'s own ServableServiceCallMap handling.
|
|
102
|
+
const classMapMerge = sortedClasses.length
|
|
103
|
+
? `declare global {\n interface ServableClassMap {\n${sortedClasses
|
|
104
|
+
.map(({ className }) => ` ${JSON.stringify(className)}: ${className}`)
|
|
105
|
+
.join('\n')}\n }\n}\n`
|
|
106
|
+
: ''
|
|
107
|
+
|
|
108
|
+
return [HEADER, interfaces, classMapMerge].filter(Boolean).join('\n')
|
|
97
109
|
}
|
|
@@ -46,6 +46,7 @@ export default class ProtocolLoaderV1_0_0 extends BaseClass {
|
|
|
46
46
|
classesSchemas(props: any): Promise<any>;
|
|
47
47
|
schemaFields(props: any): Promise<any>;
|
|
48
48
|
schemaIndexes(props: any): Promise<any>;
|
|
49
|
+
schemaEnsuredIndexes(props: any): Promise<any>;
|
|
49
50
|
schemaRaw(props?: {}): Promise<any>;
|
|
50
51
|
schemaClassLevelPermissions(props: any): Promise<any>;
|
|
51
52
|
ownProtocols(): Promise<any>;
|
|
@@ -47,6 +47,7 @@ export default class ProtocolLoaderV1_1_0 extends BaseClass {
|
|
|
47
47
|
classesSchemas(props: any): Promise<any>;
|
|
48
48
|
schemaFields(props: any): Promise<any>;
|
|
49
49
|
schemaIndexes(props: any): Promise<any>;
|
|
50
|
+
schemaEnsuredIndexes(props: any): Promise<any>;
|
|
50
51
|
schemaRaw(props?: {}): Promise<any>;
|
|
51
52
|
schemaClassLevelPermissions(props: any): Promise<any>;
|
|
52
53
|
ownProtocols(): Promise<any>;
|
package/types/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import plan from './schema/plan/index.js';
|
|
|
12
12
|
import generateSchemaTypes from './schema/types/index.js';
|
|
13
13
|
import buildProtocolResources from './schema/protocolResources/index.js';
|
|
14
14
|
import generateProtocolResourceTypes from './schema/protocolResources/generateTypes.js';
|
|
15
|
+
import ensuredIndexesProblemsOf from './schema/ensuredIndexes.js';
|
|
15
16
|
import cleanProtocols from './lib/cleanProtocols.js';
|
|
16
17
|
import generateGithubReadme from './document/gateway/generateGithubReadme/index.js';
|
|
17
18
|
import defineConfig from './lib/defineConfig.js';
|
|
@@ -19,4 +20,4 @@ export namespace Domain {
|
|
|
19
20
|
export { Servable };
|
|
20
21
|
}
|
|
21
22
|
import Servable from './domain/servable/index.js';
|
|
22
|
-
export { accessManifest, documentProtocol, extractProtocol, ManifestEnums, ManifestEnumsV1_1_0, buildSchema, validateSchema, compileArtifact, artifactHashOf, normalizeArtifact, plan, generateSchemaTypes, buildProtocolResources, generateProtocolResourceTypes, cleanProtocols, generateGithubReadme, defineConfig };
|
|
23
|
+
export { accessManifest, documentProtocol, extractProtocol, ManifestEnums, ManifestEnumsV1_1_0, buildSchema, validateSchema, compileArtifact, artifactHashOf, normalizeArtifact, plan, generateSchemaTypes, buildProtocolResources, generateProtocolResourceTypes, ensuredIndexesProblemsOf, cleanProtocols, generateGithubReadme, defineConfig };
|