Cloudflare D1 を ReadModel として扱うための軽量 ORM です。Swift から D1 の raw クエリを実行し、マクロによるテーブル定義やフォーマットスタイルによる型変換を提供します。
This repository is the original and official repository of swift-d1-lite.
swift-d1-liteis a Swift Package for macOS.- It is intended to be used via Swift Package Manager.
- I do not provide any official downloadable application, installer, or binary release for this project.
- I do not maintain, endorse, or support any third-party copies, mirrors, documentation sites, or download links.
- If a third-party repository or website contains my name, copyright notice, or Git history, that does not mean I maintain, approve, or support it.
For the latest source code and correct usage, please refer only to this repository.
このリポジトリが swift-d1-lite の原著者による公式リポジトリです。
swift-d1-liteは macOS 向けの Swift Package です。- 利用は Swift Package Manager を前提としています。
- このプロジェクトについて、公式のアプリ配布・インストーラ配布・バイナリ配布は行っていません。
- 第三者によるコピー、ミラー、ドキュメントサイト、ダウンロードリンクについて、作者は関与・保証・サポートを行いません。
- 第三者のリポジトリやサイトに作者名、著作権表示、Git の履歴が含まれていても、それは作者がその公開物を保守・承認・サポートしていることを意味しません。
最新のソースコードと正しい利用方法は、このリポジトリのみを参照してください。
@D1Table/@D1Columnによるテーブル定義の簡略化D1SQLClientによるバッチ実行D1FormatStyleによる型安全な変換- HTTP 経由の実行(
D1LiteAsyncHTTPClient)と SQLite 実行(D1LiteSQLite)を提供
- Cloudflare D1 を Swift から型安全に扱いたい方
- CQRS/ES の ReadModel を軽量に実装したい方
- テストや開発時にローカルで D1 相当の動作を再現したい方
Swift Package Manager で追加できます。
.package(url: "https://github.com/lemo-nade-room/swift-d1-lite.git", from: "0.1.0")利用するターゲットに応じて依存関係を追加してください。
.target(
name: "YourTarget",
dependencies: [
.product(name: "D1Lite", package: "swift-d1-lite"),
// HTTP 経由で D1 を実行する場合
.product(name: "D1LiteAsyncHTTPClient", package: "swift-d1-lite"),
// ローカル SQLite で実行する場合
.product(name: "D1LiteSQLite", package: "swift-d1-lite"),
]
)import D1Lite
import Foundation
@D1Table(schema: "users")
struct User: Sendable {
@D1Column
var id: UUID
@D1Column
var name: String
@D1Column(name: "created_at", formatStyle: D1DateFormatStyle(format: .epoch))
var createdAt: Date
}
let client = D1SQLClient(client: rawClient)
let userID = UUID()
let user = User(id: userID, name: "Alice", createdAt: Date())
let (insertResult, fetched) = try await client.batch(
user.create(),
User.select().filter(\.id == userID).first()
)import AsyncHTTPClient
import Configuration
import D1Lite
import D1LiteAsyncHTTPClient
import Foundation
let config = ConfigReader(providers: [
EnvironmentVariablesProvider(),
])
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
let rawClient = try AsyncHTTPD1RawDatabaseQueryClient(httpClient: httpClient, configReader: config)
let client = D1SQLClient(client: rawClient)
let userID = UUID()
let result = try await client.batch(
User.select().filter(\.id == userID).first()
)import D1Lite
import D1LiteSQLite
import Foundation
let rawClient = SQLiteD1RawDatabaseQueryClient(config: .inMemory)
let client = D1SQLClient(client: rawClient)
_ = try await client.batch(
D1Query<D1Void>(
statement: """
CREATE TABLE users(
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
count INTEGER NOT NULL
)
""",
params: []
)
)
@D1Table(schema: "users")
struct User: Sendable, Hashable {
@D1Column
var id: UUID
@D1Column
var name: String
@D1Column
var count: Int
}
let userID = UUID()
let user = User(id: userID, name: "Alice", count: 1)
let (_, fetched) = try await client.batch(
user.create(),
User.select().filter(\.id == userID).first()
)MIT License