Last active
September 7, 2019 20:29
-
-
Save ademilter/dd04716fc5749060af99519da99474da to your computer and use it in GitHub Desktop.
Firestore #firebase
This file contains hidden or 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
var citiesRef = db.collection("cities"); | |
var query = citiesRef.where("capital", "==", true); | |
citiesRef.where("state", "==", "CA") | |
citiesRef.where("population", "<", 100000) | |
citiesRef.where("name", ">=", "San Francisco") | |
citiesRef | |
.where("state", "==", "CA") | |
.where("population", "<", 1000000) |
This file contains hidden or 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
// NOT: | |
// * belirli bir döküman için "set" kullanılır. yani .doc().set(...) | |
// * onun haricinde her insert collection().add(...) ile yapılır | |
////////////////////////////////////////////// | |
// create or overwrite | |
////////////////////////////////////////////// | |
db.collection("cities").doc("LA").set({ | |
name: "Los Angeles", | |
state: "CA", | |
country: "USA" | |
}) | |
.then() | |
.catch() | |
/* | |
var cityRef = db.collection('cities').doc('BJ'); | |
var setWithMerge = cityRef.set({ | |
capital: true | |
}, { merge: true }); | |
*/ | |
////////////////////////////////////////////// | |
// create (auto-generate ID) | |
////////////////////////////////////////////// | |
db.collection("cities").add({ | |
name: "Tokyo", | |
country: "Japan" | |
}) | |
.then() | |
.catch() |
This file contains hidden or 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
citiesRef.orderBy("population").startAt(1000000) // startAfter | |
citiesRef.orderBy("population").endAt(1000000) // endBefore | |
////// | |
var citiesRef = db.collection("cities"); | |
return citiesRef.doc("SF").get().then(function(doc) { | |
// Get all cities with a population bigger than San Francisco | |
var biggerThanSf = citiesRef | |
.orderBy("population") | |
.startAt(doc); | |
}); | |
/////// | |
var first = db.collection("cities") | |
.orderBy("population") | |
.limit(25); | |
return first.get().then(function (documentSnapshots) { | |
// Get the last visible document | |
var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1]; | |
console.log("last", lastVisible); | |
// Construct a new query starting at this document, | |
// get the next 25 cities. | |
var next = db.collection("cities") | |
.orderBy("population") | |
.startAfter(lastVisible) | |
.limit(25); | |
}); |
This file contains hidden or 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
////////////////////////////////////////////// | |
// Delete Data | |
////////////////////////////////////////////// | |
db.collection("cities").doc("DC").delete().then(function() { | |
console.log("Document successfully deleted!"); | |
}).catch(function(error) { | |
console.error("Error removing document: ", error); | |
}); | |
// Delete fields | |
var cityRef = db.collection('cities').doc('BJ'); | |
var removeCapital = cityRef.update({ | |
capital: firebase.firestore.FieldValue.delete() | |
}); |
This file contains hidden or 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
////////////////////////////////////////////// | |
// get user list | |
////////////////////////////////////////////// | |
var docRef = db.collection("cities").doc("SF"); | |
docRef.get().then(function(doc) { | |
if (doc.exists) { | |
console.log("Document data:", doc.data()); | |
} else { | |
// doc.data() will be undefined in this case | |
console.log("No such document!"); | |
} | |
}).catch(function(error) { | |
console.log("Error getting document:", error); | |
}); | |
// cities => where | |
db.collection("cities") | |
.where("capital", "==", true) | |
.get() | |
.then(function(querySnapshot) { | |
querySnapshot.forEach(function(doc) { | |
// doc.data() is never undefined for query doc snapshots | |
console.log(doc.id, " => ", doc.data()); | |
}) | |
}) | |
.catch(function(error) { | |
console.log("Error getting documents: ", error); | |
}) | |
// | |
// all user | |
db.collection("users").get().then((querySnapshot) => { | |
querySnapshot.forEach((doc) => { | |
console.log(`${doc.id} => ${doc.data()}`) | |
}) | |
}) |
This file contains hidden or 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
const firebase = require("firebase"); | |
require("firebase/firestore"); | |
var db = firebase.firestore(); | |
// user list | |
var usersCollectionRef = db.collection('users') | |
// user => alovelace | |
var alovelaceDocumentRef = db.collection('users').doc('alovelace') | |
// user shortcut | |
var alovelaceDocumentRef = db.doc('users/alovelace') | |
////////////////////////////////////////////// | |
// Data types | |
////////////////////////////////////////////// | |
var docData = { | |
stringExample: "Hello world!", | |
booleanExample: true, | |
numberExample: 3.14159265, | |
dateExample: new Date("December 10, 1815"), | |
arrayExample: [5, true, "hello"], | |
nullExample: null, | |
objectExample: { | |
a: 5, | |
b: { | |
nested: "foo" | |
} | |
} | |
} |
This file contains hidden or 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
citiesRef.orderBy("name").limit(3) | |
citiesRef.orderBy("name", "desc").limit(3) | |
citiesRef.orderBy("state").orderBy("population", "desc") | |
citiesRef.where("population", ">", 100000).orderBy("population").limit(2) |
This file contains hidden or 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
// Get Realtime Updates | |
db.collection("cities").doc("SF") | |
.onSnapshot(function(doc) { | |
console.log("Current data: ", doc.data()); | |
}); | |
db.collection("cities") | |
.where("state", "==", "CA") | |
.onSnapshot(function(querySnapshot) { | |
var cities = []; | |
querySnapshot.forEach(function(doc) { | |
cities.push(doc.data().name); | |
}); | |
console.log("Current cities in CA: ", cities.join(", ")); | |
}) | |
db.collection("cities") | |
.where("state", "==", "CA") | |
.onSnapshot(function(snapshot) { | |
snapshot.docChanges.forEach(function(change) { | |
if (change.type === "added") { | |
console.log("New city: ", change.doc.data()); | |
} | |
if (change.type === "modified") { | |
console.log("Modified city: ", change.doc.data()); | |
} | |
if (change.type === "removed") { | |
console.log("Removed city: ", change.doc.data()); | |
} | |
}); | |
}); | |
// Detach a listener | |
var unsubscribe = db.collection("cities") | |
.onSnapshot(function () {}); | |
unsubscribe(); | |
//Handle listen errors | |
db.collection("cities") | |
.onSnapshot(function(snapshot) { | |
//... | |
}, function(error) { | |
//... | |
}); |
This file contains hidden or 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
////////////////////////////////////////////// | |
// Updating data with transactions | |
////////////////////////////////////////////// | |
var sfDocRef = db.collection("cities").doc("SF"); | |
// Uncomment to initialize the doc | |
// sfDocRef.set({ population: 0 }); | |
db.runTransaction(function(transaction) { | |
return transaction.get(sfDocRef).then(function(sfDoc) { | |
if (!sfDoc.exists) { | |
throw "Document does not exist!"; | |
} | |
var newPopulation = sfDoc.data().population + 1; | |
if (newPopulation <= 1000000) { | |
transaction.update(sfDocRef, { population: newPopulation }); | |
return newPopulation; | |
} else { | |
return Promise.reject("Sorry! Population is too big."); | |
} | |
}); | |
}) | |
.then(function(newPopulation) { | |
console.log("Population increased to ", newPopulation); | |
}) | |
.catch(function(err) { | |
// This will be an "population is too big" error. | |
console.error(err); | |
}); |
This file contains hidden or 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
////////////////////////////////////////////// | |
// Update a document | |
////////////////////////////////////////////// | |
var washingtonRef = db.collection("cities").doc("DC"); | |
washingtonRef.update({ | |
capital: true | |
}) | |
.then() | |
.catch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment