2020-04-28 15:32:29 +02:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* Database connector registry
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const requireConnector = require('./require-connector');
|
|
|
|
|
|
|
|
const createConnectorRegistry = ({ defaultConnection, connections }) => {
|
|
|
|
const _connectors = new Map();
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Load connector modules
|
|
|
|
*/
|
|
|
|
load() {
|
|
|
|
for (const connection of Object.values(connections)) {
|
|
|
|
const { connector } = connection;
|
|
|
|
if (!_connectors.has(connector)) {
|
|
|
|
_connectors.set(connector, requireConnector(connector)(strapi));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize connectors
|
|
|
|
*/
|
|
|
|
async initialize() {
|
|
|
|
for (const connector of _connectors.values()) {
|
|
|
|
await connector.initialize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
getAll() {
|
|
|
|
return Array.from(_connectors.values());
|
|
|
|
},
|
|
|
|
|
2020-04-28 15:32:29 +02:00
|
|
|
get(key) {
|
|
|
|
return _connectors.get(key);
|
|
|
|
},
|
|
|
|
|
2020-04-29 16:11:11 +02:00
|
|
|
set(key, val) {
|
|
|
|
_connectors.set(key, val);
|
|
|
|
return this;
|
2020-04-28 15:32:29 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
get default() {
|
|
|
|
const defaultConnector = connections[defaultConnection].connector;
|
|
|
|
return _connectors.get(defaultConnector);
|
|
|
|
},
|
|
|
|
|
2021-08-20 15:23:02 +02:00
|
|
|
getByConnection(connection) {
|
2020-04-28 15:32:29 +02:00
|
|
|
if (!_.has(connections, connection)) {
|
2020-11-27 15:58:50 +01:00
|
|
|
throw new Error('Trying to access a connector for an unknown connection');
|
2020-04-28 15:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const connectorKey = connections[connection].connector;
|
|
|
|
return _connectors.get(connectorKey);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = createConnectorRegistry;
|