Model definition in Clear is done by inclusion of the Clear::Model module in your class. Assuming we have this table in PostgreSQL:
CREATETABLEarticles ( id serialNOT NULLPRIMARY KEY,nametextNOT NULL,descriptiontext);
The definition of this model is straight forward:
article.cr
classArticleincludeClear::Model column name :String column description :String? column id :Int32,primary:true,presence:falseend
Cut step by step, this is what happens:
First, we include all the magic of Clear in our class:
includeClear::Model
Second, we define name column as String. Clear will map automatically the column to the model attribute.
column name :String
Third, we define description . We defined descriptionas NULLABLE in our database. To reflect this choice, we add Nilable? operator to our column.
Finally, we define id as our primary key for this model. While being declared as NOT NULL, the column is defined with a default value in PostgreSQL. Therefore, we tell Clear to not check value presence on save/validate by adding presence: false to the column definition.
You may now use your model :
By default, Clear will inflect the model name and use plural lower case version of the model name as table name (here articles).
You may want to override this behavior, by redefining self.table :
Next article is covering deeply the column definition and its subtleties.