finish up Firebase implementation

This commit is contained in:
Maurits Lourens 2022-03-10 00:23:36 +01:00
parent 792021616c
commit b987dc345f
1 changed files with 40 additions and 16 deletions

View File

@ -57,8 +57,13 @@ module Firebase {
type: DatasourceFieldTypes.STRING, type: DatasourceFieldTypes.STRING,
required: true, required: true,
}, },
filterField: {
displayName: "Filter field",
type: DatasourceFieldTypes.STRING,
required: false,
},
filter: { filter: {
displayName: "Filter query", displayName: "Filter comparison",
type: DatasourceFieldTypes.LIST, type: DatasourceFieldTypes.LIST,
required: false, required: false,
data: { data: {
@ -77,8 +82,8 @@ module Firebase {
], ],
}, },
}, },
queryValue: { filterValue: {
displayName: "Query value", displayName: "Filter value",
type: DatasourceFieldTypes.STRING, type: DatasourceFieldTypes.STRING,
required: false, required: false,
}, },
@ -113,7 +118,12 @@ module Firebase {
async create(query: { json: object; extra: { [key: string]: string } }) { async create(query: { json: object; extra: { [key: string]: string } }) {
try { try {
return await this.db.collection(query.extra.collection).add(query.json) const documentReference = this.db
.collection(query.extra.collection)
.doc()
await documentReference.set({ ...query.json, id: documentReference.id })
const snapshot = await documentReference.get()
return snapshot.data()
} catch (err) { } catch (err) {
console.error("Error writing to Firestore", err) console.error("Error writing to Firestore", err)
throw err throw err
@ -124,12 +134,16 @@ module Firebase {
try { try {
let snapshot let snapshot
const collectionRef = this.db.collection(query.extra.collection) const collectionRef = this.db.collection(query.extra.collection)
if (query.extra.field && query.extra.opStr && query.extra.queryValue) { if (
query.extra.filterField &&
query.extra.filter &&
query.extra.filterValue
) {
snapshot = await collectionRef snapshot = await collectionRef
.where( .where(
query.extra.field, query.extra.filterField,
query.extra.opStr as WhereFilterOp, query.extra.filter as WhereFilterOp,
query.extra.value query.extra.filterValue
) )
.get() .get()
} else { } else {
@ -146,27 +160,37 @@ module Firebase {
} }
async update(query: { async update(query: {
id: string json: Record<string, any>
json: object
extra: { [key: string]: string } extra: { [key: string]: string }
}) { }) {
try { try {
return await this.db await this.db
.collection(query.extra.collection) .collection(query.extra.collection)
.doc(query.id) .doc(query.json.id)
.update(query.json) .update(query.json)
return (
await this.db
.collection(query.extra.collection)
.doc(query.json.id)
.get()
).data()
} catch (err) { } catch (err) {
console.error("Error writing to mongodb", err) console.error("Error writing to firebase", err)
throw err throw err
} }
} }
async delete(query: { id: string; extra: { [key: string]: string } }) { async delete(query: {
json: { id: string }
extra: { [key: string]: string }
}) {
try { try {
return await this.db await this.db
.collection(query.extra.collection) .collection(query.extra.collection)
.doc(query.id) .doc(query.json.id)
.delete() .delete()
return true
} catch (err) { } catch (err) {
console.error("Error writing to mongodb", err) console.error("Error writing to mongodb", err)
throw err throw err