本指南將幫助您設定「獨立租借書店」系統的資料庫,包括 Supabase (PostgreSQL) 和 MongoDB。
- 訪問 Supabase
- 點擊 "Start your project" 或 "Sign up" 註冊帳號
- 登入後,點擊 "New Project" 建立新專案
- 填寫專案資訊:
- Name: 輸入專案名稱(如:book-rental-system)
- Database Password: 設定資料庫密碼(請妥善保存)
- Region: 選擇離您最近的區域
- Pricing Plan: 選擇免費計畫(Free tier)即可
- 點擊 "Create new project",等待專案建立完成(約 2 分鐘)
-
在專案 Dashboard 中,點擊左側選單的 Settings → API
-
記錄以下資訊:
- Project URL: 您的專案 URL
- anon public key: 公開 API 密鑰
- service_role key: 服務角色密鑰(請保密)
-
點擊左側選單的 Settings → Database
-
記錄以下資訊:
- Connection string: 資料庫連接字串
- Connection pooling: 連接池字串(可選)
您有兩種方式執行 schema:
- 在 Supabase Dashboard 中,點擊左側選單的 SQL Editor
- 點擊 "New query"
- 開啟檔案
database/relational/migrations/001_initial_schema.sql - 複製全部內容並貼上到 SQL Editor
- 點擊 "Run" 或按
Ctrl+Enter(Windows) /Cmd+Enter(Mac) 執行 - 確認看到 "Success. No rows returned" 訊息
-
安裝 Supabase CLI:
# macOS brew install supabase/tap/supabase # 或使用 npm npm install -g supabase
-
登入 Supabase:
supabase login
-
執行設定腳本:
./scripts/setup_relational.sh
-
按照提示輸入專案引用 ID(Project Ref)
- 在 SQL Editor 中開啟新查詢
- 開啟檔案
database/relational/seed.sql - 複製內容並執行
- 訪問 MongoDB Atlas
- 點擊 "Try Free" 註冊帳號
- 建立組織(Organization)和專案(Project)
- 建立免費叢集(Free Cluster):
- Cloud Provider: 選擇雲端服務提供商(AWS, Google Cloud, Azure)
- Region: 選擇離您最近的區域
- Cluster Tier: 選擇 M0 (Free) 免費層
- Cluster Name: 輸入叢集名稱
- 點擊 "Create Cluster",等待叢集建立完成(約 3-5 分鐘)
-
設定資料庫用戶:
- 在 Atlas Dashboard 中,點擊左側選單的 Database Access
- 點擊 "Add New Database User"
- 選擇 "Password" 認證方式
- 輸入用戶名和密碼(請妥善保存)
- 設定用戶权限為 "Atlas admin" 或 "Read and write to any database"
- 點擊 "Add User"
-
配置網路訪問:
- 點擊左側選單的 Network Access
- 點擊 "Add IP Address"
- 選擇 "Allow Access from Anywhere"(开发环境)或新增特定 IP(生產环境)
- 點擊 "Confirm"
- 在 Atlas Dashboard 中,點擊 Clusters → 您的叢集名稱
- 點擊 "Connect" 按钮
- 選擇 "Connect your application"
- 選擇 Driver: Node.js,Version: 5.5 or later
- 複製連接字串,格式如下:
mongodb+srv://<username>:<password>@cluster0.xxxxx.mongodb.net/?retryWrites=true&w=majority - 將
<username>和<password>替換為您建立的資料庫用戶憑證 - 在連接字串末尾新增資料庫名稱:
mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/book_rental_db?retryWrites=true&w=majority
- 確保已安裝 Node.js(版本 14 或更高)
- 執行設定腳本:
./scripts/setup_non-relational.sh
- 按照提示輸入 MongoDB 連接字串和資料庫名稱
-
安裝 MongoDB Node.js 驅動:
npm install mongodb
-
設定環境變數:
export MONGODB_URI="your_connection_string" export DATABASE_NAME="book_rental_db"
-
執行初始化腳本:
node database/non-relational/migrations/init_collections.js
-
在 Supabase Dashboard 中,開啟 Table Editor
-
確認可以看到以下表:
MEMBERSHIP_LEVELADMINMEMBERBOOKCATEGORYBOOK_CATEGORYCONDITION_DISCOUNTBOOK_COPIESBOOK_LOANLOAN_RECORDFEE_TYPEADD_FEERESERVATIONRESERVATION_RECORD
-
檢查
MEMBERSHIP_LEVEL表是否有 3 條記錄(銅、銀、金) -
檢查
CONDITION_DISCOUNT表是否有 3 條記錄(Good, Fair, Poor) -
檢查
FEE_TYPE表是否有 6 條記錄(renew, overdue, damage_good_to_fair, damage_good_to_poor, damage_fair_to_poor, lost)
- 在 MongoDB Atlas Dashboard 中,點擊 Collections
- 確認可以看到
search_history集合 - 點擊集合名稱,查看索引:
member_id_1search_date_-1member_id_1_search_date_-1search_query_textbook_ids_1
在您的應用程式中,使用以下格式的連接字串:
postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres
或使用 Supabase 客户端库的連接參數:
- URL:
https://[PROJECT-REF].supabase.co - Key:
anon public key或service_role key
使用您在 Atlas 中取得的連接字串:
mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/book_rental_db?retryWrites=true&w=majority
Q: 執行 SQL 時出現权限錯誤 A: 確保您使用的是專案所有者帳號,或具有資料庫管理員权限。
Q: 如何重置資料庫? A: 在 Supabase Dashboard 中,進入 Settings → Database → Reset Database(注意:这將刪除所有資料)
Q: 連接失敗,提示認證錯誤 A: 檢查用戶名和密碼是否正確,確保在連接字串中正確替換了佔位符。
Q: 連接失敗,提示網路錯誤 A: 檢查 Network Access 設定,確保您的 IP 地址已被允许訪問。
Q: 如何查看資料庫中的資料? A: 在 Atlas Dashboard 中,點擊 Collections,選擇資料庫和集合即可查看。
資料庫設定完成後,您可以:
- 參考 資料汇入指南 匯入業務資料
- 查看 Schema 參考文檔 了解資料庫結構
- 開始开发應用程式,連接到資料庫
如遇到問題,請:
- 查看 Supabase 文檔:https://supabase.com/docs
- 查看 MongoDB Atlas 文檔:https://docs.atlas.mongodb.com/
- 聯絡團隊成員取得幫助