fix connection to firebase using service account

This commit is contained in:
Maurits Lourens 2022-03-09 17:46:25 +01:00
parent e9405a1802
commit bdb5b127dd
1 changed files with 37 additions and 17 deletions

View File

@ -11,6 +11,7 @@ module Firebase {
email: string email: string
privateKey: string privateKey: string
projectId: string projectId: string
serviceAccount?: string
} }
const SCHEMA: Integration = { const SCHEMA: Integration = {
@ -31,6 +32,10 @@ module Firebase {
type: DatasourceFieldTypes.STRING, type: DatasourceFieldTypes.STRING,
required: true, required: true,
}, },
serviceAccount: {
type: DatasourceFieldTypes.JSON,
required: false,
},
}, },
query: { query: {
create: { create: {
@ -86,13 +91,24 @@ module Firebase {
constructor(config: FirebaseConfig) { constructor(config: FirebaseConfig) {
this.config = config this.config = config
if (config.serviceAccount) {
const serviceAccount = JSON.parse(config.serviceAccount)
this.db = new Firestore({ this.db = new Firestore({
projectId: config.projectId, projectId: serviceAccount.project_id,
credential: { credentials: {
clientEmail: config.email, client_email: serviceAccount.client_email,
privateKey: config.privateKey, 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 } }) { async create(query: { json: object; extra: { [key: string]: string } }) {
@ -104,17 +120,21 @@ module Firebase {
} }
} }
async read(query: { async read(query: { json: object; extra: { [key: string]: string } }) {
field: string
opStr: WhereFilterOp
value: any
extra: { [key: string]: string }
}) {
try { try {
const snapshot = await this.db let snapshot
.collection(query.extra.collection) const collectionRef = this.db.collection(query.extra.collection)
.where(query.field, query.opStr, query.value) 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() .get()
} else {
snapshot = await collectionRef.get()
}
const result: any[] = [] const result: any[] = []
snapshot.forEach(doc => result.push(doc.data())) snapshot.forEach(doc => result.push(doc.data()))