The Database Unit

libflitter/database/DatabaseUnit

exports:  class DatabaseUnit

class DatabaseUnit

extends:  libflitter/Unit

Creates Mongoose models from defined schemata and connects to the database.

constructor(String models_directory = './app/models')

overrides:  super.constructor()         returns:  this

Uses the path package to resolve the fully-qualified path to the models directory and stores it in this.directory, which is where model schemata should be placed. Creates a new instance of this.

go(express app, function next)

overrides:  super.go()         returns:  undefined

Recursively iterates over all files in this.directory and gets the model schemata from them. Then, creates Mongoose models for each schema using the name parsed from the file. Model names are parsed from the schema file name and follow the Flitter convention for sub-directory naming. For example, the schema file models/auth/User.model.js would create a model with the name auth:User. Creates global access functions for these models and schemata. Next, it tries to connect to the MongoDB using the credentials specified in the database config. The Database context is created here and the next function in the stack is called under it.

provides()

overrides:  super.provides()         returns:  Object

Returns an Object describing the services, templates, and directories provided by this unit. They are as follows:

Type Key Value Notes
Service Name name "database" The name of the service. This is added to the _flit.services array so other units can check for this unit.
Directory directories.models this.directory The fully-qualified path of the model schemata files. This is added to the _flit.directories store so other units can access it.
Template templates.model Object Template that creates new model schema file in this.directory.
directory

type:  String

Set on initialization. Specifies the fully-qualified path of the model schema files to be processed. Each of these files should export an Object of key-value pairs that comprise the Mongoose schema for the model to be created. Each file's schema is assigned a name based on the name of the file it was retrieved from. This includes sub-directories. As is convention in Flitter, sub-directories are delineated with a : character. So a schema loaded from the file auth/User.model.js would be named auth:Model.

Database Context

This context provides access to the imported schemata and their respective models, as well as a guaranteed-successful database connection.

_flit.orm

type:  Object

Holds variables and functions provided in this context.

_flit.orm.schemas

type:  Object

Contains the imported schemata in key-value pairs such that the keys are the parsed names of the schemata, which follow Flitter's convention for sub-directory naming, and the values are the Object instances that contain the schemata.

_flit.orm.models

type:  Object

Contains the Mongoose models created from the imported schemata in key-value pairs such that the keys are the parsed names of the models, which follow Flitter's convention for sub-directory naming, and the values are instances of mongoose.model.

_flit.orm.connect_string

type:  String

Contains the address used to connect to the MongoDB server. This is usually formatted:

mongodb://user:password@host:port/database

_flit.orm.schema(String name)

returns:  Object

Retrieves a schema with the specified name from _flit.orm.schemas and returns it.

_flit.orm.model(String name)

returns:  mongoose.model

Retrieves a model with the specified name from _flit.orm.models and returns it.

global.schema(String name)

returns:  Object

Mapped to _flit.orm.schema().

global.model(String name)

returns:  mongoose.model

Mapped to _flit.orm.model().

No Comments
Back to top