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

@@ -11,17 +11,15 @@ import (
"github.com/facebookincubator/ent/examples/edgeindex/ent"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

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()
}
// CityClient is a client for the City schema.
type CityClient struct {
config

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

View File

@@ -14,17 +14,15 @@ import (
"github.com/facebookincubator/ent/examples/m2m2types/ent"
"github.com/facebookincubator/ent/examples/m2m2types/ent/group"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -39,6 +39,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 {
@@ -74,6 +91,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()
}
// UserClient is a client for the User schema.
type UserClient struct {
config

View File

@@ -12,17 +12,15 @@ import (
"github.com/facebookincubator/ent/examples/m2mbidi/ent"
"github.com/facebookincubator/ent/examples/m2mbidi/ent/user"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -39,6 +39,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 {
@@ -74,6 +91,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()
}
// UserClient is a client for the User schema.
type UserClient struct {
config

View File

@@ -13,17 +13,15 @@ import (
"github.com/facebookincubator/ent/examples/m2mrecur/ent"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

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()
}
// PetClient is a client for the Pet schema.
type PetClient struct {
config

View File

@@ -11,17 +11,15 @@ import (
"github.com/facebookincubator/ent/examples/o2m2types/ent"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -39,6 +39,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 {
@@ -74,6 +91,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()
}
// NodeClient is a client for the Node schema.
type NodeClient struct {
config

View File

@@ -12,17 +12,15 @@ import (
"github.com/facebookincubator/ent/examples/o2mrecur/ent"
"github.com/facebookincubator/ent/examples/o2mrecur/ent/node"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

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()
}
// CardClient is a client for the Card schema.
type CardClient struct {
config

View File

@@ -12,17 +12,15 @@ import (
"github.com/facebookincubator/ent/examples/o2o2types/ent"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -39,6 +39,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 {
@@ -74,6 +91,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()
}
// UserClient is a client for the User schema.
type UserClient struct {
config

View File

@@ -12,17 +12,15 @@ import (
"github.com/facebookincubator/ent/examples/o2obidi/ent"
"github.com/facebookincubator/ent/examples/o2obidi/ent/user"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -39,6 +39,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 {
@@ -74,6 +91,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()
}
// NodeClient is a client for the Node schema.
type NodeClient struct {
config

View File

@@ -12,17 +12,15 @@ import (
"github.com/facebookincubator/ent/examples/o2orecur/ent"
"github.com/facebookincubator/ent/examples/o2orecur/ent/node"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {

View File

@@ -47,6 +47,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 {
@@ -86,6 +103,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

View File

@@ -17,17 +17,15 @@ import (
"github.com/facebookincubator/ent/examples/traversal/ent"
"github.com/facebookincubator/ent/examples/traversal/ent/group"
"github.com/facebookincubator/ent/dialect/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:o2o2types?mode=memory&cache=shared&_fk=1")
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer db.Close()
client := ent.NewClient(ent.Driver(db))
defer client.Close()
ctx := context.Background()
// run the auto migration tool.
if err := client.Schema.Create(ctx); err != nil {