All files / src/store document.store.ts

100% Statements 81/81
100% Branches 45/45
100% Functions 16/16
100% Lines 65/65

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    4x   4x 4x   4x 4x   4x 4x     79x   4x   45x 45x 45x       54x   54x 54x       54x     1x     3x 2x 1x   1x 1x         74x   15x 15x 15x   15x   3x     3x       8x 6x 5x   4x   3x       1x       62x   62x 61x   60x 60x 60x   60x   59x   59x 55x   58x   11x 4x     11x   2x 2x   1x 1x         6x   5x 5x   4x 4x 4x 2x   1x       51x       5x 5x       6x      
'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 IObservableBackend from '../backend/i.observable.backend';
import getHrtimeAsNumber from '../functions/performance/get.hrtime.as.number';
import changedFields from '../functions/query/changed.fields';
 
// 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(backend: IObservableBackend, target: string) {
		super(backend, 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 = this._backend
			.changes() //
			.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;
				if (!description) return true;
				if (this.touchesQuery(change)) return true;
 
				if (!this.shouldConsiderFields()) return true;
 
				return !_.isEmpty(_.intersection(Object.keys(this._fields), changedFields(change)));
			}
		}
 
		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);
 
		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) {
				await this._backend.populate(data, populate);
			}
 
			if (_.isEmpty(this._virtuals)) return this.emitOne(startTime, this._subscriptionId, this._backend.toJSON(data));
 
			const jsonData: any = await this._backend.resolveVirtuals(data, this._virtuals);
			this.emitOne(startTime, this._subscriptionId, jsonData);
		} catch (error) {
			console.error('[@owservable/core] -> 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;
		if (this.touchesQuery(change)) return true;
 
		return this.testDocument(document);
	}
 
	private async _loadDocumentById(id: string): Promise<any> {
		return this._backend.findById(id, this._fields, []);
	}
 
	private async _loadSortedFirstDocument(): Promise<any> {
		const docs: any = await this._backend.find(this._query, this._fields, this._paging, this._sort, []);
		return _.first(docs);
	}
 
	private async _loadDocument(): Promise<any> {
		return this._backend.findOne(this._query, this._fields, []);
	}
}