modella-mysql

MySQL storage plugin and persistence layer for Modella.

modella-mysql

MySQL persistence layer for Modella.

npm install modella-mysql
var modella = require('modella');
var mysql = require('modella-mysql');
 
var User = modella('User');
 
User.use(mysql({
  database: 'mydb',
  user: 'root'
});

Create a new Modella plugin with the given database settings and options.

settings same as settings for node-mysql

options

  • tableName The table for this model. Defaults to singularized model name.
  • maxLimit The maximum number of records to select at once. Default is 200.

Get all models using given query.

User.find({ where: { city: 'San Francisco' }}, function(errresult) {
  console.log(result);
  // => { data: [...], limit: 50, offset: 0, total: 43834 } 
});

The callback result is an object with the following properties:

data array of found models
limit maximum number of models returned (page size)
offset offset for pagination
total total number of models found with query

The limit, offset, and total properties are used in building pagination.

Find a model by given id or query.

User.find(5, function(erruser) {
  user.name(); // => Alex 
});
 
User.find({ where: { name: 'Alex' }}, function(erruser) {
  user.name(); // => Alex 
});

Find models using given query.

User.all({ $gt: { created_at: lastWeek} }, function(errusers) {
  // ... 
});

The users object looks like:

{
  "total": 32443,
  "limit": 50,
  "offset": 0,
  "data": [user, user, ...]
}

Define a "has many" relationship with given name and params.

User.hasMany('posts', { model: Post, foreignKey: 'user_id' });
 
// Creates methods: 
 
user.posts(function(errposts) {
  // ... 
})
 
var post = user.posts.create();

Define a "belongs to" relationship with given Model.

User.belongsTo(Post, { as: 'author', foreignKey: 'user_id' });
 
// Creates method: 
 
post.author(function(erruser) {
  // ... 
});

Define a "has and belongs to many" relationship with given name and params.

User.hasAndBelongsToMany(Post, {
  as: 'posts',
  through: PostUser,
  fromKey: 'user_id',
  toKey: 'post_id'
});
 
// Creates methods: 
 
user.posts(function(errposts) {
  // ... 
})
 
var post = user.posts.create();
 
post.author(function(erruser) {
  // ... 
});

Any model with a "has many" relationship can be included in the results for Model.all() and Model.get() by specifying the name of the relationship in the req.query.include parameter. Related models will be added to model.related.

User.find({ id: 1, include: 'posts' }, function(erruser) {
  console.log(user.related.posts);
  // => will contain the post models related to this user. 
});
User.on('mysql before save', function(modelattrs) {
  // ... 
});
 
user.on('mysql before save', function(attrs) {
  // ... 
});
User.on('mysql after save', function(model) {
  // ... 
});
 
user.on('mysql after save', function() {
  // ... 
});
User.on('mysql before update', function(model) {
  // ... 
});
 
user.on('mysql before update', function() {
  // ... 
});
User.on('mysql after update', function(model) {
  // ... 
});
 
user.on('mysql after update', function() {
  // ... 
});
User.on('mysql before remove', function(model) {
  // ... 
});
 
user.on('mysql before remove', function() {
  // ... 
});
User.on('mysql after remove', function(model) {
  // ... 
});
 
user.on('mysql after remove', function() {
  // ... 
});

MySQL module.

Models that share a settings object will share a connection pool, exposed via settings.pool.

The query is a subset of mongo-sql. The type, columns, and table properties are handled by modella-mysql.

Custom table names are specified using the tableName option. For example:

User.use(mysql({
  database: 'mydb',
  user: 'root'
}, {
  tableName: 'users'
}));

Custom field names are provided by a columnName property in the attribute definition. For example:

User
  .attr('id')
  .attr('firstName', {
    type: 'string',
    length: 255,
    columnName: 'first_name'
  })
  .attr('lastName', {
    type: 'string',
    length: 255,
    columnName: 'last_name'
  });

Attributes with type: "date" will be handled based on the columnType property. This property can either be "datetime", "timestamp", or "integer", corresponding to MySQL column type.

If you need to control exactly how a data-type is determined, set the attribute definition's dataFormatter function:

var Event = modella('Event');
 
Event.attr('date', { dataFormatterfunction(valueEvent) {
  value = Math.floor(value.getTime() / 1000);
  return value;
});

Tests are written with mocha and should using BDD-style assertions.

Tests require an accessible MySQL server. Configure the database using the following set of environment variables:

export NODE_TEST_MYSQL_HOST="127.0.0.1"
export NODE_TEST_MYSQL_USER="root"
export NODE_TEST_MYSQL_PASSWORD=""
export NODE_TEST_MYSQL_PORT=3306

Run the tests with npm:

npm test