npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

viceroy

v1.5.20

Published

Viceroy is a data ORM for node that works with any data source.

Downloads

236

Readme

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 = require('viceroy');
var viceroyMongo = require('viceroy-mongo');

// Provide viceroy with a configured instance of
// viceroy-mongo.
viceroy.use(viceroyMongo({
  host: 'viceroy.com',
  port: 8567,
  database: 'viceroy'
}));

// TODO: Register some models here.

// Attempt to connect to the database.
viceroy.connect(function(err) {
  if(err) { throw err; }

  // Note: we are now connected to the database.
  // At this point we could read and write to the
  // database, however we don't have any models
  // so lets look at defining those.

});

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 = require('util');

// ...

// Reference a shorthand for viceroy.Model.
var Model = viceroy.Model;

// Define a contact model that inherits from
// model.
function Contact(data) {

  // setup the instance
  Model.apply(this, arguments);

  // define a schema
  this.addSchema({
    name: {
      first: String,
      last: String
    },
    birthday: {
      day: Number,
      month: Number,
      year: Number
    }
  });

  // define some relations.
  this.hasMany('Contact', 'friends');
  this.hasMany('Contact', 'colleges');
  this.hasOne('Contact', 'mother');
  this.hasOne('Contact', 'father');

}
util.inherits(Contact, Model);

// register the model with viceroy
viceroy.model(Contact);

// ...

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.model('Contact');

// create a new contact
var robert = new Contact({
  name: {
    first: 'Robert',
    last: 'Hurst'
  },
  birthdate: {
    day: 28,
    month: 1,
    year: 1990
  }
});

// save it to the database
robert.save(function(err) {

  // Because Viceroy updates models upon save,
  // we now have an id for our new contact.
  robert._id;

});

// remove everyone born in 1985
Contact.find({ 'birthdate.year': 1985 }, function(err, contacts) {
  if(err) { throw err; }
  contacts.remove(function(err) {
    if(err) { throw err; }
  });
});

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 = require('util');
var viceroy = require('viceroy');
var viceroyMongo = require('viceroy-mongo');

// Reference a shorthand for viceroy.Model.
var Model = viceroy.Model;

// Provide viceroy with a configured instance of
// viceroy-mongo.
viceroy.use(viceroyMongo({
  host: 'viceroy.com',
  port: 8567,
  database: 'viceroy'
}));

// Define a contact model that inherits from
// model.
function Contact(data) {

  // setup the instance
  Model.call(this, data);

  // define a schema
  this.schema({
    name: {
      first: String,
      last: String
    },
    birthday: {
      day: Number,
      month: Number,
      year: Number
    }
  });

  // define some relations.
  this.hasMany('Contact', 'friends');
  this.hasMany('Contact', 'colleges');
  this.hasOne('Contact', 'mother');
  this.hasOne('Contact', 'father');

}
util.inherits(Contact, Model);

// register the model with viceroy
viceroy.model(Contact);

// Attempt to connect to the database.
viceroy.connect(function(err) {
  if(err) { throw err; }

  // create a new contact
  var robert = new Contact({
    name: {
      first: 'Robert',
      last: 'Hurst'
    },
    birthdate: {
      day: 28,
      month: 1,
      year: 1990
    }
  });

  // save it to the database
  robert.save(function(err) {

    // Because Viceroy updates models upon save,
    // we now have an id for our new contact.
    robert._id;

  });

  // remove everyone born in 1985
  Contact.find({ 'birthdate.year': 1985 }, function(err, contacts) {
    if(err) { throw err; }
    contacts.remove(function(err) {
      if(err) { throw err; }
    });
  });

});

Docs

please stand by...

Creating Middleware

Middleware must return the following object to viceroy.use.

{
  find: function(query, opts, callback) {},
  insert: function(query, opts, callback) {},
  update: function(query, delta, opts, callback) {},
  remove: function(query, opts, callback) {}
}