UNPKG

@riturajgc/upload-manager-service

Version:

Upload manager service for bitnudge

41 lines (35 loc) 1.4 kB
const { getMongoose } = require('../config/mongoose'); const mongooseOriginal = require('mongoose'); const StorageTypes = require('../lib/utils/StorageTypes'); const Storage = require('../lib/storage/Storage'); const Adapter = require('../lib/storage/Adapter'); class App { constructor( type ) { if(!type){ throw new Error('Storage type is required to instantiate upload-manager-service'); } if(!Object.values(StorageTypes.list).includes(type)){ throw new Error('Incorrect storage type is provided'); } if(this.STORAGE_TYPE === StorageTypes.list.INFS || this.STORAGE_TYPE === StorageTypes.list.INS3){ throw new Error(`Storage type ${this.STORAGE_TYPE} is not supported`); } this.STORAGE_TYPE = type; this.mongoose = mongooseOriginal; const StorageModel = require('../models/StorageModel'); const model = StorageModel(this.STORAGE_TYPE, this.mongoose); let storage; if(this.STORAGE_TYPE === StorageTypes.list.INDB) { storage = new Storage.DB({ model }) }else if(this.STORAGE_TYPE === StorageTypes.list.INFS) { storage = new Storage.FS({ model }) }else if(this.STORAGE_TYPE === StorageTypes.list.INS3) { storage = new Storage.S3({ model }) } this.adapter = new Adapter({ type: storage }); } setMongoose({dbURL }) { this.mongoose = getMongoose({ dbURL}); } } module.exports = App;