# Replace by ID

> Replace a record in a collection with a new document based on its unique identifier using API Maker's g.sys.db.replaceById method.

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

`g.sys.db.replaceById` calls the [replace by id schema API](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-replace-by-id-api.html) of any table from your code, with the same headers and query parameters as over HTTP. It returns the data, or the whole envelope when `true` is passed as the second argument.

## Replace By Id
```typescript
let replaceById = await g.sys.db.replaceById({
    instance: "mysql_8", database: "inventory", collection: "customers",
    id: 1,
    updateData: {
        "first_name": "MALLORY"
    }
});
```

## Replace By Id [primaryKey] override

**Replace By Id primary key override**

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

## Replace By Id [header]

**Replace By Id header**

```typescript
let replaceById = await g.sys.db.replaceById({
    instance: "mysql_8", database: "inventory", collection: "customers",
    id: 606,
    updateData: {
        "first_name": "MALLORY"
    },
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
});
```

## Replace By Id [upsert]

**Replace By Id upsert**

```typescript
let replaceById = await g.sys.db.replaceById({
    instance: "mysql_8", database: "inventory", collection: "customers",
    id: 505,
    updateData: {
        "first_name": "MALLORY"
    },
    upsert: true
});
```

## Replace By Id [returnDocument]

**Replace By Id returnDocument**

```typescript
let replaceById = await g.sys.db.replaceById({
    instance: "mysql_8", database: "inventory", collection: "customers",
    id: 80,
    updateData: {
        "first_name": "MALLORY"
    },
    returnDocument: 'before', // return old doc if value 'before'. Supported values are 'before' | 'after'.
});
```

## Structure

**Replace by id with all params**

```typescript
let replaceById = await g.sys.db.replaceById({
    instance: "mysql_8", database: "inventory", collection: "customers",
    id: 45855,
    primaryKey: "pincode",
    updateData: {
        "first_name": "Elonn",
        "last_name": "Mask"
    },
    returnDocument: 'before', // return old doc if value 'before'. Supported values are 'before' | 'after'.
    select: "COMMA_SEPARATED_COLUMN_NAMES",
    upsert: false,
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        "x-am-response-object-type": "make_flat", // no_action | make_flat
        "x-am-meta": "true", // true, false
        "x-am-internationalization": "USER_I18N_ID",
        "x-am-run-in-sandbox": "2",
        "x-am-content-type-response": "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        "x-am-cache-control": "reset_cache", // no_action | reset_cache
        "x-am-get-encrypted-data": "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        "x-am-sandbox-timeout": "13000",
        "x-no-compression": "true", // true | false
        "x-am-encrypted-payload": "true", // user will send 'true', when payload is encrypted for transfer
        // Multi-tenant : the tenant of the request, each instance finds it in its own tenants table
        "x-am-tenant-username": "TENANT_USERNAME", // ex: acme
        // Authorization headers
        "x-am-authorization": "AUTHORIZATION_TOKEN",
        "x-am-user-authorization": "API_USER_TOKEN",
        "x-aws-authorization": "AWS_TOKEN",
        "x-google-authorization": "GOOGLE_TOKEN",
        "x-azure-authorization": "AZURE_TOKEN",
    },
});
```

## 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.

**Replace the customer 1 of the tenant acme**

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

## Related

- [Replace by id API](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-replace-by-id-api.html) · [Generated API from code](https://docs.apimaker.dev/v1/examples/sys/db/gen/replaceByIdGen.html) · [The global object g](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html)
