-
Notifications
You must be signed in to change notification settings - Fork 3
[99] Archive: Implementation Guide 2.x.x
Sven Braune edited this page Mar 24, 2021
·
1 revision
- Add it in your root build.gradle at the end of repositories:
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
}
...
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- Add gradle dependency
compile 'com.github.Kaufland.andcouchbaseentity:couchbase-entity-api:2.2.0'
apt 'com.github.Kaufland.andcouchbaseentity:couchbase-entity:2.2.0'
- Optionally, if you use Couchbase 2.x.x (provides already implemented connector)
compile 'com.github.Kaufland.andcouchbaseentity:couchbase-entity-connector:2.2.0@aar'
Hint: For custom connectors see
- Configure library
- Add the following code in your Application.class
@Override
public void onCreate() {
super.onCreate();
PersistenceConfig.configure(new Couchbase2Connector() {
@Override
protected Database getDatabase(String name) {
if (DB.equals(name)) {
return Application.this.getDatabase();
}
throw new RuntimeException("wrong db name defined!!");
}
});
}
Hint: For other databases implement your own connector
- Annotate classes to generate entities (all generated classes have the suffix Entity or Wrapper (used for child entities or map wrapping)
@Entity(database = Application.DB)
@Fields(
Field(name = "type", type = String::class, defaultValue = "product", readonly = true),
Field(name = "name", type = String::class),
Field("comments", type = UserComment::class, list = true),
Field(name = "image", type = Blob::class),
Field(name = "identifiers", type = String::class, list = true)
)
open class Product
- Use generated classes and be happy :-)
ProductEntity.create().setName("Beer").
setComments(new ArrayList<>(Arrays.asList(UserCommentEntity.create().setComment("very awesome"),
UserCommentEntity.create().setComment("tasty")))).
setImage(new Blob("image/jpeg", getResources().openRawResource(R.raw.ic_kaufland_placeholder))).
save();
Hint: To modify child entities it's neccessary to invoke the setter before saving the parent entity
ArrayList<UserCommentEntity> data = getParentEntity().getComments();
data.remove(0);
try {
getParentEntity().setComments(data).save();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "failed to save Entity", e);
}