Skip to content

Commit 0704b40

Browse files
committed
PoolPlus: Add Promise support to pool.sync()
1 parent 9987b43 commit 0704b40

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ and perform queries and transactions using promises.
242242
* [.raw(sql)](#PoolPlus+raw) ⇒ <code>Object</code>
243243
* [.basicTable(name)](#PoolPlus+basicTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
244244
* [.defineTable(name, schema, [migrationStrategy])](#PoolPlus+defineTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
245-
* [.sync(cb)](#PoolPlus+sync) ⇒ <code>void</code>
245+
* [.sync([cb])](#PoolPlus+sync) ⇒ <code>Promise</code>
246246
* [.pquery(sql, [values], [cb])](#PoolPlus+pquery) ⇒ <code>Promise</code>
247247
* [.transaction(trxnHandler)](#PoolPlus+transaction) ⇒ <code>Promise</code>
248248
* _inner_
@@ -355,7 +355,7 @@ const userTable = pool.defineTable('user', {
355355

356356
<a name="PoolPlus+sync"></a>
357357

358-
### poolPlus.sync(cb) ⇒ <code>void</code>
358+
### poolPlus.sync([cb]) ⇒ <code>Promise</code>
359359
Syncs the defined tables to the database by creating new tables and dropping
360360
or migrating existing tables (depending on the migration setting).
361361

@@ -367,16 +367,28 @@ Always keep a backup of your database so you can restore it to the latest workin
367367

368368
| Param | Type | Description |
369369
|:--- |:--- |:--- |
370-
| cb | <code>function</code> | A callback that is called once all defined table schemas have been synced to the database. If an error occured, the first argument passed to the callback will be the error object. |
370+
| [cb] | <code>function</code> | A callback that is called once all defined table schemas have been synced to the database. If an error occured, the first argument passed to the callback will be the error object. |
371371

372-
**Example**:
372+
**Returns**: <code>?Promise</code> - If `cb` is not provided, a promise will be returned.
373+
374+
**Example**: With a callback
373375
```js
374376
pool.sync((err) => {
375377
if (err) throw err;
376378
// Now do something such as start an HTTP server
377379
});
378380
```
379381

382+
**Example**: With a promise
383+
```js
384+
pool.sync()
385+
.then(() => {
386+
// Success
387+
}, (err) => {
388+
// Error
389+
});
390+
```
391+
380392

381393
---
382394

lib/PoolPlus.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,41 @@ class PoolPlus extends Pool {
152152
* __Warning:__ If an error occurs while syncing, the database will be in an unknown state.
153153
* Always keep a backup of your database so you can restore it to the latest working state.
154154
*
155-
* @param {function} cb - A callback that is called once all defined table schemas have been synced to the
155+
* @param {function} [cb] - A callback that is called once all defined table schemas have been synced to the
156156
* database. If an error occured, the first argument passed to the callback will be the error object.
157-
* @returns {void}
157+
* @returns {?Promise} If `cb` is not provided, a promise will be returned.
158158
*
159-
* @example
159+
* @example <caption>With a callback</caption>
160160
* pool.sync((err) => {
161161
* if (err) throw err;
162162
* // Now do something such as start an HTTP server
163163
* });
164+
*
165+
* @example <caption>With a promise</caption>
166+
* pool.sync()
167+
* .then(() => {
168+
* // Success
169+
* }, (err) => {
170+
* // Error
171+
* });
164172
*/
165-
sync(cb) {
173+
sync(cb) { // eslint-disable-line consistent-return
174+
if (!cb) {
175+
return new Promise((resolve, reject) => {
176+
this.sync(err => {
177+
if (err) {
178+
reject(err);
179+
} else {
180+
resolve();
181+
}
182+
});
183+
});
184+
}
185+
166186
var tablesRemaining = this._tables.size;
167187
if (!tablesRemaining) {
168188
process.nextTick(cb);
169-
return;
189+
return; // eslint-disable-line consistent-return
170190
}
171191

172192
var error = null;

test/unit/PoolPlus.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ describe('PoolPlus', () => {
177177
});
178178
});
179179

180+
it('should return a promise if no callback is provided', () => {
181+
const promise = pool.sync();
182+
promise.should.be.an.instanceOf(Promise);
183+
return promise;
184+
});
185+
180186

181187
describe('if an error occured getting a connection', () => {
182188

@@ -200,6 +206,12 @@ describe('PoolPlus', () => {
200206
});
201207
});
202208

209+
it('should reject the returned promise with an error', () => {
210+
return pool.sync().catch(err => {
211+
err.should.equal(error);
212+
});
213+
});
214+
203215
});
204216

205217

@@ -222,6 +234,12 @@ describe('PoolPlus', () => {
222234
});
223235
});
224236

237+
it('should reject the returned promise with an error', () => {
238+
return pool.sync().catch(err => {
239+
err.should.equal(error);
240+
});
241+
});
242+
225243
});
226244

227245

@@ -247,6 +265,12 @@ describe('PoolPlus', () => {
247265
});
248266
});
249267

268+
it('should reject the returned promise with an error', () => {
269+
return pool.sync().catch(err => {
270+
err.should.equal(error);
271+
});
272+
});
273+
250274
});
251275

252276
});

0 commit comments

Comments
 (0)