Git Product home page Git Product logo

kassandra's Introduction

Kassandra

A pure Swift client library for Apache Cassandra (3.4+) and ScyllaDB using Cassandra's binary protocol, CQL 3.2.

Build Status

Installation

import PackageDescription

let package = Package(
    	dependencies: [
		.Package(url: "https://github.com/IBM-Swift/Kassandra.git", majorVersion: 0, minor: 1)
    	]
    )

Basic usage

Prepare your queries

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;")

}

Create a Model

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
    }
}

Save an object

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()

Detailed Installation

  1. Install OpenSSL:

    • macOS:
    $ brew install openssl
    
    • Linux:
    $ sudo apt-get install openssl
    
  2. Add Kassandra to your Package.swift

    import PackageDescription
    
    let package = Package(
    	dependencies: [
    	.Package(url: "https://github.com/IBM-Swift/Kassandra.git", majorVersion: 0, minor: 1)
    	]
    )
  3. 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
    
  4. In Sources/main.swift, import the Kassandra module.

    import Kassandra
  5. 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
    

License

Copyright 2016 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

http://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.

kassandra's People

Contributors

sandmman avatar chiahuang avatar rfdickerson avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

quanvo87

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.