node-mysql-model
A backbone based model for communicating with a MySQL database using felixge/node-mysql.
Install
Install from npm package:
npm install mysql-model
Or install from git:
npm install git://github.com/michalkow/node-mysql-model.git
Usage
Add the mysql-model module to your application :
var mysqlModel = ;
Then create a model that will be main one for your application (all others will extend it):
var MyAppModel = mysqlModel; var Movie = MyAppModel; movie = ; // OR movie = tableName: "movies";
To see complete list of options for creating a connection with the database visit felixge/node-mysql readme.
API
Model Settable Options
tableName
Name of a MySQL table the model will refer to:
var Movie = MyAppModel;
Methods
find
Retrieves records from database
Usage:
movie;movie;movie;movie;movie;movie;
Parameters:
- string method: uses one of find methods
- object conditions: set find conditions
- function callback: returns errors and results
Example:
movie;
save
Saves your model to database
Usage:
movie;movie;movie;movie;
Parameters:
- string where: set condition for WHERE
- function callback: returns errors and results
Example:
movie = name: 'Serenity' director: 'Joss Whedon' language: 'English' year: 2005;// Will create new recordmovie;movie;// Will update record if id existsmovie;
remove
Deletes your model from database and unsets it
Usage:
movie;movie;movie;movie;
Parameters:
- string where: set condition for WHERE
- function callback: returns errors and results
Example:
// Will delete record from database matching id modelmovie;movie;// Will delete records from database matching where conditionmovie;
read
Retrieves record from database and set it to current model
Usage:
movie;movie;movie;movie;
Parameters:
- integer id: Id of record to read
- function callback: returns errors and results
Example:
movie;movie;// ormovie;
query
Runs custom query
Usage:
movie;movie;
Parameters:
- string query: Your custom sql query to run
- function callback: returns errors and results
Example:
movie;
setSQL
Method to replace 'set', when setting results passed back by node-mysql
Usage:
movie;
Parameters:
- object result: Results passed back by find or read
Example:
movie;
'find' methods
'all'
Returns all the records matching conditions
Returns:
- array
Example:
movie;
'count'
Returns number of records matching conditions
Returns:
- integer
Example:
movie;
'first'
Returns first the records matching conditions
Returns:
- object (hash)
Example:
movie;
'field'
Returns field of the first record matching conditions
Returns:
- depends on field type
Example:
movie;
'find' conditions
fields
Fields to select from the table
Accepts:
- array
- string
Example:
movie;// SELECT id, name, year FROM moviesmovie;// SELECT name FROM movies
where
Operators for MySQL WHERE clause.
Accepts:
- string
Example:
movie;// SELECT * FROM movies WHERE year > 1987
group
Operators for MySQL GROUP BY clause.
Accepts:
- array
- string
Example:
movie;// SELECT * FROM movies GROUP BY year, namemovie;// SELECT * FROM movies GROUP BY name
groupDESC
If true, sets descending order for GROUP BY
Accepts:
- boolean
Example:
movie;// SELECT * FROM movies GROUP BY year, name DESC
having
Operators for MySQL HAVING clause.
Accepts:
- string
Example:
movie;// SELECT name, COUNT(name) FROM movies GROUP BY name HAVING COUNT(name) = 1
order
Operators for MySQL ORDER BY clause.
Accepts:
- array
- string
Example:
movie;// SELECT * FROM movies ORDER BY year, namemovie;// SELECT * FROM movies ORDER BY name
orderDESC
If true, sets descending order for ORDER BY
Accepts:
- boolean
Example:
movie;// SELECT * FROM movies ORDER BY year, name DESC
limit
Operators for MySQL LIMIT clause.
Accepts:
- array
- string
Example:
movie;// SELECT * FROM movies LIMIT 0, 30movie;// SELECT * FROM movies LIMIT 10, 40
Todo
- validation
- relations
License
node-mysql-model is released under MIT license.
Credits
node-mysql-model was created by Michał Kowalkowski. You can contact me at kowalkowski.michal@gmail.com