How to update only the not null fields?

Hi! (sorry for my bad English)

I’m working in a REST API following a tutorial, but I found a problem.

When I try to update a register in the DB I use this code.

async update ({ params, request, response, auth }) {
    const user = await auth.getUser();
    const { title, description, tags } = request.all();
    const { id } = params;
    const project = await Project(id);
    CheckResourceService.verifyResourceExists(project);
    project({
      title,
      description,
      tags
    });
    await project.save();
    return project;
  }

Well the project is updated perfectly but I noticed that if in the body of postman I don’t send all the fields “title, description and tags” the fields that I don’t send becomes disappears, but I don’t want this, I just want that adonis updates only the fields that really are in the resources.

for example if I have this stored in the DB

{
    "id": 1,
    "created_by": 1,
    "title": "Projecto Prueba",
    "description": "Descripción de prueba",
    "tags": "999, 000, 000",
    "created_at": "2019-07-16 20:37:49",
    "updated_at": "2019-07-28 20:16:50"
}

And I send this JSON to update in postman

{
	"title": "Proyecto Prueba editado",
	"description": "Descripción editada",
}

I receive this project from the DB after I updated and if you can see the tags are disappear

{
    "id": 1,
    "created_by": 1,
    "title": "Proyecto editado",
    "description": "Descripción editada",
    "created_at": "2019-07-16 20:37:49",
    "updated_at": "2019-07-28 20:18:51"
}

If I send all the fields all goes good because adonis update all the fields with the new and old values, but I like to keep the old values if I don’t send those in the JSON.

Hope you can understand my question.
Thanks!

Hey @creator! :wave:

If I correctly understand, what you want is a partial updates. Meaning you update only what you receive instead of rewriting the whole resource.

You could do that by:

  1. Fetching the resource from your database
  2. Merging the data you have from the request
  3. Saving the resource