From 792021616cd05b5ce1fbbaf96d934af5b1a0f6b0 Mon Sep 17 00:00:00 2001 From: Maurits Lourens Date: Wed, 9 Mar 2022 17:46:25 +0100 Subject: [PATCH] fix connection to firebase using service account --- packages/server/src/integrations/firebase.ts | 54 ++++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/packages/server/src/integrations/firebase.ts b/packages/server/src/integrations/firebase.ts index 8bcf198b7d..00a8e227eb 100644 --- a/packages/server/src/integrations/firebase.ts +++ b/packages/server/src/integrations/firebase.ts @@ -11,6 +11,7 @@ module Firebase { email: string privateKey: string projectId: string + serviceAccount?: string } const SCHEMA: Integration = { @@ -31,6 +32,10 @@ module Firebase { type: DatasourceFieldTypes.STRING, required: true, }, + serviceAccount: { + type: DatasourceFieldTypes.JSON, + required: false, + }, }, query: { create: { @@ -86,13 +91,24 @@ module Firebase { constructor(config: FirebaseConfig) { this.config = config - this.db = new Firestore({ - projectId: config.projectId, - credential: { - clientEmail: config.email, - privateKey: config.privateKey, - }, - }) + if (config.serviceAccount) { + const serviceAccount = JSON.parse(config.serviceAccount) + this.db = new Firestore({ + projectId: serviceAccount.project_id, + credentials: { + client_email: serviceAccount.client_email, + private_key: serviceAccount.private_key, + }, + }) + } else { + this.db = new Firestore({ + projectId: config.projectId, + credentials: { + client_email: config.email, + private_key: config.privateKey, + }, + }) + } } async create(query: { json: object; extra: { [key: string]: string } }) { @@ -104,17 +120,21 @@ module Firebase { } } - async read(query: { - field: string - opStr: WhereFilterOp - value: any - extra: { [key: string]: string } - }) { + async read(query: { json: object; extra: { [key: string]: string } }) { try { - const snapshot = await this.db - .collection(query.extra.collection) - .where(query.field, query.opStr, query.value) - .get() + let snapshot + const collectionRef = this.db.collection(query.extra.collection) + if (query.extra.field && query.extra.opStr && query.extra.queryValue) { + snapshot = await collectionRef + .where( + query.extra.field, + query.extra.opStr as WhereFilterOp, + query.extra.value + ) + .get() + } else { + snapshot = await collectionRef.get() + } const result: any[] = [] snapshot.forEach(doc => result.push(doc.data()))