GenieFramework / SearchLight.jl

ORM layer for Genie.jl, the highly productive Julia web framework
https://genieframework.com
MIT License
139 stars 16 forks source link

Optional mapping of fields to columns #37

Open dms449 opened 3 years ago

dms449 commented 3 years ago

Is your feature request related to a problem? Please describe. I have a table already in use that has a certain naming convention which is not the same as SearchLight and that makes it almost impossible to use SearchLight. Everytime I want to create a table or connect to one, it's automatically pluralizing and case-changing the name I pass in.

Describe the solution you'd like Sometimes there is an option not to pluralize but I can't find an option not to make it lower case. What is the reasoning for making it the names lowercase?

essenciary commented 3 years ago

Minimal effort has been put in using SearchLight with existing databases, to be honest. As it is, it's a better fit for building new apps. But happy to take a look and see how we can improve.

Can you give me some examples please?

dms449 commented 3 years ago

Well, I'm a database newbie so I don't understand what all is happening under the hood of the Migrations.jl. All I know is that, I have table names in CamelCase and I'm having difficulty using them because all of the generators in SearchLight generate things in all lowercase. Even when I've tried to change the appropriate names manually I'm not able to get the table to come up. Which is another question I have. Do I need any of the stuff in the create_table_ scripts if I already have the table?

Sorry, for all the questions. I love Genie, and I'm relatively new to databases. If SearchLight had more documentation (I know there's already a request for this) that would reeaaallly help.

FrankUrbach commented 3 years ago

IIRC the pluralization is hard coded in SerachLight. I understand that this is a little bit annoying but in the most cases somebody uses Genie with Searchlight the application will written on a green field. If you only want read some data, you could prepare a view with the naming conventions of SearchLight. Then you haven't something to do with a migration and could use what ever you want. If you have a given example what you try to achieve (maybe only the table structure) it should be possible to find a solution. If you could provide which database you use maybe I can proof that a given solution should work for you.

dms449 commented 2 years ago

I'm closing this as it really isn't that important or even bothersome to me anymore

FrankUrbach commented 2 years ago

I'm not fine with closing this issue. I had the same thoughts in my mind using the strong binding of fields of structs. In some cases you won't be able to build things on a green field. In this case you should have the ability to use custom ids and custom field names to store the informations In the right columns in the database without the need of using the equal names as field names in the struct. Just my 2 cents.

AbhimanyuAryan commented 2 years ago

@FrankUrbach I agree let's keep it open if this ain't fixed

ubertakter commented 1 year ago

@FrankUrbach, @AbhimanyuAryan I'll second keeping this open. I consider the pluralization unexpected behavior, especially since it is not always correct. For example, it thinks the plural version of "runway" is "runwaies" which is not correct. There is however, a pluralize keyword argument, which is not documented. Unfortunately it appears that it only sort of works (probably should be a separate issue). A working pluralize argument would be an acceptable solution.

I would also be okay with renaming things by hand, but there is a cache or something somewhere that keeps the old plural form. I have deleted the precompile cache for julia and grepped through both the local project and the .julia folder in attempts to find it and eliminate it, but I have yet to be able to get rid of it. Grepping through various folders finds no trace of it. Deleting the precompile cache also doesn't remove it.

There is a possible workaround for renaming by hand. If you rename everything, you can override the table function. So if I had originally attempted to create a "Runway" model, which ends up creating a "Runwaies" module with a "Runway" struct, which I then rename "Runways. You can then override the table function so correct SQL statements can be generated:

SearchLight.table(m::Type{Runway}) = "runways"

At this time, this fixes the SQL statement for me but now I'm getting some type mismatch errors when I attempt to get a model from the database. I'm not sure if that's an artifact of this problem or something else. NOTE: I have confirmed that type conversion problem is a separate issue (perhaps another SearchLight issue...)

essenciary commented 1 year ago

Of course, the pluralising logic is quite simple, applying some basic English grammar rules. These are provided by the Inflector.jl package (https://github.com/GenieFramework/Inflector.jl). For exceptions, you just need to add the to inflector list of words in your app.

Thinking about it, maybe you could even trick it by passing the same form for both singular and plural word, effectively getting rid of pluralising.

But overall, adding custom column names and disabling the pluralising should be quite straightforward to add in the code. We're just super busy with Genie and Stipple for now to look into these.