diff --git a/lib/natural/util/storage/StorageBackend.js b/lib/natural/util/storage/StorageBackend.js index a6187ae42..bba65ea7e 100644 --- a/lib/natural/util/storage/StorageBackend.js +++ b/lib/natural/util/storage/StorageBackend.js @@ -20,12 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -const PostgresPlugin = require('./Postgres') -const MongoDBPlugin = require('./MongoDB') -const RedisPlugin = require('./Redis') -const MemcachedPlugin = require('./Memcached') -const FilePlugin = require('./File') - // Enum for different types of storage backends that can be used const STORAGE_TYPES = { POSTGRES: 'POSTGRES', @@ -49,6 +43,8 @@ class StorageBackend { } // Sets the storage type. Valid values are POSTGRES, REDIS, MONGODB, MEMCACHED, FILE + // Storage plugins are loaded on demand to avoid pulling in database drivers + // and their dependencies for users who only need NLP features. async setStorageType (storageType) { if (!storageType) { throw new Error('No storage type specified') @@ -58,22 +54,32 @@ class StorageBackend { } this.storageType = storageType switch (storageType) { - case STORAGE_TYPES.POSTGRES: + case STORAGE_TYPES.POSTGRES: { + const PostgresPlugin = require('./Postgres') this.client = new PostgresPlugin() await this.client.configPostgres() break - case STORAGE_TYPES.REDIS: + } + case STORAGE_TYPES.REDIS: { + const RedisPlugin = require('./Redis') this.client = new RedisPlugin() break - case STORAGE_TYPES.MONGODB: + } + case STORAGE_TYPES.MONGODB: { + const MongoDBPlugin = require('./MongoDB') this.client = new MongoDBPlugin() break - case STORAGE_TYPES.MEMCACHED: + } + case STORAGE_TYPES.MEMCACHED: { + const MemcachedPlugin = require('./Memcached') this.client = new MemcachedPlugin() break - case STORAGE_TYPES.FILE: + } + case STORAGE_TYPES.FILE: { + const FilePlugin = require('./File') this.client = new FilePlugin() break + } default: throw new Error('Invalid storage type') }