Comments (12)
Hey, closing this issue since I don't think there's an action item here.
If you want to add custom methods to *ent.Client
(or any other type), you can either use external templates, or just add them manually to an external.go
file under the <project>/ent
directory. For example:
func (c *Client) DB() *sql.DB {
return c.driver.(*entsql.Driver).DB()
}
from ent.
Hey all! I added a feature flag named sql/execquery to simplify the process for those who need it.
from ent.
Only one reason for me to access underly *sql.DB
is creating sqlhealth.Checker
. So, I write custom template that generates health.Checker
for gocloud kit.
{{ define "sqlhealth" }}
{{ $pkg := base $.Config.Package }}
{{- with extend $ "Package" $pkg }}
{{ template "header" . }}
{{- end }}
import (
"entgo.io/ent/dialect/sql"
"gocloud.dev/server/health/sqlhealth"
)
func (c *Client) HealthCheck() *sqlhealth.Checker {
db := c.driver.(*sql.Driver).DB()
return sqlhealth.New(db)
}
{{ end }}
// appHealthChecks returns a health check for the database. This will signal
// to Kubernetes or other orchestrators that the server should not receive
// traffic until the server is able to connect to its database.
func appHealthChecks(ent *entv1.Client) ([]health.Checker, func()) {
dbCheck := ent.HealthCheck()
list := []health.Checker{dbCheck}
return list, func() {
dbCheck.Stop()
}
}
from ent.
If you use debug mode by default and run into the error:
interface conversion: dialect.Driver is *dialect.DebugDriver, not *sql.Driver
Try this one.
func (c *Client) DB() *sql.DB {
if c.debug {
return c.driver.(*dialect.DebugDriver).Driver.(*entsql.Driver).DB()
}
return c.driver.(*entsql.Driver).DB()
}
from ent.
ent.Client
doesn't expose an API for interacting directly with the database.
If you need to do this, you can either use the dialect.Driver
or sql.DB
in your case.
Let me know if you need any help with that.
from ent.
dialect.Driver
or sql.DB
are both OK, but that requires passing additional struct around since ent.Client
doesn't provide a way to get underlying driver or db.
from ent.
ent.Client doesn't provide a way to get underlying driver or db.
Yeah, because we believe it's not required for most of the use cases, and we prefer to stick with the type-safe API. However, predicates can be customized.
Can you share your use case, or what is missing in the client? Thanks.
from ent.
It would be great If ent.Client exposes underlying driver or db by default so that we don't have to do it our self every time we generate the code. I want to execute following commands after client is connected to database.
set search_path to 'schema'
set timezone TO 'GMT';
from ent.
that we don't have to do it our self every time we generate the code.
How do you run codegen? I'm trying to figure out why do you need to rewrite it after each codegen?
If you delete the ent
directory before each codegen, I'll recommend you to use templates. For example:
{{ define "external" }}
package ent
func (c *Client) DB() *sql.DB {
return c.driver.(*entsql.Driver).DB()
}
{{ end }}
from ent.
I mean every time we generate the code for a new project. The workaround is simple but its more convenient to expose DB() method as part of the client api by default specially for PostgreSQL.
from ent.
@a8m I am trying the following command:
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/execquery ./ent/schema
But I don't seem to be finding the expected methods afterwards. Could it be that I am caching the generator somehow?
from ent.
Hey @leafduo 👋
Can you please update ent to the latest master and try again? go get entgo.io/ent/cmd/ent@master
from ent.
Related Issues (20)
- [migrate] default value diff query=SELECT '0.0' = 0 HOT 1
- Codegen is creating duplicate functions HOT 4
- Create a record without needing to write every field line by line HOT 2
- [Question] Will Cloud Spanner be supported in the future? HOT 1
- question: Spatial Data Types for PostGIS HOT 2
- does entio support postgreesql RUM feature? HOT 1
- Expected value of the generated WithFixedNodeType?
- How to generate a schema file from an existing MySQL database HOT 1
- schema.WriteDriver neglects dialect
- How to use "sql.In" with uuids? HOT 1
- Opt-in mode for entgql.WithSchemaGenerator
- Use ent to import sql dump for postgres
- Partial indexes are always re-created when generating a migration file HOT 2
- table COMMENT Annotations will be automatically migrated and overwritten HOT 1
- entc/gen: set "TUser" foreign-keys: edge "t_wallets_user_id" has a field "user_id" but it is not holding a foreign key
- Atlas migrations don't work with sqlite fts5 virtual tables HOT 6
- Type safe IDs do not work
- Creating a row with a JSON column fails in Planetscale
- CreateBulk fails when entity has only ID field HOT 1
- Annotations in third-party mixins
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ent.