# 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/v4/examples/sys/db/replaceById.html

> **The code in every language**
>
> Each example is the inside of `main`, written in every [language](https://docs.apimaker.dev/v4/docs/languages/index.html) API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (`GetAll`, `DeepItem`…) are those of `am` : `from am import GetAll` in Python, `use am::GetAll;` in Rust, `import am.GetAll;` in Java and Kotlin ; PHP and C# have them without an import, Go names them `am.GetAll`.

`g.sys.db.replaceById` calls the [replace by id schema API](https://docs.apimaker.dev/v4/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. The whole envelope (`success`, `statusCode`, `data`, `totalCount`, `errors`…) comes back when `true` is passed as the second argument in TypeScript, and through `full` in the other languages : `g.sys.db.full` (`g.Sys.Db.Full` in C# and Go, `$g->sys->db->full` in PHP).

## Replace By Id

**TypeScript**

**Replace By Id**

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

**Python**

**Replace By Id**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    1,
    update_data={
        'first_name': 'MALLORY',
    },
)
```

**PHP**

**Replace By Id**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    1,
    updateData: [
        'first_name' => 'MALLORY',
    ],
);
```

**C#**

**Replace By Id**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", 1)
{
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
    },
});
```

**Go**

**Replace By Id**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id: 1,
	UpdateData: am.M{
		"first_name": "MALLORY",
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace By Id**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", json!(1))
        .update_data(json!({
            "first_name": "MALLORY",
        })),
)?;
```

**Java**

**Replace By Id**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", 1)
    .updateData(Map.of(
        "first_name", "MALLORY"
    )));
```

**Kotlin**

**Replace By Id**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", 1)
        .updateData(mapOf(
            "first_name" to "MALLORY",
        )),
)
```

## Replace By Id [primaryKey] override

**TypeScript**

**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"
    }
});
```

**Python**

**Replace By Id primary key override**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    'Bob',
    primary_key='first_name',
    update_data={
        'first_name': 'MALLORY',
    },
)
```

**PHP**

**Replace By Id primary key override**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    'Bob',
    primaryKey: 'first_name',
    updateData: [
        'first_name' => 'MALLORY',
    ],
);
```

**C#**

**Replace By Id primary key override**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", "Bob")
{
    PrimaryKey = "first_name",
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
    },
});
```

**Go**

**Replace By Id primary key override**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id:         "Bob",
	PrimaryKey: "first_name",
	UpdateData: am.M{
		"first_name": "MALLORY",
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace By Id primary key override**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", "Bob")
        .primary_key("first_name")
        .update_data(json!({
            "first_name": "MALLORY",
        })),
)?;
```

**Java**

**Replace By Id primary key override**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", "Bob")
    .primaryKey("first_name")
    .updateData(Map.of(
        "first_name", "MALLORY"
    )));
```

**Kotlin**

**Replace By Id primary key override**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", "Bob")
        .primaryKey("first_name")
        .updateData(mapOf(
            "first_name" to "MALLORY",
        )),
)
```

## Replace By Id [header]

**TypeScript**

**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
    },
});
```

**Python**

**Replace By Id header**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    606,
    update_data={
        'first_name': 'MALLORY',
    },
    headers={
        'x-am-response-case': 'capitalCase',  # noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
)
```

**PHP**

**Replace By Id header**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    606,
    updateData: [
        'first_name' => 'MALLORY',
    ],
    headers: [
        'x-am-response-case' => 'capitalCase', // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    ],
);
```

**C#**

**Replace By Id header**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", 606)
{
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
    },
    Headers = new JsonObject
    {
        ["x-am-response-case"] = "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
});
```

**Go**

**Replace By Id header**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id: 606,
	UpdateData: am.M{
		"first_name": "MALLORY",
	},
	Headers: am.M{
		"x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace By Id header**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", json!(606))
        .update_data(json!({
            "first_name": "MALLORY",
        }))
        .headers(json!({
            "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        })),
)?;
```

**Java**

**Replace By Id header**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", 606)
    .updateData(Map.of(
        "first_name", "MALLORY"
    ))
    .headers(Map.of(
        "x-am-response-case", "capitalCase" // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    )));
```

**Kotlin**

**Replace By Id header**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", 606)
        .updateData(mapOf(
            "first_name" to "MALLORY",
        ))
        .headers(mapOf(
            "x-am-response-case" to "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        )),
)
```

## Replace By Id [upsert]

**TypeScript**

**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
});
```

**Python**

**Replace By Id upsert**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    505,
    update_data={
        'first_name': 'MALLORY',
    },
    upsert=True,
)
```

