Skip to main content

Define Models

You can define Models by extending BaseModel, or using Connection#models.

class User extends cormo.BaseModel {
name?: string;
age?: number;
}

User.column('name', String);
User.column('age', cormo.types.Integer);

// or if you don't want to use class keyword
const User = connection.model('User', {
name: String,
age: cormo.types.Integer,
});

// or if you want to use TypeScript decorators
@cormo.Model()
class User extends cormo.BaseModel {
@cormo.Column(String)
name?: string;

@cormo.Column(cormo.types.Integer)
age?: number;
}

You can pass an object with type property instead of a type class. Use this form to give additional options.

User.column('name', { type: String, required: true });
User.column('age', { type: cormo.types.Integer, description: 'age of the user' });

types

You can use any of CORMO type classes(e.g. cormo.types.String), strings(e.g. 'string'), or native JavaScript Function(e.g. String) to specify a type.

Currently supported types:

type options

You can give options for types in some adapters.

To specify length for string type in MySQL or PostgreSQL, you should do

Model.column('method_1', cormo.types.String(50));
// or
Model.column('method_2', 'string(50)');

Please note that you must use cormo.types.String, not String.