Skip to content

Commit

Permalink
Derive all database classes from a new AbstractDatabase class
Browse files Browse the repository at this point in the history
This will make it possible to implement default methods and move over
common behavior.
  • Loading branch information
rhansen committed May 13, 2021
1 parent fb181db commit 52a882e
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 28 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,18 @@ environment variable `NODE_TLS_REJECT_UNAUTHORIZED = 0` and add the flag

## How to add support for another database

1. Add your database to `packages.json`, this will happen automatically if you
run `npm install %yourdatabase%`
1. Add some example settings to `test/lib/databases.js` for your database.
1. Look at `databases/mysql_db.js`, your module have to provide the same
functions. Call it `DATABASENAME_db.js` and reimplement the functions for
your database. Most of your work here will be copy/paste from other databases
so don't be scared.
1. Add your database Travis setup steps to `.travis.yml`, see the
`before_install` section and MySQL example. Note that MySQL has a preloaded
script (comes from mysql.sql) which preloads the database with 1M records. If
you can, you should do the same.
1. Run `npm test` and ensure it's working.
1. Branch from master `git checkout -b my-awesome-database` and submit a pull
request including the changes which should include **1 new and 3 modified
files**.
1. Once merged we really need you to be on top of maintaining your code.
1. Add the database driver to `packages.json`, this will happen automatically if
you run `npm install %yourdatabase%`
1. Create `databases/DATABASENAME_db.js` and have it export a `Database` class
that derives from `lib/AbstractDatabase.js`. Implement the required
functions.
1. Add a service for the database to the test job in
`.github/workflows/npmpublish.yml`.
1. Add an entry to `test/lib/databases.js` for your database and configure it to
work with the service added to the GitHub workflow.
1. Install and start the database server and configure it to work with the
settings in your `test/lib/databases.js` entry.
1. Run `npm test` to ensure that it works.

## License

Expand Down
4 changes: 3 additions & 1 deletion databases/cassandra_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const cassandra = require('cassandra-driver');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
/**
* @param {Object} settings The required settings object to initiate the Cassandra database
* @param {String[]} settings.clientOptions See
Expand All @@ -28,6 +29,7 @@ exports.Database = class {
* information
*/
constructor(settings) {
super();
if (!settings.clientOptions) {
throw new Error('The Cassandra client options should be defined');
}
Expand Down
4 changes: 3 additions & 1 deletion databases/couch_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const nano = require('nano');
const async = require('async');

Expand All @@ -25,8 +26,9 @@ const handleError = (er) => {
if (er) throw new Error(er);
};

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.db = null;
this.client = null;
this.settings = settings;
Expand Down
4 changes: 3 additions & 1 deletion databases/dirty_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
*
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const Dirty = require('dirty');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.db = null;

if (!settings || !settings.filename) {
Expand Down
4 changes: 3 additions & 1 deletion databases/dirty_git_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const Dirty = require('dirty');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.db = null;

if (!settings || !settings.filename) {
Expand Down
4 changes: 3 additions & 1 deletion databases/elasticsearch_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const es = require('elasticsearch');

// initialize w/ default settings
Expand All @@ -30,8 +31,9 @@ const elasticsearchSettings = {

let client;

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.db = null;

this.settings = settings || {};
Expand Down
5 changes: 4 additions & 1 deletion databases/mongodb_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
* limitations under the License.
*/

exports.Database = class {
const AbstractDatabase = require('../lib/AbstractDatabase');

exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.settings = settings;

if (!this.settings.url) throw new Error('You must specify a mongodb url');
Expand Down
4 changes: 3 additions & 1 deletion databases/mssql_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
*
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const async = require('async');
const mssql = require('mssql');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
settings = settings || {};

if (settings.json != null) {
Expand Down
4 changes: 3 additions & 1 deletion databases/mysql_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const mysql = require('mysql');
const util = require('util');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.logger = console;
this._mysqlSettings = {
charset: 'utf8mb4', // temp hack needs a proper fix..
Expand Down
3 changes: 2 additions & 1 deletion databases/postgres_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const async = require('async');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
init(callback) {
const testTableExists = "SELECT 1 as exists FROM pg_tables WHERE tablename = 'store'";

Expand Down
4 changes: 3 additions & 1 deletion databases/redis_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const async = require('async');
const redis = require('redis');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.client = null;
this.settings = settings || {};
}
Expand Down
4 changes: 3 additions & 1 deletion databases/rethink_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const r = require('rethinkdb');
const async = require('async');

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
if (!settings) settings = {};
if (!settings.host) { settings.host = 'localhost'; }
if (!settings.port) { settings.port = 28015; }
Expand Down
4 changes: 3 additions & 1 deletion databases/sqlite_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ try {
'"npm install sqlite3" in your etherpad-lite root folder.');
}

const AbstractDatabase = require('../lib/AbstractDatabase');
const util = require('util');

const escape = (val) => `'${val.replace(/'/g, "''")}'`;

exports.Database = class {
exports.Database = class extends AbstractDatabase {
constructor(settings) {
super();
this.db = null;

if (!settings || !settings.filename) {
Expand Down
18 changes: 18 additions & 0 deletions lib/AbstractDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = class AbstractDatabase {
constructor() {
if (new.target === module.exports) {
throw new TypeError('cannot instantiate Abstract Database directly');
}
for (const fn of ['init', 'close', 'get', 'findKeys', 'remove', 'set']) {
if (typeof this[fn] !== 'function') throw new TypeError(`method ${fn} not defined`);
}
}

doBulk(operations, cb) {
throw new Error('the doBulk method must be implemented if write caching is enabled');
}

get isAsync() { return false; }
};

0 comments on commit 52a882e

Please sign in to comment.