Swift-build-testing

KurrentDB

这是一个非官方的 Kurrent (前身为:EventStore) 数据库 gRPC 客户端 SDK,使用 Swift 编写。

Kurrent 是第一个也是唯一一个原生事件数据平台。它旨在将数据存储和流式传输为事件,用于高级分析、微服务和 AI/ML 计划等下游用例。

实现状态

客户端设置

功能 已实现
ConnectionString 已解析
Endpoint (ip, port)
UserCredentials ( 用户名, 密码 )
Gossip 集群模式

功能 已实现
追加
读取
元数据
订阅指定流
订阅所有流
批量追加

投影

功能 已实现
创建
更新
结果
删除
启用
禁用
状态
统计信息
重置
重启子系统

持久订阅

功能 已实现
创建
删除
获取信息
列表
读取
重播已暂停
重启子系统
订阅
更新

用户

功能 已实现
创建
详情
禁用
启用
更新
修改密码
重置密码

获取库

Swift 包管理器

Swift Package Manager 是获取 EventStoreDB 的首选方法。只需将包依赖项添加到您的 Package.swift

dependencies: [
  .package(url: "https://github.com/gradyzhuo/eventstoredb-swift.git", from: "1.0.0-beta.2")
]

...并在必要的 target 中依赖 "EventStoreDB"

.target(
  name: ...,
  dependencies: [.product(name: "EventStoreDB", package: "eventstoredb-swift")]
]

示例

要导入的库名称。

import KurrentDB

ClientSettings

// Using a client settings for a single node configuration by parsing a connection string.
let settings: ClientSettings = .parse(connectionString: "esdb://admin:changeit@localhost:2113")

// convenience 
let settings: ClientSettings = "esdb://admin:changeit@localhost:2113".parse()

// using string literal 
let settings: ClientSettings = "esdb://admin:changeit@localhost:2113"

//using constructor
let settings: ClientSettings = .localhost()


// settings with credentials
let settings: ClientSettings = .localhost(userCredentials: .init(username: "admin", 
                                                                   password: "changeit")

//settings with credentials with adding ssl file by path
let settings: ClientSettings = .localhost(userCredentials: .init(username: "admin", 
                                                                            password: "changeit"), 
                                                                 trustRoots: .file("...filePath..."))

//or add ssl file with bundle
let settings: ClientSettings = .localhost(userCredentials: .init(username: "admin", 
                                                                 password: "changeit"), 
                                                                 trustRoots: .fileInBundle(forResource: "ca", 
                                                                                           withExtension: "crt", 
                                                                                           inBundle: .main))

追加事件

// Import packages of KurrentDB.
import KurrentDB

// Using a client settings for a single node configuration by parsing a connection string.
let settings: ClientSettings = .localhost()

// Create the data array of events.
let events:[EventData] = [
    .init(id: .init(uuidString: "b989fe21-9469-4017-8d71-9820b8dd1164")!, eventType: "ItemAdded", payload: ["Description": "Xbox One S 1TB (Console)"]),
    .init(id: .init(uuidString: "b989fe21-9469-4017-8d71-9820b8dd1174")!, eventType: "ItemAdded", payload: "Gears of War 4")
        ]

// Create an identifier of stream
let streamIdentifier = StreamIdentifier(name: "stream_for_testing")

// Build a streams client.
let streams = Streams(settings: settings)

// Append two events with one response
let appendResponse = try await streams.append(to: streamIdentifier, events: events)
print("The latest revision of events appended:", appendResponse.currentRevision!)

读取事件

// Import packages of KurrentDB.
import KurrentDB

// Using a client settings for a single node configuration by parsing a connection string.
let settings: ClientSettings = .localhost()

// Create an identifier of stream.
let streamIdentifier = StreamIdentifier(name: "stream_for_testing")

// Build a streams client.
let streams = Streams(settings: settings)

// Read events from stream.
let readResponses = try await streams.read(streamIdentifier, cursor: .start)

// loop it.
for try await response in readResponses {
    //handle response
}

持久订阅

创建

// Import packages of KurrentDB.
import KurrentDB

// Using a client settings for a single node configuration by parsing a connection string.
let settings: ClientSettings = .localhost()

// Build a persistentSubscriptions client.
let persistentSubscriptions = PersistentSubscriptions(settings: settings)

// Create it to specified identifier of streams. 
try await persistentSubscriptions.createToStream(streamIdentifier: streamIdentifier, groupName: "mytest")

订阅

// Import packages of KurrentDB.
import KurrentDB

// Using a client settings for a single node configuration by parsing a connection string.
let settings: ClientSettings = .localhost()

// Build a persistentSubscriptions client.
let persistentSubscriptions = PersistentSubscriptions(settings: settings)

// Subscribe to stream or all, and get a subscription.
let subscription = try await persistentSubscriptions.subscribe(.specified(streamIdentifier), groupName: "mytest")

// Loop all results by subscription.events
for try await result in subscription.events {
    //handle result
    // ...
    
    // ack the readEvent if succeed
    try await subscription.ack(readEvents: result.event)
    // else nack thr readEvent if not succeed.
    // try await subscription.nack(readEvents: result.event, action: .park, reason: "It's failed.")
}