Skip to content

Latest commit

 

History

History
127 lines (92 loc) · 2.68 KB

Model.md

File metadata and controls

127 lines (92 loc) · 2.68 KB

Mavea - Model

Model holds the structure definition for your data. Mainly this means setting a type per field. An example of a type could be a string or a number. These are the other options to model your data:

Usage

// Data
const rawData = {foo: true, bar: 2};

// Model
const dataModel = data.model('rawData', {foo: Boolean, bar: Number});

Name

name is required. It points to a collection/table name.

Here's a MySQL example:

// Data
const playerModel = data.model('players', {name: String});

data.connect(mysql('mysql://localhost'));

await data.findOne(playerModel, {name: 'Joe'});

// SELECT * FROM `players` WHERE `name`="Joe";

Fields

fields is a required field. It is an object which keys are field names and values its type.

Value must be an instance of DataType or a native type.

Native types

You can use one of the following native types:

  • Boolean
  • Date
  • Number
  • String
data.model('players', {
  dateOfBirth: Date,
  isCaptain: Boolean,
  name: String,
  score: Number,
})

Advanced types

The following advanced types are also made available to you:

Required

A collection of field names that are required upon insertion.

data.model('users', {
  email: String,
  password: String,
}, {required: ['email', 'password']})

Default

You can set default values. If default value is a function, it will be executed.

data.model('players', {score: Number}, [], {score: 0});
data.model('players', {joined: Date}, [], {joined: () => new Date()});

Validation

You can set validations for fields that should return a boolean.

Validations can be either a function or a regular expression (for strings).

// Using regular expression
data.model({
  name: 'players',
  fields: {email: String},
  validate: {email: /^.+@.+\..+$/}
});

// Using function
data.model({
  name: 'players',
  fields: {password: String},
  validate: {password: (password) => password.length >= 4 && password.length <= 16},
})

Hooks (before and after)

See Hooks