# Updating data

{% hint style="info" %}
Please see the reserved keyword [`save`](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-data/mandarine-query-language#save-definer) and [Interacting with data](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-data/orm/repositories/interacting-with-data)
{% endhint %}

## Main

Updating data through the use of Mandarine-powered repositories is not hard. All it requires is to know how to save a record in your entity *(Please refer to the links above).*

## Updating Data

In order to update existent data in your tables by using Mandarine-powered repositories, you will be using the same method that is used to save new information. The [`save`](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-data/mandarine-query-language#save-definer) method.

[`save`](https://mandarineframework.gitbook.io/mandarine-ts/mandarine-data/mandarine-query-language#save-definer) takes one argument which will be your model, but unlike when saving new data, your model's primary key cannot be null/undefined. When your model's primary key has a value, Mandarine automatically detects that the model is not meant for saving information but for updating.

## Example

{% tabs %}
{% tab title="Model" %}

```typescript
// usersModel.ts

import { Table, Id, GeneratedValue, Column } from "https://deno.land/x/mandarinets/mod.ts";

@Table({ schema: "public" })
class Users {

    @Id()
    @GeneratedValue({strategy: "SEQUENCE"})
    @Column()
    public id: number;

    @Column()
    public firstname: string;

    @Column()
    public lastname: string;

    @Column()
    public country: string;
}
```

{% endtab %}

{% tab title="Repository" %}

```typescript
// usersRepository.ts

import { Repository, MandarineRepository } from "https://deno.land/x/mandarinets/mod.ts";
import { Users } from "./usersModel.ts";

@Repository()
abstract class UsersRepository extends MandarineRepository<Users> {

    constructor() {
        super(Users);
    }
    
    public findByFirstname(firstname: string) {}
}
```

{% endtab %}

{% tab title="Component Usage" %}

```typescript
import { Component } from "https://deno.land/x/mandarinets/mod.ts";
import { Users } from "./usersModel.ts";
import { UsersRepository } from "./usersRepository.ts";

@Component()
export class MyComponent() {

    constructor(private readonly usersRepo: UsersRepository) {
    }

    public updateBillsCountry() {
        // [0] to select the first record found
        let billUser: Users = await this.usersRepo.findByFirstname("Bill")[0];
        // { id: 1, firstname: "Bill", lastname: "Clark", country: "Canada" }
        
        billUser.country = "Croatia";
        
        // Updating bill's country.
        await this.usersRepo.save(billUser);
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: 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://mandarineframework.gitbook.io/mandarine-ts/v1.1.1/mandarine-data/orm/repositories/updating-data.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.
