Inserting and Updating Related Models
The insert
Method
Mongoloquent provides convenient methods for adding new models to relationships. For example, perhaps you need to add a new comment to a post. Instead of manually setting the postId
attribute on the Comment
model you may insert the comment using the relationship's insert
method:
import Post from "./yourPath/Post";
const post = await Post.find("77ab7e3d05d58a1ad246ee87");
await post.comments().save({ message: "A new comment." });
// or
await post.comments().insert({ message: "A new comment." });
Note that we did not access the comments
relationship as a dynamic property. Instead, we called the comments
method to obtain an instance of the relationship. The save method will automatically add the appropriate postId
value to the new Comment
model.
If you need to save multiple related models, you may use the insertMany
method:
const post = await Post.find("77ab7e3d05d58a1ad246ee87");
await post
.comments()
.insertMany([
{ message: "A new comment." },
{ message: "Another new comment." },
]);
You may also use the save
and create
methods to create models on relationships.
Many to Many Relationships
Attaching / Detaching
Mongoloquent also provides methods to make working with many-to-many
relationships more convenient. For example, let's imagine a user
can have many roles
and a role can have many users. You may use the attach
method to attach a role
to a user
by inserting a record in the relationship's intermediate collection:
import User from "./yourPath/User"
const user = await User.find("65ab7e3d05d58a1ad246ee87");
// Attach a single role from the user...
await user->roles()->attach("60ab7e3d05d58a1ad246ee60");
// Attach multiple roles from the user...
await user->roles()->attach(["60ab7e3d05d58a1ad246ee60", "60ab7e3d05d58a1ad246ee61"]);
Sometimes it may be necessary to remove a role from a user. To remove a many-to-many relationship record, use the detach
method. The detach
method will delete the appropriate record out of the intermediate collection; however, both models will remain in the database:
// Detach a single role from the user...
await user.roles().detach(roleId);
// Detach multiple roles from the user...
await user
.roles()
.detach(["60ab7e3d05d58a1ad246ee60", "70ab7e3d05d58a1ad246ee70"]);
For convenience, attach
and detach
also accept arrays of IDs as input:
const user = await User.find("65ab7e3d05d58a1ad246ee87");
await user
.roles()
.detach(["60ab7e3d05d58a1ad246ee60", "70ab7e3d05d58a1ad246ee70"]);
await user
.roles()
.attach(["60ab7e3d05d58a1ad246ee60", "70ab7e3d05d58a1ad246ee70"]);
Syncing Associations
You may also use the sync
method to construct many-to-many
associations. The sync
method accepts an array of IDs to place on the intermediate collection. Any IDs that are not in the given array will be removed from the intermediate collection. So, after this operation is complete, only the IDs in the given array will exist in the intermediate collection:
await user.roles().sync("60ab7e3d05d58a1ad246ee60");
For convenience, sync
also accept arrays of IDs as input:
await user
.roles()
.sync(["60ab7e3d05d58a1ad246ee60", "70ab7e3d05d58a1ad246ee70"]);
Support us
Mongoloquent is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.
Sponsors
_