Skip to content

Commit 2ca6e43

Browse files
committed
PoolPlus: Add .basicTable() method that just creates a MySQLTable instance
1 parent 591d004 commit 2ca6e43

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ and perform queries and transactions using promises.</p>
128128
</dd>
129129
<dt><a href="#MySQLTable">MySQLTable</a></dt>
130130
<dd><p>A class that provides convenient methods for performing queries.<br>To create
131-
an instance, use <a href="#PoolPlus+defineTable"><code>poolPlus.defineTable()</code></a>.</p>
131+
an instance, use <a href="#PoolPlus+defineTable"><code>poolPlus.defineTable()</code></a> or
132+
<a href="#PoolPlus+basicTable"><code>poolPlus.basicTable()</code></a>.</p>
132133
</dd>
133134
</dl>
134135

@@ -240,6 +241,7 @@ and perform queries and transactions using promises.
240241
* [PoolPlus](#PoolPlus) ⇐ <code>Pool</code>
241242
* _instance_
242243
* [.ColTypes](#PoolPlus+ColTypes)
244+
* [.basicTable(name)](#PoolPlus+basicTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
243245
* [.defineTable(name, schema, [migrationStrategy])](#PoolPlus+defineTable) ⇒ <code>[MySQLTable](#MySQLTable)</code>
244246
* [.sync(cb)](#PoolPlus+sync) ⇒ <code>void</code>
245247
* [.pquery(sql, [values], [cb])](#PoolPlus+pquery) ⇒ <code>Promise</code>
@@ -272,6 +274,21 @@ const userTable = pool.defineTable('user', {
272274
```
273275

274276

277+
---
278+
279+
<a name="PoolPlus+basicTable"></a>
280+
281+
### poolPlus.basicTable(name) ⇒ <code>[MySQLTable](#MySQLTable)</code>
282+
Simply returns an instance of [`MySQLTable`](#MySQLTable)
283+
for querying the table with the given `name`.
284+
285+
286+
| Param | Type | Description |
287+
|:--- |:--- |:--- |
288+
| name | <code>string</code> | The name of the table. |
289+
290+
**Returns**: <code>[MySQLTable](#MySQLTable)</code> - A `MySQLTable` instance.
291+
275292
---
276293

277294
<a name="PoolPlus+defineTable"></a>
@@ -286,7 +303,7 @@ Defines a table to be created or updated in the database.
286303
| schema | <code>Object</code> | An object that defines the table's schema. See the [Defining Table Schemas](#defining-table-schemas) section. |
287304
| [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). |
288305

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

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

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

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

lib/MySQLTable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
* A class that provides convenient methods for performing queries.<br>To create
5-
* an instance, use {@link PoolPlus#defineTable|`poolPlus.defineTable()`}.
5+
* an instance, use {@link PoolPlus#defineTable|`poolPlus.defineTable()`} or
6+
* {@link PoolPlus#basicTable|`poolPlus.basicTable()`}.
67
*
78
* @see {@link https://github.com/mysqljs/mysql#performing-queries}
89
*/

lib/PoolPlus.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ class PoolPlus extends Pool {
5454
return SqlString.format(sql, values, connConfig.stringifyObjects, connConfig.timezone);
5555
}
5656

57+
/**
58+
* Simply returns an instance of {@link MySQLTable|`MySQLTable`}
59+
* for querying the table with the given `name`.
60+
*
61+
* @param {string} name - The name of the table.
62+
* @returns {MySQLTable} A `MySQLTable` instance.
63+
*/
64+
basicTable(name) {
65+
if (typeof name !== 'string') {
66+
throw new TypeError('The table name must be a string');
67+
}
68+
69+
return new MySQLTable(name, undefined, this);
70+
}
71+
5772
/**
5873
* Defines a table to be created or updated in the database.
5974
*
@@ -63,7 +78,7 @@ class PoolPlus extends Pool {
6378
* @param {string} [migrationStrategy] - One of `safe`, `alter`, or `drop`. This will override
6479
* the `migrationStrategy` value from the {@link module:mysql-plus~createPool|`config`}
6580
* (but is still subject to the same restrictions in production environments).
66-
* @returns {MySQLTable} A `MySQLTable` instance that lets you perform operations on the table.
81+
* @returns {MySQLTable} A `MySQLTable` instance that lets you perform queries on the table.
6782
* @see [Defining Table Schemas](#defining-table-schemas)
6883
*
6984
* @example

test/unit/PoolPlus.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,33 @@ describe('PoolPlus', () => {
6969
});
7070

7171

72+
describe('#basicTable()', () => {
73+
74+
it('should return a MySQLTable instance', () => {
75+
const table = pool.basicTable(TEST_TABLE_NAME, TEST_TABLE_SCHEMA);
76+
table.should.be.an.instanceOf(MySQLTable);
77+
table.name.should.equal(TEST_TABLE_NAME);
78+
should.strictEqual(table.schema, undefined);
79+
table.pool.should.equal(pool);
80+
});
81+
82+
it('should throw if the table name is not a string', () => {
83+
(() => pool.basicTable()).should.throw(TypeError);
84+
(() => pool.basicTable()).should.throw(/The table name must be a string/);
85+
(() => pool.basicTable(/table/)).should.throw(/The table name must be a string/);
86+
});
87+
88+
});
89+
90+
7291
describe('#defineTable()', () => {
7392

7493
it('should return a MySQLTable instance', () => {
7594
const table = pool.defineTable(TEST_TABLE_NAME, TEST_TABLE_SCHEMA);
7695
table.should.be.an.instanceOf(MySQLTable);
7796
table.name.should.equal(TEST_TABLE_NAME);
7897
table.schema.should.equal(TEST_TABLE_SCHEMA);
98+
table.pool.should.equal(pool);
7999
});
80100

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

0 commit comments

Comments
 (0)