5 ms·
According to the article, this is how you load messages and their users from a DB via Firebase: const querySnapshot = await getDocs(collection(db, "message
by TekMol 4y ago
According to the article, this is how you load messages and their users from a DB via Firebase:
const querySnapshot = await getDocs(collection(db, "messages"));
const userSnapshots = await Promise.all(
querySnapshot.docs().map(async messageSnapshot => {
return await getDoc(docSnapshot.data().creator);
})
);
Phew!
Thanks, but no. Never. I will keep doing it server side:
$messages = DB::select(
'SELECT * FROM messages JOIN users ON users.id=messages.user_id'
);
It is amazing with how much cruft developers are willing to deal with these days. And how much CPU cycles get burned for nothing, as the Firebase example fires one query per message to get the user. This would be bad enough on the server. But with the Firebase example, it would also create a client-server http roundtrip for each message. Mind-boggling.
- duxup 4y agoYou can in fact create a server side query in Firebase if you wanted. The fact that Firebase isn't using SQL is obvious and comes with up and downsides.
- okhobb 4y agoThis is a strawman and no one would actually design a messaging app using a NoSQL db like that. Typically one would employ a strategy to fan-out data such that the message document has everything needed to render a message in the UI.
- danielvaughn 4y agoTechnically that's how you load data from Firestore specifically, but yes, that's how it's done. I started working with a team who uses Firestore, and let me tell you, I've never hated a database so much. No disrespect to anyone from the Google team, but I cannot fathom why they made the decisions they made. 1) You are severely limited in how you can query. The list of limitations is too long to recount here, but querying is nearly worthless. 2) The database design strongly pushes you towards nesting collections, making side effects cleanup a disaster, especially as a database grows in complexity. 3) You cannot sort on a field without creating an index first. I get why creating an index is a good idea, but I can't even write a simple analytics script without indexing the fields first. 4) It gears itself towards frontend developers who don't know how to write a backend, and encourages bad practices for them. One example: Firebase lets you manually edit production data very easily from within their dashboard. Like it's treated almost like it's a CSV. 5) The recommended development approach is to directly query the DB from the frontend, with no server in between. This means any data security has to be implemented in a separate DB rules document. The syntax and structure of this doc is super limited and often results in a giant, unmaintainable file. 6) Firestore cannot count. As in, you literally cannot query the number of records in a collection. If you want that value, you have to store it as a separate field in the collection, and then update that value each time you add and remove a doc. MADNESS. I could go on, and on, and on.
- RayVR 4y agoGreat list! I think we've had similar frustrations with firestore. #3 is not really correct. You can sort on any single field but if you have multiple fields, then yes, you must create an index. #4 I partially agree. The web dashboard makes things I don't want to do (accidentally edit or delete a field/document) dangerously easy, and things I do want to do (copy the contents of a document, save the results of a query, copy the text of a field) exceedingly difficult. The truth is that firestore is geared toward people who want an easy way to get near real-time data synchronization. It really sacrifices almost everything else. The number one most annoying thing to do with firestore is work with their security rules.
- mquander 4y agoI also just joined a team using Firestore and I feel exactly this same way. It absolutely blew me away how bad Firestore is for any kind of conventional CRUD app use cases. I have never worked with a database less fit for purpose, and I worked with MongoDB in 2012.
- danielvaughn 4y agoSame. I find myself wishing we were on Mongo, it's that bad.
- randomdata 4y agoYup. One of our products was built on Firestore and I expect 90% of my time on that project is spent trying to turn Firestore into a more featureful product to meet the application's requirements. You can push it pretty far if you have the developer resources, but it often seems ludicrous that someone is willing to spend that much to build what other database solutions provide out of the box for free.
- agrippanux 4y agoFirestore makes it easy to get productive quickly and then drive straight into quicksand.
- habosa 4y agoA few points here: 1) That’s a JOIN and Firestore is not unique among NoSQL databases for being bad at it. 2) That’s the code you’d use in a web browser to load Firestore data. That code also handles all of the API and Authentication code you’d normally put in front of another DB. It’s serverless. So that is a fully functional snippet, your SQL example needs an API layer and frontend deserialization code. 3) It does not make a new HTTP connection per request. It uses a long-lived gRPC connection.
- TekMol 4y ago2: The code does not handle authentification. The code literally just says "Give me this, give me that...". 3: It is still a "a client-server http roundtrip" for every query. It does not open and close the http connection every time. But it sends a "GET / HTTP ..." request for every query. With hostname, accepted formats, encodings etc.
- shhsshs 4y agoThe code does not handle authentication because it does not need to - all data access and auth rules happen within Firestore/Firebase. If a client requests data they do not have access to, they won’t get it.
- stillkicking 4y ago...and you will show your users stale data, which is a non-starter for a chat app. You will then need to implement some custom push/invalidate mechanism, based on a custom interpretation of the DB event log. The entire point of the Fire* style of database is that these trade-offs are not worth it in the long run, that few development teams have the skill and time to implement this themselves, and that databases can and should solve this for you. I have little love for Cloud Firestore, it's a trash fire riddled with poor decisions, but if you don't even understand what the problem is with that SQL query, you don't understand the expectations users have nowadays of front-end applications.
- folkrav 4y agoYou're on HN, where we still long for the "documents with links" days of yore.
- manigandham 4y agoWhy would a SQL query show stale data? Inserts and selects are fine, the only thing needed is signaling. You can use Firestore for that, or just a separate thin layer on a much more capable database backend. > "that few development teams have the skill" This is the fundamental problem. There's no magic answer to a lack of skill.
- mhoad 4y agoBecause the way that you will ultimately have to scale something like PostgreSQL for example is going to end up with eventual consistency which Firestore doesn’t have to deal with.
- manigandham 4y agoThere's no reason for eventual consistency. Firestore is backed by BigTable and Spanner (both strongly consistent just like Postgresql). There's nothing magic about this - it just provide a pub/sub channel on the watched keys and automatically does a SELECT loop in the background once an INSERT triggers that key. You can do the same thing on a simple by selecting by a `chatid` in a table that'll get the latest messages/inserts. Again, the only thing needed is the pub/sub layer, not an entirely different database.
- moomoo11 4y agoMost apps I build have a rdbms main data store but I use firestore for things like real time interactions or more recently real time commenting. It would be a bit more time consuming and challenging to set this up across iOS, android, and web. A lot more time would be spent figuring out the system to make this work well and without issues at scale. On the other hand… It takes like 20 min to set up a real time feature with firestore that works across all platforms. It scales well and works great for what it is. If people try to use it in place of a rdbms or complex use cases, they’re going to have a bad time. But for those who can utilize their tool belt effectively, it’s awesome. I love firebase products.
- lucideer 4y agoI mean if it's brevity you're after, the first example can be rewritten in a terser style: const userSnapshots = await Promise.all( getDocs(collection(db, "messages")).then( docs => docs().map(snapshot => getDoc(snapshot).data().creator) ) ) Ultimately you're kinda just comparing preference for SQL syntax over JS in that case. The JS example is also async (always adds visual complexity to code calls), whereas the PHP example is blocking. That SQL query doesn't look particularly efficient either... There's plenty of good solid criticisms of Firebase & NoSQL but these aren't it. e.g. your HTTP roundtrip argument has been debunked by sibling commenters, but client-side DB calls is still riddled with adjacent problems & ultimately adding more complexity to the server-side avoid the need for client-side DB access is often worthwhile.
- rowanG077 4y agoI definitely would recomend keeping it server side if you don't need any other firebase features. You don't use firebase as a normal database. You use it for its realtime featureset.
- deleted 4y ago[deleted]