Skip to content

Commit 221b3e0

Browse files
fix(datastore): add syncExpression method to configuration builder that takes the modelName as a String (#1330)
1 parent 3fafb85 commit 221b3e0

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

aws-datastore/src/main/java/com/amplifyframework/datastore/DataStoreConfiguration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,23 @@ public Builder syncExpression(@NonNull Class<? extends Model> modelClass,
341341
return Builder.this;
342342
}
343343

344+
/**
345+
* Sets a sync expression for a particular model to filter which data is synced locally. The expression
346+
* is evaluated each time DataStore is started. The QueryPredicate is applied on both sync and subscriptions.
347+
* @param modelName the name of the model for which the filter applies
348+
* @param syncExpression DataStoreSyncExpression that should be used to filter the data that is synced.
349+
* @return Current builder
350+
*/
351+
@NonNull
352+
public Builder syncExpression(@NonNull String modelName,
353+
@NonNull DataStoreSyncExpression syncExpression) {
354+
this.syncExpressions.put(
355+
Objects.requireNonNull(modelName),
356+
Objects.requireNonNull(syncExpression)
357+
);
358+
return Builder.this;
359+
}
360+
344361
private void populateSettingsFromJson() throws DataStoreException {
345362
if (pluginJson == null) {
346363
return;

aws-datastore/src/test/java/com/amplifyframework/datastore/DataStoreConfigurationTest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
import androidx.annotation.NonNull;
1919

20+
import com.amplifyframework.AmplifyException;
2021
import com.amplifyframework.core.Consumer;
2122
import com.amplifyframework.core.model.Model;
23+
import com.amplifyframework.core.model.ModelSchema;
2224
import com.amplifyframework.datastore.DataStoreConfiguration.ConfigKey;
2325
import com.amplifyframework.datastore.DataStoreConflictHandler.AlwaysApplyRemoteHandler;
2426
import com.amplifyframework.testmodels.commentsblog.BlogOwner;
27+
import com.amplifyframework.testmodels.commentsblog.Post;
2528
import com.amplifyframework.testutils.random.RandomString;
2629

2730
import org.json.JSONException;
@@ -31,6 +34,8 @@
3134
import org.robolectric.RobolectricTestRunner;
3235

3336
import java.util.Collections;
37+
import java.util.HashMap;
38+
import java.util.Map;
3439
import java.util.concurrent.TimeUnit;
3540

3641
import static org.junit.Assert.assertEquals;
@@ -91,23 +96,27 @@ public void testDefaultOverriddenFromConfiguration() throws JSONException, DataS
9196
* When building a configuration from both a config file and a configuration object,
9297
* default values should be overridden, and the provided ones shall be used, instead.
9398
* @throws JSONException While arranging config file JSON
94-
* @throws DataStoreException While building DataStoreConfiguration instances via build()
99+
* @throws AmplifyException While building DataStoreConfiguration instances via build(), or when deriving a
100+
* {@link ModelSchema} from a {@link Model} class.
95101
*/
96102
@Test
97-
public void testDefaultOverriddenFromConfigurationAndObject() throws JSONException, DataStoreException {
103+
public void testDefaultOverriddenFromConfigurationAndObject()
104+
throws JSONException, AmplifyException {
98105
long expectedSyncIntervalMinutes = 6L;
99106
Long expectedSyncIntervalMs = TimeUnit.MINUTES.toMillis(expectedSyncIntervalMinutes);
100107
Integer expectedSyncMaxRecords = 3;
101108
DummyConflictHandler dummyConflictHandler = new DummyConflictHandler();
102109
DataStoreErrorHandler errorHandler = DefaultDataStoreErrorHandler.instance();
103110

104-
DataStoreSyncExpression syncExpression = () -> BlogOwner.ID.beginsWith(RandomString.string());
111+
DataStoreSyncExpression ownerSyncExpression = () -> BlogOwner.ID.beginsWith(RandomString.string());
112+
DataStoreSyncExpression postSyncExpression = () -> Post.ID.beginsWith(RandomString.string());
105113
DataStoreConfiguration configObject = DataStoreConfiguration
106114
.builder()
107115
.syncMaxRecords(expectedSyncMaxRecords)
108116
.conflictHandler(dummyConflictHandler)
109117
.errorHandler(errorHandler)
110-
.syncExpression(BlogOwner.class, syncExpression)
118+
.syncExpression(BlogOwner.class, ownerSyncExpression)
119+
.syncExpression("Post", postSyncExpression)
111120
.build();
112121

113122
JSONObject jsonConfigFromFile = new JSONObject()
@@ -123,8 +132,11 @@ public void testDefaultOverriddenFromConfigurationAndObject() throws JSONExcepti
123132

124133
assertEquals(dummyConflictHandler, dataStoreConfiguration.getConflictHandler());
125134
assertEquals(errorHandler, dataStoreConfiguration.getErrorHandler());
126-
assertEquals(Collections.singletonMap(BlogOwner.class.getSimpleName(), syncExpression),
127-
dataStoreConfiguration.getSyncExpressions());
135+
136+
Map<String, DataStoreSyncExpression> expectedSyncExpressions = new HashMap<>();
137+
expectedSyncExpressions.put(BlogOwner.class.getSimpleName(), ownerSyncExpression);
138+
expectedSyncExpressions.put(Post.class.getSimpleName(), postSyncExpression);
139+
assertEquals(expectedSyncExpressions, dataStoreConfiguration.getSyncExpressions());
128140
}
129141

130142
/**

0 commit comments

Comments
 (0)