[question] how to initialize db = getFirestore(...)?

#18 · closed · 3 comments

View on GitHub ↗

fredguth

How do you handle loading data server side and/versus handling with Firestore db in the browser? Server side... I guess I will have to initialize the app within admin.ts and import it in +page.ts. But as long as I am in the "browser" how to I handle queries locally? Here is my "broken" code, btw: https://github.com/CaptainCodeman/sveltekit-example/compare/master...fredguth:sveltekit-example:master

Comments

CaptainCodeman

I have some articles that focus on the client-side querying & lazy-loading on my blog. Just like with auth, I usually wrap things in a store as the subscription mechanism aligns well with the Firestore subscription: https://www.captaincodeman.com/lazy-loading-and-querying-firestore-with-sveltekit

fredguth

1) I understand from your post that you use Firebase only in the client, not in the server. Nothin server side, right? 2) why not generic `collection` and `document` stores? See example https://github.com/fredguth/sveltekit-example/blob/master/src/lib/stores/fireStores.ts

CaptainCodeman

> I understand from your post that you use Firebase only in the client, not in the server. Nothin server side, right? I use either or both, but generally it's easier to explain something specific if you don't try to include everything. Examples are meant to demonstrate some technique, not be a fully comprehensive blueprint. > why not generic collection and document stores? See example https://github.com/fredguth/sveltekit-example/blob/master/src/lib/stores/fireStores.ts Use whatever approach makes sense for you. I have also used wrapper / decorator stores to add things like pagination, but so much depends on the app your building and the structure of the data. Unless you're trying to create a generic svelte+firebase library, it's usually diminishing returns and added complexity trying to make everything re-usable vs just implementing what you need for each collection as you need it. If there is duplication, extract those pieces out if they are repeated enough times and big enough to warrant it. Readability and maintainability is an important feature to keep in mind.