fields
Contains the logic for how fields on [Model](Model)s work
and which descriptors must be installed.
If your goal is to define fields on a Model class,
please use the more convenient methods [attr](attr),
[fk](fk), [many](many) and [oneToOne](oneToOne).
- fields
attr([opts])
⇒ Attributefk(options, [relatedName])
⇒ ForeignKeymany(options, [relatedName])
⇒ ManyToManyoneToOne(options, [relatedName])
⇒ OneToOne
attr([opts])
⇒ Attribute
Defines a value attribute on the model.
Though not required, it is recommended to define this for each non-foreign key you wish to use.
Getters and setters need to be defined on each Model
instantiation for undeclared data fields, which is slower.
You can use the optional getDefault
parameter to fill in unpassed values
to [Model.create](Model.create), such as for generating ID's with UUID:
import getUUID from 'your-uuid-package-of-choice';
fields = {
id: attr({ getDefault: () => getUUID() }),
title: attr(),
}
Kind: inner method of fields
Param | Type | Description |
---|---|---|
[opts] | Object | |
[opts.getDefault] | function | If you give a function here, its return |
fk(options, [relatedName])
⇒ ForeignKey
Defines a foreign key on a model, which points
to a single entity on another model.
You can pass arguments as either a single object,
or two arguments.
If you pass two arguments, the first one is the name
of the Model the foreign key is pointing to, and
the second one is an optional related name, which will
be used to access the Model the foreign key
is being defined from, from the target Model.
If the related name is not passed, it will be set as
${toModelName}Set
.
If you pass an object to fk
, it has to be in the form
fields = {
author: fk({ to: 'Author', relatedName: 'books' })
}
Which is equal to
fields = {
author: fk('Author', 'books'),
}
Kind: inner method of fields
Param | Type | Description |
---|---|---|
options | string ⎮ Class. | The target Model class, its |
options.to | string ⎮ Class. | The target Model class or its |
[options.as] | string | Name for the new accessor defined for this field. If you don't |
[options.relatedName] | string | The property name that will be used to access |
[relatedName] | string | If you didn't pass an object as the first argument, |
many(options, [relatedName])
⇒ ManyToMany
Defines a many-to-many relationship between
this (source) and another (target) model.
The relationship is modeled with an extra model called the through model.
The through model has foreign keys to both the source and target models.
You can define your own through model if you want to associate more information
to the relationship. A custom through model must have at least two foreign keys,
one pointing to the source Model, and one pointing to the target Model.
Like fk
, this function accepts one or two string arguments specifying the other
Model and the related name, or a single object argument that allows you to pass
a custom through model.
If you have more than one foreign key pointing to a source or target Model in the
through Model, you must pass the option throughFields
, which is an array of two
strings, where the strings are the field names that identify the foreign keys to
be used for the many-to-many relationship. Redux-ORM will figure out which field name
points to which model by checking the "through model" definition.
class Authorship extends Model {}
Authorship.modelName = 'Authorship';
Authorship.fields = {
author: fk('Author', 'authorships'),
book: fk('Book', 'authorships'),
};
class Author extends Model {}
Author.modelName = 'Author';
Author.fields = {
books: many({
to: 'Book',
relatedName: 'authors',
through: 'Authorship',
// here this is optional: Redux-ORM can figure
// out the through fields itself since the two
// foreign key fields point to different Models
throughFields: ['author', 'book'],
})
};
class Book extends Model {}
Book.modelName = 'Book';
You should only define the many-to-many relationship on one side. In the
above case of Authors to Books through Authorships, the relationship is
defined only on the Author model.
Kind: inner method of fields
Param | Type | Description |
---|---|---|
options | string ⎮ Class. | The target Model class, its |
options.to | string ⎮ Class. | The target Model class or its |
[options.as] | string | Name for the new accessor defined for this field. If you don't |
[options.through] | string ⎮ Class. | The through Model class or its |
[options.throughFields] | Array. | Must be supplied only when a custom through |
[options.relatedName] | string | The attribute used to access a QuerySet for all |
[relatedName] | string | If you didn't pass an object as the first argument, |
oneToOne(options, [relatedName])
⇒ OneToOne
Defines a one-to-one relationship. In database terms, this is a foreign key with the
added restriction that only one entity can point to single target entity.
The arguments are the same as with fk
. If relatedName
is not supplied,
the source model name in lowercase will be used. Note that with the one-to-one
relationship, the relatedName
should be in singular, not plural.
Kind: inner method of fields
Param | Type | Description |
---|---|---|
options | string ⎮ Class. | The target Model class, its |
options.to | string ⎮ Class. | The target Model class or its |
[options.as] | string | Name for the new accessor defined for this field. If you don't |
[options.relatedName] | string | The property name that will be used to access the source |
[relatedName] | string | The property name that will be used to access the source |