volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.59k stars 536 forks source link

Adding custom table properties to sqlboiler.toml #865

Open wilberto-montoya-vertiv opened 3 years ago

wilberto-montoya-vertiv commented 3 years ago

This is a new feature not really an error but the CONTRIBUTING.md requires me to open a new PR

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler V4.2.0

What is your database and version (eg. Postgresql 10)

Postgres 12

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

Yes the feature is required at generation time when I use cystom templates sqlboiler -c ..\boil\sqlboiler.toml -o ..\boil\repository psql --templates .\templates

If this happened at runtime what code produced the issue? (if not applicable leave blank)

no runtime

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

no error return because is a new feature

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

not specific to a db schema but to the template generation

Further information. What did you do, what did you expect?

When I writing/modifying templates I need a way to add a custom property to a table. by example, in my case, I need to add a security (access right) check at query time to filter the number of records the table returns. This feature is specific for some tables so I need a flag indicating if table1 support access rights check and table2 does not. something like this:

pkgname = "repository" output = "pkg/repository" add-global-variants = true wipe = true no-tests = false

[psql] dbname = "auth" host = "localhost" port = 5432 user = "postgres" pass = "xxxxxx" sslmode = "disable"

[custom.tables.user] needs_auth = true auth_column = "id"

[aliases.tables.resourcegroup.relationships.resourcegroup_children_fkey] AuthColumn = "Children" foreign = "Parent"

As you can see the whole [custom.tables.XXXX] is a new section where I can pass custom properties to templates, then in my custom template I can retrieve the values just like any other table property:

... {{- $authCol := .Custom.Value .Table.Name "auth_column" -}} .... func (q {{$alias.DownSingular}}Query) ApplyAuthorizationModifier(ctx context.Context) { qm.InnerJoin("{{printf $authFunction $authCol}}", pq.Array(ctx.Value("usergroups")) ,ctx.Value("accessrigth")).Apply(q.Query) } ...

aarondl commented 3 years ago

Hi there @wilberto-montoya-vertiv. Thank you for following our development processes closely by opening this issue before a PR.

The goal of the sqlboiler generated models is to create a one-to-one database-to-Go-struct mapping. Adding custom properties isn't supported on purpose.

We have tools that support use cases like this though. Have you taken a look at hooks in the README?

wilberto-montoya-vertiv commented 3 years ago

Thank you Aaron for the quick response

I think there is a misunderstanding here the custom properties I am talking about are TEMPLATE properties no Entity properties. if you have User table with 4 columns the go struct will have the same 4 attributes. The template properties are just properties associated to the table and Template generation time. so lets say I want to say that User requiresLogs=true i will add to config

[custom.tables.user] requires_log = true

that DOES NOT means that User table now has an attribute "user.requireslog", NO. it means that when I am in the template I can make reference to

{{- $addlogs := .Custom.Value .Table.Name "requires_log" -}}

and aff the code in my template so the repository generated will add logs only for the User table. (logs is just an example, not that I need to add logs)

aarondl commented 3 years ago

Hi there @wilberto-montoya-vertiv. Sorry for the delay in response this time.

So if I'm looking at this again it sounds like what you actually want is some way to inject data into the templating so that you can use custom templates to enhance your generated models further?