/
ant-primary-query-manager.ts
130 lines (119 loc) · 3.93 KB
/
ant-primary-query-manager.ts
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
import * as _ from 'lodash';
import { BasePrimaryQueryManager, PrimaryQueryManager } from './primary-query-manager';
import { QueryResult, TMQuery, TQuery, TResult } from './query-types';
import { Entity } from '../../../model/entity';
import { PersistencySearchOptions } from '../options/persistency-search-options';
import { PrimaryEntityManager } from '../primary-entity-manager';
import { RedisMiddleware } from '../redis-middleware';
export abstract class AntPrimaryQueryManager<TEntity extends Entity, TQueryResult extends QueryResult>
implements BasePrimaryQueryManager<TEntity, TResult<TEntity, TQueryResult>>, PrimaryQueryManager<TEntity> {
/**
* Entity key generator.
*/
protected _entityKeyGen: (entity: TEntity) => string;
/**
* Query key generator.
*/
protected _queryKeyGen: (params: any) => string;
/**
* Multiple query
*/
protected _mquery: TMQuery<TQueryResult>;
/**
* Primary entity manager.
*/
protected _primaryEntityManager: PrimaryEntityManager<TEntity>;
/**
* Query to obtain ids.
*/
protected _query: TQuery<TQueryResult>;
/**
* Redis connection to manage queries.
*/
protected _redis: RedisMiddleware;
/**
* Key of the reverse structure to obtain a map of entities to queries.
*/
protected _reverseHashKey: string;
/**
* Lua expression generator.
*/
protected _luaKeyGeneratorFromId: (alias: string) => string;
/**
* Creates primary query manager.
* @param query Query to obtain ids.
* @param primaryEntityManager Primary entity manager.
* @param redis Redis connection to manage queries.
* @param reverseHashKey Key of the reverse structure to obtain a map of entities to queries.
* @param queryKeyGen Query key generator.
* @param mQuery Multiple query.
*/
public constructor(
query: TQuery<TQueryResult>,
primaryEntityManager: PrimaryEntityManager<TEntity>,
redis: RedisMiddleware,
reverseHashKey: string,
queryKeyGen: (params: any) => string,
entityKeyGen?: (entity: TEntity) => string,
mQuery?: TMQuery<TQueryResult>,
) {
this._primaryEntityManager = primaryEntityManager;
this._query = query;
this._redis = redis;
this._reverseHashKey = reverseHashKey;
this._queryKeyGen = queryKeyGen;
this._entityKeyGen = entityKeyGen ? entityKeyGen : queryKeyGen;
this._luaKeyGeneratorFromId = this._primaryEntityManager.getLuaKeyGeneratorFromId();
this._setMQuery(query, mQuery);
}
/**
* True if the queries managed can return multiple results.
*/
public abstract get isMultiple(): boolean;
/**
* Query key generator.
*/
public get entityKeyGen(): (entity: TEntity) => string {
return this._entityKeyGen;
}
/**
* Query key generator.
*/
public get queryKeyGen(): (params: any) => string {
return this._queryKeyGen;
}
/**
* Obtains the reverse hash key.
*/
public get reverseHashKey(): string {
return this._reverseHashKey;
}
/**
* Gets a query result.
* @param params query params.
* @returns query results.
*/
public abstract get(params: any, options?: PersistencySearchOptions): Promise<TResult<TEntity, TQueryResult>>;
/**
* Gets the result of multiple queries.
* @param paramsArray Queries parameters.
* @param options Cache options.
* @returns Queries results.
*/
public abstract mGet(paramsArray: any[], options?: PersistencySearchOptions): Promise<TEntity[]>;
/**
* Creates an standard mquery.
* @param query query to manage.
*/
private _getDefaultMQuery(query: TQuery<TQueryResult>): TMQuery<TQueryResult> {
return (paramsArray: any[]): Promise<TQueryResult[]> => Promise.all(_.map(paramsArray, query));
}
/**
* Sets the mquery (if provided) or creates a default one.
* @param query query to manage.
* @param mQuery mquery to manage.
*/
private _setMQuery(query: TQuery<TQueryResult>, mQuery: TMQuery<TQueryResult>): void {
this._mquery = mQuery ?? this._getDefaultMQuery(query);
}
}