Skip to content

Function MorphMap

morph map is merged by default

typescript
const map1 = {
  user: FedacoTestUser
};
const map2 = {
  post: FedacoTestPost
};
Relation.morphMap(map1);
Relation.morphMap(map2);

see also prerequisites

morph map is used for creating and fetching through relation

typescript
Relation.morphMap({
  user: FedacoTestUser,
  post: FedacoTestPost
});
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await user.NewRelation('photos').create({
  name: 'Avatar 1'
});
await user.NewRelation('photos').create({
  name: 'Avatar 2'
});
const post = await user.NewRelation('posts').create({
  name: 'First Post'
});
await post.NewRelation('photos').create({
  name: 'Hero 1'
});
await post.NewRelation('photos').create({
  name: 'Hero 2'
});
ReferenceLooks LikeValue
(await user.photos)[0]instance type exactly matchFedacoTestPhoto
isArray(await post.photos)exactly matchtrue
(await post.photos)[0]instance type exactly matchFedacoTestPhoto
(await user.photos).lengthexactly match2
(await post.photos).lengthexactly match2
(await user.photos)[0].nameexactly match'Avatar 1'
(await user.photos)[1].nameexactly match'Avatar 2'
(await post.photos)[0].nameexactly match'Hero 1'
(await post.photos)[1].nameexactly match'Hero 2'
(await user.photos)[0].getAttribute('imageable_type')exactly match'user'
(await user.photos)[1].getAttribute('imageable_type')exactly match'user'
(await post.photos)[0].getAttribute('imageable_type')exactly match'post'
(await post.photos)[1].getAttribute('imageable_type')exactly match'post'

see also prerequisites

morph map is used when fetching parent

typescript
Relation.morphMap({
  user: FedacoTestUser,
  post: FedacoTestPost
});
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await user.NewRelation('photos').create({
  name: 'Avatar 1'
});
const photo = await FedacoTestPhoto.createQuery().first();
ReferenceLooks LikeValue
await photo.imageableinstance type exactly matchFedacoTestUser

see also prerequisites

morph map overwrites current map

typescript
const map1 = {
  user: FedacoTestUser
};
const map2 = {
  post: FedacoTestPost
};
Relation.morphMap(map1, false);
typescript
Relation.morphMap(map2, false);

see also prerequisites

Released under the MIT License.