Created
June 2, 2022 11:08
-
-
Save Crashcodebug/9178133c9e5417a7311f7ab8da29e6d2 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
// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers. | |
const functions = require('firebase-functions'); | |
// Cloud Firestore: Node.js Client | |
const { Firestore } = require("@google-cloud/firestore"); | |
// The Firebase Admin SDK to access Firestore. | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
const db = admin.firestore(); | |
// Firebase USA access account | |
const adminUSA = new Firestore({projectId:"firestoreusa"}); | |
// onCreate Triggered when a document is written to for the first time. | |
exports.createUser = functions.region('europe-west1').firestore | |
.document('bridges/{id}') | |
.onCreate(async(querysnap, context) => { | |
for (const snap of querysnap.get()) { | |
console.log('onCreate', snap.ref().path, snap.data()); | |
const values = snap.data(); | |
await adminUSA.doc(snap.ref().path).set(values); | |
}; | |
}) | |
// onUpdate Triggered when a document already exists and has any value changed. // | |
exports.upUser = functions.region('europe-west1').firestore | |
.document('bridges/{id}') | |
.onUpdate(async({ before, after }, context) => { | |
for (const snap of after.get()) { | |
adminUSA.doc(snap.ref().path).set(snap.data()); | |
} | |
}) | |
// onDelete Triggered when a document is deleted. | |
exports.deleteUser = functions.region('europe-west1').firestore | |
.document('bridges/{id}') | |
.onDelete(async(snap, context) => { | |
console.log('onDelete', snap.ref()); | |
await adminUSA.doc(snap.ref()).delete(); | |
}) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.id != oldValues.id){ | |
// const change = await adminUSA.collection('bridges').where('id', '==', oldValues.id).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('bridges').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.allChanges2 = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.firmware != oldValues.firmware){ | |
// const change = await adminUSA.collection('bridges').where('firmware', '==', oldValues.firmware).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('bridges').doc(doc.id).update({firmware:newValues.firmware})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.allChanges3 = functions.region('europe-west1').firestore | |
// .document('tokens/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.id != oldValues.id){ | |
// const change = await adminUSA.collection('tokens').where('id', '==', oldValues.id).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('tokens').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges4 = functions.region('europe-west1').firestore | |
// .document('tokens/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.meta_data != oldValues.meta_data){ | |
// const change = await adminUSA.collection('tokens').where('meta_data', '==', oldValues.meta_data).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('tokens').doc(doc.id).update({meta_data:newValues.meta_data})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges5 = functions.region('europe-west1').firestore | |
// .document('users/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.id != oldValues.id){ | |
// const change = await adminUSA.collection('users').where('id', '==', oldValues.id).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('users').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges2 = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.firmware != oldValues.firmware){ | |
// const change = await adminUSA.collection('bridges').where('firmware', '==', oldValues.firmware).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('bridges').doc(doc.id).update({firmware:newValues.firmware})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.allChanges3 = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.firmware != oldValues.firmware){ | |
// const change = await adminUSA.collection('firmware').where('firmware', '==', oldValues.firmware).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('firmware').doc(doc.firmware).update({id:newValues.firmware})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.allChanges4 = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.meta_data != oldValues.meta_data){ | |
// const change = await adminUSA.collection('meta_data').where('meta_data', '==', oldValues.meta_data).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('meta_data').doc(doc.meta_data).update({meta_data:newValues.meta_data})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.upUser = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onUpdate(async(snap, context) => { | |
// const newValues = snap.after.data(); | |
// const preveosValues = snap.before.data(); | |
// if(newValues.context.params.id != preveosValues.){ | |
// const snapshot = await adminUSA.collection('test').where('id', '==', preveosValues.id).get(); | |
// let updatePromises = []; | |
// snapshot.forEach(doc =>{ | |
// updatePromises.push(adminUSA.collection('test').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// exports.upUser = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onUpdate(async(snap, context) => { | |
// const newValues = snap.after.data(); | |
// const preveosValues = snap.before.data(); | |
// if(newValues.id != preveosValues.id){ | |
// const snapshot = await adminUSA.collection('test').where('id', '==', preveosValues.id).get(); | |
// let updatePromises = []; | |
// snapshot.forEach(doc =>{ | |
// updatePromises.push(adminUSA.collection('test').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// await Promise.all(updatePromises) | |
// } | |
// }) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.id != oldValues.id){ | |
// const change = await adminUSA('bridges').where('id', '==', oldValues.id).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA('bridges').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// } | |
// }) | |
// // onWrite Triggered when onCreate, onUpdate or onDelete is triggered. | |
// exports.allChanges = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onWrite(async(change, context) => { | |
// const newValues = change.after.data() | |
// const oldValues = change.before.data() | |
// if (newValues.id != oldValues.id){ | |
// const change = await db.collection('test').where('id', '==', oldValues.id).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(db.collection('test').doc(doc.id).update({id:newValues.id})) | |
// }) | |
// } | |
// }) | |
// // onDelete Triggered when a document with data is deleted. | |
// exports.deleteUser = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onDelete(async(snap, context) => { | |
// const deletedPost = snap.data(); | |
// }) | |
// // onUpdate Triggered when a document already exists and has any value changed. | |
// exports.updateUser = functions.region('europe-west1').firestore | |
// .document('bridge/{id}') | |
// .onUpdate(async(snap, context) => { | |
// const newValues = snap.after.data(); | |
// const oldValues = snap.before.data(); | |
// if (newValues != oldValues){ | |
// const change = await db.collection('app_name').where('app_name', '==', oldValues.app_name).get(); | |
// let updatePromises = [] | |
// change.forEach(doc => { | |
// updatePromises.push(adminUSA.collection('app_name').doc(doc.app_name).update({app_name:newValues.app_name})) | |
// }); | |
// await Promise.all(updatePromises); | |
// } | |
// }) | |
// // onCreate Triggered when a document is written to for the first time. | |
// exports.createUser = functions.region('europe-west1').firestore | |
// .document('bridges/{id}') | |
// .onCreate(async(snap, context) => { | |
// const values = snap.data(); | |
// //Send Created user | |
// await db.collection('logging').add({description:`Email was sent: ${values.username}`}); | |
// await adminUSA.collection('logging').add({description:`Email was sent: ${values.username}`}); | |
// }) | |
// const functions = require("firebase-functions"); | |
// const { Firestore } = require("@google-cloud/firestore"); | |
// // Create a new client | |
// const firestore = new Firestore({projectId:"firestoreusa"}); | |
// exports.createUser2 = functions.firestore | |
// .region('europe-west1') | |
// .document('bridges') | |
// .onCreate((snap, context) => { | |
// const newValue = snap.data(); | |
// // access a particular field as you would any JS property | |
// const name = newValue.name; | |
// // perform desired operations ... | |
// }); | |
// // Create and Deploy Your First Cloud Functions | |
// // https://firebase.google.com/docs/functions/write-firebase-functions | |
// exports.helloWorld = functions.region('europe-west1').https.onRequest((request, response) => { | |
// functions.logger.info("Hello logs!", {structuredData: true}); | |
// response.send("Hello from Firebase!"); | |
// }); | |
// // sdasdasdsadd | |
// exports.createUser = functions.firestore.document('bridges/{id}').onCreate((snap, context) => { | |
// console.log(snapshot.data()) | |
// return Promise.resolve() | |
// }); | |
// async function quickstart() { | |
// // Obtain a document reference. | |
// const document = firestore.doc('posts/intro-to-firestore'); | |
// // Enter new data into the document. | |
// await document.set({ | |
// title: 'Welcome to Firestore', | |
// body: 'Hello World', | |
// }); | |
// console.log('Entered new data into the document'); | |
// // Update an existing document. | |
// await document.update({ | |
// body: 'My first Firestore app', | |
// }); | |
// console.log('Updated an existing document'); | |
// // Read the document. | |
// const doc = await document.get(); | |
// console.log('Read the document'); | |
// // Delete the document. | |
// await document.delete(); | |
// console.log('Deleted the document'); | |
// } | |
// quickstart(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment