Skip to content

Lightweight persistence engine for Swift structures

License

Notifications You must be signed in to change notification settings

infinitetoken/Arcade

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arcade

Arcade is a lightweight persistence layer for Swift structures or objects!

Usage

Models

Models (structures) in your project must conform to Storable.

import Arcade

struct Owner: Storable {

    static var table: Table = AppTable.owner

    var uuid: String
    var name: String?

}

Table

The Table protocol defines the names of the tables in which to store your models.

import Arcade

enum AppTable: String, Table {
    case owner = "Owner"

    var name: String {
        return self.rawValue
    }
}

Configure Adapter

import Arcade

let arcade = Arcade(adapter: InMemoryAdapter())

Connect to Database

import Arcade

arcade.connect().subscribe({ (success) in
    // Connected!
}) { (error) in
    // Error
}

Inserting

import Arcade

let owner = Owner(uuid: UUID(), name: "Foo")

arcade.insert(storable: owner).subscribe({ (owner) in
    // Inserted!
}) { (error) in
    // Error
}

Updating

import Arcade

owner.name = "Fred"

arcade.update(storable: owner).subscribe({ (owner) in
    // Updated!
}) { (error) in
    // Error
}

Deleting

import Arcade

let uuid = owner.uuid

arcade.delete(uuid: uuid, type: Owner.self).subscribe({ (success) in
    // Deleted!
}) { (error) in
    // Error
}

Fetching

To find a specific item by UUID:

import Arcade

let future: Future<Owner> = arcade.find(uuid: owner.uuid)

future.subscribe({ (owner) in
    guard let owner = owner else {
        // Not found
    }

    // Found it!
}) { (error) in
    // Error
}

To query a set of items:

import Arcade

let expression = Expression.equal("name", "Foo")
let query = Query.expression(expression)
let future: Future<Owner> = arcade.fetch(query: query)

future.subscribe({ (owners) in
    // Do something with owners...
}) { (error) in
    // Error
}

CoreData

To use Arcade with the CoreData adapter some additional protocol conformance must be setup. Your CoreData entites should conform to CoreDataStorable:

@objc(OwnerEntity)
class OwnerEntity: NSManagedObject {

    @NSManaged var uuid: UUID
    @NSManaged var name: String?

    override func awakeFromInsert() {
        super.awakeFromInsert()

        self.uuid = UUID()
    }

}

extension OwnerEntity: CoreDataStorable {

    public var viewable: Viewable {
        return Owner(uuid: self.uuid, name: self.name)
    }

    public var storable: Storable {
        return Owner(uuid: self.uuid, name: self.name)
    }

    public func update(withStorable dictionary: [String : Any]) -> Bool {
        if let uuid = dictionary["uuid"] as? UUID {
            self.uuid = uuid
        }
    
        if let name = dictionary["name"] as? String {
            self.name = name
        } else if dictionary["name"] is NSNull {
            self.name = nil
        }

        return true
    }

}

That's it! Now you can save your objects using the CoreDataAdapter just like any other object!

License

Arcade is released under the MIT license. See LICENSE for details.