CyCraft / CyCraft/magnetar

[Bug] undefined document fetch/stream get the whole collection

Open
#79 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
46
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Hi,

I noticed an odd behavior from magnetar in a edge case. It might not be a bug but then I think it could be highlighted in the documentation 🙂

## Use case

I have a method to stream a document that looks like this :
````typescript
const gamesModule = magnetar.collection(GAMES_COLLECTION, {
modifyPayloadOn: { insert: gamesDefaults },
modifyReadResponseOn: { added: gamesDefaults },
})
export const getGame = (id: string) => {
const gameModule = gamesModule.doc(id);
gameModule.stream().catch((error) => {
console.error(`Game ${id} stream was closed due to an error`, error);
});
return gameModule.data;
}
````

## Expected result
Magnetar streams the document with the id `id`. If `id` is `undefined`, nothing happens.

## Actual result
If, for some reasons, `id` is `undefined` (e.g. when the id is a reactive variable and is not yet set), magnetar streams the whole collection. This behavior led me to massive amount of document streams/fetches on my firestore DB (I reached the 50k free quota in 2h).

## Workaround
My current solution was to add a check on the value of the `id` parameter.
````typescript
export const getGame = (id: string) => {
if(!id) return undefined;
const gameModule = gamesModule.doc(id);
gameModule.stream().catch((error) => {
console.error(`Game ${id} stream was closed due to an error`, error);
})
return gameModule.data;
}
````

## Suggestion

I think it would be nice if magnetar could handle undefined document ids natively. Else, it would be good to document this behavior.

Again, I am very grateful for the work you have done here! Thanks to magnetar, I was able to write a soon-to-be-published opensource full-stack pwa in ~3 weeks. This app is going to help my non-profit organisation to manage the games and the players of a real-life game that gathers +1200 boyscouts in Belgium next weekend 😉

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.