modella-mysql
MySQL persistence layer for Modella.
Installation
npm install modella-mysql
Example
var modella = ;var mysql = ; var User = ; User
API
exports(settings, options)
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.
Model.all(query, callback)
Get all models using given query
.
User;
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.
Model.find(id|query, callback)
Find a model by given id
or query
.
User; User;
Model.all(query, callback)
Find models using given query
.
Userall $gt: created_at: lastWeek { // ...};
The users
object looks like:
Model.hasMany(name, params)
Define a "has many" relationship with given name
and params
.
User; // Creates methods: user var post = userposts;
Model.belongsTo(Model, params)
Define a "belongs to" relationship with given Model
.
User; // Creates method: post;
Model.hasAndBelongsToMany(name, params)
Define a "has and belongs to many" relationship with given name
and params
.
User; // Creates methods: user var post = userposts; post;
Querying for relations
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;
Events
mysql before save
User; user;
mysql after save
User; user;
mysql before update
User; user;
mysql after update
User; user;
mysql before remove
User; user;
mysql after remove
User; user;
exports.adapter
MySQL module.
Connection pooling
Models that share a settings object will share a connection pool, exposed via
settings.pool
.
Queries
The query is a subset of mongo-sql.
The type
, columns
, and table
properties are handled by modella-mysql.
Custom table / field names
Custom table names are specified using the tableName
option. For example:
User;
Custom field names are provided by a columnName
property in the attribute
definition. For example:
User ;
Date types
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.
Data formatters
If you need to control exactly how a data-type is determined, set the attribute
definition's dataFormatter
function:
var Event = ; Event
Tests
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