initial commit
[urlittler] / src / db / index.js
1 'use strict';
2
3 const common = require('../common');
4 const DBErrors = require('./errors');
5
6 const _fileScope = common.fileScope(__filename);
7
8 const defaultOptions = {
9 connectionString: '',
10 };
11
12 class DatabaseFactory {
13 constructor(logger, options, ...rest) {
14 const _scope = _fileScope + ':constructor';
15 options = Object.assign({}, defaultOptions, options);
16 const protocol = options.connectionString.slice(0, options.connectionString.indexOf('://')).toLowerCase();
17 // eslint-disable-next-line sonarjs/no-small-switch
18 switch (protocol) {
19 case DatabaseFactory.Engines.PostgreSQL: {
20 const Postgres = require('./postgres');
21 return new Postgres(logger, options, ...rest);
22 }
23
24 case DatabaseFactory.Engines.SQLite: {
25 const SQLite = require('./sqlite');
26 return new SQLite(logger, options, ...rest);
27 }
28
29 default:
30 logger.error(_scope, 'unsupported connectionString', { options });
31 throw new DBErrors.UnsupportedEngine(protocol);
32 }
33 }
34
35 static get Engines() {
36 return {
37 PostgreSQL: 'postgresql',
38 SQLite: 'sqlite',
39 };
40 }
41
42 }
43
44 module.exports = DatabaseFactory;