Code Monkey home page Code Monkey logo

realmfieldnameshelper's Introduction

Realm Field Names Helper

This library auto-generate helper classes that can help make Realm queries more type safe.

For each Realm model class a corresponding <class>Fields class is created with static references to all queryable field names.

Installation

Just include the following dependency in your gradle.build file

// Android Java projects
annotationProcessor 'dk.ilios:realmfieldnameshelper:1.1.1'

// Android Kotlin projects
kapt 'dk.ilios:realmfieldnameshelper:1.1.1'

This library is compatible with Realm 1.1.1 and onwards.

Plugin order with Kotlin

When combining Realm and Kotlin, the order of plugins matter in order for everything to work correctly. Plugins should be applied in the following order:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

dependencies {
    kapt 'dk.ilios:realmfieldnameshelper:1.1.1'
}

Usage

The library adds an annotation processor that automatically detects all Realm model classes and generated an extra class called <className>Fields. This class will contain static references to all field names that can be queried.

// Standard Realm Model class
public class Person extends RealmObject {
    private String name;
    private boolean hasDogs; // camel case naming gets converted to uppercase separated by "_"
    private boolean mHasCats; // Hungarian notation is evil but support for m starting prefix.
    private boolean has_fish; // fields already using "_" are just converted as they are.
    private Dog favoriteDog; // linked fields are generated one link away
    private RealmList<Dog> dogs; // linked fields are generated one link away

    @Ignore
    private int random;

    // Getters and setters ...
}

public class Dog extends RealmObject {
    private String name;

    // Getters and setters ...
}


// This class is automatically generated by this library
public class PersonFields {
    public static final String NAME = "name";
    public static final String HAS_DOGS = "hasDogs";
    public static final String HAS_CATS = "mHasCats";
    public static final String HAS_FISH = "has_fish";
    public static final class FAVORITE_DOG {
        public static final String $ = "favoriteDog"; // field name in parent object
        public static final String NAME = "favoriteDog.name";
    }
    public static final class DOGS {
        public static final String $ = "dogs"; // field name in parent object
        public static final String NAME = "dogs.name";
    }
}

// And can be used when creating queries
Realm realm = Realm.getDefaultInstance();
RealmResults<Person> results = realm.where(Person.class)
                                    .equalTo(PersonFields.NAME, "John")
                                    .findAll();

RealmResults<Person> results = realm.where(Person.class)
                                    .equalTo(PersonFields.FAVORITE_DOG.NAME, "Fido")
                                    .findAll();

You can also see an example here.

About this library

Even though I am a contributor to Realm, this project is not officially affiliated with Realm and was created purely to scratch an itch in a side project I was working on.

Use it with that it mind.

realmfieldnameshelper's People

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.