node-mysql-backbone
A backbone based model and collection for communicating with a MySQL database using mysqljs/mysql.
Install
Install from npm package with mysql peer dependecy:
npm install mysql-backbone mysql
Usage
Add the mysql-backbone module to your application and create a model or collection that will be main one for your application (all others will extend it):
var mysql = ;var mysqlBackbone = ; var connection = mysql; connection; var Movie = mysqlBackboneModel; var Movies = mysqlBackboneCollection; var movies = ; var movie = name: 'Serenity' director: 'Joss Whedon' language: 'English' year: 2005; connection;
To see complete list of options for creating a connection with the database visit mysqljs/mysql readme.
API
Collection Settable Options
var Movies = MyAppModel;
Collection Methods
fetch
Retrieves records from database
Usage:
movies;movies;
Parameters:
- object conditions: set find conditions
Example:
movies;
create
Adds new model to collection and the database
Usage:
movies;
Parameters:
- object data: fields for the new object
Example:
movies;
destroy
Deletes models from database and removes them from this collection
Usage:
movies;
Parameters:
- array models: Array of Models or primary keys
Returns:
- object
Example:
movies;
count
Returns number of records matching conditions
Usage:
movies;movies;
Parameters:
- object conditions: set find conditions
Returns:
- integer
Example:
movies;
Model Settable Options
var Movie = MyAppModel;
Model Methods
fetch
Retrieves records from database
Usage:
movie;movie;movie;
Parameters:
- integer id: primary key to fetch
- array fields: array of fields to set in model
Example:
movie
save
Saves your model to database
Usage:
movie;movie;
Parameters:
- integer id: primary key to save
Example:
movie = name: 'Serenity' director: 'Joss Whedon' language: 'English' year: 2005;// Will create new recordmovie;movie;// Will update record if id existsmovie;// Alternativemovie;
destroy
Deletes your model from database and unsets it
movie;movie;
Parameters:
- integer id: primary key to destroy
Example:
// Will delete record from database matching id modelmovie;movie;// Alternativemovie;
'fetch' conditions
fields
Fields to select from the table
Accepts:
- array
- string
Example:
movies;// SELECT id, name, year FROM moviesmovies;// SELECT name FROM movies
where
Operators for MySQL WHERE clause.
Accepts:
- string
Example:
movies;// SELECT * FROM movies WHERE year > 1987
group
Operators for MySQL GROUP BY clause.
Accepts:
- array
- string
Example:
movies;// SELECT * FROM movies GROUP BY year, namemovies;// SELECT * FROM movies GROUP BY name
groupDESC
If true, sets descending order for GROUP BY
Accepts:
- boolean
Example:
movies;// SELECT * FROM movies GROUP BY year, name DESC
having
Operators for MySQL HAVING clause.
Accepts:
- string
Example:
movies;// SELECT name, COUNT(name) FROM movies GROUP BY name HAVING COUNT(name) = 1
order
Operators for MySQL ORDER BY clause.
Accepts:
- array
- string
Example:
movies;// SELECT * FROM movies ORDER BY year, namemovies;// SELECT * FROM movies ORDER BY name
orderDESC
If true, sets descending order for ORDER BY
Accepts:
- boolean
Example:
movies;// SELECT * FROM movies ORDER BY year, name DESC
limit
Operators for MySQL LIMIT clause.
Accepts:
- array
- string
Example:
movies;// SELECT * FROM movies LIMIT 0, 30movies;// SELECT * FROM movies LIMIT 10, 40
Todo
- write sync function for collection
- validation schemas
- use 3rd party sql query builder
- write more tests
License
node-mysql-backbone is released under MIT license.
Credits
node-mysql-backbone was created by Michał Kowalkowski. You can contact me at kowalkowski.michal@gmail.com