All files / src postgres.backend.ts

100% Statements 66/66
100% Branches 45/45
100% Functions 23/23
100% Lines 55/55

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      3x 3x           3x   3x               31x 31x 31x   31x 31x 31x       1x       2x       4x             4x 4x 4x       3x 3x       3x       1x 1x       1x 1x       1x       1x       2x 2x 2x   2x       2x       3x       4x       14x 9x   7x 7x 11x 11x 8x 5x   7x       4x   1x 1x 4x 4x   1x       7x 4x   5x 3x       7x   3x 7x   3x      
'use strict';
 
import {Observable} from 'rxjs';
import {wrap} from '@mikro-orm/core';
import {cloneDeep, each, isArray, isEmpty, isString, keys, omit} from 'lodash';
 
import type {IObservableBackend} from '@owservable/core';
 
import PostgresListener from './postgres.listener';
import PostgresObservableTable from './functions/observable.table';
import PostgresObservableTablesMap from './functions/observable.tables.map';
 
export default class PostgresBackend implements IObservableBackend {
	private readonly _orm: any;
	private readonly _entity: any;
	private readonly _listener: PostgresListener;
	private readonly _tableName: string;
	private readonly _pkProperty: string;
 
	constructor(orm: any, entity: any, listener: PostgresListener) {
		this._orm = orm;
		this._entity = entity;
		this._listener = listener;
 
		const meta: any = orm.getMetadata().get(entity.name);
		this._tableName = meta.tableName;
		this._pkProperty = meta.primaryKeys[0];
	}
 
	public target(): string {
		return this._tableName;
	}
 
	public changes(): Observable<any> {
		return this._observableTable();
	}
 
	public async find(query: any, fields: any, paging: any, sort: any, populates: any[]): Promise<any[]> {
		const options: any = {
			fields: this._translateFields(fields),
			orderBy: this._translateSort(sort),
			offset: paging?.skip,
			limit: paging?.limit,
			populate: this._translatePopulates(populates)
		};
		const em: any = this._orm.em.fork();
		const entities: any[] = await em.find(this._entity, this._translateQuery(query), options);
		return entities.map((entity: any): any => this._toObject(entity));
	}
 
	public async findOne(query: any, fields: any, populates: any[]): Promise<any> {
		const em: any = this._orm.em.fork();
		const entity: any = await em.findOne(this._entity, this._translateQuery(query), {
			fields: this._translateFields(fields),
			populate: this._translatePopulates(populates)
		});
		return entity ? this._toObject(entity) : entity;
	}
 
	public async findById(id: string, fields: any, populates: any[]): Promise<any> {
		const observableTable: PostgresObservableTable = this._observableTable();
		return this.findOne({[this._pkProperty]: observableTable.coercePrimaryKey(id)}, fields, populates);
	}
 
	public async count(query: any): Promise<number> {
		const em: any = this._orm.em.fork();
		return em.count(this._entity, this._translateQuery(query));
	}
 
	public async populate(document: any, _populate: any): Promise<any> {
		return document;
	}
 
	public toJSON(document: any): any {
		return document;
	}
 
	public async resolveVirtuals(document: any, virtuals: string[]): Promise<any> {
		const replacement: any = cloneDeep(omit(document, virtuals));
		for (const virtual of virtuals) {
			replacement[virtual] = await Promise.resolve(document[virtual]);
		}
		return replacement;
	}
 
	public get entity(): any {
		return this._entity;
	}
 
	private _observableTable(): PostgresObservableTable {
		return PostgresObservableTablesMap.get(this._orm, this._entity, this._listener);
	}
 
	private _toObject(entity: any): any {
		return wrap(entity).toObject();
	}
 
	private _translateQuery(query: any): any {
		if (!query || isString(query)) return query;
		if (isArray(query)) return query.map((entry: any): any => this._translateQuery(entry));
 
		const translated: any = {};
		each(keys(query), (key: string): void => {
			const value: any = query[key];
			if ('_id' === key) translated[this._pkProperty] = value;
			else if ('$and' === key || '$or' === key || '$nor' === key) translated[key] = this._translateQuery(value);
			else translated[key] = value;
		});
		return translated;
	}
 
	private _translateSort(sort: any): any {
		if (isEmpty(sort)) return undefined;
 
		const orderBy: any = {};
		each(keys(sort), (key: string): void => {
			const direction: any = sort[key];
			orderBy[key] = -1 === direction || 'desc' === direction ? 'desc' : 'asc';
		});
		return orderBy;
	}
 
	private _translateFields(fields: any): string[] | undefined {
		if (isEmpty(fields)) return undefined;
		if (isArray(fields)) return fields;
 
		const included: string[] = keys(fields).filter((key: string): boolean => !!fields[key]);
		return isEmpty(included) ? undefined : included;
	}
 
	private _translatePopulates(populates: any[]): string[] | undefined {
		if (isEmpty(populates)) return undefined;
 
		const paths: string[] = populates //
			.map((populate: any): string => (isString(populate) ? populate : populate?.path))
			.filter(Boolean);
		return isEmpty(paths) ? undefined : paths;
	}
}