**PHP**

**Replace By Id upsert**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    505,
    updateData: [
        'first_name' => 'MALLORY',
    ],
    upsert: true,
);
```

**C#**

**Replace By Id upsert**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", 505)
{
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
    },
    Upsert = true,
});
```

**Go**

**Replace By Id upsert**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id: 505,
	UpdateData: am.M{
		"first_name": "MALLORY",
	},
	Upsert: true,
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace By Id upsert**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", json!(505))
        .update_data(json!({
            "first_name": "MALLORY",
        }))
        .upsert(true),
)?;
```

**Java**

**Replace By Id upsert**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", 505)
    .updateData(Map.of(
        "first_name", "MALLORY"
    ))
    .upsert(true));
```

**Kotlin**

**Replace By Id upsert**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", 505)
        .updateData(mapOf(
            "first_name" to "MALLORY",
        ))
        .upsert(true),
)
```

## Replace By Id [returnDocument]

**TypeScript**

**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'.
});
```

**Python**

**Replace By Id returnDocument**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    80,
    update_data={
        'first_name': 'MALLORY',
    },
    return_document='before',  # return old doc if value 'before'. Supported values are 'before' | 'after'.
)
```

**PHP**

**Replace By Id returnDocument**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    80,
    updateData: [
        'first_name' => 'MALLORY',
    ],
    returnDocument: 'before', // return old doc if value 'before'. Supported values are 'before' | 'after'.
);
```

**C#**

**Replace By Id returnDocument**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", 80)
{
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
    },
    ReturnDocument = "before", // return old doc if value 'before'. Supported values are 'before' | 'after'.
});
```

**Go**

**Replace By Id returnDocument**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id: 80,
	UpdateData: am.M{
		"first_name": "MALLORY",
	},
	ReturnDocument: "before", // return old doc if value 'before'. Supported values are 'before' | 'after'.
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace By Id returnDocument**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", json!(80))
        .update_data(json!({
            "first_name": "MALLORY",
        }))
        .return_document("before"), // return old doc if value 'before'. Supported values are 'before' | 'after'.
)?;
```

**Java**

**Replace By Id returnDocument**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", 80)
    .updateData(Map.of(
        "first_name", "MALLORY"
    ))
    .returnDocument("before")); // return old doc if value 'before'. Supported values are 'before' | 'after'.
```

**Kotlin**

**Replace By Id returnDocument**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", 80)
        .updateData(mapOf(
            "first_name" to "MALLORY",
        ))
        .returnDocument("before"), // return old doc if value 'before'. Supported values are 'before' | 'after'.
)
```

## Structure

**TypeScript**

**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",
    },
});
```

**Python**

**Replace by id with all params**

```python
replace_by_id = await g.sys.db.replace_by_id(
    'mysql_8', 'inventory', 'customers',
    45855,
    primary_key='pincode',
    update_data={
        'first_name': 'Elonn',
        'last_name': 'Mask',
    },
    return_document='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',
    },
)
```

**PHP**

**Replace by id with all params**

```php
$replaceById = $g->sys->db->replaceById(
    'mysql_8', 'inventory', 'customers',
    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',
    ],
);
```

**C#**

**Replace by id with all params**

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("mysql_8", "inventory", "customers", 45855)
{
    PrimaryKey = "pincode",
    UpdateData = new JsonObject
    {
        ["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 = new JsonObject
    {
        ["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",
    },
});
```

**Go**

**Replace by id with all params**

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "mysql_8", Database: "inventory", Collection: "customers",
	Id:         45855,
	PrimaryKey: "pincode",
	UpdateData: am.M{
		"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: am.M{
		"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",
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

**Replace by id with all params**

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("mysql_8", "inventory", "customers", json!(45855))
        .primary_key("pincode")
        .update_data(json!({
            "first_name": "Elonn",
            "last_name": "Mask",
        }))
        .return_document("before") // return old doc if value 'before'. Supported values are 'before' | 'after'.
        .select("COMMA_SEPARATED_COLUMN_NAMES")
        .upsert(false)
        .headers(json!({
            "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",
        })),
)?;
```

**Java**

**Replace by id with all params**

```java
var replaceById = g.sys.db.replaceById(new UpdateById("mysql_8", "inventory", "customers", 45855)
    .primaryKey("pincode")
    .updateData(Map.of(
        "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(JsonObject.of(
        "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"
    )));
