Loading schemas with SchemaLoader¶
Schemas define the object types and structure of the database. See Object schemas for detailed information about schemas in ZooDb.
import { SchemaLoader } from '@phfaist/zoodb/schemaloader';
- class SchemaLoader()¶
Load schema object data from a collection of YAML or JSON source files, and dereference any
{ "$ref": ... }internal and external references.Configuration options:
schemas- an object with the following properties:schemas.schema_root- a filesystem path or URL where to look for schemas.schemas.schema_rel_path- a relative path to prefix any schema name that we might want to look up. It will be interpreted as a path relative to the schema_root.schemas.schema_add_extension- a filename extension to add to the bare schema name when looking up the schema. E.g. ‘.json’ or ‘.yml’.
Example:
schemas: { schema_root: 'https://errorcorrectionzoo.org/', schema_rel_path: '/schemas/', schema_add_extension: '.json', }
schema_names- a list of names of schemas to load for the zoo database. If null or undefined and the schema_root is a filesystem path, then the corresponding directory contents is inspected and files matching the given schema_add_extension are loaded as schemas.fs- the filesystem access handle. We need the method readFile() for any requested filesystem paths. We also need the readdir() method if schema_names is null or undefined.
See also
makeStandardYamlDbDataLoader()for a simplified loading.- SchemaLoader.SchemaLoader¶
- SchemaLoader.load_schema_by_name(schema_name)¶
Load and return a single schema by name, dereferencing any $ref references. Repeated calls for the same name return the cached result without re-reading the file.
This method allows loading an additional schema that is not listed in the constructor’s schema_names config — for example, a supplementary schema needed at runtime.
- Arguments:
schema_name (string) – The bare schema name (without path prefix or file extension) corresponding to a file under the configured schema_root / schema_rel_path.
- Throws:
Error – If the file cannot be read, parsed, or dereferenced.
- Returns:
Promise.<Object> – The resolved JSON Schema object.
- SchemaLoader.load_schemas()¶
Load all schemas specified by the schema_names config property (or, when schema_names is null, by listing all matching files in the schema root directory) and return an object
{ schemas }whose schemas property maps each schema name to its resolved JSON Schema object.Each referenced $ref within a schema is dereferenced automatically. The returned schemas object may be passed directly to
ZooDb.load_data().- Throws:
Error – If schema_names is null but schema_root is not a file: URL, or if any schema file cannot be read or parsed.
- Returns:
Promise.<{schemas: Object}> –