The main ZooDb object

import { ZooDb } from '@phfaist/zoodb';
class ZooDb(options)

The main database class.

The constructor accepts a single object with the following properties:

  • processors - list of DB processors to install. See module Database processors.

  • schema_validator - [Optional] You can specify a schema validator, which should provide the method validate(object, schema) (such as a Validator object instance from the jsonschema npm package).

If you specify a schema validator, objects that are added to the database will automatically be validated against the schema using the provided validator.

Note that if you use a YamlDbDataLoader() to load your data, then that class already performs schema validation so you don’t need to validate your objects here again. In this case, you may simply leave schema_validator be null or undefined.

ZooDb.object_types

A list of object types that are stored in this database (read-only property).

ZooDb.objects

Read-only properties to access the dictionary of objects. Access objects as stored_object = zoodbobject.objects[object_type][object_id].

ZooDb.schemas

Read-only properties to access the dictionary of schemas.

ZooDb.data_dump(options)

Produce a serializable data dump of the contents of the database.

WARNING: Depending on the options provided here and any installed database processors, YOU MIGHT BE GETTING ACCESS TO THE RAW OBJECTS IN THE DATABASE. For instance, you might have access to the original FLMFragment instances if you’re using a FLM content processor and provide the relevant options. ANY MODIFICATIONS MIGHT AFFECT THE ORIGINAL DATABASE CONTENT.

This method will call all database processors’ process_data_dump() methods to ensure that the database is prepared for serialization.

ZooDb.install_zoo_loader_handler(zoo_loader_handler)

Install a zoo loader handler.

The loader handler’s task is to invoke a dbdataloader (e.g. YamlDbDataLoader()) to obtain the database data from some source (e.g. YAML files) and then to properly initialize and validate the zoo with that data.

The zoo_loader_handler is pretty much expected to be a ZooDbDataLoaderHandler() instance. If you specify null here, any existing loader handler will be removed.

The reason for splitting off the logic of the loader handler is to avoid bloating the ZooDb class definition, especially in case a user would like to create a barebones ZooDb, e.g. with hard-coded JSON data, without reloading features, in which case neither a loader nor a loader handler are not necessary.

ZooDb.load(options)

Load or reload the zoo, using the loader handler that was installed via install_zoo_loader_handler().

ZooDb.load_data(db)

Initialize the database processors and then directly load objects whose data is given in the argument dictionary db. We ensure that the objects are copies of the probvided data, by serializing them & deserializing them again from JSON.

Note that this method is meant to load raw data that needs to be processed. You are not likely to be able to load data dumped using data_dump(), unless you dumped that data by using raw data dump options and skipping the db processors’ fiddling. See data_dump() for details.

Arguments:
  • db (Object) – an object with keys ‘schemas’ and ‘objects’.

ZooDb.schema(object_type)

Get the schema associated with a given object_type. Returns the schema object.

ZooDb.validate()

Can be overridden to proceed to validatation of the zoo. E.g., you can enforce any constraints, sanity checks, etc.

The base class applies checks that all IDs are unique modulo the normalizer set as constructor argument.

(Schema validation doesn’t happen here, it happens instead directly when loading the data in load_data() because the check can be performed earlier. The ID uniqueness check happens here because it must act globally on the database.)