Viceroy
Control your empire
Battlefy introduces Viceroy. An ORM to rule over your data. Viceroy uses 'middleware' to connect models to data sources. Middleware allows Viceroy to read and write to many different types of databases. Middleware can also allow data filtering and analytics.
Viceroy Supports hasOne and hasMany relationships, easing the noSQL experience and allowing to you work on what you care about rather than accessing your data.
Getting started
Using Viceroy is easy. Lets get you familiar with the library and how to use it. We'll go through a few key examples of how to connect to your database, define a model, and interact with your database. In this example we will be using a mongo database via the viceroy-mongo
middleware.
Connecting to your Database
Viceroy uses middleware to connect to databases. Currently mongo is the only supported database, however Redis, and Postgres are planned. We would also like to encurage others to author middleware for Viceroy, so feel free. If you do author your own middleware, please let us know so we can add it to our list.
Below we will pass our database information to viceroy mongo and connect to the database.
// Require viceroy and viceroy-mongo.var viceroy = ;var viceroyMongo = ; // Provide viceroy with a configured instance of// viceroy-mongo.viceroy; // TODO: Register some models here. // Attempt to connect to the database.viceroy;
Creating your First Model
In order to work with our database we need models to abstract our data. Viceroy models are fun to work with and come with a few nice conveniences.
Below we will create a contact model with a schema and some relationships. Then wee will register the model with viceroy so it can read and write to the database.
// ... // Require the util module.var util = ; // ... // Reference a shorthand for viceroy.Model.var Model = viceroyModel; // Define a contact model that inherits from// model. { // setup the instance Model; // define a schema this; // define some relations. this; this; this; this; }util; // register the model with viceroyviceroy; // ...
Reading and Writing to the database
Now that we have a connection, and we know how to author our own models, lets try creating a contact and saving it to the database. We will also query our database for all contacts born in the year 1985, then delete them.
Once registered with viceroy, model classes are outfitted with several static methods, and will update the database when save or remove is called.
// ... // retrieve our contact model.var Contact = viceroy; // create a new contactvar robert = name: first: 'Robert' last: 'Hurst' birthdate: day: 28 month: 1 year: 1990 ; // save it to the databaserobert; // remove everyone born in 1985Contact;
All Together
If we take the code above and put it all together it looks like this.
// Require util, viceroy, and viceroy-mongo.var util = ;var viceroy = ;var viceroyMongo = ; // Reference a shorthand for viceroy.Model.var Model = viceroyModel; // Provide viceroy with a configured instance of// viceroy-mongo.viceroy; // Define a contact model that inherits from// model. { // setup the instance Model; // define a schema this; // define some relations. this; this; this; this; }util; // register the model with viceroyviceroy; // Attempt to connect to the database.viceroy;
Docs
please stand by...
Creating Middleware
Middleware must return the following object to viceroy.use
.
{} {} {} {}