genesis

A simple library for creating data models in Node.

genesis

A simple library for creating data models in Node.

npm install genesis

Creates a new Model class.

  • options -- A hash of options describing a new data model.

Options:

  • index -- The name of the attribute that should be used as the unique identifier of an instance. Defaults to null.
  • methods -- A hash of methods to add to instances. Defaults to {}.
  • parent -- The parent class that the new model should extend. Defaults to Model.
  • schema -- A Joi schema describing acceptable model attributes. Defaults to Joi.any().

Returns a constructor function.

  • options -- A hash of options describing additional attributes for the new data model.

Options:

  • index -- The name of the attribute that should be used as the unique identifier of an instance. Defaults to the parent model's index.
  • methods -- A hash of additional methods to add to instances. This hash is merged with the methods from the parent model.
  • schema -- A Joi schema describing acceptable model attributes. This schema is merged with the parent model's schema.

Creates a new model instance. A model is an immutable object with a pre-defined set of attributes and methods.

  • schema -- A Joi schema describing the acceptable attributes.
  • options -- A hash of attributes to assign to the instance.

Returns a new model instance.

Mappers implement persistence strategies for models and can be used to store and retrieve model instances. All mappers follow the some API and all models that are used with mappers must define an index.

Persists a new model instance. An error is thrown if the instance already exists in the underlying data store.

  • instance -- A Model instance to persist.

Returns the instance.

Removes a model instance from the data store. An error is thrown if the instance has not been persisted in the data store.

  • instance -- A Model instance to persist.

Returns the instance.

Finds all persisted instances of Constructor that matches the query object.

  • Constructor -- A Model constructor function indicating the type of instance that should be retrieved.
  • query -- A hash of attributes used to match persisted instances.

Returns a list of matching instances.

Finds one matching instance of Constructor that matches the query object.

  • Constructor -- A Model constructor function indicating the type of instance that should be retrived.
  • query -- A hash of attributes used to match persisted instances.

Returns a single matching instance.

Persists a new version of a previously persisted model instance. An error is thrown if a previous version is not in the data store.

  • instance -- The new Model instance.

Returns the instance.

Genesis comes with the following mapper implementations:

The MemoryMapper stores all instances in memory.