Skip to content

Commit 047a838

Browse files
committed
return cacheMode into query object
1 parent c4e6fed commit 047a838

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ import {
5252
PreAggJob,
5353
PreAggJobStatusItem,
5454
PreAggJobStatusResponse,
55-
SqlApiRequest, MetaResponseResultFn,
55+
SqlApiRequest,
56+
MetaResponseResultFn,
57+
RequestQuery,
5658
} from './types/request';
5759
import {
5860
CheckAuthInternalOptions,
@@ -319,7 +321,7 @@ class ApiGateway {
319321
context: req.context,
320322
res: this.resToResultFn(res),
321323
queryType: req.query.queryType,
322-
cacheMode: this.normalizeCacheMode(req.query.query, req.query.cache),
324+
cacheMode: req.query.cache,
323325
});
324326
}));
325327

@@ -330,7 +332,7 @@ class ApiGateway {
330332
context: req.context,
331333
res: this.resToResultFn(res),
332334
queryType: req.body.queryType,
333-
cacheMode: this.normalizeCacheMode(req.body.query, req.body.cache),
335+
cacheMode: req.body.cache,
334336
});
335337
}));
336338

@@ -340,7 +342,7 @@ class ApiGateway {
340342
context: req.context,
341343
res: this.resToResultFn(res),
342344
queryType: req.query.queryType,
343-
cacheMode: this.normalizeCacheMode(req.query.query, req.query.cache),
345+
cacheMode: req.query.cache,
344346
});
345347
}));
346348

@@ -587,17 +589,22 @@ class ApiGateway {
587589
return requestStarted && (new Date().getTime() - requestStarted.getTime());
588590
}
589591

