All files / src/store document.store.ts

93.97% Statements 78/83
91.48% Branches 43/47
75% Functions 12/16
94.11% Lines 64/68

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143    4x   4x 4x   4x 4x 4x 4x     62x   4x   34x 34x 34x       42x   42x 42x       42x                 1x 1x         39x   10x 10x 10x   10x   1x     2x       6x 3x   3x   2x 2x 2x 2x       1x       52x   52x 51x   50x 50x 50x   50x     49x   49x 45x   47x   10x 4x     10x   2x 2x 2x   2x   3x 3x         4x   3x 3x   2x 2x 2x   1x       26x       3x         2x       2x      
'use strict';
 
import * as _ from 'lodash';
 
import {asyncScheduler} from 'rxjs';
import {filter, throttleTime} from 'rxjs/operators';
 
import AStore from './a.store';
import EStoreType from '../enums/store.type.enum';
import observableModel from '../mongodb/functions/observable.model.factory';
import getHrtimeAsNumber from '../functions/performance/get.hrtime.as.number';
 
// tslint:disable-next-line:variable-name
const _getIdFromQuery = (query: any): string => (_.isString(query) ? query : _.get(query, '_id', '').toString());
 
export default class DocumentStore extends AStore {
	constructor(model: any, target: string) {
		super(model, target);
		this._type = EStoreType.DOCUMENT;
		Object.setPrototypeOf(this, DocumentStore.prototype);
	}
 
	protected extractFromConfig(): void {
		super.extractFromConfig();
 
		const {skip = 0} = this._config;
		this._paging = skip ? {} : {skip, limit: 1};
	}
 
	public restartSubscription(): void {
		this.subscription = observableModel(this.model) //
			.pipe(throttleTime(this._delay, asyncScheduler, {leading: true, trailing: true}))
			.pipe(filter((change) => this._pipeFilter(change)))
			.subscribe({
				next: (change: any): void => {
					this.load(change) //
						.then((): null => null)
						.catch((e: any): void => this.error(e));
				},
				error: (e: any): void => this.error(e),
				complete: (): void => this.complete()
			});
	}
 
	protected shouldReload(change: any): boolean {
		if (this.isInitialSubscription(change)) return true;
 
		const id: string = _getIdFromQuery(this._query);
		const {operationType: type, documentKey, updateDescription: description} = change;
		const key = _.get(documentKey, '_id', '').toString();
 
		switch (type) {
			case 'delete':
				return true;
 
			case 'insert':
				return !id;
 
			case 'replace':
			case 'update': {
				if (id && id === key) return true;
				Iif (!description) return true;
 
				if (!this.shouldConsiderFields()) return true;
 
				const {updatedFields, removedFields} = description;
				const us: any[] = _.concat(removedFields, _.keys(updatedFields));
				const qs: string[] = _.keys(this._fields);
				return !_.isEmpty(_.intersection(qs, us));
			}
		}
 
		return false;
	}
 
	protected async load(change: any): Promise<void> {
		const startTime: number = getHrtimeAsNumber();
 
		if (_.isEmpty(this._config)) return this.emitOne(startTime, this._subscriptionId);
		if (!this.shouldReload(change)) return;
 
		const id: string = _getIdFromQuery(this._query);
		const {operationType: type, documentKey} = change;
		const key = _.get(documentKey, '_id', '').toString();
 
		if (type === 'delete' && id === key) return this.emitDelete(startTime, this._subscriptionId, key);
 
		// console.log('[@owservable] -> DB Reload Document for query:', this._query);
		try {
			let data;
			if (!_.isEmpty(this._sort)) data = await this._loadSortedFirstDocument();
			else data = id ? await this._loadDocumentById(id) : await this._loadDocument();
 
			if (!data) return this.emitOne(startTime, this._subscriptionId);
 
			for (const populate of this._populates) {
				Eif (data?.populate) await data.populate(populate);
			}
 
			if (_.isEmpty(this._virtuals)) return this.emitOne(startTime, this._subscriptionId, data.toJSON());
 
			const jsonData: any = _.cloneDeep(_.omit(data.toJSON(), this._virtuals));
			for (const virtual of this._virtuals) {
				jsonData[virtual] = await Promise.resolve(data[virtual]);
			}
			this.emitOne(startTime, this._subscriptionId, jsonData);
		} catch (error) {
			console.error('[@owservable] -> DocumentStore::load Error:', {change, error});
			this.emitError(startTime, this._subscriptionId, error);
		}
	}
 
	private _pipeFilter(change: any): boolean {
		if (!_.isEmpty(this._sort)) return true;
 
		const {operationType: type} = change;
		if ('delete' === type) return true;
 
		const {documentKey, fullDocument: document} = change;
		const key = _.get(documentKey, '_id', '').toString();
		if (key === _getIdFromQuery(this._query)) return true;
 
		return this.testDocument(document);
	}
 
	private async _loadDocumentById(id: string): Promise<any> {
		return this._model.findById(id, this._fields);
	}
 
	private async _loadSortedFirstDocument(): Promise<any> {
		const docs: any = await this._model //
			.find(this._query, this._fields, this._paging)
			// .collation({locale: 'en'})
			.sort(this._sort) // @ts-ignore
			.setOptions({allowDiskUse: true});
		return _.first(docs);
	}
 
	private async _loadDocument(): Promise<any> {
		return this._model.findOne(this._query, this._fields);
	}
}