All Projects → Oyvindkg → Swiftydb

Oyvindkg / Swiftydb

Licence: mit
💀Neither supported nor maintained for years. A type-safe, protocol-based, pure Swift database offering effortless persistence of any object

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Swiftydb

Nodejs Firestore
Node.js client for Google Cloud Firestore: a NoSQL document database built for automatic scaling, high performance, and ease of application development.
Stars: ✭ 475 (-4.04%)
Mutual labels:  database
Awesome Db Tools
Everything that makes working with databases easier
Stars: ✭ 479 (-3.23%)
Mutual labels:  database
Pg
Golang ORM with focus on PostgreSQL features and performance
Stars: ✭ 4,918 (+893.54%)
Mutual labels:  database
Wp Eloquent
Eloquent ORM for WordPress
Stars: ✭ 478 (-3.43%)
Mutual labels:  database
Aura.sql
SQL database access through PDO.
Stars: ✭ 483 (-2.42%)
Mutual labels:  database
Ormlite Core
Core ORMLite functionality that provides a lite Java ORM in conjunction with ormlite-jdbc or ormlite-android
Stars: ✭ 488 (-1.41%)
Mutual labels:  database
Sol Journal
✎ Simple, personal journaling progressive web app
Stars: ✭ 470 (-5.05%)
Mutual labels:  database
Things.sh
Simple read-only comand-line interface to your Things 3 database
Stars: ✭ 492 (-0.61%)
Mutual labels:  database
Gpdb
Greenplum Database - Massively Parallel PostgreSQL for Analytics. An open-source massively parallel data platform for analytics, machine learning and AI.
Stars: ✭ 4,928 (+895.56%)
Mutual labels:  database
Debezium
Change data capture for a variety of databases. Please log issues at https://issues.redhat.com/browse/DBZ.
Stars: ✭ 5,937 (+1099.39%)
Mutual labels:  database
Csharp Driver
DataStax C# Driver for Apache Cassandra
Stars: ✭ 477 (-3.64%)
Mutual labels:  database
Maghead
The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7
Stars: ✭ 483 (-2.42%)
Mutual labels:  database
Web Karma
Information Integration Tool
Stars: ✭ 489 (-1.21%)
Mutual labels:  database
Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (-3.64%)
Mutual labels:  database
Sled
the champagne of beta embedded databases
Stars: ✭ 5,423 (+995.56%)
Mutual labels:  database
Laravel Backup
A package to backup your Laravel app
Stars: ✭ 4,752 (+860%)
Mutual labels:  database
V8 Archive
Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
Stars: ✭ 486 (-1.82%)
Mutual labels:  database
Phpmyfaq
phpMyFAQ - Open Source FAQ web application for PHP and MySQL, PostgreSQL and other databases
Stars: ✭ 494 (-0.2%)
Mutual labels:  database
Finviz
Unofficial API for finviz.com
Stars: ✭ 493 (-0.4%)
Mutual labels:  database
Dictionary Of Pentesting
Dictionary collection project such as Pentesing, Fuzzing, Bruteforce and BugBounty. 渗透测试、SRC漏洞挖掘、爆破、Fuzzing等字典收集项目。
Stars: ✭ 492 (-0.61%)
Mutual labels:  database

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 Swift

You can find the documentation here

Content
Features
Usage
Access the database
   Synchronous access
   Aynchronous access
Result format
Filter results
Defining your classes
   Primary keys
   Ignoring properties
How to retrieve objects
Installation
License

Features

  • [x] Creates and updates databases, tables, and records automatically
  • [x] Supports optional types
  • [x] Simple equality-based filtering
  • [x] Thread safe database operations
  • [x] Supports asynchronous database access
  • [x] 100% documented
  • [x] Complex filtering
  • [x] Store collections
  • [ ] Store nested objects
  • [ ] Automated migration
  • [ ] Custom indices

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 🎩

Access the database

Tell SwiftyDB what you want to call your database, and you are ready to go. If a database with the provided name does not exist, it will be created.

let database = SwiftyDB(databaseName: "dogtopia")

Synchronous access

Add or update a record
database.addObject(dog, update: true)
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
]
Retrieve objects

Retrieve objects with data from the database

database.objectsForType(Dog.self)
database.objectsForType(Dog.self, matchingFilters: ["id": 1])

In order to retrieve objects, Swift currently imposes some restictions on your classes

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

Asynchronous access

Add or update a record
database.asyncAddObject(dog) { (result) -> Void in
    if let error = result.error {
        // Handle error
    }
}
Retrieve data

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

database.asyncDataForType(Dog.self) { (result) -> Void in
    if let data = result.value {
        // Process data
    }
}
Retrieve objects

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

database.asyncObjectsForType(Dog.self) { (result) -> Void in
    if let objects = result.value {
        // Handle objects
    }
}

In order to retrieve objects, Swift currently imposes some restictions on your classes

Delete records
database.asyncDeleteObjectsForType(Dog.self) { (result) -> Void in
    if let error = result.error {
        // Handle error
    }
}

Filter results

Filter objects are used to filter queries. All filters are translated to SQLite before querying the database.

Simple filters

The easiest way to define your filter, is by using a dictionary:

database.objectsForType(Dog.self, matchingFilters: ["name": "Ghost"])

All objects with the name 'Ghost' will be retrieved

Complex filters

For more complex filters, you can instantiate a new Filter object, and define your filters

let filter = Filter.equal("name", value: "Ghost")
                   .like("owner", pattern: "J_h%")
                   .greaterThan("id", value: 3)

database.objectsForType(Dog.self, matchingFilters: filter)

See all available filters in the documentation.

It is not possible to filter results using the content of stored collections as these are stored as blobs in the database

Result format

All queries returns the result as a Result. It will either be a .Success wrapping data from the query, or an .Error wrapping the thrown error.

enum Result<A: Any>: BooleanType {
    case Success(A)
    case Error(ErrorType)
    
    var data: A?
    var error: ErrorType?
    var isSuccess: Bool
    var boolValue: Bool {return isSuccess}
}

Handling results

The implementation of Result makes it a versatile tool that can (hopefully 😬) be adapted to your programming style

Handling values

You can capture the data from a query with the value property. If an error was thrown, this property will be nil.

if let object = result.value {
    // Process objects
}
Handling errors

You can detect an error like this

if !database.addObject(dog) {
    // An error occured
}

or capture it using the error property like this

if let error = result.error {
    // Process objects
}

If you want to, you can even bring your sledgehammer and start cracking some nuts

switch result {
    case .Success(let value):
        // Process value
    case .Error(let error):
        // Handle error
}

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 to 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 Value protocol, will automatically be ignored by SwiftyDB

How to 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 currently does not support dynamic assignment of properties.

Dynamic property types

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

An updated Dog class that can be used to retrieve complete objects from the database:

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].