TestsTested | โ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | May 2016 |
SPMSupports SPM | โ |
Maintained by Jeff Hurray.
A developer friendly Object Relational Model for SQLite3, wrapped over SQLite.swift
struct Person: SQLiteModel {
// Required by SQLiteModel protocol
var localID: SQLiteModelID = -1
static let Name = Expression<String>("name")
static let Age = Expression<Int>("age")
static let BFF = Relationship<Person?>("best_friend")
// Required by SQLiteModel protocol
static func buildTable(tableBuilder: TableBuilder) {
tableBuilder.column(Name)
tableBuilder.column(Age, defaultValue: 0)
tableBuilder.relationship(BFF, mappedFrom: self)
}
}
let jack = try Person.new([
Person.Age <- 10,
Person.Name <- "Jack"
])
let jill = try Person.new([
Person.Age <- 12,
Person.Name <- "Jill"
])
// Set age
// same as jack.set(Person.Age, 11)
jack <| Person.Age |> 11
// Get age
// same as jack.get(Person.Age)
let age = jack => Person.Age
// Set Best Friend
// same as jack.set(Person.BFF, jill)
jack <| Person.BFF |> jill
let people = try Person.fetchAll()
SQLiteModel requires Swift 2 (and Xcode 7) or greater.
To install SQLite.swift as an Xcode sub-project:
Drag the SQLite.xcodeproj file into your own project. (Submodule, clone, or download the project first.)
In your targetโs General tab, click the + button under Linked Frameworks and Libraries.
Select the appropriate SQLite.framework for your platform.
Add.
The wiki for this repo contains extensive documentation.
There are a lot of good data storage solutions out there, Realm and CoreData being the most popular. The biggest issue with these solutions is that they force your models be reference types (classes) instead of value types (structs).
Appleโs documentation lists a couple conditions where if true, a struct is probably a better choice than a class here. The following condition is especially relevant:
The structureโs primary purpose is to encapsulate a few relatively simple data values.
Sounds like a database row fits that description. Ideally if one are trying to model a database row, one should use structs, which SQLiteModel supports.
That being said, structs arent always the answer. SQLiteModel also supports using classes, but as of now they have to be final
.
There are also a lot of wrappers over SQLite that exist, but arenโt object relational models. SQLite.swift and FMDB are 2 great libraries that serve this functionality. These are very powerful and flexible, but they take a while to set up the right way as they require a lot of boilerplate code.
With SQLiteModel, the boilerplate code is already written. Obviously you are sacrificing flexibility for ease of use, but for most data storage needs this is acceptable (IMO).
There a couple good examples of how to use SQLiteModel
Included in this repo is a playground that you can use to fool around with the syntax and features of SQLiteModel. Make sure you open SQLiteModel.xcodeproject
since this playgrounf imports the SQLiteModel
framework, and make sure you are using the SQLiteModel-iOS
scheme.
There is a repo with example applications for iOS, TVOS, AND OSX that can be found here. These projects all use CocoaPods, so make sure to open the .xcworkspace
to get them running.
The iOS example that is provided is the best and most thorough example of how to use SQLiteModel. The app is a blog platform that allows users create, delete, and save blog posts. Users can also add images to blogs using relationships, and view all images on another tab.
Feel free to email me at [email protected]. Iโd love to hear your thoughts on this, or see examples where this has been used.
You can also hit me up on twitter @JeffHurray.
If you want to add functionality please open an issue and/or create a pull request.
Big thank you to Stephen Celis for writing SQLite.swift
which (IMHO) is one of the best Swift open source libraries that exists today.
SQLiteModel is available under the MIT license. See the LICENSE file for more information.