Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty constructor, explicit save, and other DAO improvements #794

Closed
AliLozano opened this issue Feb 12, 2020 · 3 comments
Closed

Empty constructor, explicit save, and other DAO improvements #794

AliLozano opened this issue Feb 12, 2020 · 3 comments

Comments

@AliLozano
Copy link
Contributor

AliLozano commented Feb 12, 2020

I was looking for some ORM to work with a database in Kotlin and I found this library that I like very much, its way of making queries reminds me a little of Django, however I still believe that we need to improve the daos:

  • They are not serializable. A Serializer can be provided, without having much impact on the current design, however there are other problems:
      * Creating a new entity is a bit complex because it has no empty constructor.
      * You cannot instantiate an Entity without queuing it to insert it.

A solution

  • Add an empty constructor
  • The save should be explicit, currently if a data of the bean is changed at the end of the transaction that is automatically flush, without the developer having said if a object should be inserted.

I have made some slight changes in the library of the exposed-dao, I was careful with back compatibility, however I can't run tests. so I would very much like you to take a look.

You can see SamplesDAO to see the change I've made:

class City : IntEntity() { // simplified without EntityID<Int> parameter
    companion object : IntEntityClass<City>(Cities)
    var name by Cities.name
    val users by User referrersOn Users.city
}

val stPete = City()
stPete.name = "St. Petersburg"

val aUser = User()
aUser.name = "a"
aUser.city = stPete
aUser.age = 5

transaction {
    addLogger(StdOutSqlLogger)
    SchemaUtils.create(Cities, Users)
    stPete.save()
    aUser.save()
}

I would like to add methods to detach object to work outside a transaction, but I want to know if this is aligned to the project.

https://github.com/AliLozano/Exposed/

PD: If you add to the documentation how to run the tests, I would greatly appreciate it.

@Tapac
Copy link
Contributor

Tapac commented Feb 13, 2020

First of all, thank you for an interest in Exposed.
Currently Exposed uses an active-record pattern for DAO which doesn't force you to call save or something.
But a lot of users ask to implement straight-forward DAO based on data classes as it automatically allows to serialize them the way you like.

You could read a bit more here and also look for krush framework (sample in comments).

I will try to look at your code a bit later (maybe next week).

PD: If you add to the documentation how to run the tests, I would greatly appreciate it.

I was sure what gradlew test is enough. Or gradlew :exposed-tests:test. Please tell me it won't work.

@AliLozano
Copy link
Contributor Author

We could use a flag to use active-record or explicit save()?

I was reviewing using data class with reflections, I even adapted exposed-dao to save and consult with data class, but it is a problem to work relationships and lazy load, we would have to do something like User.city.value <- where city is a lazy object, it is dirty and anyway we will have to create a custom serializer to the lazy object, That's why I ended up leaning towards making a serializer for exposed and continue using delegated methods that is more transparent.

@AliLozano
Copy link
Contributor Author

AliLozano commented Feb 17, 2020

I have updated my repository with some improvements, which can be seen in the file https://github.com/AliLozano/Exposed/blob/master/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityWihtoutIdConstructor.kt

Some of the improvements added:

  • Optional explicit transaction for EntityClass.
  • Entities without ID and outside a transaction
  • Filters with more intuitive joins
School.objects.filter { Schools.region innerJoin Regions.name like "%Region" }
  • Save explicit
  • Save implicit with School.objects.forUpdate()
  • Added EntityQuery, for DAO's queries.

I'm missing:

  • Be able to use references consulted once the transaction is finished.

PD: I cannot run the tests because the mysql cannot be initialized, I have not delved into it.

@AliLozano AliLozano changed the title Empty constructor and explicit Save. Empty constructor, explicit save, and other DAO improvements Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants