-
Notifications
You must be signed in to change notification settings - Fork 517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for Cloud Firestore #268
Open
shoNagai
wants to merge
2
commits into
graphql:main
Choose a base branch
from
shoNagai:example/cloud-firestore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Using DataLoader with Cloud Firestore | ||
|
||
Cloud Firestore is a "NoSQL" document-oriented database which supports [in operator](https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any), | ||
making it can be used with DataLoader. | ||
|
||
Here we build an example Cloud Firestore DataLoader using [firebase-admin](https://firebase.google.com/docs/admin/setup?hl=en). | ||
|
||
```js | ||
const admin = require("firebase-admin"); | ||
|
||
admin.initializeApp({ | ||
// You need to Initialize the SDK | ||
}); | ||
|
||
const datastoreLoader = new DataLoader( | ||
async (keys) => { | ||
const snapshot = await admin | ||
.firestore() | ||
.collection(`users`) | ||
.where(admin.firestore.FieldPath.documentId(), `in`, keys) | ||
.get(); | ||
return snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id })); | ||
}, | ||
{ | ||
// The in clause of Cloud Firestore is support up to 10 comparison values | ||
maxBatchSize: 10, | ||
} | ||
); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does firestore guarantee the results will be in the same order as the
keys
input? If you're not sure this is the case, I think it might be safer if this line was something like:(Obviously test this works before adopting it; I've never used Firestore.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjie
Thank you for your review.
As you said, firestore is retrieved in ascending order by document ID by default, so I think sorting by input key is necessary.
I have added the correction and comments.
I have also implemented and confirmed the test in my repository.