All files / src/store count.store.ts

27.27% Statements 6/22
0% Branches 0/8
0% Functions 0/3
31.57% Lines 6/19

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 43 44 45  1x     1x 1x 1x   1x   1x                                                                    
'use strict';
import {isEmpty} from 'lodash';
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';
 
require('json-circular-stringify');
 
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 {
		Iif (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();
 
		Iif (isEmpty(this._config)) return this.emitOne(startTime, this._subscriptionId);
		Iif (!this.shouldReload(change)) return;
		// console.log('[@owservable] -> CountStore::load', JSON.stringify(change));
 
		const count = await this._model.countDocuments(this._query);
		this.emitOne(startTime, this._subscriptionId, count);
	}
}