Usage examples below show you how to pass them in Model classes.
import { Types, createModel } from 'tydel';
const Todo = createModel({
title: Types.string
});
For boolean values:
import { Types, createModel } from 'tydel';
const Todo = createModel({
completed: Types.bool
});
import { Types, createModel } from 'tydel';
const Person = createModel({
age: Types.number
});
Even though this type exists in the API, you are highly advised to use Collection for any array-like values.
import { Types, createModel } from 'tydel';
const Author = createModel({
books: Types.array
});
Even though this type exists in the API, you are highly advised to use Model for any object-like values.
import { Types, createModel } from 'tydel';
const Person = createModel({
address: Types.object
});
And for defining schema of nested object, you can use object.of
:
const Person = createModel({
address: Types.object.of({
street: Types.string,
city: Types.string,
postalCode: Types.number,
country: Types.string
});
});
If you want the value to be one of the pre-defined list of values:
import { Types, createModel } from 'tydel';
const Book = createModel({
category: Types.enum([
'history',
'fiction',
'romance'
])
});
And if you want the enum to be of specific types, you can use enum.of
:
const Book = createModel({
category: Types.enum.of([
Types.string,
Types.number
])
});
If the value is expected to be an UUID:
import { Types, createModel } from 'tydel';
const Book = createModel({
id: Types.uuid
});
Example UUID value: 27961a0e-f4e8-4eb3-bf95-c5203e1d87b9
.
If the value can be of any type. You are advised not to use this:
import { Types, createModel } from 'tydel';
const Person = createModel({
bio: Types.any
});
Models can embed other models too:
import { Types, createModel } from 'tydel';
const Person = createModel({
address: Types.model
});
If you want to be more strict about which Model class can be embedded, use model.of
:
const Address = createModel({
street: Types.string,
city: Types.string
});
const Person = createModel({
address: Types.model.of(Address)
})
Collections can also be embedded in models:
import { Types, createModel } from 'tydel';
const Author = createModel({
books: Types.collection
});
If you want to be more strict about which Collection class can be embedded, use collection.of
:
const Book = createModel({
title: Types.string
});
const Books = createCollection(Book);
const Author = createModel({
books: Types.collection.of(Books)
});