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
this.db = new Firestore({ if (config.serviceAccount) {
projectId: config.projectId, const serviceAccount = JSON.parse(config.serviceAccount)
credential: { this.db = new Firestore({
clientEmail: config.email, projectId: serviceAccount.project_id,
privateKey: config.privateKey, 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 } }) { 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) {
.get() snapshot = await collectionRef
.where(
query.extra.field,
query.extra.opStr as WhereFilterOp,
query.extra.value
)
.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()))