Skip to content
/ Turf Public

A SQLite based key-value database written entirely in Swift

License

Notifications You must be signed in to change notification settings

TurfDb/Turf

Repository files navigation

Turf

Turf is a document store database built entirely in Swift using SQLite.

Built from the ground up to take advantage of Swift's type system, it has full support for persisting any struct, class, enum or even tuple.

Turf makes heavy use of generics and Swift's protocol constraints to provide a very safe API for reading, writing and query collections.

Build Status Carthage compatible MIT License

Features

  • 100% support for Swift value types (struct, enum, tuple).
  • Type safe collections.
  • Non-invasive approach to persisting any model - no subclasses, no protocols.
  • Multiple connection support.
  • Object caching - Skip deserializing objects if they have previously been deserialized on the same connection
  • Thread safety. Turf is safe even across multiple connections. Only one connection can write to the database at any given time yet you can read from multiple threads at the same time.
  • Secondary indexing. Index any persisted or computed properties of your models for fast querying.
  • Strongly typed queries - No more strings and NSPredicates
  • Reactive - Observe changes to any collection with the ability to filter down and watch a specific row change.
  • Migrations framework built in. Your requirements change, so rather than forcing you to devise your own migration framework, Turf already has one.

You can play with Turf in these Playgrounds.

Example

// Set up a collection to persist a tuple
final class PeopleCollection: TurfCollection {
    typealias Value = (name: String, age: UInt)

    let name = "People"
    let schemaVersion: UInt64 = 1
    let valueCacheSize: Int? = nil

    func setUp<Collections: CollectionsContainer>(using transaction: ReadWriteTransaction<Collections>) throws {
        try transaction.registerCollection(self)
    }

    func serialize(value: Value) -> NSData {
        let dict = [
            "name": value.name,
            "age": value.age
        ]

        return try! NSJSONSerialization.dataWithJSONObject(dict, options: [])
    }

    func deserialize(data: NSData) -> Value? {
        let dict = try! NSJSONSerialization.JSONObjectWithData(data, options: [])
        return (name: dict["name"] as! String, age: dict["age"] as! UInt)
    }
}

// List the available collections

final class Collections: CollectionsContainer {
	let people = PeopleCollection()

	func setUpCollections<Collections: CollectionsContainer>(using transaction: ReadWriteTransaction<Collections>) throws {
		try people.setUp(using: transaction)
	}
}

// Open a database connection

let collections = Collections()
let database = try Database(path: "test.sqlite", collections: collections)
let connection = try database.newConnection()

// Write a person to the people collection

try connection.readWriteTransaction { transaction, collections in
    transaction.readWrite(collections.people)
        .set(value: ("Kelsey", 30), forKey: "kelsey")
}

// Read a person back

try connection.readWriteTransaction { transaction, collections in
    let kelsey = transaction.readOnly(collections.people)
        .value(for: "kelsey")
    print(kelsey)
}

Installation

Requirements: Swift 4

Carthage

  1. Add the following to your Cartfile

github "TurfDb/Turf"

  1. Run carthage update

CocoaPods

Coming soon

Usage

Coming soon

License

Turf is available under the MIT license. See the LICENSE file for more info.