Using Custom Models in Query #376
Answered
by
houten11
safaci2000
asked this question in
Q&A
-
I am successfully using some custom models I created but it just seems overly verbose for no good reason. stmt := SELECT(
storageSchema.Buckets.BucketID.AS("getbucketpolicybyenvrow.bucket_id"),
storageSchema.BucketPolicy.PolicyID.AS("getbucketpolicybyenvrow.policy_id"),
storageSchema.Buckets.EnvironmentName.AS("getbucketpolicybyenvrow.environment_name"),
storageSchema.Buckets.CreateBucket.AS("getbucketpolicybyenvrow.create_bucket"),
storageSchema.Buckets.BucketName.AS("getbucketpolicybyenvrow.bucket_name"),
storageSchema.BucketPolicy.PolicyName.AS("getbucketpolicybyenvrow.policy_name"),
storageSchema.BucketPolicy.Duration.AS("getbucketpolicybyenvrow.duration"),
storageSchema.BucketPolicy.Prefix.AS("getbucketpolicybyenvrow.prefix"),
).FROM(storageSchema.Buckets.
INNER_JOIN(storageSchema.BucketPolicy, storageSchema.BucketPolicy.BucketID.EQ(storageSchema.Buckets.BucketID))).
WHERE(storageSchema.Buckets.EnvironmentName.EQ(NewEnumValue(enumVal.String())))
ORDER_BY(storageSchema.Buckets.BucketName, storageSchema.Buckets.EnvironmentName, storageSchema.BucketPolicy.PolicyName)
var dest = make([]custom.GetBucketPolicyByEnvRow, 0)
err = stmt.Query(r.GetStdConnection(context.Background()), &dest)
return dest, err My struct looks like this: type GetBucketPolicyByEnvRow struct {
BucketID int64 `db:"bucket_id" json:"bucketId"`
PolicyID int64 `db:"policy_id" json:"policyId"`
EnvironmentName string `db:"environment_name" json:"environmentName"`
CreateBucket bool `db:"create_bucket" json:"createBucket"`
BucketName string `db:"bucket_name" json:"bucketName"`
PolicyName string `db:"policy_name" json:"policyName"`
Duration int16 `db:"duration" json:"duration"`
Prefix string `db:"prefix" json:"prefix"`
} Ideally it would be nice to just have a db tag or any other tag that's needed where the full structname doesn't need to be part of the SQL. Any suggestions would be appreciated. |
Beta Was this translation helpful? Give feedback.
Answered by
houten11
Aug 22, 2024
Replies: 1 comment 1 reply
-
Jet doesn't use type GetBucketPolicyByEnvRow struct {
BucketID int64 `alias:"buckets.bucket_id" json:"bucketId"`
PolicyID int64 `alias:"bucket_policy.policy_id" json:"policyId"`
EnvironmentName string `alias:"buckets.environment_name" json:"environmentName"`
CreateBucket bool `alias:"buckets.create_bucket" json:"createBucket"`
BucketName string `alias:"buckets.bucket_name" json:"bucketName"`
PolicyName string `alias:"bucket_policy.policy_name" json:"policyName"`
Duration int16 `alias:"bucket_policy.duration" json:"duration"`
Prefix string `alias:"bucket_policy.prefix" json:"prefix"`
} Now you can write a query without realiasing: stmt := SELECT(
storageSchema.Buckets.BucketID,
storageSchema.BucketPolicy,
storageSchema.Buckets.EnvironmentName,
storageSchema.Buckets.CreateBucket,
storageSchema.Buckets.BucketName,
storageSchema.BucketPolicy.PolicyName,
storageSchema.BucketPolicy.Duration,
storageSchema.BucketPolicy.Prefix,
).FROM(storageSchema.Buckets.
INNER_JOIN(storageSchema.BucketPolicy, storageSchema.BucketPolicy.BucketID.EQ(storageSchema.Buckets.BucketID))).
WHERE(storageSchema.Buckets.EnvironmentName.EQ(NewEnumValue(enumVal.String())))
ORDER_BY(storageSchema.Buckets.BucketName, storageSchema.Buckets.EnvironmentName, storageSchema.BucketPolicy.PolicyName) More info here: https://github.com/go-jet/jet/wiki/Query-Result-Mapping-(QRM) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
safaci2000
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jet doesn't use
db
tag. To avoid writing full struct name you can usealias
tag: