Initial release
[websub-hub] / src / db / index.js
1 'use strict';
2
3 const common = require('../common');
4 const DatabaseErrors = require('./errors');
5
6 const _fileScope = common.fileScope(__filename);
7
8 class DatabaseFactory {
9 constructor(logger, options, ...rest) {
10 const _scope = _fileScope('constructor');
11
12 const connectionString = options.db.connectionString || '';
13 const protocol = connectionString.slice(0, connectionString.indexOf('://')).toLowerCase();
14
15 let Engine;
16 switch (protocol) {
17 case DatabaseFactory.Engines.PostgreSQL:
18 Engine = require('./postgres');
19 break;
20
21 case DatabaseFactory.Engines.SQLite:
22 Engine = require('./sqlite');
23 break;
24
25 default:
26 logger.error(_scope, 'unsupported connectionString', { protocol, options });
27 throw new DatabaseErrors.UnsupportedEngine(protocol);
28 }
29
30 return new Engine(logger, options, ...rest);
31 }
32
33 static get Engines() {
34 return {
35 PostgreSQL: 'postgresql',
36 SQLite: 'sqlite',
37 };
38 }
39
40 }
41
42 module.exports = DatabaseFactory;