Created
December 11, 2020 18:33
-
-
Save PamornT/7c2ef1bb07a73097899cf424e94e0818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as functions from 'firebase-functions'; | |
import * as admin from 'firebase-admin'; | |
admin.initializeApp(); | |
const db = admin.firestore(); | |
// Start writing Firebase Functions | |
// https://firebase.google.com/docs/functions/typescript | |
export const helloWorld = functions.https.onRequest((request, response) => { | |
functions.logger.info("Hello logs!", {structuredData: true}); | |
response.send("Hello from Firebase!"); | |
}); | |
export const insertData = functions.https.onRequest(async(request, response) => { | |
const params = { | |
id: '1', | |
name: 'AAAA', | |
position: 'Admin', | |
} | |
await db.collection('user').add(params) | |
response.send("Insert Data Complete!!"); | |
}); | |
export const getData = functions.https.onRequest(async(request, response) => { | |
let allUser: any = [] | |
await db.collection("user").get().then((data) => { | |
if (!data.empty) { | |
allUser = data.docs.map(doc => { | |
return doc.data() | |
}) | |
} | |
}) | |
response.send(allUser); | |
}); | |
export const updateData = functions.https.onRequest(async(request, response) => { | |
await db.collection("user").where('id', '==', '1').get().then((data) => { | |
if (!data.empty) { | |
data.docs.map( async(doc) => { | |
const id = doc.id | |
await db.collection("user").doc(id).update({'name':'BBBB'}) | |
}) | |
} | |
}) | |
response.send("Update Data Complete!!"); | |
}); | |
export const deleteData = functions.https.onRequest(async(request, response) => { | |
await db.collection("user").where('id', '==', '1').get().then((data) => { | |
if (!data.empty) { | |
data.docs.map( async(doc) => { | |
const id = doc.id | |
await db.collection("user").doc(id).delete() | |
}) | |
} | |
}) | |
response.send("Delete Data Complete!!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment