> For the complete documentation index, see [llms.txt](https://mandarineframework.gitbook.io/mandarine-ts/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mandarineframework.gitbook.io/mandarine-ts/v1.1.1/mandarine-data/orm/repositories/updating-data.md).

# 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 %}
