Add InsForge to your Swift Package Manager dependencies:
Enable Logging (Optional)
For debugging, you can configure the SDK log level and destination:
Log Levels:
| Level | Description |
|---|
.trace | Most verbose, includes all internal details |
.debug | Detailed information for debugging |
.info | General operational information (default) |
.warning | Warnings that don’t prevent operation |
.error | Errors that affect functionality |
.critical | Critical failures |
Log Destinations:
| Destination | Description |
|---|
.console | Standard output (print) |
.osLog | Apple’s unified logging system (recommended for iOS/macOS) |
.none | Disable logging |
.custom | Provide your own LogHandler factory |
Use .info or .error in production to avoid exposing sensitive data in logs.
定義模型
使用 Swift 的 Codable 通訊協定定義資料模型:
insert()
將新記錄插入到資料表中。
values - 單個 Codable 物件或要插入的物件陣列
update()
更新資料表中的現有記錄。
delete()
從資料表中刪除記錄。
select()
從資料表查詢記錄。
rpc()
呼叫 PostgreSQL 預存程式(RPC - 遠端程序呼叫)。此方法可讓您直接叫用在資料庫中定義的 SQL 函數。
functionName (String) - 要呼叫的 PostgreSQL 函數名稱
args ([String: Any]?, optional) - 要傳遞給函數的引數
實作詳細資料
- 無引數:使用 GET 要求至
/api/database/rpc/{functionName}
- 含引數:使用 POST 要求搭配 JSON 主體至
/api/database/rpc/{functionName}
- 對於傳回陣列或 void 的函數,使用
execute()
- 對於傳回單一物件或值的函數,使用
executeSingle()
篩選器
| 篩選器 | 描述 | 範例 |
|---|
.eq(column, value:) | 等於 | .eq("status", value: "active") |
.neq(column, value:) | 不等於 | .neq("status", value: "banned") |
.gt(column, value:) | 大於 | .gt("age", value: 18) |
.gte(column, value:) | 大於或等於 | .gte("price", value: 100) |
.lt(column, value:) | 小於 | .lt("stock", value: 10) |
.lte(column, value:) | 小於或等於 | .lte("priority", value: 3) |
.like(column, pattern:) | 區分大小寫的模式 | .like("name", pattern: "%Widget%") |
.ilike(column, pattern:) | 不區分大小寫的模式 | .ilike("email", pattern: "%@gmail.com") |
.in(column, values:) | 值在陣列中 | .in("status", values: ["pending", "active"]) |
.is(column, value:) | 完全等於(用於 nil/bool) | .is("deleted_at", value: nil) |
修飾詞
| 修飾詞 | 描述 | 範例 |
|---|
.order(column, ascending:) | 排序結果 | .order("createdAt", ascending: false) |
.limit(count) | 限制列數 | .limit(10) |
.offset(count) | 跳過列數 | .offset(20) |
.range(from:, to:) | 範圍分頁 | .range(from: 0, to: 9) |