Skip to content

Database Connections

Jeff Hurray edited this page May 3, 2016 · 1 revision

##Database Connections

There are 4 different types of Databases supported in this framework:

  • Disk
  • Temporary Disk
  • In Memory
  • Read Only

It is up to the developer to create and retain a database, and to associate it with a model type. If the developer does not do this the model will default to using a shared Disk database that is created by the framework.

The database can be associated with a model type conforming to SQLiteModel by returning it in from connection variable declared in SQLiteConvertible.

let myDatabase = let disk = Database(databaseType: DatabaseType.Disk(name: "my_project_database"))

struct MyModel: SQLiteModel {   
    ...
    static var connection: Database {
        return myDatabase
    }
    ...
}

####Read Only Databases To read a database from the application bundle, use the .ReadOnly type. The name you provide must match ne name of the resource, and the resource must be a file of type sqlite3.

i.e. if you have a file myStaticDB.sqlite3 added to your bundle, you can access it like this:

let myDB = Database(databaseType: DatabaseType.ReadOnly(name: "myStaticDB"))

####tvOS Caveat tvOS does not support persistent file storage, so any disk databases will be cached in the file directory and left up to the mercy of the cache cleanup algorithm of the OS.

Clone this wiki locally