Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
node-pool-query-builder
Advanced tools
A lightweight MySQL query builder on top of the node-mysql module.
MySQL Query Builder for Node.js (tags: nodejs, node, mysql, query builder, query-builder)
Query builder on top of node-mysql2 module (https://github.com/sidorares/node-mysql2).
The main benefit of the query builder is its ability to easily send JavaScript objects directly to MySQL query components, eliminating the need to manually construct the query. This offers more control over database queries compared to traditional ORM, where queries are hidden within the business logic and may not be optimized. Despite being named after CodeIgniter's "Active Record" class, the library does not strictly follow the active record pattern.
This query builder:
npm install node-query-builder
var adapter = require('node-query-builder');
var db = adapter.connect({ server: 'localhost', username: 'root', password: '12345', database: 'test' });
server
: The IP address or hostname to connect to.username
: MySQL username to connect with.password
: MySQL password to connect with.database
: Database to switch to initially (optional). If omitted, no database will be selected.port
: The port to connect to (optional). If omitted, 3306 will be used.reconnectTimeout
: Milliseconds after which to try to reconnect to the MySQL server if a disconnect happens (optional). If omitted, the default value of 2000 will be used. If set to false
, no reconnecting will take place.Specifies the field(s) to use in the SELECT query as a atring.
db.select("id, CONCAT(first_name, ' ', last_name) as full_name, email");
// This would produce: SELECT id, CONCAT(first_name, ' ', last_name) as full_name, email …
You can call .select() multiple times within the scope of one query — all parameters will be used in the final query. E.g.
db.select('id');
// do some advanced checking and calculations here (only synchronous work, though!)
db.select('first_name, last_name');
// This would procude: SELECT id, first_name, last_name …
Same as above, with a difference of taking in fields list as an array.
db.select(['id', 'first_name', 'last_name']);
// This would produce: SELECT id, first_name, last_name …
Specifies a where clause component.
db.where('add_time is null');
// This would produce: … WHERE add_time is null …
You can call .where() multiple times within the scope of one query — all parameters will be used in the final query.
Specifies a WHERE IN structure to use in the query.
db.where('first_name', ['John', 'Maria', 'Jason', 'Herbert']);
// This would produce: … WHERE first_name in ('John', 'Maria', 'Jason', 'Herbert') …
Specifies a single WHERE condition to use in the query.
db.where('first_name', 'John');
// This would produce: … WHERE first_name = 'John' …
Specifies multiple WHERE conditions to use in the query.
var conditions = {
first_name: 'John',
last_name: 'Smith'
};
db.where(conditions);
// This would produce: … WHERE first_name = 'John' AND last_name = 'Smith' …
Specifies the ORDER BY condition as a full string.
db.order_by('name asc');
// This would produce: … ORDER BY name asc …
You can call .order_by() multiple times within the scope of one query — all parameters will be used in the final query.
Specifies multiple ORDER BY conditions as an array.
db.order_by(['name asc', 'last_name desc']);
// This would produce: … ORDER BY name asc, last_name desc …
Specifies the GROUP BY condition as a full string.
db.group_by('name asc');
// This would produce: … GROUP BY name asc …
You can call .group_by() multiple times within the scope of one query — all parameters will be used in the final query.
Specifies the GROUP BY condition as a full string.
db.group_by(['name asc', 'last_name desc']);
// This would produce: … GROUP BY name asc, last_name desc …
Join additional tables to the query.
db.join('pets', 'pets.owner_id = people.id', 'LEFT');
// This would produce: … LEFT JOIN pets ON pets.owner_id = people.id …
db.join('pets', 'pets.owner_id = people.id');
// This would produce: … JOIN pets ON pets.owner_id = people.id …
Adds a row limit to query results.
db.limit(10);
// Limits query results to 10 rows.
Adds a row limit with an offset pointer position to query results.
db.limit(10, 30);
// Limits query results to 10 rows, starting from the 30th row in the full matching set.
After execution of a query, all query conditions are cleared. Results are passed down to responseCallback function. The parameters handed over to responseCallback match exactly what the underlying node-mysql module produces. See documentation from https://github.com/felixge/node-mysql
Produces and executes UPDATE query.
db.update('people', { first_name: 'John', last_name: 'Smith' }, function(err) { ... });
// This would produce: … UPDATE people SET first_name = 'John', last_name = 'Smith' …
Produces and executes DELETE query. Be sure to specify some WHERE clause components using .where() not to truncate an entire table. ✌
db.delete('people', function(err) { ... });
Produces and executes a single-row INSERT query.
db.insert('people', { first_name: 'John', last_name: 'Smith' }, function(err, info) { ... });
// This would produce: … INSERT INTO people SET first_name = 'John', last_name = 'Smith' …
Produces and executes a multi-row INSERT query.
var person1 = { first_name: 'John', last_name: 'Smith' };
var person2 = { first_name: 'Jason', last_name: 'Binder' };
var person3 = { first_name: 'Herbert', last_name: 'von Kellogg' };
db.insert('people', [person1, person2, person3], function(err, info) { ... });
// This would produce: … INSERT INTO people (first_name, last_name) VALUES (('John','Smith'),('Jason','Binder'),('Herbert','von Kellogg')) …
Produces and executes an INSERT IGNORE query. Note that the newData parameter can be either a string (produces single-row INSERT) or an array (produces multi-row INSERT). You can also specify an optional onDuplicateKeyClause, e.g.
db.insert_ignore('people', { first_name: 'John', last_name: 'Smith' }, function(err, info) { ... }, 'ON DUPLICATE KEY UPDATE duplicate_count = duplicate_count + 1');
// This would produce: … INSERT IGNORE INTO people SET first_name = 'John', last_name = 'Smith' … ON DUPLICATE KEY UPDATE duplicate_count = duplicate_count + 1
Produces and executes a SELECT query.
db.get('people', function(err, rows, fields) { ... });
// This would produce: SELECT … FROM people …
Produces and executes a SELECT query with count.
db.get('people', function(err, rows, fields) { ... });
// This would produce: SELECT count(*) FROM people …
Produces and executes a raw query. Note that while no set query conditions will be used in this query, they will all be reset nevertheless with the execution.
db.query('SHOW TABLES FROM test_database', function(err, results) { ... });
Pings the connection. This is useful when extending idle timeouts.
Returns the last executed query as a string.
Returns the underlying database connection object, ultimately what https://github.com/sidorares/node-mysql2#using-connection-pools .createPool() returns.
var adapter = require('node-query-builder');
var db = adapter.connect({
server: 'localhost',
username: 'root',
password: '12345',
database: 'test'
});
db.get('people', function(err, results, fields) {
console.log(results);
});
var data = {
name: 'Martin',
email: 'martin@example.com'
};
db.insert('people', data, function(err, info) {
console.log('New row ID is ' + info.insertId);
});
var data = {
name: 'Martin',
email: 'martin@example.com'
};
db.insert_ignore('people', data, function(err, info) {
console.log('New row ID is ' + info.insertId);
}, 'ON DUPLICATE KEY SET counter = counter + 1');
db
.where({ name: 'Martin' })
.get('people', function(err, results, fields) {
console.log(results);
});
db
.select(['people.id', 'people.name', 'people.email', 'songs.title'])
.join('songs', 'people.favorite_song_id', 'left')
.where({
'people.name': 'Martin',
'songs.title': 'Yesterday'
})
.limit(5, 10)
.order_by('people.name asc')
.get('people', function(err, results, fields) {
console.log(results);
});
db
.where({
'people.name': 'Martin',
'songs.title': 'Yesterday'
})
.count('people', function(err, results, fields) {
console.log(results);
});
db
.select('name, COUNT(name) AS name_count')
.group_by('name')
.order_by('name_count DESC')
.get('people', function(err, results, fields) {
console.log(results);
});
var newData = {
name: 'John',
email: 'john@doe.com'
};
db
.where({ id: 1 })
.update('people', newData, function(err) {
if (!err) {
console.log('Updated!');
}
});
db
.where({ id: 1 })
.delete('people', function(err) {
if (!err) {
console.log('Deleted!')
}
});
db
.where("title not like '%Jackson%'")
.where("date_created > '2012-03-10'")
.where({ owner_id: 32 })
.delete('records', function(err) {
if (!err) {
console.log('Deleted!')
}
});
Got a missing feature you'd like to use? Found a bug? Go ahead and fork this repo, build the feature and issue a pull request.
Licensed under the GPL license and MIT:
FAQs
A lightweight MySQL query builder on top of the node-mysql module.
The npm package node-pool-query-builder receives a total of 0 weekly downloads. As such, node-pool-query-builder popularity was classified as not popular.
We found that node-pool-query-builder 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.