How to only update specific attribute of a record in DataStore amplify

55 views Asked by At

This is my method for adding/updating a record on my Configuration table

public static void saveConfiguration(Configuration model) {
    Amplify.DataStore.save(model,
                success-> logEvent("Configuration added to DataStore"),
                failure -> logErrorEvent("Failed to add configuration in DataStore")
    );
}

Amplify Model/Schema:

type Configuration @model @auth(rules: [{allow: owner}]) {
id: ID!
name: String
email: String
companyName: String
}

For example, I have a record:

id name email companyName
12345 Name A [email protected] Company A

To update this, I will construct a Configuration model and call saveConfiguration method:

Configuration amplifyConfiguration = Configuration.builder()
            .name("Name B")
            .email("[email protected]")
            .companyName("Company B")
            .id("12345") // primary key
            .build();

saveConfiguration(amplifyConfiguration);

The record will be updated to:

id name email companyName
12345 Name B [email protected] Company B

For some specific scenario, what I want is when I construct a Configuration model with updated name only for example, it will only update that column.

// updated name only
Configuration amplifyConfiguration = Configuration.builder()
            .name("Name C") 
            .id("12345") // primary key
            .build();

saveConfiguration(amplifyConfiguration);

I need the record to be like this:

id name email companyName
12345 Name C [email protected] Company A

Actual result:

id name email companyName
12345 Name C

email and companyName is empty since I didn't set any value.

0

There are 0 answers