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

Support for custom models #755

Open
mlukasik-dev opened this issue Oct 29, 2020 · 4 comments
Open

Support for custom models #755

mlukasik-dev opened this issue Oct 29, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@mlukasik-dev
Copy link

mlukasik-dev commented Oct 29, 2020

Example:

db/queries/roles.sql

-- name: ListRolesWithPermissions :many
-- model: Roles
-- path: internal/domain/roles
SELECT
  r.role_id,
  r.display_name,
  r.description,
  array_agg(permissions.method)::text[] AS permissions
  FROM roles r
    LEFT JOIN permissions USING (role_id)
GROUP BY r.role_id;

-- name ListRoles :many
-- model: Roles
-- path: internal/domain/roles
SELECT  role_id, display_name, description FROM roles; /* returned colums exists in the model so we can reuse it */

It isn't generated file!
internal/domain/roles

type Role struct {
	RoleID      uuid.UUID // here we can add some custom struct tags and methods
	DisplayName string
	Description string
	Permissions []sql.NullString // using []string throws an error if an array is empty 
}

Generated method

func (q *Queries) ListRoles(ctx context.Context) ([]roles.Role, error)

P.S. These features can be useful because with it we can both use sqlc and follow Clean Architecture. And reuse structs for tables for complex queries with joins.

So my question is if I'd correctly implement this functionality would you approve PR (I'm asking because this feature can be not in the spirit of the project)

@mlukasik-dev
Copy link
Author

mlukasik-dev commented Nov 1, 2020

This feature could help solving issues like: #185 , #534 , #658

@jonleopard
Copy link

Would also love this! I use sqlx and frequently use tags like db:"user_id" json:"id" or json:"password_confirm" validate:"eqfield=Password"

@Jille
Copy link
Contributor

Jille commented Jul 26, 2023

I agree this would be useful. Especially when you have a few queries with different WHERE clauses that return the same columns.

This proposal has one big question: How would we verify that the given struct matches the data types returned from the query?

  • Do we assert they match exactly with the types sqlc would've chosen? That'd be safe, but I think a bunch of people here are expecting to use this to override exactly that? Also, if the types match 1:1, Go would allow you to cast it anyway.
  • If we allow uncheck structs, wouldn't that defeat one of sqlc's USPs: That you don't have runtime failures for type mismatches.

Would love to hear your thoughts on this.

@jamesleeht
Copy link

jamesleeht commented Aug 2, 2023

Also very interested in this feature.

Here are some other closely related issues: #387 #444

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants