AbstractDynamoDao extends AbstractDao<M,P,K,I> { protected boolean checkUniqueField(String indexName, String field, Object value, K excludeId) { Index index = getTable().getIndex(indexName); QuerySpec spec = new QuerySpec() .withKeyConditionExpression("#field = :value") .withNameMap(new NameMap() .with("#field", field) ) .withValueMap(new ValueMap() .with(":value", value) ); Object excludeIdValue = toStorageValue(excludeId); for (Item item : index.query(spec)) { Object itemId = toStorageValue(item.get(getFieldData().getId().getStorageName())); if (!itemId.equals(excludeIdValue)) { return false; } } return true; } AbstractDynamoDao(Class<M> modelClass, Class<P> partialClass, AmazonDynamoDB db, String tableName);  AbstractDynamoDao(Class<M> modelClass, Class<P> partialClass, AmazonDynamoDB db, String tableName, ItemMapper<M> modelMapper, ItemMapper<P> partialMapper); String getTableName(); Table getTable(); Optional<M> loadOpt(K id); Optional<P> loadOpt(K id, Iterable<String> fields); @Override Iterable<M> iterateAll(); Results<M,K> scanAll(); @Override M create(P partial, Instant createAt, I creatorId); void update(K id, Update<P> update, Instant updateAt, I updaterId); M updateAndReturn(K id, Update<P> update, Instant updateAt, I updaterId); Update<P> updateOf(P partial); Update<P> updateOf(P partial, Iterable<String> removeFields); @Override void delete(K id); abstract List<KeySchemaElement> getKeySchema(); abstract List<AttributeDefinition> getAttributeDefinitions(); List<GlobalSecondaryIndex> getGlobalSecondaryIndexes(); void initTable(); void dropTable(); void copyTable(AmazonDynamoDB sourceDb, String sourceTable); void ensureIndexes();  }
@Test public void testCheckUniqueField() throws Exception { populateSampleData(); assertTrue(dao.checkUniqueField("email", "email", "new@example.com", null)); assertFalse(dao.checkUniqueField("email", "email", "bob@example.com", null)); TestModel bob = dao.loadByIndex("email", "email", "bob@example.com"); assertTrue(dao.checkUniqueField("email", "email", "bob@exmaple.com", bob.getId())); }