Skip to content

Commit f4c9d92

Browse files
committed
MySQLTable: Allow .insert() to accept a string as the first parameter
1 parent 4637ca2 commit f4c9d92

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ A class that provides convenient methods for performing queries.
523523
* [.pool](#MySQLTable+pool) : <code>[PoolPlus](#PoolPlus)</code>
524524
* [.trxn](#MySQLTable+trxn) : <code>?[Connection](#Connection)</code>
525525
* [.select(columns, [sqlString], [values], [cb])](#MySQLTable+select) ⇒ <code>Promise</code>
526-
* [.insert(data, [sqlString], [values], [cb])](#MySQLTable+insert) ⇒ <code>Promise</code>
526+
* [.insert([data], [sqlString], [values], [cb])](#MySQLTable+insert) ⇒ <code>Promise</code>
527527
* [.update([data], [sqlString], [values], [cb])](#MySQLTable+update) ⇒ <code>Promise</code>
528528
* [.delete([sqlString], [values], [cb])](#MySQLTable+delete) ⇒ <code>Promise</code>
529529
* [.query()](#MySQLTable+query) ⇒ <code>Promise</code>
@@ -631,14 +631,17 @@ userTable.select('COUNT(*) AS `highScorers`', 'WHERE `points` > 10000', (err, ro
631631

632632
<a name="MySQLTable+insert"></a>
633633

634-
### mySQLTable.insert(data, [sqlString], [values], [cb]) ⇒ <code>Promise</code>
634+
### mySQLTable.insert([data], [sqlString], [values], [cb]) ⇒ <code>Promise</code>
635635
Inserts data into a new row in the table.
636636

637+
__Note:__ The `data` and `sqlString` arguments are individually
638+
optional but at least one of them must be specified.
639+
637640

638641
| Param | Type | Description |
639642
|:--- |:--- |:--- |
640-
| data | <code>Object</code> &#124; <code>Array</code> | An object of (column name)-(data value) pairs or an array containing either 1) an array of arrays of data values or 2) an array of column names and the data array from 1). |
641-
| [sqlString] | <code>string</code> | SQL to be appended to the query.<br>This would only be used to add an `ON DUPLICATE KEY UPDATE` clause. |
643+
| [data] | <code>Object</code> &#124; <code>Array</code> | An object of (column name)-(data value) pairs or an array containing either 1) an array of arrays of data values or 2) an array of column names and the data array from 1). |
644+
| [sqlString] | <code>string</code> | SQL to be appended to the query. If `data` is provided, it is appended directly after the formatted data, otherwise it is appended after `"INSERT INTO tableName"`. |
642645
| [values] | <code>Array</code> | Values to replace the placeholders in `sqlString`. |
643646
| [cb] | <code>[queryCallback](#module_mysql-plus..queryCallback)</code> | A callback that gets called with the results of the query. |
644647

lib/MySQLTable.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,14 @@ class MySQLTable {
9797
/**
9898
* Inserts data into a new row in the table.
9999
*
100-
* @param {Object|Array} data - An object of (column name)-(data value) pairs or
100+
* __Note:__ The `data` and `sqlString` arguments are individually
101+
* optional but at least one of them must be specified.
102+
*
103+
* @param {Object|Array} [data] - An object of (column name)-(data value) pairs or
101104
* an array containing either 1) an array of arrays of data values or 2) an array
102105
* of column names and the data array from 1).
103-
* @param {string} [sqlString] - SQL to be appended to the query.<br>This would only be used to add
104-
* an `ON DUPLICATE KEY UPDATE` clause.
106+
* @param {string} [sqlString] - SQL to be appended to the query. If `data` is provided, it is appended
107+
* directly after the formatted data, otherwise it is appended after `"INSERT INTO tableName"`.
105108
* @param {Array} [values] - Values to replace the placeholders in `sqlString`.
106109
* @param {module:mysql-plus~queryCallback} [cb] - A callback that gets called with the results of the query.
107110
* @returns {?Promise} If the `cb` parameter is omitted, a promise that will resolve with the results
@@ -143,6 +146,14 @@ class MySQLTable {
143146
* });
144147
*/
145148
insert(data, sqlString, values, cb) {
149+
if (typeof data === 'string') {
150+
return this._db.pquery(
151+
'INSERT INTO ' + this._escapedName + ' ' + data,
152+
sqlString,
153+
values
154+
);
155+
}
156+
146157
if (cb) {
147158
sqlString = this._db.format(sqlString, values);
148159
} else if (values) {

test/unit/MySQLTable.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ describe('MySQLTable', () => {
281281
});
282282
});
283283

284+
it('should allow the first parameter to be a string', done => {
285+
testTable.insert("(`email`) VALUES ('six@email.com'), ('seven@email.com')", (err, result1) => {
286+
if (err) throw err;
287+
result1.affectedRows.should.equal(2);
288+
289+
testTable.insert('SET ?? = ?', ['email', 'eight@email.com'], (err, result2) => {
290+
if (err) throw err;
291+
result2.affectedRows.should.equal(1);
292+
done();
293+
});
294+
});
295+
});
296+
284297
});
285298

286299

@@ -342,6 +355,16 @@ describe('MySQLTable', () => {
342355
});
343356
});
344357

358+
it('should allow the first parameter to be a string', () => {
359+
return Promise.all([
360+
testTable.insert("(`email`) VALUES ('six@email.com'), ('seven@email.com')"),
361+
testTable.insert('SET ?? = ?', ['email', 'eight@email.com']),
362+
]).then(results => {
363+
results[0].affectedRows.should.equal(2);
364+
results[1].affectedRows.should.equal(1);
365+
});
366+
});
367+
345368
});
346369

347370
});

0 commit comments

Comments
 (0)