initial commit
[squeep-indie-auther] / src / db / index.js
diff --git a/src/db/index.js b/src/db/index.js
new file mode 100644 (file)
index 0000000..0d5ef16
--- /dev/null
@@ -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;