mikolajgs / crud

WIP. Write structs, get REST API endpoints and PostgreSQL database generated for you just like that.
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Extract saving to database as a separate struct2db pkg #8

Closed mikolajgs closed 1 month ago

mikolajgs commented 1 month ago

ORM functionality should be extracted into a separate package so that it can be re-used somewhere else.

We might add some functionality in the future which is going to be only in the scope of this package and not the whole CRUD.

mikolajgs commented 1 month ago

crud.Controller will have a struct2db field with instance of struct2db.Controller.

For HTTP endpoints, we have this situation where we use a struct like User_UpdatePrice that has only few fields, and its update query is limited to these, but it still updates the users table. In this case, we add struct2sql.Struct2sql with forced table name of users. Now, the struct2sql.Struct2sql object (called "helper" in the whole context) is hidden in struct2db.Controller, so we need to deal with it somehow. Also, naming convention might be confusing, as "helper" is too general.

mikolajgs commented 1 month ago

Maybe in struct2db, it should be called sqlGenerators and the method getSQLGenerator (previously getHelper).

In struct2db this has to be done somehow different, like we need to mark that User_UpdatePrice should use table users, eg. with c.dbBackend.RegisterStruct(&User_UpdatePrice{}, &User) where the latter argument could be nil... (dbBackend being new name for struct2db field). This method would then deal with creating a new sqlGenerator ("helper") underhood.