> For the complete documentation index, see [llms.txt](https://sebloix.gitbook.io/gstore-node/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sebloix.gitbook.io/gstore-node/v5.x+v6.x/model/methods/update-method.md).

# UPDATE

This method allows you to **partially** update (providing a few properties) or completely **replace** an entity in the Datastore.\
It will first fetch the entity, then update its data with the new ones and finally save the data back to the Datastore. The whole operation occurs inside a [Datastore transaction](https://googleapis.dev/nodejs/datastore/latest/Transaction.html).\
\
**Note:** This method is different than using \`Entity.save(null, { method: 'update' })\` (which correspond to the [datastore update() method](https://googleapis.dev/nodejs/datastore/latest/Datastore.html#update)).&#x20;

This method accepts the following arguments:

```javascript
MyModel.update(
    /* {int|string}. -- the id of the entity to update */
    <id>,
    /* {object} -- the data to update */
    <data>,
    /* {Array} -- optional. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>,
    /* {Transaction} -- optional. The transaction currently in progress */
    <transaction>,
    /* {object} -- optional. Additional config */
    <options>
)
```

**@Returns** -- an entity **instance**.

Example

```javascript
const BlogPost = require('./blog-post.model');

const blogPostData = {
    title : 'New title'
};

BlogPost.update(123, blogPostData).then((entity) => {
    console.log(entity.plain());
});

// with *ancestors* and a *namespace*
BlogPost.update(123, data, ['Grandpa', 123, 'Dad', 123], 'dev.namespace.com')
        .then((entity) => {
            console.log(entity);
        });

// from inside a Transaction
const transaction = gstore.transaction();
transaction.run().then(async () => {
    await BlogPost.update(123, data, null, null, transaction);

    transaction.commit().then(() => { ... });
});
```

## Options

**options** properties

* *dataloader* (a Dataloader instance)
* *replace* (Boolean. default: false)

**dataloader** The **dataloader** instance must be created on *each* request. [Read the documentation](/gstore-node/v5.x+v6.x/cache-dataloader/dataloader.md) for more information on this.\
The dataloader instance will be added to the entity instance being saved. This means that it is available in your "pre" save middlewares (attached on the "this" scope (`const dataloader = this.dataloader;`))

```javascript
const { instances } = require('gstore-node');

const gstore = instances.get('default');

// Important! This should be done on **each** request (read the Dataloader documentation)
const dataloader = gstore.createDataLoader();
BlogPost.update(123, data, null, null, null, { dataloader }).then( ... );
```

**replace** If you set it to *true* gstore will replace the the entity data in the Datastore without merging the data first. By default, `MyModel.update()` does **2 operations** inside one transaction:

* get() the entity and merges its data with the new data
* save() the entity

If you just want to override the entity data without doing any merge set *replace* to **true** in the options parameter.

```javascript
BlogPost.update(123, data, null, null, null, { replace:true }).then( ... );

// which is the same as doing

const blogPost = new BlogPost(data, 123);
blogPost.save().then( ... );
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sebloix.gitbook.io/gstore-node/v5.x+v6.x/model/methods/update-method.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
