Skip to content

Setting up a model

Jeff Hurray edited this page Apr 25, 2016 · 3 revisions

SQLiteModel is a protocol, so to add functionality to a model, your struct or class must conform to SQLiteModel.

Caveat: classes conforming to SQLiteModel must be final

There are only 2 methods / properties that need to be overridden:

####Local ID:

// Swift protocols do not support variable storage :(
var localID: SQLiteModelID

localID is populated under the hood and is used as a hash for an in-memory cache. Generally you should never set localID. It is a good idea to initialize localID to -1 to indicate that the model is unassociated with a row, although its not technically necessary.

####Build Table:

// A method used to add columns / relationships to the database table
static func buildTable(tableBuilder: TableBuilder) -> Void

buildTable is used to create columns (Expressions and Relationships) for the table associated with a model.

TableBuilder is a class from SQLite.swift. A full list of TableBuilder methods can be found here

####Putting it all together

Say we want to model a Movie. We can set up our model like this:

struct Movie: SQLiteModel {
    
    var localID: SQLiteModelID = -1

    static let Title = Expression<String>("title")
    static let ReleaseDate = Expression<NSDate>("release_date")
    static let Gross = Expression<Double>("gross")

    static func buildTable(tableBuilder: TableBuilder) {
        tableBuilder.column(Title)
        tableBuilder.column(ReleaseDate)
        tableBuilder.column(Gross, defaultValue: 0)
    }
}

localID is an auto-incrementing primary key for the table associated with your model. We need to create it in the struct or class because Swift protocols don't support stored properties. It is required because it is used as a hash for caching. SQLiteModelID is just a redeclaration of Int64.

You can think of Expression<T> as a column in a database table. I personally like to have my columns as static properties of my model, but they can be declared anywhere as long as they are accessible.

Expression<T> is a generic type from SQLite.swift. More info on Expression<T> can be found here

The following examples show different ways to declare expressions.

struct MovieColumns {
    static let Title = Expression<String>("title")
    static let ReleaseDate = Expression<NSDate>("release_date")
    static let Gross = Expression<Double>("gross")
}

struct Movie: SQLiteModel {
    
    var localID: SQLiteModelID = -1

    static func buildTable(tableBuilder: TableBuilder) {
        tableBuilder.column(MovieColumns.Title)
        tableBuilder.column(MovieColumns.ReleaseDate)
        tableBuilder.column(MovieColumns.Gross, defaultValue: 0)
    }
}
struct Movie: SQLiteModel {
    
    var localID: SQLiteModelID = -1

    static struct Columns {
        static let Title = Expression<String>("title")
        static let ReleaseDate = Expression<NSDate>("release_date")
        static let Gross = Expression<Double>("gross")
    }

    static func buildTable(tableBuilder: TableBuilder) {
        tableBuilder.column(Columns.Title)
        tableBuilder.column(Columns.ReleaseDate)
        tableBuilder.column(Columns.Gross, defaultValue: 0)
    }
}
Clone this wiki locally