# Master Save/Update

> Perform save or update operations on single or multiple records with master-level control using API Maker.

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

`g.sys.db.masterSave` calls the [master save schema API](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-master-save-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.

## master save single data

**Master save**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "mysql_8", database: "inventory", collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

## master save multiple data

**Master save**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "mysql_8", database: "inventory", collection: "customers",
    saveData: [
        {
            "first_name": "Bob",
            "last_name": "Lin"
        },
        {
            "first_name": "Bob",
            "last_name": "Lin"
        }
    ]
});
```

## with header

**Master save with header**

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

## multiple database entry

**Master save with multy data[with help of schema configuration]**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "mysql_8", database: "inventory", collection: "customers",
    saveData: {
        "city_name": "Wembley", //city_name save in mysql database
        "state_id": {
            "state_name": "London", //state_name save in maria database
            "country_id": {
                "country_name":"UK" //country_name save in postGreSql database
            }
        }
    },
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
});
```

## multiple database multiple entry

**Master save with multy data[with help of schema configuration]**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "mysql_8", database: "inventory", collection: "customers",
    saveData: [{
        "city_name": "Wembley",
        "state_id": {
            "state_name": "London",
            "country_id": 302
        }
    },
    {
        "city_name": "Buffalo",
        "state_id": {
            "state_name": "Chicago",
            "country_id": 303
        }
    }],
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
    },
});
```

## Structure

**Master save**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    saveData: {
        "COLUMN_NAME": "VALUE",
        "COLUMN_NAME2": "VALUE",
    },
    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.

**Master save a customer of the tenant acme**

```typescript
let masterSave = await g.sys.db.masterSave({
    instance: "crm::acme", database: "crm", collection: "customers",
    saveData: {
        "first_name": "Bob",
        "last_name": "Lin"
    }
});
```

## Related

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