jennifer.cr icon indicating copy to clipboard operation
jennifer.cr copied to clipboard

Use schema.cr instead of mappings?

Open watzon opened this issue 7 years ago • 12 comments

This a more of a question/request, but have you considered using a schema.cr file instead of a mapping macro inside of the model? I realize it would be a big change, but it would bring an important benefit, the ability to have the model definition update automatically. It also makes the model files look a little less cluttered.

Obviously, the one downside to this is that you don't have everything to do with the model all in one place, but I feel like it would be an ok tradeoff.

Thoughts?

watzon avatar Jun 12 '17 00:06 watzon

@watzon really interesting thought but it needs to make database structure analyzer. Will take a look when will got free time

imdrasil avatar Jun 12 '17 13:06 imdrasil

problem could be with allowing manual overriding fields under the model as well

imdrasil avatar Jun 12 '17 13:06 imdrasil

In case you would be looking into this, I would suggest to keep the model related code to models and extract the mapping using modules for those who feel clutter if there is any way,

class User < Jennifer::Model::Base
  include UserMapping
end

src/user/user_mapping.cr:

module UserMapping
  mapping(
    id: { type: Int32, primary: true }
  )
end

I see this as a cosmetic change rather than an improvement at this stage.

thomasnal avatar Aug 29 '17 21:08 thomasnal

@thomasnal I think you misunderstand the direction of the issue.

The proposal is to have Jennifer analyze the database itself and build the mapping for you, such that you don't need to do mapping() at all, and it would "magically" update with any migrations you made / whatever state the DB tables are in.

(similar to Ruby's sequel or ActiveRecord)

z64 avatar Aug 29 '17 22:08 z64

Haven't seen it described above this way. Any idea how to get mapping automatically for free, having it inferred from migrations and database is welcome for the future.

I see quite a number of other practical hurdles than to be concerned about this one at the moment.

thomasnal avatar Aug 29 '17 22:08 thomasnal

@z64 is right - this one is more about automatic schema generation. I have idea how to do this but for now there are a lot of important things to fix. So this will be added not as soon as I thought previously

imdrasil avatar Aug 29 '17 23:08 imdrasil

I'm glad that this has at least been getting some attention. @z64 is right, but I understand that this isn't exactly top priority. It would definitely be a "nice to have" though.

watzon avatar Aug 31 '17 15:08 watzon

Yeah. I keep this in mind and try make all new staff easy to migrate to this approach later, but for now main point is to make Jennifer useful and working tool for production purposes.

imdrasil avatar Aug 31 '17 19:08 imdrasil

Hey, just wanted to touch base and see if any progress was made on this front in the last 2 years. I just started using Jennifer with Amber.

AndiLavera avatar Dec 31 '19 16:12 AndiLavera

Hi. For that period of time this feature had low priority. The reason is that model declaration is inside of a model, so all fields and their types are inside of related file; database structure can be quickly loaded from structure.sql file. It is likely we will get it in 0.9.x or 0.10.0

imdrasil avatar Jan 02 '20 21:01 imdrasil

Completely Understand. Glad to hear it'll be coming sometime in the future and wasn't totally benched. Love your orm. Thanks!

AndiLavera avatar Jan 03 '20 21:01 AndiLavera

+1 It would be awesome to have this feature, this is something I love in ActiveRecord (from Rails), it maintains a schema.rb always up to date.

jamesst20 avatar Sep 28 '20 13:09 jamesst20