Giter Club home page Giter Club logo

swiftydb's Introduction

![alt text] (http://i.imgur.com/uQhXJLJ.png?1 "Logo")

There are many libraries out there that aims to help developers easily create and use SQLite databases. Unfortunately developers still have to get bogged down in simple tasks such as writing table definitions and SQL queries. SwiftyDB automatically handles everything you don't want to spend your time doing.

CI Status Version License Platform

Features

  • Creates and updates databases, tables, and records automatically
  • Store any native Swift type
  • Supports optional types
  • Simple equality-based filtering
  • Thread safe database operations
  • Complex filtering
  • Store nested objects
  • Store collections

Usage

Almost pure plug and play. All you have to do is create an instance of SwiftyDB, and everything will be handled automagically behind the scenes 🎩

let database = SwiftyDB(databaseName: "dogtopia")

Add or update a record

try database.addObject(dog, update: true)
try database.addObjects(dogs, update: true)

Retrieve data

Retrieve data with datatypes matching those of the type's properties

/* Array of dictionaries representing `Dog` objects from the database */
database.dataForType(Dog.self)
database.dataForType(Dog.self, matchingFilters: ["id": 1])

Dog data example

[
    "id": 1,                // As an Int
    "name": "Ghost",        // As a String
    "owner": "John Snow",   // As a String
    "birth": August 6, 1996 // As an NSDate
]

Delete records

try database.deleteObjectsForType(Dog.self)
try database.deleteObjectsForType(Dog.self, matchingFilters: ["name": "Max"])

Defining your classes

Let's use this simple Dog class as an example

class Dog {
    var id: Int?
    var name: String?
    var owner: String?
    var birth: NSDate?
}

All objects must conform to the Storable protocol.

public protocol Storable {
    init()
}

By adding the Storable protocol and implementing init(), you are already ready to go.

class Dog: Storable {
    var id: Int?
    var name: String?
    var owner: String?
    var birth: NSDate?
    
    required init() {}
}

SwiftyDB supports inheritance. Valid properties from both the class and the superclass will be stored automatically

Primary keys

It is recommended you can implement the PrimaryKeys protocol. The primaryKeys() method should return a set of property names which uniquely identifies an object.

extension Dog: PrimaryKeys {
    class func primaryKeys() -> Set<String> {
        return ["id"]
    }
}
Ignoring properties

If your class contains properties that you don't want in your database, you can implement the IgnoredProperties protocol.

extension Dog: IgnoredProperties {
    class func ignoredProperties() -> Set<String> {
        return ["name"]
    }
}

Properties with datatypes that are not part of the SQLiteValue protocol, as defined by TinySQLite, will automatically be ignored by SwiftyDB

Retrieve objects

SwiftyDB can also retrieve complete objects with all properties assigned with data from the database. In order to achieve this, the type must be a subclass of NSObject, and all property types must be representable in in Objective-C. This is because pure Swift does not support dynamic, name-based assignment of properties.

Dynamic property types

  • Int
  • UInt
  • Float
  • Double
  • Bool
  • String / String?
  • NSNumber / NSNumber?
  • NSString / NSString?
  • NSDate / NSDate?
  • NSData / NSData?

Defining your dynamic classes

Updated Dog class subclassing NSObject, and using valid property types:

class Dog: NSObject, Storable {
    var id: NSNumber? // Notice that 'Int?' is not supported. Use NSNumber? instead
    var name: String?
    var owner: String?
    var birth: NSDate?
    
    override required init() {
        super.init()
    }
}

Retrieve objects

/* Returns an array of Dog objects */
try database.objectsForType(Dog.self)
try database.objectsForType(Dog.self, matchingFilters: ["name": "Max"])

Installation

SwiftyDB is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftyDB"

Author

Øyvind Grimnes, [email protected]

License

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

swiftydb's People

Contributors

oyvindkg avatar

Watchers

 avatar

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.