```

**Kotlin**

**Replace by id with all params**

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("mysql_8", "inventory", "customers", 45855)
        .primaryKey("pincode")
        .updateData(mapOf(
            "first_name" to "Elonn",
            "last_name" to "Mask",
        ))
        .returnDocument("before") // return old doc if value 'before'. Supported values are 'before' | 'after'.
        .select("COMMA_SEPARATED_COLUMN_NAMES")
        .upsert(false)
        .headers(mapOf(
            "x-am-response-case" to "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
            "x-am-response-object-type" to "make_flat", // no_action | make_flat
            "x-am-meta" to "true", // true, false
            "x-am-internationalization" to "USER_I18N_ID",
            "x-am-run-in-sandbox" to "2",
            "x-am-content-type-response" to "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
            "x-am-cache-control" to "reset_cache", // no_action | reset_cache
            "x-am-get-encrypted-data" to "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
            "x-am-sandbox-timeout" to "13000",
            "x-no-compression" to "true", // true | false
            "x-am-encrypted-payload" to "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" to "TENANT_USERNAME", // ex: acme
            // Authorization headers
            "x-am-authorization" to "AUTHORIZATION_TOKEN",
            "x-am-user-authorization" to "API_USER_TOKEN",
            "x-aws-authorization" to "AWS_TOKEN",
            "x-google-authorization" to "GOOGLE_TOKEN",
            "x-azure-authorization" to "AZURE_TOKEN",
        )),
)
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v4/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/v4/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.

**TypeScript**

**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"
    }
});
```

**Python**

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

```python
replace_by_id = await g.sys.db.replace_by_id(
    'crm::acme', 'crm', 'customers',
    1,
    update_data={
        'first_name': 'MALLORY',
        'last_name': 'Lin',
    },
)
```

**PHP**

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

```php
$replaceById = $g->sys->db->replaceById(
    'crm::acme', 'crm', 'customers',
    1,
    updateData: [
        'first_name' => 'MALLORY',
        'last_name' => 'Lin',
    ],
);
```

**C#**

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

```csharp
var replaceById = await g.Sys.Db.ReplaceById(new UpdateById("crm::acme", "crm", "customers", 1)
{
    UpdateData = new JsonObject
    {
        ["first_name"] = "MALLORY",
        ["last_name"] = "Lin",
    },
});
```

**Go**

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

```go
replaceById, err := g.Sys.Db.ReplaceById(am.UpdateById{
	Instance: "crm::acme", Database: "crm", Collection: "customers",
	Id: 1,
	UpdateData: am.M{
		"first_name": "MALLORY",
		"last_name":  "Lin",
	},
})
if err != nil {
	return nil, err
}
```

**Rust**

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

```rust
let replace_by_id = g.sys.db.replace_by_id(
    UpdateById::new("crm::acme", "crm", "customers", json!(1))
        .update_data(json!({
            "first_name": "MALLORY",
            "last_name": "Lin",
        })),
)?;
```

**Java**

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

```java
var replaceById = g.sys.db.replaceById(new UpdateById("crm::acme", "crm", "customers", 1)
    .updateData(Map.of(
        "first_name", "MALLORY",
        "last_name", "Lin"
    )));
```

**Kotlin**

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

```kotlin
val replaceById = g.sys.db.replaceById(
    UpdateById("crm::acme", "crm", "customers", 1)
        .updateData(mapOf(
            "first_name" to "MALLORY",
            "last_name" to "Lin",
        )),
)
```

## Without a schema

`g.sys.db.gen.replaceByIdGen` takes the same arguments and calls the [replace by id generated API](https://docs.apimaker.dev/v4/docs/apis-all/schema-apis/auto-generated-schema-based-replace-by-id-api.html#without-a-schema) : no schema, so the data is read and written as it is in the table, without the conversions, validations and defaults of the schema.

| Language | The call |
|---|---|
| TypeScript | `g.sys.db.gen.replaceByIdGen(…)` |
| Python | `g.sys.db.gen.replace_by_id_gen(…)` |
| PHP | `$g->sys->db->gen->replaceByIdGen(…)` |
| C#, Go | `g.Sys.Db.Gen.ReplaceByIdGen(…)` |
| Rust | `g.sys.db.replace_by_id_gen(UpdateById::new(…))` |
| Java, Kotlin | `g.sys.db.gen.replaceByIdGen(…)` |

## Related

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