No description
  • JavaScript 98.6%
  • HTML 1.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-11 14:34:14 +00:00
lib first commit 2026-11-02 11:15:10 +01:00
test Adds subscribe, for PostgreSQL's notifications 2026-11-09 10:49:45 +01:00
db-connector.js Loads the drivers from the new pages host 2026-11-11 15:22:15 +01:00
LICENSE first commit 2026-11-02 11:15:10 +01:00
README.md Points the links at the new pages host and Forgejo 2026-11-11 15:33:34 +01:00
test.html first commit 2026-11-02 11:15:10 +01:00
test.js first commit 2026-11-02 11:15:10 +01:00

db-connector

Run the tests

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.