Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
mysql-node
Advanced tools
Readme
A backbone based model for communicating with a MySQL database using felixge/node-mysql.
Install from npm package:
npm install mysql-model
Or install from git:
npm install git://github.com/michalkow/node-mysql-model.git
Add the mysql-model module to your application :
var mysqlModel = require('mysql-model');
Then create a model that will be main one for your application (all others will extend it):
var MyAppModel = mysqlModel.createConnection({
host : 'database-host',
user : 'database-user',
password : 'database-password',
database : 'database-name',
});
var Movie = MyAppModel.extend({
tableName: "movies",
});
movie = new Movie();
// OR
movie = new MyAppModel({tableName: "movies"});
To see complete list of options for creating a connection with the database visit felixge/node-mysql readme.
Model Settable Options
tableName
Name of a MySQL table the model will refer to:
var Movie = MyAppModel.extend({
tableName: "movies",
});
Methods
find
Retrieves records from database
Usage:
movie.find();
movie.find(method);
movie.find(callback);
movie.find(method, conditions);
movie.find(method, callback);
movie.find(method, conditions, callback);
Parameters:
Example:
movie.find('all', {where: "year > 2001"}, function(err, rows, fields) {
// Do something...
});
save
Saves your model to database
Usage:
movie.save();
movie.save(where);
movie.save(callback);
movie.save(where, callback);
Parameters:
Example:
movie = new Movie({
name: 'Serenity',
director: 'Joss Whedon',
language: 'English',
year: 2005
});
// Will create new record
movie.save();
movie.set('id', 4);
// Will update record if id exists
movie.save();
remove
Deletes your model from database and unsets it
Usage:
movie.remove();
movie.remove(where);
movie.remove(callback);
movie.remove(where, callback);
Parameters:
Example:
// Will delete record from database matching id model
movie.set('id', 8);
movie.remove();
// Will delete records from database matching where condition
movie.remove('year < 1980');
read
Retrieves record from database and set it to current model
Usage:
movie.read();
movie.read(id);
movie.read(callback);
movie.read(id, callback);
Parameters:
Example:
movie.set('id', 6);
movie.read();
// or
movie.read(6);
query
Runs custom query
Usage:
movie.query(query);
movie.query(query, callback);
Parameters:
Example:
movie.query("SELECT name FROM movies WHERE director = 'James Cameron' ORDER BY year", function(err, rows, fields) {
// Do something...
});
setSQL
Method to replace 'set', when setting results passed back by node-mysql
Usage:
movie.setSQL(result);
Parameters:
Example:
movie.find('first', {where: "id=12"}, function(err, row) {
movie.setSQL(row);
});
'find' methods
'all'
Returns all the records matching conditions
Returns:
Example:
movie.find('all', {where: "language = 'German'", limit: [0, 30]}, function(err, rows) {
for(var i=0; i<rows.length; i++) {
console.log(rows[i]);
}
});
'count'
Returns number of records matching conditions
Returns:
Example:
movie.find('count', {where: "year = 2012"}, function(err, result) {
console.log(result);
});
'first'
Returns first the records matching conditions
Returns:
Example:
movie.find('first', {where: "id = 3"}, function(err, row) {
console.log(row);
});
'field'
Returns field of the first record matching conditions
Returns:
Example:
movie.find('field', {fields: ['name'], where: "id = 3"}, function(err, field) {
console.log(field);
});
'find' conditions
fields
Fields to select from the table
Accepts:
Example:
movie.find('all', {fields: ['id', 'name', 'year']});
// SELECT id, name, year FROM movies
movie.find('all', {fields: "name"});
// SELECT name FROM movies
where
Operators for MySQL WHERE clause.
Accepts:
Example:
movie.find('all', {where: "year > 1987"});
// SELECT * FROM movies WHERE year > 1987
group
Operators for MySQL GROUP BY clause.
Accepts:
Example:
movie.find('all', {group: ['year', 'name']});
// SELECT * FROM movies GROUP BY year, name
movie.find('all', {group: "name"});
// SELECT * FROM movies GROUP BY name
groupDESC
If true, sets descending order for GROUP BY
Accepts:
Example:
movie.find('all', {group: ['year', 'name'], groupDESC:true});
// SELECT * FROM movies GROUP BY year, name DESC
having
Operators for MySQL HAVING clause.
Accepts:
Example:
movie.find('all', {fields: ['name', 'COUNT(name)'], group: "name", having: "COUNT(name) = 1"});
// SELECT name, COUNT(name) FROM movies GROUP BY name HAVING COUNT(name) = 1
order
Operators for MySQL ORDER BY clause.
Accepts:
Example:
movie.find('all', {group: ['year', 'name']});
// SELECT * FROM movies ORDER BY year, name
movie.find('all', {group: "name"});
// SELECT * FROM movies ORDER BY name
orderDESC
If true, sets descending order for ORDER BY
Accepts:
Example:
movie.find('all', {group: ['year', 'name'], orderDESC:true});
// SELECT * FROM movies ORDER BY year, name DESC
limit
Operators for MySQL LIMIT clause.
Accepts:
Example:
movie.find('all', {limit: [0, 30]});
// SELECT * FROM movies LIMIT 0, 30
movie.find('all', {limit: "10, 40"});
// SELECT * FROM movies LIMIT 10, 40
node-mysql-model is released under MIT license.
node-mysql-model was created by Michał Kowalkowski. You can contact me at kowalkowski.michal@gmail.com
FAQs
A backbone based model for communicating with a MySQL database using felixge/node-mysql.
The npm package mysql-node receives a total of 2 weekly downloads. As such, mysql-node popularity was classified as not popular.
We found that mysql-node demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.