Skip to content

Commit

Permalink
PoolPlus: Add .basicTable() method that just creates a MySQLTable i…
Browse files Browse the repository at this point in the history
…nstance
  • Loading branch information
nwoltman committed Apr 5, 2017
1 parent 591d004 commit 2ca6e43
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ and perform queries and transactions using promises.</p>
</dd>
<dt><a href="#MySQLTable">MySQLTable</a></dt>
<dd><p>A class that provides convenient methods for performing queries.<br>To create
an instance, use <a href="#PoolPlus+defineTable"><code>poolPlus.defineTable()</code></a>.</p>
an instance, use <a href="#PoolPlus+defineTable"><code>poolPlus.defineTable()</code></a> or
<a href="#PoolPlus+basicTable"><code>poolPlus.basicTable()</code></a>.</p>
</dd>
</dl>

Expand Down Expand Up @@ -240,6 +241,7 @@ and perform queries and transactions using promises.
* [PoolPlus](#PoolPlus) ⇐ <code>Pool</code>
* _instance_
* [.ColTypes](#PoolPlus+ColTypes)
* [.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>
* [.pquery(sql, [values], [cb])](#PoolPlus+pquery) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -272,6 +274,21 @@ const userTable = pool.defineTable('user', {
```


---

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

### poolPlus.basicTable(name) ⇒ <code>[MySQLTable](#MySQLTable)</code>
Simply returns an instance of [`MySQLTable`](#MySQLTable)
for querying the table with the given `name`.


| Param | Type | Description |
|:--- |:--- |:--- |
| name | <code>string</code> | The name of the table. |

**Returns**: <code>[MySQLTable](#MySQLTable)</code> - A `MySQLTable` instance.

---

<a name="PoolPlus+defineTable"></a>
Expand All @@ -286,7 +303,7 @@ Defines a table to be created or updated in the database.
| schema | <code>Object</code> | An object that defines the table's schema. See the [Defining Table Schemas](#defining-table-schemas) section. |
| [migrationStrategy] | <code>string</code> | One of `safe`, `alter`, or `drop`. This will override the `migrationStrategy` value from the [`config`](#module_mysql-plus..createPool) (but is still subject to the same restrictions in production environments). |

**Returns**: <code>[MySQLTable](#MySQLTable)</code> - A `MySQLTable` instance that lets you perform operations on the table.
**Returns**: <code>[MySQLTable](#MySQLTable)</code> - A `MySQLTable` instance that lets you perform queries on the table.
**See**: [Defining Table Schemas](#defining-table-schemas)

**Example**:
Expand Down Expand Up @@ -513,7 +530,8 @@ connection.pquery('SELECT * FROM `books` WHERE `author` = "David"')

## MySQLTable
A class that provides convenient methods for performing queries.<br>To create
an instance, use [`poolPlus.defineTable()`](#PoolPlus+defineTable).
an instance, use [`poolPlus.defineTable()`](#PoolPlus+defineTable) or
[`poolPlus.basicTable()`](#PoolPlus+basicTable).

**See**: [https://github.com/mysqljs/mysql#performing-queries](https://github.com/mysqljs/mysql#performing-queries)

Expand Down
3 changes: 2 additions & 1 deletion lib/MySQLTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

/**
* A class that provides convenient methods for performing queries.<br>To create
* an instance, use {@link PoolPlus#defineTable|`poolPlus.defineTable()`}.
* an instance, use {@link PoolPlus#defineTable|`poolPlus.defineTable()`} or
* {@link PoolPlus#basicTable|`poolPlus.basicTable()`}.
*
* @see {@link https://github.com/mysqljs/mysql#performing-queries}
*/
Expand Down
17 changes: 16 additions & 1 deletion lib/PoolPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ class PoolPlus extends Pool {
return SqlString.format(sql, values, connConfig.stringifyObjects, connConfig.timezone);
}

/**
* Simply returns an instance of {@link MySQLTable|`MySQLTable`}
* for querying the table with the given `name`.
*
* @param {string} name - The name of the table.
* @returns {MySQLTable} A `MySQLTable` instance.
*/
basicTable(name) {
if (typeof name !== 'string') {
throw new TypeError('The table name must be a string');
}

return new MySQLTable(name, undefined, this);
}

/**
* Defines a table to be created or updated in the database.
*
Expand All @@ -63,7 +78,7 @@ class PoolPlus extends Pool {
* @param {string} [migrationStrategy] - One of `safe`, `alter`, or `drop`. This will override
* the `migrationStrategy` value from the {@link module:mysql-plus~createPool|`config`}
* (but is still subject to the same restrictions in production environments).
* @returns {MySQLTable} A `MySQLTable` instance that lets you perform operations on the table.
* @returns {MySQLTable} A `MySQLTable` instance that lets you perform queries on the table.
* @see [Defining Table Schemas](#defining-table-schemas)
*
* @example
Expand Down
20 changes: 20 additions & 0 deletions test/unit/PoolPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,33 @@ describe('PoolPlus', () => {
});


describe('#basicTable()', () => {

it('should return a MySQLTable instance', () => {
const table = pool.basicTable(TEST_TABLE_NAME, TEST_TABLE_SCHEMA);
table.should.be.an.instanceOf(MySQLTable);
table.name.should.equal(TEST_TABLE_NAME);
should.strictEqual(table.schema, undefined);
table.pool.should.equal(pool);
});

it('should throw if the table name is not a string', () => {
(() => pool.basicTable()).should.throw(TypeError);
(() => pool.basicTable()).should.throw(/The table name must be a string/);
(() => pool.basicTable(/table/)).should.throw(/The table name must be a string/);
});

});


describe('#defineTable()', () => {

it('should return a MySQLTable instance', () => {
const table = pool.defineTable(TEST_TABLE_NAME, TEST_TABLE_SCHEMA);
table.should.be.an.instanceOf(MySQLTable);
table.name.should.equal(TEST_TABLE_NAME);
table.schema.should.equal(TEST_TABLE_SCHEMA);
table.pool.should.equal(pool);
});

it('should throw if no arguments are provided', () => {
Expand Down

0 comments on commit 2ca6e43

Please sign in to comment.