Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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