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:
// Data
const rawData = {foo: true, bar: 2};
// Model
const dataModel = data.model('rawData', {foo: Boolean, bar: Number});
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
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.
You can use one of the following native types:
Boolean
Date
Number
String
data.model('players', {
dateOfBirth: Date,
isCaptain: Boolean,
name: String,
score: Number,
})
The following advanced types are also made available to you:
A collection of field names that are required upon insertion.
data.model('users', {
email: String,
password: String,
}, {required: ['email', 'password']})
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()});
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},
})
See Hooks