Skip to content

Commit bfcc04a

Browse files
committed
feat: Add support for wasm when using in-memory-only sqlite database
1 parent 22e8dfb commit bfcc04a

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
unit-tests:
1717
uses: vapor/ci/.github/workflows/run-unit-tests.yml@main
1818
secrets: inherit
19+
with:
20+
with_wasm: true
1921

2022
# Make sure downstream dependents still work
2123
dependents-check:

Package@swift-5.9.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ let package = Package(
1313
.library(name: "SQLiteKit", targets: ["SQLiteKit"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
17-
.package(url: "https://github.com/vapor/sqlite-nio.git", from: "1.9.0"),
18-
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.3"),
19-
.package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"),
16+
// TODO: SM: Update swift-nio version once NIOAsyncRuntime is available from swift-nio
17+
// .package(url: "https://github.com/apple/swift-nio.git", from: "2.89.0"),
18+
.package(url: "https://github.com/PassiveLogic/swift-nio.git", branch: "feat/addNIOAsyncRuntimeForWasm"),
19+
20+
// TODO: SM: Update below once everything is merged and release to the proper repositories
21+
// .package(url: "https://github.com/vapor/sqlite-nio.git", from: "1.9.0"),
22+
.package(url: "https://github.com/PassiveLogic/sqlite-nio.git", branch: "feat/swift-wasm-support-v2"),
23+
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.33.1"),
24+
// .package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"),
25+
.package(url: "https://github.com/PassiveLogic/async-kit.git", branch: "feat/swift-wasm-support-v2"),
2026
],
2127
targets: [
2228
.target(
2329
name: "SQLiteKit",
2430
dependencies: [
2531
.product(name: "NIOFoundationCompat", package: "swift-nio"),
32+
.product(name: "NIOAsyncRuntime", package: "swift-nio", condition: .when(platforms: [.wasi])),
33+
.product(name: "NIOPosix", package: "swift-nio"),
2634
.product(name: "AsyncKit", package: "async-kit"),
2735
.product(name: "SQLiteNIO", package: "sqlite-nio"),
2836
.product(name: "SQLKit", package: "sql-kit"),

Sources/SQLiteKit/SQLiteConnectionSource.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import Foundation
55
#endif
66
import Logging
77
import AsyncKit
8+
#if canImport(NIOAsyncRuntime)
9+
import NIOAsyncRuntime
10+
public typealias NIOThreadPool = AsyncThreadPool
11+
#endif
812
import NIOPosix
913
import SQLiteNIO
1014
import NIOCore
@@ -16,7 +20,13 @@ public struct SQLiteConnectionSource: ConnectionPoolSource, Sendable {
1620
private let threadPool: NIOThreadPool
1721

1822
private var connectionStorage: SQLiteConnection.Storage {
23+
#if os(WASI)
24+
// NOTE: For WASI platforms, file urls and paths cause runtime errors currently. Using
25+
// in-memory connection only as a workaround.
26+
.memory
27+
#else
1928
.file(path: self.actualURL.absoluteString)
29+
#endif
2030
}
2131

2232
/// Create a new ``SQLiteConnectionSource``.

Tests/SQLiteKitTests/SQLiteKitTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ final class SQLiteKitTests: XCTestCase {
124124
XCTAssertEqual(bar.baz, "qux")
125125
}
126126

127+
// NOTE: The following test doesn't work in a runtime environment
128+
// due to reliance on temp files. The test is elided for now
129+
// for WASI targets, but could be used in the future once
130+
// a persistence solution is working for WASI platforms.
131+
#if !os(WASI)
127132
func testMultipleInMemoryDatabases() async throws {
128133
let a = SQLiteConnectionSource(
129134
configuration: .init(storage: .memory, enableForeignKeys: true),
@@ -149,6 +154,7 @@ final class SQLiteKitTests: XCTestCase {
149154
try! await a2.close().get()
150155
try! await a1.close().get()
151156
}
157+
#endif // !os(WASI)
152158

153159
// https://github.com/vapor/sqlite-kit/issues/56
154160
func testDoubleConstraintError() async throws {

0 commit comments

Comments
 (0)