590-
// TODO: Drop this when renewQuery will be removed
591-
private normalizeCacheMode(query, cache: string): CacheMode {
592-
if (cache !== undefined) {
593-
return cache as CacheMode;
594-
} else if (query?.renewQuery !== undefined) {
595-
return query.renewQuery === true
592+
private normalizeQueryCacheMode(query: Query, cacheMode: CacheMode | undefined): Query {
593+
if (cacheMode !== undefined) {
594+
query.cacheMode = cacheMode;
595+
} else if (!query.cacheMode && query?.renewQuery !== undefined) {
596+
// TODO: Drop this when renewQuery will be removed
597+
query.cacheMode = query.renewQuery === true
596598
? 'must-revalidate'
597599
: 'stale-if-slow';
600+
} else if (!query.cacheMode) {
601+
query.cacheMode = 'stale-if-slow';
598602
}
599603

600-
return 'stale-if-slow';
604+
// TODO: Drop this when renewQuery will be removed
605+
query.renewQuery = undefined;
606+
607+
return query;
601608
}
602609

603610
private filterVisibleItemsInMeta(context: RequestContext, cubes: any[]) {
@@ -1224,8 +1231,11 @@ class ApiGateway {
12241231
context: RequestContext,
12251232
persistent = false,
12261233
memberExpressions: boolean = false,
1234+
cacheMode?: CacheMode,
12271235
): Promise<[QueryType, NormalizedQuery[], NormalizedQuery[]]> {
12281236
let query = this.parseQueryParam(inputQuery);
1237+
query = Array.isArray(query) ? query.map(q => this.normalizeQueryCacheMode(q, cacheMode))
1238+
: this.normalizeQueryCacheMode(query, cacheMode);
12291239

12301240
let queryType: QueryType = QueryTypeEnum.REGULAR_QUERY;
12311241
if (!Array.isArray(query)) {
@@ -1309,13 +1319,13 @@ class ApiGateway {
13091319
type: 'Query Rewrite completed',
13101320
queryRewriteId,
13111321
normalizedQueries,
1312-
duration: new Date().getTime() - startTime,
1322+
duration: Date.now() - startTime,
13131323
query
13141324
}, context);
13151325

13161326
normalizedQueries = normalizedQueries.map(q => remapToQueryAdapterFormat(q));
13171327

1318-
if (normalizedQueries.find((currentQuery) => !currentQuery)) {
1328+
if (normalizedQueries.some((currentQuery) => !currentQuery)) {
13191329
throw new Error('queryTransformer returned null query. Please check your queryTransformer implementation');
13201330
}
13211331

@@ -1660,14 +1670,13 @@ class ApiGateway {
16601670
context: RequestContext,
16611671
normalizedQuery: NormalizedQuery,
16621672
sqlQuery: any,
1663-
cacheMode: CacheMode = 'stale-if-slow',
16641673
): Promise<ResultWrapper> {
16651674
const queries: QueryBody[] = [{
16661675
...sqlQuery,
16671676
query: sqlQuery.sql[0],
16681677
values: sqlQuery.sql[1],
16691678
renewQuery: normalizedQuery.renewQuery,
1670-
cacheMode,
1679+
cacheMode: normalizedQuery.cacheMode,
16711680
requestId: context.requestId,
16721681
context,
16731682
persistent: false,
@@ -1691,7 +1700,7 @@ class ApiGateway {
16911700
query: totalQuery.sql[0],
16921701
values: totalQuery.sql[1],
16931702
renewQuery: normalizedTotal.renewQuery,
1694-
cacheMode,
1703+
cacheMode: normalizedTotal.cacheMode,
16951704
requestId: context.requestId,
16961705
context
16971706
});
@@ -1851,6 +1860,7 @@ class ApiGateway {
18511860
context,
18521861
res,
18531862
apiType = 'rest',
1863+
cacheMode,
18541864
...props
18551865
} = request;
18561866
const requestStarted = new Date();
@@ -1872,7 +1882,7 @@ class ApiGateway {
18721882
}, context);
18731883

18741884
const [queryType, normalizedQueries] =
1875-
await this.getNormalizedQueries(query, context);
1885+
await this.getNormalizedQueries(query, context, false, false, cacheMode);
18761886

18771887
if (
18781888
queryType !== QueryTypeEnum.REGULAR_QUERY &&
@@ -1905,7 +1915,6 @@ class ApiGateway {
19051915
context,
19061916
normalizedQuery,
19071917
sqlQueries[index],
1908-
props.cacheMode,
19091918
);
19101919

19111920
const annotation = prepareAnnotation(
@@ -1967,6 +1976,7 @@ class ApiGateway {
19671976
const {
19681977
context,
19691978
res,
1979+
cacheMode,
19701980
} = request;
19711981
const requestStarted = new Date();
19721982

@@ -1981,7 +1991,7 @@ class ApiGateway {
19811991
}
19821992

19831993
const [queryType, normalizedQueries] =
1984-
await this.getNormalizedQueries(query, context, request.streaming, request.memberExpressions);
1994+
await this.getNormalizedQueries(query, context, request.streaming, request.memberExpressions, cacheMode);
19851995

19861996
const compilerApi = await this.getCompilerApi(context);
19871997
let metaConfigResult = await compilerApi.metaConfig(request.context, {
@@ -2099,7 +2109,7 @@ class ApiGateway {
20992109
}
21002110

21012111
public async subscribe({
2102-
query, context, res, subscribe, subscriptionState, queryType, apiType
2112+
query, context, res, subscribe, subscriptionState, queryType, apiType, cacheMode
21032113
}) {
21042114
const requestStarted = new Date();
21052115
try {
@@ -2112,7 +2122,7 @@ class ApiGateway {
21122122
let error: any = null;
21132123

21142124
if (!subscribe) {
2115-
await this.load({ query, context, res, queryType, apiType });
2125+
await this.load({ query, context, res, queryType, apiType, cacheMode });
21162126
return;
21172127
}
21182128

@@ -2129,6 +2139,7 @@ class ApiGateway {
21292139
},
21302140
queryType,
21312141
apiType,
2142+
cacheMode,
21322143
});
21332144
const state = await subscriptionState();
21342145
if (result && (!state || JSON.stringify(state.result) !== JSON.stringify(result))) {
@@ -2159,7 +2170,7 @@ class ApiGateway {
21592170
};
21602171
}
21612172

2162-
protected parseQueryParam(query): Query | Query[] {
2173+
protected parseQueryParam(query: RequestQuery | 'undefined'): Query | Query[] {
21632174
if (!query || query === 'undefined') {
21642175
throw new UserError('Query param is required');
21652176
}

packages/cubejs-api-gateway/src/query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const querySchema = Joi.object().keys({
189189
total: Joi.boolean(),
190190
// @deprecated
191191
renewQuery: Joi.boolean(),
192+
cacheMode: Joi.valid('stale-if-slow', 'stale-while-revalidate', 'must-revalidate', 'no-cache'),
192193
ungrouped: Joi.boolean(),
193194
responseFormat: Joi.valid('default', 'compact'),
194195
subqueryJoins: Joi.array().items(subqueryJoin),

packages/cubejs-api-gateway/src/types/query.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
QueryTimeDimensionGranularity,
1313
} from './strings';
1414
import { ResultType } from './enums';
15+
import { CacheMode } from '@cubejs-backend/shared';
1516

1617
/**
1718
* Query base filter definition.
@@ -141,6 +142,7 @@ interface Query {
141142
timezone?: string;
142143
// @deprecated
143144
renewQuery?: boolean;
145+
cacheMode?: CacheMode;
144146
ungrouped?: boolean;
145147
responseFormat?: ResultType;
146148

packages/cubejs-api-gateway/src/types/request.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,17 @@ type BaseRequest = {
120120
res: ResponseResultFn
121121
};
122122

123+
type RequestQuery = Record<string, any> | Record<string, any>[] & {
124+
renewQuery?: boolean;
125+
cacheMode?: CacheMode;
126+
cache?: CacheMode;
127+
};
128+
123129
/**
124130
* Data query HTTP request parameters map data type.
125131
*/
126132
type QueryRequest = BaseRequest & {
127-
query: Record<string, any> | Record<string, any>[];
133+
query: RequestQuery;
128134
queryType?: RequestType;
129135
apiType?: ApiType;
130136
resType?: ResultType
@@ -221,6 +227,7 @@ export {
221227
ResponseResultFn,
222228
MetaResponseResultFn,
223229
BaseRequest,
230+
RequestQuery,
224231
QueryRequest,
225232
PreAggsJobsRequest,
226233
PreAggsSelector,

0 commit comments

Comments
 (0)