Firestore Sweet
Cloud Firestore made super easy with sweet syntactic sugar
firestore-sweet
works with both client-sidefirebase
and server-sidefirebase-admin
.
Project Website / Documentation
https://warashibe.github.io/firestore-sweet/
Installation
yarn add firestore-sweet
Examples
firebase
client-side initialization : firebase; const db =
firebase-admin
server-side initialization : const serviceAccount = ; admin const db =
get
firestore-sweet
automatically knows whether the ref
is a collection
or a doc
based on the position.
It returns actual data instead of snapshot.
/* for comparisons with the original APIs const fs = firebase.firestore() */ // fs.collection("users").get()await db // fs.collection("users").doc("Bob").get()await db // fs.collection("users").doc("Bob").collection("comments").get()await db // fs.collection("users").doc("Bob").collection("comments").doc("no3").get()await db
where
orderBy
limit
firestore-sweet
knows whether the argument is for where
, orderBy
, or limit
based on the array length.
// fs.collection("users").where("age", "==", 20).get()await db // fs.collection("users").where("age", "==", 20).orderBy("age", "desc").get()await db // fs.collection("users").where("age", "==", 20).orderBy("age", "desc").limit(5).get()await db
startAt
startAfter
endAt
endBefore
// fs.collection("users").orderBy("age").startAt(20).get()await db
add
set
upsert
update
delete
// fs.collection("users").doc("Bob").set({name:"Bob", age: 30})await db // fs.collection("users").doc("Bob").set({name:"Bob", age: 30}, {merge: true})await db // fs.collection("users").doc("Bob").update({name:"Bob", age: 30})await db // fs.collection("users").doc("Bob").delete()await db
del
delete field : // fs.collection("users").doc("Bob").update({age: firebase.firestore.FieldValue.delete()})await db
inc(n)
increment field : // fs.collection("users").doc("Bob").update({age: firebase.firestore.FieldValue.increment(3)})await db
ts
serverTimestamp : // fs.collection("users").doc("Bob").update({date: firebase.firestore.FieldValue.serverTimestamp()})await db
union
arrayUnion : // fs.collection("users").doc("Bob").update({favorites: firebase.firestore.FieldValue.arrayUnion("tomato")})await db
remove
arrayRemove : // fs.collection("users").doc("Bob").update({favorites: firebase.firestore.FieldValue.arrayRemove("tomato")})await db
on
onSnapShot : const unsubscribe = db
drop
This is a unique method only seen in firestore-sweet
to delete everything in a collection.
Be careful using it since it's powerful and dangerous if misused.
await db
ref
Use ref
to simply get a native ref object from the Firestore SDK.
await db
Bulk write operations with query
With firestore sweet
, multiple write operations are possible with queries in one method.
This is not something possible with the Firestore APIs. Firestore batch has 500 operations at a time limit, but firestore sweet
automatically bypasses the limit by dividing the operations into chunks of 500 and parallelly executes those. It can execute 50,000 operations in a few seconds this way, but watch out for your bill.
add
andupsert
don't make sense with this operation.add
doesn't do anything andupsert
works the same asupdate
in this context.
await db
await db
await db
tx
runTransaction : await db
batch
batch : await db
Getting document ids and snapshots as return values
You can also get document id
and snapshot
with the actual data by adding K
or S
to the method names.
get
=>getK
getS
/tx
=>txK
txS
/on
=>onK
onS
// getK returns document id and dataconst users = await dbforconst id in users console // getS returns Object with snapshot, document id and dataforconst id ss data of await db const user = ssdata // same as data console // getR returns a raw snapshot which is the same behavior as the original firestore API but as an arrayawait db
Test
You need service-account credentials for a Firebase project at /test/.service-account.json
to run the tests.
Use a disposable project if you are to run the tests since the tests manipulate and delete actual data from your Firestore.
yarn run test