Setting up a class to use StORM

When you create a new class that will model a database table, it should inherit from a parent StORM class. This will provide most of the goodness you will need.

If you are using PostgreSQL:

class User: PostgresStORM {
}

If you are using SQLite:

class User: SQLiteStORM {
}

Next, you will need to add properties to the class. They will mirror closely your columns in the associated table. It's strongly recommended that you keep non-standard characters out of the column names and therefore property names too.

The first property should be your table's primary key. Convention is that this column is called id but in reality it can be any valid column name. Common data types for primary keys in SQL datasources are integers, strings, and UUIDs. If your primary key is not an auto-incrementing integer sequence, take care with setting your id and maintaining integrity.

You will see above that the default values are set for each property rather than set via an init(). This is simply to make this explanation easier. If you choose to write your own init() function, include super.init() and beware of the need to add the connection property.

Specifying the table

The table name is to be manually specified in a function. This is to avoid any possible collision with the introspection that is performed on the class for database operations.

Include a function to specify the table as follows:

override open func table() -> String {
return "users"
}

Class-specific assignment functions

In order for the class to do certain things, such as taking a result set from the database and converting the rows to an array of classed objects, add two functions to the class: