Skip to content

Instantly share code, notes, and snippets.

@PamornT
Created December 11, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PamornT/7c2ef1bb07a73097899cf424e94e0818 to your computer and use it in GitHub Desktop.
Save PamornT/7c2ef1bb07a73097899cf424e94e0818 to your computer and use it in GitHub Desktop.
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