# Update Record by ID API

> Quickly update a record by its ID using API Maker—ensuring precise, efficient data modification.

Source: https://docs.apimaker.dev/v1/examples/sys/db/gen/updateByIdGen.html

`g.sys.db.gen.updateByIdGen` calls the [update by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-update-by-id-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Update by id**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    id: 1008,
    primaryKey: "id",
    updateData: {
        "first_name": "Tanmay",
        "last_name": "Samay"
    },
    returnOriginal: true,
    select: "COLUMN_NAMES",
    upsert: false
});
```

## Simple updateById

**Update By Id**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## updateById primaryKey override

**Update By Id primarykey override**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## updateById [select]

**Update By Id with select**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name"
});
```

## updateById [headers]

**Update By Id with select**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## updateById [upsert]

**Update By Id with upsert**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    upsert: true
});
```

## updateById [returnDocument]

**Update By Id with upsert**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    id: "Alice",
    primaryKey: "first_name",
    updateData: {
        "first_name": "MALLORY"
    },
    select: "first_name,last_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    returnDocument: true 
});
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v1/docs/features/multi-tenant.html) instance `crm`.
- With `instance: "crm"`, the call works on the database of the tenant of the request: a custom API called with the [x-am-tenant-username](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html#x-am-tenant-username) header passes it on to the APIs it calls. A request without a tenant works on the structure database.

**Update the customer 1 of the tenant acme**

```typescript
let updateById = await g.sys.db.gen.updateByIdGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## Related

- [Update by id generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-update-by-id-api.html) · [Schema API from code](https://docs.apimaker.dev/v1/examples/sys/db/updateById.html) · [The global object g](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html)
