A pure Swift client library for Apache Cassandra (3.4+) and ScyllaDB using Cassandra's binary protocol, CQL 3.2.
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kassandra.git", majorVersion: 1)
]
)
You must first connect to the database before you can execute SQL statements. You can specify the keyspace to use in the connect statement.
let kassandra = Kassandra()
try kassandra.connect(with: "mykeyspace") { error in
kassandra.execute("SELECT * FROM bread;")
}
In order to persist objects, they must conform to the Model
protocol.
struct Post {
var id: UUID?
let user: String
let body: String
let timestamp: Date
}
The model specifies the table name to use, the primary key that is used, and the fieldTypes.
extension Post: Model {
enum Field: String {
case id = "id"
case user = "user"
case body = "message"
case timestamp = "timestamp"
}
static let tableName = "Posts"
static var primaryKey: Field {
return Field.id
}
static var fieldTypes: [Field: DataType] {
return [
.id : .uuid,
.user : .text,
.body : .text,
.timestamp : .timestamp
]
}
var key: UUID? {
get {
return self.id
}
set {
self.id = newValue
}
}
init(row: Row) {
self.id = row["id"] as? UUID
self.user = row["user"] as! String
self.body = row["body"] as! String
self.timestamp = row["timestamp"] as! Date
}
}
Persisting an object with Kassandra is easy, and only requires the save method.
let post = Post(id: UUID(), user: user, body: message, timestamp: Date())
post.save()
-
Install OpenSSL:
- macOS:
$ brew install openssl
- Linux:
$ sudo apt-get install openssl
-
Add
Kassandra
to yourPackage.swift
import PackageDescription let package = Package( dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kassandra.git", majorVersion: 1) ] )
-
Create XCode project to build library (Optional)
$ swift package generate-xcodeproj \ -Xswiftc -I/usr/local/opt/openssl/include \ -Xlinker -L/usr/local/opt/openssl/lib
-
In Sources/main.swift, import the Kassandra module.
import Kassandra
-
Build Locally
- macOS
$ swift build -Xcc -I/usr/local/opt/openssl/include -Xlinker -L/usr/local/opt/openssl/lib
- Linux
$ swift build -Xcc -I/usr/local/opt/openssl/include -Xlinker -L/usr/local/opt/openssl/lib
Copyright 2017 IBM
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.