Skip to content

Commit eebba69

Browse files
authored
feat(data-connect): Caching for FDC (#9439)
1 parent 6041509 commit eebba69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3206
-534
lines changed

.changeset/fast-cherries-fold.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@firebase/data-connect": minor
3+
"@firebase/util": minor
4+
"firebase": minor
5+
---
6+
7+
Add Memory-Based caching to Queries in Firebase Data Connect.

common/api-review/data-connect.api.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ import { FirebaseError } from '@firebase/util';
1111
import { LogLevelString } from '@firebase/logger';
1212
import { Provider } from '@firebase/component';
1313

14+
// @public (undocumented)
15+
export interface CacheProvider<T extends StorageType> {
16+
// (undocumented)
17+
type: T;
18+
}
19+
20+
// @public (undocumented)
21+
export interface CacheSettings {
22+
// (undocumented)
23+
cacheProvider: CacheProvider<StorageType>;
24+
// (undocumented)
25+
maxAgeSeconds?: number;
26+
}
27+
1428
// @public
1529
export type CallerSdkType = 'Base' | 'Generated' | 'TanstackReactCore' | 'GeneratedReact' | 'TanstackAngularCore' | 'GeneratedAngular';
1630

@@ -66,6 +80,12 @@ export class DataConnect {
6680
setInitialized(): void;
6781
}
6882

83+
// @public (undocumented)
84+
export interface DataConnectEntityArray {
85+
// (undocumented)
86+
entityIds: string[];
87+
}
88+
6989
// @public
7090
export class DataConnectError extends FirebaseError {
7191
/* Excluded from this release type: name */
@@ -75,6 +95,11 @@ export class DataConnectError extends FirebaseError {
7595
// @public (undocumented)
7696
export type DataConnectErrorCode = 'other' | 'already-initialized' | 'not-initialized' | 'not-supported' | 'invalid-argument' | 'partial-error' | 'unauthorized';
7797

98+
// @public (undocumented)
99+
export type DataConnectExtension = {
100+
path: Array<string | number>;
101+
} & (DataConnectEntityArray | DataConnectSingleEntity);
102+
78103
// @public
79104
export class DataConnectOperationError extends DataConnectError {
80105
/* Excluded from this release type: name */
@@ -109,20 +134,63 @@ export interface DataConnectResult<Data, Variables> extends OpResult<Data> {
109134
ref: OperationRef<Data, Variables>;
110135
}
111136

137+
// @public (undocumented)
138+
export interface DataConnectSettings {
139+
// (undocumented)
140+
cacheSettings?: CacheSettings;
141+
}
142+
143+
// @public (undocumented)
144+
export interface DataConnectSingleEntity {
145+
// (undocumented)
146+
entityId: string;
147+
}
148+
149+
// @public
150+
export interface DataConnectSubscription<Data, Variables> {
151+
// (undocumented)
152+
errCallback?: (e?: DataConnectError) => void;
153+
// (undocumented)
154+
unsubscribe: () => void;
155+
// (undocumented)
156+
userCallback: OnResultSubscription<Data, Variables>;
157+
}
158+
112159
// @public (undocumented)
113160
export type DataSource = typeof SOURCE_CACHE | typeof SOURCE_SERVER;
114161

115162
// @public
116163
export function executeMutation<Data, Variables>(mutationRef: MutationRef<Data, Variables>): MutationPromise<Data, Variables>;
117164

118165
// @public
119-
export function executeQuery<Data, Variables>(queryRef: QueryRef<Data, Variables>): QueryPromise<Data, Variables>;
166+
export function executeQuery<Data, Variables>(queryRef: QueryRef<Data, Variables>, options?: ExecuteQueryOptions): QueryPromise<Data, Variables>;
167+
168+
// @public (undocumented)
169+
export interface ExecuteQueryOptions {
170+
// (undocumented)
171+
fetchPolicy: QueryFetchPolicy;
172+
}
173+
174+
// @public (undocumented)
175+
export interface Extensions {
176+
// (undocumented)
177+
dataConnect?: DataConnectExtension[];
178+
}
120179

121180
// @public
181+
export function getDataConnect(options: ConnectorConfig, settings?: DataConnectSettings): DataConnect;
182+
183+
// @public (undocumented)
122184
export function getDataConnect(options: ConnectorConfig): DataConnect;
123185

124186
// @public
125-
export function getDataConnect(app: FirebaseApp, options: ConnectorConfig): DataConnect;
187+
export function getDataConnect(app: FirebaseApp, connectorConfig: ConnectorConfig): DataConnect;
188+
189+
// @public
190+
export function getDataConnect(app: FirebaseApp, connectorConfig: ConnectorConfig, settings: DataConnectSettings): DataConnect;
191+
192+
// @public (undocumented)
193+
export function makeMemoryCacheProvider(): CacheProvider<'MEMORY'>;
126194

127195
// @public (undocumented)
128196
export const MUTATION_STR = "mutation";
@@ -175,6 +243,8 @@ export interface OpResult<Data> {
175243
// (undocumented)
176244
data: Data;
177245
// (undocumented)
246+
extensions?: Extensions;
247+
// (undocumented)
178248
fetchTime: string;
179249
// (undocumented)
180250
source: DataSource;
@@ -183,6 +253,16 @@ export interface OpResult<Data> {
183253
// @public (undocumented)
184254
export const QUERY_STR = "query";
185255

256+
// @public
257+
export const QueryFetchPolicy: {
258+
readonly PREFER_CACHE: "PREFER_CACHE";
259+
readonly CACHE_ONLY: "CACHE_ONLY";
260+
readonly SERVER_ONLY: "SERVER_ONLY";
261+
};
262+
263+
// @public (undocumented)
264+
export type QueryFetchPolicy = (typeof QueryFetchPolicy)[keyof typeof QueryFetchPolicy];
265+
186266
// @public
187267
export interface QueryPromise<Data, Variables> extends Promise<QueryResult<Data, Variables>> {
188268
}
@@ -238,6 +318,14 @@ export const SOURCE_CACHE = "CACHE";
238318
// @public (undocumented)
239319
export const SOURCE_SERVER = "SERVER";
240320

321+
// @public (undocumented)
322+
export const StorageType: {
323+
readonly MEMORY: "MEMORY";
324+
};
325+
326+
// @public (undocumented)
327+
export type StorageType = (typeof StorageType)[keyof typeof StorageType];
328+
241329
// @public
242330
export function subscribe<Data, Variables>(queryRefOrSerializedResult: QueryRef<Data, Variables> | SerializedRef<Data, Variables>, observer: SubscriptionOptions<Data, Variables>): QueryUnsubscribe;
243331

common/api-review/util.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ export class FirebaseError extends Error {
221221
// @public
222222
export type FirebaseSignInProvider = 'custom' | 'email' | 'password' | 'phone' | 'anonymous' | 'google.com' | 'facebook.com' | 'github.com' | 'twitter.com' | 'microsoft.com' | 'apple.com';
223223

224+
// @public
225+
export function generateSHA256Hash(input: string): Promise<string>;
226+
224227
// @public
225228
export const getDefaultAppConfig: () => Record<string, string> | undefined;
226229

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"description": "A set of guides for interacting with the generated firebase dataconnect sdk",
3+
"mcpServers": {
4+
"firebase": {
5+
"command": "npx",
6+
"args": ["-y", "firebase-tools@latest", "experimental:mcp"]
7+
}
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Setup
2+
3+
If the user hasn't already installed the SDK, always run the user's node package manager of choice, and install the package in the directory ../package.json.
4+
For more information on where the library is located, look at the connector.yaml file.
5+
6+
```ts
7+
import { initializeApp } from 'firebase/app';
8+
9+
initializeApp({
10+
// fill in your project config here using the values from your Firebase project or from the `firebase_get_sdk_config` tool from the Firebase MCP server.
11+
});
12+
```
13+
14+
Then, you can run the SDK as needed.
15+
```ts
16+
import { ... } from '@firebasegen/default-connector';
17+
```
18+
19+
20+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Basic Usage
2+
3+
Always prioritize using a supported framework over using the generated SDK
4+
directly. Supported frameworks simplify the developer experience and help ensure
5+
best practices are followed.
6+
7+
8+
9+
10+
11+
## Advanced Usage
12+
If a user is not using a supported framework, they can use the generated SDK directly.
13+
14+
Here's an example of how to use it with the first 5 operations:
15+
16+
```js
17+
import { createMovie, listMovies } from '@firebasegen/default-connector';
18+
19+
20+
// Operation CreateMovie: For variables, look at type CreateMovieVars in ../index.d.ts
21+
const { data } = await CreateMovie(dataConnect, createMovieVars);
22+
23+
// Operation ListMovies:
24+
const { data } = await ListMovies(dataConnect);
25+
26+
27+
```

0 commit comments

Comments
 (0)