Giter Site home page Giter Site logo

Changing default alias about jet HOT 4 CLOSED

go-jet avatar go-jet commented on May 8, 2024
Changing default alias

from jet.

Comments (4)

go-jet avatar go-jet commented on May 8, 2024

@wijayaerick You can use standard table sql aliasing. For instance:
client := Clients.AS("client")
and then use aliased table instead of Clients:
stmt := SELECT(client.AllColumns).FROM(client)

Or you can tag the fields. But you should use alias tag instead of db.
Sample usage can be seen here: https://github.com/go-jet/jet/wiki/Query-Result-Mapping-(QRM)#tagging-model-files

For example:

type Client struct {
	ID        uuid.UUID `alias:"clients.id"` 
	CreatedAt time.Time `alias:"clients.created_at"`
	UpdatedAt time.Time `alias:"clients.updated_at"`
	Username  string    `alias:"clients.username"`
}

Future version will add support for customizing generated model files(like names and types).
Is the additional 's' in the name, the only reason why you didn't want to use generated model files?

from jet.

wijayaerick avatar wijayaerick commented on May 8, 2024

@go-jet Thank you for the suggestion. Using empty alias does the trick; it removes the table name prefix in columns and still preserve table name in FROM statement.

The reason I'm using my own model Struct is because I have methods defined in my models, for example func NewModel(..) *Model, func (m *Model) IsSomething() bool, etc..

from jet.

go-jet avatar go-jet commented on May 8, 2024

@wijayaerick If that's the case you can embed generated model struct into your custom model struct. For instance:

package mymodel

import ".../model"

struct Client struct {
   model.Clients
}

func (c Client) isSomething() bool {
   return c.Username != ""
}

With this approach you don't need to change SQL query for scan to work.

from jet.

wijayaerick avatar wijayaerick commented on May 8, 2024

Haven't thought about using embedding. Yes, that should work well for me. Thank you for your help. Also this is a great project and I'm looking forward to future development of this project.

from jet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.