Skip to content

Commit

Permalink
PoolPlus: Add Promise support to pool.sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Feb 14, 2018
1 parent 9987b43 commit 0704b40
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ and perform queries and transactions using promises.
* [.raw(sql)](#PoolPlus+raw) ⇒ <code>Object</code>
* [.basicTable(name)](#PoolPlus+basicTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
* [.defineTable(name, schema, [migrationStrategy])](#PoolPlus+defineTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
* [.sync(cb)](#PoolPlus+sync) ⇒ <code>void</code>
* [.sync([cb])](#PoolPlus+sync) ⇒ <code>Promise</code>
* [.pquery(sql, [values], [cb])](#PoolPlus+pquery) ⇒ <code>Promise</code>
* [.transaction(trxnHandler)](#PoolPlus+transaction) ⇒ <code>Promise</code>
* _inner_
Expand Down Expand Up @@ -355,7 +355,7 @@ const userTable = pool.defineTable('user', {

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

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

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

| Param | Type | Description |
|:--- |:--- |:--- |
| 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. |
| [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. |

**Example**:
**Returns**: <code>?Promise</code> - If `cb` is not provided, a promise will be returned.

**Example**: With a callback
```js
pool.sync((err) => {
if (err) throw err;
// Now do something such as start an HTTP server
});
```

**Example**: With a promise
```js
pool.sync()
.then(() => {
// Success
}, (err) => {
// Error
});
```


---

Expand Down
30 changes: 25 additions & 5 deletions lib/PoolPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,41 @@ class PoolPlus extends Pool {
* __Warning:__ If an error occurs while syncing, the database will be in an unknown state.
* Always keep a backup of your database so you can restore it to the latest working state.
*
* @param {function} cb - A callback that is called once all defined table schemas have been synced to the
* @param {function} [cb] - 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.
* @returns {void}
* @returns {?Promise} If `cb` is not provided, a promise will be returned.
*
* @example
* @example <caption>With a callback</caption>
* pool.sync((err) => {
* if (err) throw err;
* // Now do something such as start an HTTP server
* });
*
* @example <caption>With a promise</caption>
* pool.sync()
* .then(() => {
* // Success
* }, (err) => {
* // Error
* });
*/
sync(cb) {
sync(cb) { // eslint-disable-line consistent-return
if (!cb) {
return new Promise((resolve, reject) => {
this.sync(err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

var tablesRemaining = this._tables.size;
if (!tablesRemaining) {
process.nextTick(cb);
return;
return; // eslint-disable-line consistent-return
}

var error = null;
Expand Down
24 changes: 24 additions & 0 deletions test/unit/PoolPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ describe('PoolPlus', () => {
});
});

it('should return a promise if no callback is provided', () => {
const promise = pool.sync();
promise.should.be.an.instanceOf(Promise);
return promise;
});


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

Expand All @@ -200,6 +206,12 @@ describe('PoolPlus', () => {
});
});

it('should reject the returned promise with an error', () => {
return pool.sync().catch(err => {
err.should.equal(error);
});
});

});


Expand All @@ -222,6 +234,12 @@ describe('PoolPlus', () => {
});
});

it('should reject the returned promise with an error', () => {
return pool.sync().catch(err => {
err.should.equal(error);
});
});

});


Expand All @@ -247,6 +265,12 @@ describe('PoolPlus', () => {
});
});

it('should reject the returned promise with an error', () => {
return pool.sync().catch(err => {
err.should.equal(error);
});
});

});

});
Expand Down

0 comments on commit 0704b40

Please sign in to comment.