mysql2-observable
Convenient access to promise-based node-mysql2 using rxjs Observables and various ES6 features.
Installation
npm i --save mysql2-observable
AbstractMySQLDatabase example
Provides convenient & safe database object lifecycle management.
First, create a config.json
file in the directory where your
script is located (__dirname__
)
Then, you can create a custom subclass of AbstractMySQLDatabase
to automatically setup the mysql2
connection pool.
const AbstractMySQLDatabase runWithDB = ;const map = ; async { let rows fields = await this return rows } ;
Within the MyDatabase
class, you can use this.Query()
, this.Execute()
equivalently to the respective mysql2
calls. this.pool
provides the raw mysql2
connection pool. Also, you can use this.QueryObservable()
, which we'll cover in the next section.
Observable
to auto-paginate queries
Using For this example, we're assuming that you've created config.json
and you've setup the following table:
(`val` int(11) NOT NULL);INSERT INTO `testtable` (`val`) VALUES (1), (2), (3), (4), (5);
Then, you can use db.QueryObservable()
just like db.Query
.
const AbstractMySQLDatabase runWithDB = ;const map = ;const do = ;const ignoreElements = ;const toPromise = ; { return this } ;
This works internally by inserting a LIMIT ?,?
string at the end of the statement and modifying the offset for each page until no values are left. All values are inserted into the Observable
synchronously, so you won't run out of memory easily even for huge tables.
The default page size is 10000
, but you can modify that by using a different value in the third argument of db.QueryObservable()
. It is recommended to increase this value for very small result rows and decrease it for very large result rows.
Note that in order to avoid having the query still running when the connection is closed (after the async
callback to runWithDB
is finished, the connection pool is closed), we need to convert the Observable
to a Promise
using toPromise()
.
Also note that the member function is not declared async
because it doesn't contain any await
statements, but it acts like an async
function as it returns a Promise
and can therefore be await
ed. This is not a requirement relating to QueryObservable
, if you do some postprocessing outside the Observable
chain, you can await
the Promise
which you converted the Observable
into and postprocess it in an async
member function.
One example of this would be to take the sum of the integers in the table:
const AbstractMySQLDatabase runWithDB = ;const map = ;const toArray = ;const toPromise = ; async { const result = await this // Postprocess let sum = 0; forconst val of result sum += val; return sum } ;
For more information on what you can do with Observable
s, see the RxJS documentation.