How to Set Up Model Classes


Models are object representations of tables present within your database schema. In order to create a new Model within DenoGres, users will need to declare an accompanying interface which has the same name as the Model. This step is necessary in order to addresses type-checking when you generate new instances of the particular model classes.


Creating Models


To create a new table within your PostgreSQL database, create a new class which extends the Model class, along with an accompanying Interface. Note that each newly created class MUST have the following static properties.

1. Table: Name of the table within your PostgreSQL database schema

2. Column: This property should be assigned as an object, which contains properties corresponding to columns within the table. The values assigned to these properties should also be objects, which contain properties defining the attributes and constraints related to the particular column in question.

Below is a list of properties/constraints that can be applied to a given column, as well as possible values that can be associated with those properties/constraints:


  • primaryKey: Boolean
  • notNull: Boolean
  • unique: Boolean
  • checks: Object [key: string]: string
  • defaultVal: string | number | boolean | Date
  • checks: Object [key: string]: string
  • autoIncrement: boolean
  • association: { name: string, mappedTable: string, mappedColumn: string }
  • (name of the check): { [(name of the check): string]: string }

Included below is a snippet of what a Model within your model.ts file should potentially look like:


import { Model } from 'https://deno.land/x/denogres/mod.ts'

interface User {
  id:string;
  firstname:string;
  lastname?:string;
  points?: number;
  team_id:number;
}

class User extends Model {
  static table = 'users';
  static columns = {
    id: { type:'uuid', primaryKey: true },
    firstName: { type:'string', notNull: true },
    lastName: { type:'string', notNull: false },
    points: { type:'number', notNull: false },
    team_id: { type:'number', noNull: true }
  }
}

Updating Models


To update tables within your database schema, simply make changes to the interface and the associated Model class within model.ts and run --db-sync to synchronize those changes to your database schema. DenoGres will delete the asssociated table from your database schema.


Deleting Models


Delete the interface and the associated Model class within model.ts and run --db-sync to synchronize those changes to your database schema. DenoGres will automatically delete the asssociated table from your database schema.