denoDB

denoDB

  • Docs
  • GitHub
  • Help

›Models

Guides

  • Getting started
  • Connect to a database
  • Clients

    • Using MariaDB
    • Using MongoDB
    • Using MySQL
    • Using PostgreSQL
    • Using SQLite
  • Create models
  • Synchronize database
  • Query models
  • Transactions
  • Relationships

    • Foreign key
    • One-to-one
    • One-to-many
    • Many-to-many
  • Model events

API Reference

    Models

    • Data types
    • Field descriptors
    • Model methods
    • Model records

Field descriptors

A field can simply be defined as such: field: DataTypes.TYPE, but in some cases you might need a primary key or a given length for this field.

static fields = {
  name: {
    type: DataTypes.STRING,
    length: 22,
    allowNull: true,
  },
  // or
  otherName: {
    ...DataTypes.string(22),
    allowNull: true,
  },
  // or if `allowNull = false`
  realName: DataTypes.string(22),
};

Shortcuts

There are a few shortcuts to define field types.

String

Set a maximum number of characters for this field.

static fields = {
  name: DataTypes.string(25),
};

Decimal

Set a precision and an optional scale values for a decimal field.

static fields = {
  // Precision, then scale (which is optional)
  price: DataTypes.decimal(2, 1),
};

Enum

Provide the list of possible values.

static fields = {
  status: DataTypes.enum(['active', 'canceled']),
};

Integer

Set a maximum number of digits for this field.

static fields = {
  quantity: DataTypes.integer(2)
}

Descriptors

Here is a list of all the field descriptors available:

DescriptorObject attributeExample
As (rename fields)asas: "field_name_in_database"
Typetypetype: DataTypes.STRING
Primary keyprimaryKeyprimaryKey: true
Uniqueuniqueunique: true
Auto incrementautoIncrementautoIncrement: true
Lengthlengthlength: 64
Allow null/is nullableallowNullallowNull: false
Precision (for decimals)precisionprecision: 3
Scale (for decimals)scalescale: 2
Values (for enums)valuesvalues: ["active", "canceled"]
Commentcommentcomment: "User name"
Last updated on 4/18/2021 by eveningkid
← Data typesModel methods →
  • Shortcuts
    • String
    • Decimal
    • Enum
    • Integer
  • Descriptors
Docs
Getting StartedAPI Reference
More
GitHubStar