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

Common interface for generated classes #427

Open
simon-winter opened this issue Nov 24, 2024 · 3 comments
Open

Common interface for generated classes #427

simon-winter opened this issue Nov 24, 2024 · 3 comments

Comments

@simon-winter
Copy link

simon-winter commented Nov 24, 2024

When building business webApps one needs to expose much of the database with the default operations of CRUD.
An ORM lib would handle this, but i prefer to stay much closer to SQL queries than pushing everything into an ORM pattern and learn all the quirks and maintenance of such a system.

Ideally i would like to be able to write common functions for every model/table struct generated through jet's code gen, so that i can, for example, provide Create/Read/Update/Delete Rest endpoints for my database entities without having to write/generate these functions for each type.

Describe the solution you'd like
A possibility to access the table functionalities provided by this lib via interface or common struct. For example embed a "jettable" in the generated struct isntead of the postgres.table like this:

type JetTable struct{
	postgres.Table

	ID        postgres.ColumnInteger
	AllColumns     postgres.ColumnList
	MutableColumns postgres.ColumnList
}

type messageTable struct {
	JetTable	

	// Columns	
	ChannelID postgres.ColumnInteger
	Text      postgres.ColumnString
	IDUser    postgres.ColumnInteger	
}

so i could write functions for the jettable that allow me to implement the CRUD operations where i don't need the actual columns, but just ID and MutableColumns.
Would also be possible by providing some interface functions to access these fields instead of the embedded struct.

Also, currently tables and models are completly unrelated, maybe it could be part of the solution to wrap these two packages into one "entity" package, that allows access to either the model or the table of an entity via a single struct, so i don't have to write the boilerplate code to combine an empty struct and its table i.e.:
message := entity.NewEntity(model.Message{}, &table.Message, db, ginEngine) could be handled in a package in a sleeker way.

This is what i try to achieve:

//main: 
message := entity.NewEntity(model.Message{}, &table.Message.JetTable, db, ginEngine)

//entity
type Entity struct {
	Name      string
	Model     model.Model
	Table     *table.JetTable
	Db        *sql.DB
	GinEngine *gin.Engine
}

func NewEntity(model model.Model, table *table.JetTable, db *sql.DB, ginEngine *gin.Engine) (e Entity) {
	name := reflect.TypeOf(model).Name()
	e = Entity{
		Name:      name,
		Model:     model,
		Table:     table,
		Db:        db,
		GinEngine: ginEngine,
	}

 ---> // automatic create endpoint for any entity created
	ginEngine.POST("/"+name, func(ctx *gin.Context) { e.Create(ctx) })
	return
}


func (e Entity) Create(ctx *gin.Context) {
	if !e.bindModel(ctx) {
		return
	}

	// Insert model into database
	stmt := e.Table.
		INSERT(e.Table.MutableColumns).
		MODEL(e.Model).
		RETURNING(e.Table.AllColumns)

	err := stmt.Query(e.Db, e.Model)
	handleError(ctx, err)

	// Return inserted message
	ctx.JSON(http.StatusOK, e.Model)
}
@simon-winter
Copy link
Author

i will try to modify the generator i find a nice solution for myself to achieve this, but maybe i oversee an easy solution or you want to give it a spin to make it a proper implementation knowing your lib.

i will propose my solution to this, if i find one, as PR

@simon-winter
Copy link
Author

simon-winter commented Nov 24, 2024

Note: My embedding solution is flawed as i cannot access the parent struct from the model struct without reflection. Interfaces are the way to go

@houten11
Copy link

If I understood correctly, you are probably looking for some sort of CRUD HTTP generator library.
Since your queries are hardcoded and always the same, a type-safe SQL builder is not that useful in your case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants