Performing CRUD and Relationship Operations

Ok, we already know how to build a query, so now it's time to perform operations!

CRUD

See the API reference for a list of available operation methods.

Let's start performing CRUD operations.

There are two CRUD operation methods:

  • save - Create and update models.
  • delete - Delete a model.

Saving a Model

See the API reference.

We can create and update models using the save method. If the model doesn't have an ID, the model will be created, otherwise it will be updated.

We can create a new Post:

const post = new Post({
  title: 'My Super Post!',
  text: 'Some text here... yay!'
})

await post.save()
const post = new Post()

post.title = 'My Super Post!'
post.text = 'Some text here... yay!'

await post.save()
POST /posts
When uploading files, the Content-Type will be set to multipart/form-data .

Then we can update our newly created Post:

const post = await Post.find(1)

post.text = 'An updated text for our Post!'

await post.save()
GET /posts/1
PUT /posts/1

And if we want to use PATCH, we can easily do that using patch.

const post = await Post.find(1)

post.text = 'An updated text for our Post!'

await post.patch()
GET /posts/1
PATCH /posts/1
You can safely use PATCH with save() . The POST method will not be overridden, only PUT .

Deleting a Model

See the API reference.

We can use the delete method to delete a model. It's really simple!

Time to delete our newly created Post:

const post = await Post.find(1)

await post.delete()
GET /posts/1
DELETE /posts/1

Relationship

See the API reference for a list of available operation methods.

Now let's perform relationship operations.

There are three relationship operation methods:

  • for - Create a new related model.
  • attach - Also create a new related model.
  • sync - Update a related model.

See the API reference.

When creating a new model, we can use the for method to make it related to another model. The for method will build a hierarchical resource endpoint for that.

The arguments are models. The model's resource will be used, as well as its primary key's value.

We can create a Comment for a Post:

const post = await Post.find(1)
const comment = new Comment({
  text: 'Awesome post!'
}).for(post) 

await comment.save()
GET /posts/1
POST /posts/1/comments

Or we can create a Comment for a Post of an User, by building hierarchy levels:

const user = await User.find(1)
const post = await user.posts().first()
const comment = new Comment({
  text: 'Awesome post!'
}).for(user, post) 

await comment.save()
GET /users/1
GET /users/1/posts
POST /users/1/posts/1/comments

Attaching a Model

See the API reference.

Another way to create a model related to another model is using the attach method. It will make a POST request to the resource endpoint of the relation.

The argument is an object, which is the data to be created.

We can create a Comment for a Post:

const post = await Post.find(1)
const comment = await post.comments().attach({
  text: 'Awesome post!'
})
GET /posts/1
POST /posts/1/comments

Syncing a Model

See the API reference.

The sync method is very similar to attach, but it's used to update a model. It makes a PUT request to the resource endpoint of the relation.

The argument is an object, which is the data to be updated.

We can update a Comment of a Post:

const post = await Post.find(1)
const comment = await post.comments().sync({
  text: 'Awesome post!'
})
GET /posts/1
PUT /posts/1/comments