createRelationalObject()
Used to define the object and its relationships with other objects.
Basic usage
import { createRelationalObject } from "@jjmyers/object-relationship-store";
const user = createRelationalObject("user");
const image = createRelationalObject("image");
user.hasOne(image, "profileImage");
Properties
createRelationalObject(name, primaryKey)
name
Assigns the given name to the object. The object will be selected by the give name when using store.select()
or store.selectIndex()
createRelationalObject("user");
primaryKey
The field in the object that will be used as a primaryKey
to identify the object.
default value
id
createRelationalObject("user", "id");
Return values
const user = createRelationalObject("user");
user.hasOne();
user.hasMany();
hasOne(object, as)
Tells the target object that it has one field that contains another related object.
const user = createRelationalObject("user");
const image = createRelationalObject("image");
user.hasOne(image, "profileImage");
Expected shape of the object
{
id: 1,
username: "the_overlord",
profileImage: {
id: 249,
uri: "https://example.com/some-image"
}
}
hasMany(object, as)
Tells the target object that it has one field that contains and Array
of related object.
const user = createRelationalObject("user");
const image = createRelationalObject("image");
user.hasMany(image, "galleryImages");
Expected shape of the object
{
id: 1,
username: "the_overlord",
galleryImages: [
{ id: 150, uri: "https://example.com/some-image" }
{ id: 151, uri: "https://example.com/some-image" }
]
}
API
Properties
Property | Type | Default | Description |
---|---|---|---|
name | string | undefined | Sets the name of the object. The object will be selected by the give name when using store.select() or store.selectIndex() |
primaryKey | string | id | Sets the field name in the object that will be used by the store as a primaryKey to maintain relationships and uniqueness. |
Return values
Name | Type | Description |
---|---|---|
.hasOne(object, as) | ORS.RelationalCreator<string>.hasOne | Creates a one to one relationship between the target object and the give object , optionally you can pass an alias if the give object comes under a different field name in the target object . |
.hasMany(object, as) | ORS.RelationalCreator<string>.hasMany | Creates a one to many relationship between the target object and the give object , optionally you can pass an alias if the give object comes under a different field name in the target object . |