Giter Site home page Giter Site logo

poetry's Introduction

ARCHIVED

This project is no longer being worked on.

Consider using Room instead.

Poetry

License

Poetry is an Android persistence library that allows you to persist a JSON object tree directly into an SQLite database through OrmLite. Poetry enables you to write less code and persist data much faster.

Poetry is forked from github.com/elastique/poetry and is now maintained here, by the original developer.

Consider this JSON:

{
	"id" : 1,
	"name" : "John Doe"
}

And this Java model:

@DatabaseTable
class User
{
	@DatabaseField(id = true)
	public int id;

	@DatabaseField)
	public String name;
}

They can be stored into the database like this:

JSONObject json_object = ...; // processed JSON tree
DatabaseHelper helper = ...; // OrmLite databasehelper;
JsonPersister persister = new JsonPersister(helper.getWritableDatabase());
persister.persistObject(User.class, json_object);

Features

  • Annotation-based model configuration
  • Advanced DatabaseHelper with easy DatabaseConfiguration
  • Support for relationships:
    • One-to-one
    • Many-to-one
    • One-to-many
    • Many-to-many
  • Persist objects and arrays of objects
  • Persist objects with arrays of base types (e.g. JSON String array persisted to separate table)

Requirements

  • Android 4.1 (API level 16) or higher

Usage

build.gradle

repositories {
    jcenter()
}
dependencies {
    compile (
        [group: 'com.bytewelder.poetry', name: 'poetry', version: '4.0.0']
    )
}

Behaviors

  • Models that are imported more than once within a single JSON tree are updated every time they are processed.
  • Attributes that are not specified in JSON are not updated
  • Attributes that are imported with null value will have a null value in the database. Make sure your model allows this.
  • When you use JsonPersister's persistArray() method, it will import the array and delete all objects from the database that do not correspond to any of the imported IDs.

Tutorial

Creating a DatabaseHelper

The Poetry DatabaseHelper allows you to easily configure your database. In the example below, you can see a custom DatabaseHelper with a DatabaseConfiguration that holds the model version and model classes.

MyDatabaseHelper.java

import android.database.sqlite.SQLiteDatabase;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.support.ConnectionSource;
import poetry.database.DatabaseConfiguration;

public class MyDatabaseHelper extends poetry.database.DatabaseHelper
{
    public final static DatabaseConfiguration sConfiguration = new DatabaseConfiguration(1, new Class<?>[]
    {
        User.class,
        Group.class,
        UserTag.class,
        UserGroup.class
    });

    public DatabaseHelper(Context context)
    {
        super(context, sConfiguration);
    }

    public static DatabaseHelper getHelper(Context context)
    {
        return OpenHelperManager.getHelper(context, DatabaseHelper.class);
    }
}

Mapping custom JSON properties

By default, the name of the attribute is used to map from JSON. Your naming conventions might now allow this. You can specify the json key name with the @MapFrom annotation.

User.java

@DatabaseTable
class User
{
	@DatabaseField(id = true, columnName = "id")
	@MapFrom("id")
    private int mId;

	@DatabaseField(columnName = "name")
	@MapFrom("name")
    private String mName;
}

One-to-many relationships

In this example, a Game object holds a list of Player objects.

game.json

{
    "id": 1,
    "players" : [
        {
	        "id": 1,
	        "name": "John"
        },
        {
	        "id": 2,
	        "name": "Jane"
        }
    ]
}

Game.java

@DatabaseTable
public class Game
{
    @DatabaseField(id = true)
    public int id;
    
	@ForeignCollectionField(eager = true)
	public ForeignCollection<Player> players;
}

Player.java

@DatabaseTable
public class Player
{
    @DatabaseField(id = true)
    public int id;

    @DatabaseField
    public String name;
}

Many-to-many relationships

In this example, a User can have 0 or more Groups, and a Group can have 0 or more Users.

users.json

[
	{
	    "id" : 1,
	    "name" : "John",
	    "groups" : [
		    {
			    "id" : 1,
			    "name" : "Group 1"
		    },
		    {
			    "id" : 2,
			    "name" : "Group 2"
		    }
	    ]
	},
	{
	    "id" : 2,
	    "name" : "Jane",
	    "groups" : [
		    {
			    "id" : 1,
			    "name" : "Group 1"
		    }
	    ]
	}
]

User.java

@DatabaseTable
public class User
{
    @DatabaseField(id = true)
    public int id;

    @DatabaseField
    public String name;
    
    /**
     * Many-to-many relationships.
     *
     * OrmLite requires a ForeignCollectionField with the helper-type UserGroup to assist in the database relational mapping.
     * JSON-to-SQLite persistence also requires the additional annotation "ManyToManyField"
     */
    @ForeignCollectionField(eager = true)
    @ManyToManyField(targetType = Group.class)
	public ForeignCollection<UserGroup> groups;
}

UserGroup.java

@DatabaseTable
public class UserGroup
{
    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField(foreign = true)
	public User user;

    @DatabaseField(foreign = true)
    public Group group;
}

Group.java

@DatabaseTable
public class Group
{
    @DatabaseField(id = true)
    public int id;

    @DatabaseField
    public String name;
}

One-to-one relationships

In this example, a User can have 0 or 1 Friend user.

users.json

[
	{
	    "id": 1,
	    "name" : "John",
	    "friend" : { "id" : 2 }
	},
	{
	    "id": 2,
	    "name" : "Jane",
	    "friend" : { "id" : 1 }
	}
]

The following alternative JSON is also valid:

users.json

[
	{
	    "id": 1,
	    "name" : "John",
	    "friend" : 2
	},
	{
	    "id": 2,
	    "name" : "Jane",
	    "friend" : 1
	}
]

User.java

@DatabaseTable
public class User
{
    @DatabaseField(id = true)
    public int id;

    @DatabaseField
    public String name;
    
    @DatabaseField(foreign = true)
    public User friend;
}

Arrays of base types

Arrays of base types work the same as one-to-many relationships. The only difference is that you have to define a model that holds the base types and use the @ForeignCollectionFieldSingleTarget annotation to specify the database column name that holds the value.

user.json

{
    "id": 1,
    "tags" : [
        "tag1",
        "tag2"
    ]
}

User.java

@DatabaseTable
public class User
{
    @DatabaseField(id = true)
    public int id;
    
	// The targetField refers to the table's column name
	@ForeignCollectionField(eager = true)
	@ForeignCollectionFieldSingleTarget(targetField = "value")
	@MapFrom("tags")
	public ForeignCollection<UserTag> userTags;
}

UserTag.java

@DatabaseTable
public class UserTag
{
    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField(foreign = true)
    public User user;

    @DatabaseField
	public String value;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.