Stop Using Firebase for Everything: Why I Switched to PostgreSQL (Supabase)
Stop Using Firebase for Everything: Why I Switched to PostgreSQL (Supabase)
别再什么都用 Firebase 了:为什么我转向了 PostgreSQL (Supabase)
1. The Hook
We’ve all been there. You start a new project, you want to move fast, so you plug in Firebase. It feels like magic at first. But what happens 6 months down the line when your app scales, your data becomes relational, and suddenly you are paying a premium just to run complex queries? You hit the NoSQL wall.
1. 引子
我们都经历过这种情况:当你开启一个新项目,想要快速迭代,于是直接接入了 Firebase。起初,它感觉就像魔法一样。但六个月后,当你的应用规模扩大、数据变得复杂且互相关联时,你会发现为了运行复杂的查询,你不得不支付高昂的费用。这时,你就撞上了 NoSQL 的“南墙”。
2. The Problem
The biggest trap modern developers fall into is treating a NoSQL document store (like Firebase/Firestore) as the ultimate solution for every single app. Here is where the pain starts:
- Vendor Lock-in: Moving away from Firebase once your app is live is a nightmare.
- Complex Queries: Need to filter data based on multiple nested conditions? Good luck writing that without fetching half your database.
- Pricing Surprises: It’s free until you accidentally run a bad query in an infinite loop and wake up to a massive bill.
2. 问题所在
现代开发者最容易掉进的陷阱,就是把 NoSQL 文档存储(如 Firebase/Firestore)当作所有应用的终极解决方案。痛苦往往从这里开始:
- 厂商锁定 (Vendor Lock-in): 一旦应用上线,想要迁移出 Firebase 简直是一场噩梦。
- 复杂查询: 需要基于多个嵌套条件筛选数据?如果不把半个数据库的数据拉取到本地,你很难写出这样的查询。
- 价格陷阱: 它起初是免费的,直到你因为一个死循环的错误查询,一觉醒来发现账单金额惊人。
3. The ‘StackByUjjwal’ Solution
It’s time to respect Relational Databases again. If you love the ease of Firebase but need the power of PostgreSQL, the industry is rapidly shifting towards tools like Supabase (the open-source Firebase alternative). Here is how beautifully simple it is to fetch relational data using Supabase in your JavaScript app, without losing the power of SQL:
3. “StackByUjjwal” 的解决方案
是时候重新重视关系型数据库了。如果你喜欢 Firebase 的易用性,但又需要 PostgreSQL 的强大功能,那么行业正在迅速转向像 Supabase(开源的 Firebase 替代品)这样的工具。以下是在 JavaScript 应用中使用 Supabase 获取关系型数据是多么简洁,同时又保留了 SQL 的强大能力:
// 📁 db-service.js
import { createClient } from '@supabase/supabase-js'
// Initialize the PostgreSQL connection via Supabase
// 通过 Supabase 初始化 PostgreSQL 连接
const supabaseUrl = 'https://your-project-id.supabase.co'
const supabaseKey = 'YOUR_ANON_KEY'
const supabase = createClient(supabaseUrl, supabaseKey)
// Fetching Relational Data
// 获取关系型数据
export const getActiveUsersWithProfiles = async () => {
try {
const { data, error } = await supabase
.from('users')
.select(`
id,
username,
profiles (avatar_url, bio)
`)
.eq('status', 'active');
if (error) throw error;
return data;
} catch (error) {
console.error("Database Query Failed: ", error.message);
}
}
4. The Link Dump
Written by Ujjwal Sharma, Founder of @stackbyujjwal. Passionate about Full-Stack Web Development, building scalable architectures, and sharing code that makes sense. Let’s connect and build something awesome:
🔗 Linktree: https://linktr.ee/stackbyujjwal 🐙 GitHub: https://github.com/stackbyujjwal 📺 YouTube: https://youtube.com/@stackbyujjwal?si=mRRyKaWoZ-xbXQWp
4. 相关链接
本文作者:Ujjwal Sharma,@stackbyujjwal 创始人。热衷于全栈 Web 开发、构建可扩展架构以及分享有价值的代码。欢迎与我联系,一起构建伟大的产品:
🔗 Linktree: https://linktr.ee/stackbyujjwal 🐙 GitHub: https://github.com/stackbyujjwal 📺 YouTube: https://youtube.com/@stackbyujjwal?si=mRRyKaWoZ-xbXQWp