All files / src/storage datasource-factory.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30      21x 21x 21x         21x 21x             6x 6x 5x   1x       1x      
import { IDictionary } from '../utils';
import { Datasource } from './datasource';
import { IDatasourceType, Newable } from './interfaces';
import { MemoryDatasource } from './memory-datasource';
import { RestDatasource } from './rest-datasource';
import { SimpleDatasource } from './simple-datasource';
 
/**
 * Base class used to factory datasource based on type
 */
export class DatasourceFactory {
	protected static types: IDictionary<any> = {
		rest: RestDatasource,
		memory: MemoryDatasource,
		simple: SimpleDatasource,
	};
 
	public static factory(datasource: IDatasourceType = {}) {
		const config = { type: 'memory', ...datasource };
		if (DatasourceFactory.types[config.type]) {
			return new DatasourceFactory.types[config.type](config);
		}
		throw new Error(`Datasource ${datasource.type} not registered`);
	}
 
	public static register(type: string, classReference: Newable<Datasource>) {
		DatasourceFactory.types[type] = classReference;
	}
}