entc/gen: add open func for creating client

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/44

Reviewed By: alexsn

Differential Revision: D17657719

fbshipit-source-id: 9bbe18dc0e9c553f8a2df81018d7db06f38f5e0e
This commit is contained in:
Ariel Mashraki
2019-10-02 01:39:14 -07:00
committed by Facebook Github Bot
parent 86a6fbe48e
commit cb1f78f233
35 changed files with 525 additions and 70 deletions

View File

@@ -43,6 +43,23 @@ func NewClient(opts ...Option) *Client {
}
}
// Open opens a connection to the database specified by the driver name and a
// driver-specific data source name, and returns a new client attached to it.
// Optional parameters can be added for configuring the client.
func Open(driverName, dataSourceName string, options ...Option) (*Client, error) {
switch driverName {
case dialect.MySQL, dialect.SQLite:
drv, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
return NewClient(append(options, Driver(drv))...), nil
default:
return nil, fmt.Errorf("unsupported driver: %q", driverName)
}
}
// Tx returns a new transactional client.
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
if _, ok := c.driver.(*txDriver); ok {
@@ -80,6 +97,11 @@ func (c *Client) Debug() *Client {
}
}
// Close closes the database connection and prevents new queries from starting.
func (c *Client) Close() error {
return c.driver.Close()
}
// GroupClient is a client for the Group schema.
type GroupClient struct {
config