Skip to content

Commit 98976e0

Browse files
authored
feat(client-core): add CacheMode type (#10091)
* feat(client-core): add CacheMode type * feat(client-core): add synchronization comment for CacheMode type
1 parent 3164adc commit 98976e0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/cubejs-client-core/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ProgressResult from './ProgressResult';
66
import HttpTransport, { ErrorResponse, ITransport, TransportOptions } from './HttpTransport';
77
import RequestError from './RequestError';
88
import {
9+
CacheMode,
910
ExtractTimeMembers,
1011
LoadResponse,
1112
MetaResponse,
@@ -107,6 +108,10 @@ export type CubeSqlOptions = LoadMethodOptions & {
107108
* Query timeout in milliseconds
108109
*/
109110
timeout?: number;
111+
/**
112+
* Cache mode for query execution
113+
*/
114+
cache?: CacheMode;
110115
};
111116

112117
export type CubeSqlSchemaColumn = {
@@ -711,6 +716,7 @@ class CubeApi {
711716
() => {
712717
const request = this.request('cubesql', {
713718
query: sqlQuery,
719+
cache: options?.cache,
714720
method: 'POST',
715721
signal: options?.signal,
716722
fetchTimeout: options?.timeout
@@ -768,7 +774,8 @@ class CubeApi {
768774
fetchTimeout: options?.timeout,
769775
baseRequestId: uuidv4(),
770776
params: {
771-
query: sqlQuery
777+
query: sqlQuery,
778+
cache: options?.cache
772779
}
773780
});
774781

packages/cubejs-client-core/src/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,30 @@ export type ProgressResponse = {
526526
stage: string;
527527
timeElapsed: number;
528528
};
529+
530+
// NOTE: This type must be kept in sync with CacheMode in @cubejs-backend/shared (packages/cubejs-backend-shared/src/shared-types.ts)
531+
// This duplication is intentional as @cubejs-client/core does not depend on @cubejs-backend/shared
532+
/**
533+
* Cache mode options for query execution.
534+
*
535+
* - **stale-if-slow** (default): Equivalent to previously used `renewQuery: false`.
536+
* If refresh keys are up-to-date, returns the value from cache.
537+
* If refresh keys are expired, tries to return the value from the database.
538+
* Returns fresh value from the database if the query executed until the first "Continue wait" interval is reached.
539+
* Returns stale value from cache otherwise.
540+
*
541+
* - **stale-while-revalidate**: AKA "backgroundRefresh".
542+
* If refresh keys are up-to-date, returns the value from cache.
543+
* If refresh keys are expired, returns stale data from cache.
544+
* Updates the cache in background.
545+
*
546+
* - **must-revalidate**: Equivalent to previously used `renewQuery: true`.
547+
* If refresh keys are up-to-date, returns the value from cache.
548+
* If refresh keys are expired, tries to return the value from the database.
549+
* Returns fresh value from the database even if it takes minutes and many "Continue wait" intervals.
550+
*
551+
* - **no-cache**: AKA "forceRefresh".
552+
* Skips refresh key checks.
553+
* Returns fresh data from the database, even if it takes minutes and many "Continue wait" intervals.
554+
*/
555+
export type CacheMode = 'stale-if-slow' | 'stale-while-revalidate' | 'must-revalidate' | 'no-cache';

0 commit comments

Comments
 (0)