X-Git-Url: http://git.squeep.com/?p=squeep-indie-auther;a=blobdiff_plain;f=src%2Fdb%2Findex.js;fp=src%2Fdb%2Findex.js;h=0d5ef16be8bf042b47c77a6b9c44cabd84ea4c1c;hp=0000000000000000000000000000000000000000;hb=b0103b0d496262c438b40bc20304081dbfe41e73;hpb=8ed81748bce7cea7904cac7225b20a60cafdfc16 diff --git a/src/db/index.js b/src/db/index.js new file mode 100644 index 0000000..0d5ef16 --- /dev/null +++ b/src/db/index.js @@ -0,0 +1,42 @@ +'use strict'; + +const common = require('../common'); +const DatabaseErrors = require('./errors'); + +const _fileScope = common.fileScope(__filename); + +class DatabaseFactory { + constructor(logger, options, ...rest) { + const _scope = _fileScope('constructor'); + + const connectionString = options.db.connectionString || ''; + const protocol = connectionString.slice(0, connectionString.indexOf('://')).toLowerCase(); + + let Engine; + switch (protocol) { + case DatabaseFactory.Engines.PostgreSQL: + Engine = require('./postgres'); + break; + + case DatabaseFactory.Engines.SQLite: + Engine = require('./sqlite'); + break; + + default: + logger.error(_scope, 'unsupported connectionString', { protocol, options }); + throw new DatabaseErrors.UnsupportedEngine(protocol); + } + + return new Engine(logger, options, ...rest); + } + + static get Engines() { + return { + PostgreSQL: 'postgresql', + SQLite: 'sqlite', + }; + } + +} + +module.exports = DatabaseFactory;