Skip to content

Instantly share code, notes, and snippets.

Avatar
🔱

Akshay Chordiya AkshayChordiya

🔱
View GitHub Profile
@AkshayChordiya
AkshayChordiya / User.kt
Created Dec 9, 2017
User entity with multiple index
View User.kt
@Entity(tableName = "users", indices = arrayOf(Index("full_name"), Index("phone_number")))
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
@ColumnInfo(name = "full_name")
var name: String = "",
@ColumnInfo(name = "phone_number")
var phoneNumber: String = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created Dec 9, 2017
User entity with single index
View User.kt
@Entity(tableName = "users", indices = arrayOf(Index("full_name")))
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
@ColumnInfo(name = "full_name")
var name: String = ""
)
@AkshayChordiya
AkshayChordiya / UserDao.kt
Created Oct 22, 2017
User DAO with Address - Room
View UserDao.kt
@Query("SELECT users.*, city FROM users JOIN addresses ON users.address_id = addresses.id")
fun getUsersWithCity(): LiveData<List<UserWithAddress>>
View UserWithAddress.kt
data class UserWithAddress(
@Embedded
var user: User,
@ColumnInfo(name = "city")
var city: String? = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created Oct 22, 2017
User entity for relation - Room
View User.kt
@Entity(tableName = "users", , foreignKeys = arrayOf(
ForeignKey(entity = Address::class, parentColumns = arrayOf("id"), childColumns = arrayOf("address_id"))
)
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var name: String = "",
@ColumnInfo(name = "address_id")
var address: Long? = null,
)
@AkshayChordiya
AkshayChordiya / Address.kt
Created Oct 22, 2017
Address entity for relation - Room
View Address.kt
@Entity(tableName = "addresses")
data class Address(
@PrimaryKey
var id: Long = 0,
var state: String = "",
var city: String = ""
)
View Address.kt
data class Address(
var state: String = "",
var city: String = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created Oct 22, 2017
User entity with embedded address
View User.kt
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var name: String = "",
@Embedded
var address: Address
)
@AkshayChordiya
AkshayChordiya / Init.kt
Last active Oct 22, 2017
Create the database instance - Room
View Init.kt
// Get instance
database = Room.databaseBuilder(context, AppDatabase::class.java, "app.db")
.build();
// Example Usage
val userDao = database.userDao()
View AppDatabase.kt
@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}