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

Adding custom table properties to sqlboiler.toml #865

Open
wilberto-montoya-vertiv opened this issue Nov 16, 2020 · 2 comments
Open

Adding custom table properties to sqlboiler.toml #865

wilberto-montoya-vertiv opened this issue Nov 16, 2020 · 2 comments

Comments

@wilberto-montoya-vertiv
Copy link

@wilberto-montoya-vertiv wilberto-montoya-vertiv commented Nov 16, 2020

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
Copy link
Member

@aarondl aarondl commented Nov 17, 2020

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
Copy link
Author

@wilberto-montoya-vertiv wilberto-montoya-vertiv commented Nov 17, 2020

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.