跳转到主要内容

安装

Add InsForge to your Swift Package Manager dependencies:

Enable Logging (Optional)

For debugging, you can configure the SDK log level and destination:
Log Levels:
LevelDescription
.traceMost verbose, includes all internal details
.debugDetailed information for debugging
.infoGeneral operational information (default)
.warningWarnings that don’t prevent operation
.errorErrors that affect functionality
.criticalCritical failures
Log Destinations:
DestinationDescription
.consoleStandard output (print)
.osLogApple’s unified logging system (recommended for iOS/macOS)
.noneDisable logging
.customProvide 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)