Skip to content

Commit 4361fb8

Browse files
committed
clean up the new api
1 parent 777e30f commit 4361fb8

2 files changed

Lines changed: 53 additions & 60 deletions

File tree

src/data-connect/data-connect-api-client-internal.ts

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,6 @@ import * as utils from '../utils/index';
2525
import * as validator from '../utils/validator';
2626
import { ConnectorConfig, ExecuteGraphqlResponse, GraphqlOptions } from './data-connect-api';
2727

28-
/**
29-
* Converts a JavaScript value into a GraphQL literal string.
30-
* Handles nested objects, arrays, strings, numbers, and booleans.
31-
* Ensures strings are properly escaped.
32-
*/
33-
function objectToString(data: any): string {
34-
if (typeof data !== 'object' || data === null) {
35-
if (typeof data === 'string') {
36-
// Properly escape double quotes and backslashes within strings
37-
const escapedString = data.replace(/\//g, '\\').replace(/,"/g, '"');
38-
return `"${escapedString}"`;
39-
}
40-
// Handle numbers, booleans, null directly
41-
return String(data);
42-
}
43-
44-
if (Array.isArray(data)) {
45-
const elements = data.map(item => objectToString(item)).join(', ');
46-
return `[${elements}]`;
47-
}
48-
49-
// Handle plain objects
50-
const entries = Object.entries(data).map(([key, value]) => {
51-
// GraphQL object keys are typically unquoted identifiers
52-
return `${key}: ${objectToString(value)}`;
53-
});
54-
55-
return `{ ${entries.join(', ')} }`;
56-
}
57-
5828
const API_VERSION = 'v1alpha';
5929

6030
/** The Firebase Data Connect backend base URL format. */
@@ -229,9 +199,38 @@ export class DataConnectApiClient {
229199
return new FirebaseDataConnectError(code, message);
230200
}
231201

202+
/**
203+
* Converts JSON data into a GraphQL literal string.
204+
* Handles nested objects, arrays, strings, numbers, and booleans.
205+
* Ensures strings are properly escaped.
206+
*/
207+
private objectToString(data: any): string {
208+
if (typeof data !== 'object' || data === null) {
209+
if (typeof data === 'string') {
210+
// Properly escape double quotes and backslashes within strings
211+
const escapedString = data.replace(/\//g, '\\').replace(/,"/g, '"');
212+
return `"${escapedString}"`;
213+
}
214+
// Handle numbers, booleans, null directly
215+
return String(data);
216+
}
217+
218+
if (validator.isArray(data)) {
219+
const elements = data.map(item => this.objectToString(item)).join(', ');
220+
return `[${elements}]`;
221+
}
222+
223+
// Handle plain objects
224+
const entries = Object.entries(data).map(([key, value]) => {
225+
// GraphQL object keys are typically unquoted identifiers
226+
return `${key}: ${this.objectToString(value)}`;
227+
});
228+
229+
return `{ ${entries.join(', ')} }`;
230+
}
231+
232232
/**
233233
* Insert a single row into the specified table.
234-
* (Implementation moved from DataConnect class)
235234
*/
236235
public async insert<GraphQlResponse, Variables extends object>(
237236
tableName: string,
@@ -243,13 +242,13 @@ export class DataConnectApiClient {
243242
if (!validator.isNonNullObject(data)) {
244243
throw new FirebaseDataConnectError('invalid-argument', '`data` must be a non-null object.');
245244
}
246-
if (Array.isArray(data)) {
245+
if (validator.isArray(data)) {
247246
throw new FirebaseDataConnectError(
248247
'invalid-argument', '`data` must be an object, not an array, for single insert.');
249248
}
250249

251250
try {
252-
const gqlDataString = objectToString(data);
251+
const gqlDataString = this.objectToString(data);
253252
const mutation = `mutation { ${tableName}_insert(data: ${gqlDataString}) }`;
254253
// Use internal executeGraphql
255254
return this.executeGraphql<GraphQlResponse, Variables>(mutation);
@@ -260,7 +259,6 @@ export class DataConnectApiClient {
260259

261260
/**
262261
* Insert multiple rows into the specified table.
263-
* (Implementation moved from DataConnect class)
264262
*/
265263
public async insertMany<GraphQlResponse, Variables extends Array<unknown>>(
266264
tableName: string,
@@ -274,7 +272,7 @@ export class DataConnectApiClient {
274272
}
275273

276274
try {
277-
const gqlDataString = objectToString(data);
275+
const gqlDataString = this.objectToString(data);
278276
const mutation = `mutation { ${tableName}_insertMany(data: ${gqlDataString}) }`;
279277
// Use internal executeGraphql
280278
return this.executeGraphql<GraphQlResponse, Variables>(mutation);
@@ -285,7 +283,6 @@ export class DataConnectApiClient {
285283

286284
/**
287285
* Insert a single row into the specified table, or update it if it already exists.
288-
* (Implementation moved from DataConnect class)
289286
*/
290287
public async upsert<GraphQlResponse, Variables extends object>(
291288
tableName: string,
@@ -297,13 +294,13 @@ export class DataConnectApiClient {
297294
if (!validator.isNonNullObject(data)) {
298295
throw new FirebaseDataConnectError('invalid-argument', '`data` must be a non-null object.');
299296
}
300-
if (Array.isArray(data)) {
297+
if (validator.isArray(data)) {
301298
throw new FirebaseDataConnectError(
302299
'invalid-argument', '`data` must be an object, not an array, for single upsert.');
303300
}
304301

305302
try {
306-
const gqlDataString = objectToString(data);
303+
const gqlDataString = this.objectToString(data);
307304
const mutation = `mutation { ${tableName}_upsert(data: ${gqlDataString}) }`;
308305
// Use internal executeGraphql
309306
return this.executeGraphql<GraphQlResponse, Variables>(mutation);
@@ -314,7 +311,6 @@ export class DataConnectApiClient {
314311

315312
/**
316313
* Insert multiple rows into the specified table, or update them if they already exist.
317-
* (Implementation moved from DataConnect class)
318314
*/
319315
public async upsertMany<GraphQlResponse, Variables extends Array<unknown>>(
320316
tableName: string,
@@ -328,7 +324,7 @@ export class DataConnectApiClient {
328324
}
329325

330326
try {
331-
const gqlDataString = objectToString(data);
327+
const gqlDataString = this.objectToString(data);
332328
const mutation = `mutation { ${tableName}_upsertMany(data: ${gqlDataString}) }`;
333329
// Use internal executeGraphql
334330
return this.executeGraphql<GraphQlResponse, Variables>(mutation);

src/data-connect/data-connect.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import { App } from '../app';
1919
import { DataConnectApiClient } from './data-connect-api-client-internal';
20-
// FirebaseDataConnectError, objectToString, and validator are no longer needed here
2120

2221
import {
2322
ConnectorConfig,
@@ -47,10 +46,10 @@ export class DataConnectService {
4746
}
4847

4948
/**
50-
* Returns the app associated with this `DataConnectService` instance.
51-
*
52-
* @returns The app associated with this `DataConnectService` instance.
53-
*/
49+
* Returns the app associated with this `DataConnectService` instance.
50+
*
51+
* @returns The app associated with this `DataConnectService` instance.
52+
*/
5453
get app(): App {
5554
return this.appInternal;
5655
}
@@ -64,24 +63,24 @@ export class DataConnect {
6463
private readonly client: DataConnectApiClient;
6564

6665
/**
67-
* @param connectorConfig - The connector configuration.
68-
* @param app - The app for this `DataConnect` service.
69-
* @constructor
70-
* @internal
71-
*/
66+
* @param connectorConfig - The connector configuration.
67+
* @param app - The app for this `DataConnect` service.
68+
* @constructor
69+
* @internal
70+
*/
7271
constructor(readonly connectorConfig: ConnectorConfig, readonly app: App) {
7372
this.client = new DataConnectApiClient(connectorConfig, app);
7473
}
7574

7675
/**
77-
* Execute an arbitrary GraphQL query or mutation
78-
*
79-
* @param query - The GraphQL query or mutation.
80-
* @param options - Optional {@link GraphqlOptions} when executing a GraphQL query or mutation.
81-
*
82-
* @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
83-
* @beta
84-
*/
76+
* Execute an arbitrary GraphQL query or mutation
77+
*
78+
* @param query - The GraphQL query or mutation.
79+
* @param options - Optional {@link GraphqlOptions} when executing a GraphQL query or mutation.
80+
*
81+
* @returns A promise that fulfills with a `ExecuteGraphqlResponse`.
82+
* @beta
83+
*/
8584
public executeGraphql<GraphqlResponse, Variables>(
8685
query: string,
8786
options?: GraphqlOptions<Variables>,
@@ -137,7 +136,6 @@ export class DataConnect {
137136

138137
/**
139138
* Insert a single row into the specified table, or update it if it already exists.
140-
* The specific behavior (insert or update) depends on the underlying database and schema configuration.
141139
*
142140
* @param tableName - The name of the table to upsert data into.
143141
* @param variables - The data object to upsert. The keys should correspond to the column names.
@@ -153,7 +151,6 @@ export class DataConnect {
153151

154152
/**
155153
* Insert multiple rows into the specified table, or update them if they already exist.
156-
* The specific behavior (insert or update) depends on the underlying database and schema configuration.
157154
*
158155
* @param tableName - The name of the table to upsert data into.
159156
* @param variables - An array of data objects to upsert. Each object's keys should correspond to the column names.

0 commit comments

Comments
 (0)