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'
});
Reference Looks Like Value (await user.photos)[0]instance type exactly match FedacoTestPhotoisArray(await post.photos)exactly match true(await post.photos)[0]instance type exactly match FedacoTestPhoto(await user.photos).lengthexactly match 2(await post.photos).lengthexactly match 2(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();
Reference Looks Like Value await photo.imageableinstance type exactly match FedacoTestUser
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