@@ -25,36 +25,6 @@ import * as utils from '../utils/index';
2525import * as validator from '../utils/validator' ;
2626import { 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-
5828const 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 ) ;
0 commit comments