All files / src/store count.store.ts

100% Statements 20/20
100% Branches 11/11
100% Functions 3/3
100% Lines 17/17

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 31 32 33 34 35 36 37 38 39 40 41 42        4x 4x 4x   4x   18x 18x 18x       9x   8x 8x     4x         4x         8x   8x 7x   6x 6x      
'use strict';
 
import {Model} from 'mongoose';
 
import AStore from './a.store';
import EStoreType from '../enums/store.type.enum';
import getHrtimeAsNumber from '../functions/performance/get.hrtime.as.number';
 
export default class CountStore extends AStore {
	constructor(model: Model<any>, target: string) {
		super(model, target);
		this._type = EStoreType.COUNT;
		Object.setPrototypeOf(this, CountStore.prototype);
	}
 
	protected shouldReload(change: any): boolean {
		if (this.isInitialSubscription(change)) return true;
 
		const {operationType: type} = change;
		switch (type) {
			case 'delete':
			case 'insert':
				return true;
 
			case 'replace':
			case 'update':
			default:
				return false;
		}
	}
 
	protected async load(change: any): Promise<void> {
		const startTime: number = getHrtimeAsNumber();
 
		if (Object.keys(this._config).length === 0) return this.emitOne(startTime, this._subscriptionId);
		if (!this.shouldReload(change)) return;
 
		const count = await this._model.countDocuments(this._query);
		this.emitOne(startTime, this._subscriptionId, count);
	}
}