@@ -49,34 +49,39 @@ class MySQLTable {
49
49
* // rows contains all data for all users
50
50
* });
51
51
*
52
+ * // SELECT * FROM `user`;
53
+ *
52
54
* @example <caption>Select specific columns</caption>
53
55
* userTable.select(['email', 'name'], 'WHERE `points` > 10000', (err, rows) => {
54
56
* if (err) throw err;
55
- * console.log(rows); // -> [{email: 'email@example .com', name: 'John Doe'}, etc .]
57
+ * console.log(rows); // -> [{email: 'email@example .com', name: 'John Doe'}, .. .]
56
58
* });
57
59
*
60
+ * // SELECT `email`, `name` FROM `user` WHERE `points` > 10000;
61
+ *
58
62
* @example <caption>Select with placeholders</caption>
59
- * userTable.select(['email'], 'WHERE `id` = ?', [5], (err, rows) => {
60
- * if (err) throw err;
61
- * console.log(rows); // -> [{email: 'email@example .com'}]
62
- * });
63
+ * userTable.select(['email'], 'WHERE `id` = ?', [5])
64
+ * .then(rows => console.log(rows)); // -> [{email: 'email@example .com'}]
63
65
*
64
- * userTable.select('??', 'WHERE ?', ['email', {id: 5}], (err, rows) => {
65
- * if (err) throw err;
66
- * console.log(rows); // -> [{email: 'email@example .com'}]
67
- * });
66
+ * // SELECT `email` FROM `user` WHERE `id` = 5;
67
+ *
68
+ *
69
+ * userTable.select('??', 'WHERE ?', ['email', {id: 5}])
70
+ * .then(rows => console.log(rows)); // -> [{email: 'email@example.com' }]
71
+ *
72
+ * // SELECT `email` FROM `user` WHERE `id` = 5;
68
73
*
69
74
* @example <caption>Select columns with aliases</caption>
70
- * userTable.select('`display_name ` AS `name `', 'WHERE `points` > 10000', (err, rows) => {
71
- * if (err) throw err;
72
- * console.log(rows); // -> [{name: 'JohnD'}, etc.]
73
- * }) ;
75
+ * userTable.select('`name ` AS `fullName `', 'WHERE `points` > 10000')
76
+ * .then(rows => console.log(rows)); // -> [{fullName: 'John Doe'}, ...]
77
+ *
78
+ * // SELECT `name` AS `fullName` FROM `user` WHERE `points` > 10000 ;
74
79
*
75
80
* @example <caption>Select using a function</caption>
76
- * userTable.select('COUNT(*) AS `highScorers`', 'WHERE `points` > 10000', (err, rows) => {
77
- * if (err) throw err;
78
- * console.log(rows); // -> [{highScorers: 27}]
79
- * }) ;
81
+ * userTable.select('COUNT(*) AS `highScorers`', 'WHERE `points` > 10000')
82
+ * .then(rows => console.log(rows)); // -> [{highScorers: 27}]
83
+ *
84
+ * // SELECT COUNT(*) AS `highScorers` FROM `user` WHERE `points` > 10000 ;
80
85
*/
81
86
select ( columns , sqlString , values , cb ) {
82
87
if ( typeof columns !== 'string' ) {
@@ -111,39 +116,52 @@ class MySQLTable {
111
116
* of the query is returned.
112
117
*
113
118
* @example <caption>Insert a new user</caption>
114
- * userTable.insert({email: 'email@example .com', name: 'John Doe'}, (err, result) => {
115
- * if (err) throw err;
116
- * // data inserted!
117
- * });
119
+ * userTable.insert({email: 'email@example .com', name: 'John Doe'})
120
+ * .then(result => result.affectedRows); // 1
121
+ *
122
+ * // INSERT INTO `user`
123
+ * // SET `email` = 'email@example.com', `name` = 'John Doe';
118
124
*
119
125
* @example <caption>Insert or update</caption>
120
126
* const data = {id: 5, points: 100};
121
127
* // If duplicate key (id), add the points
122
128
* const onDuplicateKeySQL = 'ON DUPLICATE KEY UPDATE `points` = `points` + ?';
123
- * userTable.insert(data, onDuplicateKeySQL, [data.points], (err, result) => {
124
- * if (err) throw err;
125
- * // data inserted or updated!
126
- * });
129
+ * userTable.insert(data, onDuplicateKeySQL, [data.points])
130
+ * .then(result => result.affectedRows); // 1 if inserted, 2 if updated
131
+ *
132
+ * // INSERT INTO `user` SET `id` = 5, `points` = 100
133
+ * // ON DUPLICATE KEY UPDATE `points` = `points` + 100;
134
+ *
135
+ * @example <caption>With only the `sqlString` argument</caption>
136
+ * placeTable.insert('`location` = POINT(0, 0)');
137
+ * // INSERT INTO `place` SET `location` = POINT(0, 0);
138
+ *
139
+ * placeTable.insert('`location` = POINT(?, ?)', [8, 2]);
140
+ * // INSERT INTO `place` SET `location` = POINT(8, 2);
127
141
*
128
142
* @example <caption>Bulk insert</caption>
129
143
* const users = [
130
144
* [1, 'john@email.com', 'John Doe'],
131
145
* [2, 'jane@email.com', 'Jane Brown'],
132
146
* ];
133
- * userTable.insert([users], (err, result) => {
134
- * if (err) throw err;
135
- * // users inserted!
136
- * });
147
+ * userTable.insert([users])
148
+ * .then(result => result.insertId); // 2 (ID of the last inserted row)
149
+ *
150
+ * // INSERT INTO `user` VALUES
151
+ * // (1, 'john@email.com', 'John Doe'),
152
+ * // (2, 'jane@email.com', 'Jane Brown');
137
153
*
138
154
* @example <caption>Bulk insert with specified columns</caption>
139
155
* const users = [
140
156
* ['john@email.com', 'John Doe'],
141
157
* ['jane@email.com', 'Jane Brown'],
142
158
* ];
143
- * userTable.insert([['email', 'name'], users], (err, result) => {
144
- * if (err) throw err;
145
- * // users inserted!
146
- * });
159
+ * userTable.insert([['email', 'name'], users])
160
+ * .then(result => result.affectedRows); // 2
161
+ *
162
+ * // INSERT INTO `user` (`email`, `name`) VALUES
163
+ * // ('john@email.com', 'John Doe'),
164
+ * // ('jane@email.com', 'Jane Brown');
147
165
*/
148
166
insert ( data , sqlString , values , cb ) {
149
167
if ( typeof data === 'string' ) {
@@ -199,27 +217,23 @@ class MySQLTable {
199
217
* of the query is returned.
200
218
*
201
219
* @example <caption>With both the `data` and `sqlString` arguments</caption>
202
- * userTable.update({email: 'updated@email .com'}, 'WHERE `id` = ?', [5], (err, result) => {
203
- * if (err) throw err;
204
- * // email updated!
205
- * });
220
+ * userTable.update({email: 'updated@email .com'}, 'WHERE `id` = ?', [5])
221
+ * .then(result => result.changedRows); // 1
222
+ *
223
+ * // UPDATE `user` SET `email` = 'updated@email.com'
224
+ * // WHERE `id` = 5;
206
225
*
207
226
* @example <caption>With only the `sqlString` argument</caption>
208
- * userTable.update("`word` = CONCAT('prefix', `word`)", (err, result) => {
209
- * if (err) throw err;
210
- * // prefix added to all words!
211
- * });
227
+ * userTable.update("`word` = CONCAT('prefix', `word`)");
228
+ * // UPDATE `user` SET `word` = CONCAT('prefix', `word`);
212
229
*
213
- * userTable.update('`points` = `points` + ? WHERE `winner` = ?', [1, 1], (err, result) => {
214
- * if (err) throw err;
215
- * // 1 point added to all winners!
216
- * });
230
+ * userTable.update('`points` = `points` + ? WHERE `winner` = ?', [10, 1]);
231
+ * // UPDATE `user` SET `points` = `points` + 10
232
+ * // WHERE `winner` = 1;
217
233
*
218
234
* @example <caption>With only the `data` argument (updates all rows)</caption>
219
- * userTable.update({points: 1000}, (err, result) => {
220
- * if (err) throw err;
221
- * // Now everyone has 1000 points!
222
- * });
235
+ * userTable.update({points: 1000});
236
+ * // UPDATE `user` SET `points` = 1000;
223
237
*/
224
238
update ( data , sqlString , values , cb ) {
225
239
if ( typeof data === 'string' ) {
@@ -257,16 +271,18 @@ class MySQLTable {
257
271
* of the query is returned.
258
272
*
259
273
* @example <caption>Delete specific rows</caption>
260
- * userTable.delete('WHERE `spammer` = 1', (err, result) => {
261
- * if (err) throw err;
262
- * // spammers deleted!
263
- * }) ;
274
+ * userTable.delete('WHERE `spammer` = 1')
275
+ * .then(result => result.affectedRows); // The number of deleted spammers
276
+ *
277
+ * // DELETE FROM `user` WHERE `spammer` = 1 ;
264
278
*
265
279
* @example <caption>Delete all rows (you probably don't want to do this)</caption>
266
280
* userTable.delete((err, result) => {
267
281
* if (err) throw err;
268
282
* // all rows deleted :(
269
283
* });
284
+ *
285
+ * // DELETE FROM `user`;
270
286
*/
271
287
delete ( sqlString , values , cb ) {
272
288
if ( sqlString === undefined || typeof sqlString === 'function' ) {
0 commit comments