A Node.js model system for mysql and redis
npm install roads-modelsRoads-Models
============
A Node.js model system for mysql and redis
The documentation is a work in progress as I continue to add features and test this in production environments. Check out the example for more information.
Your first step is to create the ModelModule. Base this off the example. Each module will have a handful of properties.
###Saving
When saving a model, you will also have a validationError function, which will be called if validation fails on any of the model fields. The first parameter is an object representing the invalid fields.
###Loading
When loading a collection, you will have a preload function. Preloading allows you to perform mysql joins in node instead of mysql (and works better with caching).
The preload method takes a single parameter, the field name which should be preloaded. This field in the model definition should define which model to load, and what key to assign the model to.
To create new ConnectionTypes, you must follow these steps
1. Extend the require('Roads-Models').Connection.ConnectionType object.
2. Create the connection in the constructor. The constructor takes your config object, and assigns the connection to this.connection
3. In the case of an error, you should call this._ready(err);
4. On success you should call this._ready(null, this.connection);
5. You must also implement the disconnect method, so all connections can be closed when necessary
Create the database using the following:
create database roadsmodelstest;
create user roadsmodelstest identified by 'roads';
grant all on roadsmodelstest.* to roadsmodelstest;
use roadsmodelstest;
CREATE TABLE user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
email varchar(128) NOT NULL,
password varchar(64) NOT NULL,
name varchar(128) NOT NULL,
role varchar(32) NOT NULL DEFAULT 'user',
PRIMARY KEY (id)
);
CREATE TABLE preload (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
user_id int(10) unsigned NOT NULL,
PRIMARY KEY (id)
);
insert into user (email, password, name, role) values ('aaron@dashron.com', '1234', 'aaron', 'user');
insert into user (email, password, name, role) values ('zena@dashron.com', '1234', 'zena', 'user');
insert into preload (user_id) values ((select id from user where name = 'aaron'));
insert into preload (user_id) values ((select id from user where name = 'zena'));
insert into preload (user_id) values ((select id from user where name = 'aaron'));
insert into preload (user_id) values ((select id from user where name = 'zena'));
insert into preload (user_id) values ((select id from user where name = 'zena'));
insert into preload (user_id) values ((select id from user where name = 'aaron'));
Now from within the project directory, run ``node example/index.js``
#TODO
Connection pools