objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.41k stars 302 forks source link

Additional Kotlin extensions #446

Open greenrobot-team opened 6 years ago

greenrobot-team commented 6 years ago

ObjectBox already provides some minimal Kotlin extensions:

// Regular
val box: Box<DataClassEntity> = store.boxFor(DataClassEntity::class.java)

// With extension
val box: Box<DataClassEntity> = store.boxFor()
// Regular
val query = box.query().`in`(property, array).build()

// With extension
val query = box.query().inValues(property, array).build()

This issue is to track what other Kotlin extensions may be added.

Some ground rules:

Things to look out for:

-ut

greenrobot-team commented 6 years ago

My suggestions:

// With extension val query = box.query { equal(property, value) order(property) }


- **shipped with 2.0.0 beta** ToMany applyChangesToDb
```kotlin
// Regular
toMany.apply { 
    reset()
    add(entity)
    removeById(id)
    applyChangesToDb()
}

// With extension
toMany.applyChangesToDb(resetFirst = true) { // default is false
    add(entity)
    removeById(id)
}

// With extension val liveData = query.toLiveData()



-ut
greenrobot-team commented 6 years ago

The first two suggestions are released as part of 2.0.0-beta. Also added docs for the existing extensions. -ut

greenrobot-team commented 5 years ago

An upcoming release will add extensions that are shortcuts for query methods that accept Long and Double. However, the shortcuts can be used with Int, Short and Float without having to call .toLong() or .toDouble():

// Regular
queryBuilder.equal(Model_.type, typeId.toLong())

// With extension
queryBuilder.equal(Model_.type, typeId)

-ut

irvine752 commented 2 years ago

Could we also get "notIn" added for String Arrays or something the equivalent of not inValues for the Strings?

greenrobot-team commented 2 years ago

@irvine752 This issue is about Kotlin extension functions, not query conditions. Anyhow, not-in is not supported for String properties. Please thumbs up #941!

HosseinKurd commented 2 years ago

What about :

val query = box.query().notIn(User_.phone, stringListPhoneNumbers)

notIn usage Error:

None of the following functions can be called with the arguments supplied.

greenrobot-team commented 2 years ago

@HosseinKurd The notIn (or notOneOf using the new query API) conditions are only supported for integers (Integer and Long)! For Strings only in/oneOf is possible.

HosseinKurd commented 2 years ago

@HosseinKurd The notIn (or notOneOf using the new query API) conditions are only supported for integers (Integer and Long)! For Strings only in/oneOf is possible.

So how can I Reverse in/oneOf? there is not any not() function

greenrobot-team commented 2 years ago

@HosseinKurd You can't. Put differently: notIn is not supported for String. You'll have to find another way, e.g. by changing your model or writing custom filter code.