No description
- JavaScript 98.6%
- HTML 1.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| lib | ||
| test | ||
| db-connector.js | ||
| LICENSE | ||
| README.md | ||
| test.html | ||
| test.js | ||
db-connector
Reaches a MySQL or a PostgreSQL server the same way from the browser, through db-ws-relay. It tells which server is behind the relay from its handshake, loads that server's driver, mysql-ws or pg-ws, and calls stored procedures the server's way, giving back their rows the same on either. Callbacks only.
Getting started
Load triad.js first, then db-connector.js with withFiles. It defines the global dbConnector; connecting loads the rest on its first use.
withFiles(['https://spa-mvc-pages.duckdns.org/db-connector/db-connector.js'], function () {
dbConnector.connect({ relay: 'ws://localhost:8080', user: 'alice', password: 'secret', database: 'app' }, function (err, connection) {
if (dbConnector.failedWith(err, 'accessDenied')) return showLogin();
dbConnector.call(connection, 'app', 'items_list', [], function (err, rows) {
console.log(err || rows);
});
});
});
Calls
| Call | |
|---|---|
dbConnector.server(relay, callback(err, server)) |
'mysql' or 'postgresql', told from the handshake without logging in; err.code is SERVER_UNKNOWN when the relay reaches neither. A relay is asked once per page. |
dbConnector.connect(options, callback(err, connection)) |
options are mysql-ws's: relay, user, password, database, multipleStatements. Loads the server's driver and logs in. On PostgreSQL it connects to the postgres database, with database as the search path: each app is a schema there. The connection has the driver's own calls. |
dbConnector.call(connection, database, procedure, args, callback(err, rows)) |
CALL `database`.procedure(?, …) on MySQL, SELECT * FROM "database".procedure($1, …) on PostgreSQL; rows are objects by column name on either. |
dbConnector.subscribe(connection, channel, onMessage(payload), callback(err, unsubscribe)) |
PostgreSQL's LISTEN: calls onMessage for each notification on channel from any other connection. err.code is UNSUPPORTED on MySQL. unsubscribe(callback(err)) ends it. |
dbConnector.failedWith(err, failure) |
Whether err is accessDenied, limitReached, nameTaken, notAllowed or idleTimeout, by either server's code. |
dbConnector.isPostgresql(connection) |
Whether connection is pg-ws's. |
dbConnector.drivers |
The driver each server's connections load, by server. |
Tests
test.html runs against a fake relay and fake drivers, so no relay or database is needed. It loads triad.js and testrunner from folders beside this one, as online.