mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Summary: Saves typing `.Query()` every time we want to use `.Get()` Reviewed By: a8m Differential Revision: D17396667 fbshipit-source-id: 103c10d25bd1181b8e52fa272f2c1a26f47ed9f6
187 lines
5.3 KiB
Cheetah
187 lines
5.3 KiB
Cheetah
{{/*
|
|
Copyright 2019-present Facebook Inc. All rights reserved.
|
|
This source code is licensed under the Apache 2.0 license found
|
|
in the LICENSE file in the root directory of this source tree.
|
|
*/}}
|
|
|
|
{{ define "client" }}
|
|
|
|
{{ $pkg := base $.Config.Package }}
|
|
{{ template "header" $ }}
|
|
|
|
import (
|
|
"log"
|
|
|
|
"{{ $.Config.Package }}/migrate"
|
|
{{ range $_, $n := $.Nodes }}
|
|
"{{ $n.Config.Package }}/{{ $n.Package }}"
|
|
{{- end }}
|
|
|
|
"github.com/facebookincubator/ent/dialect"
|
|
{{ range $_, $storage := $.Storage -}}
|
|
{{ range $_, $import := $storage.Imports -}}
|
|
"{{ $import }}"
|
|
{{ end -}}
|
|
{{ end -}}
|
|
)
|
|
|
|
// Client is the client that holds all ent builders.
|
|
type Client struct {
|
|
config
|
|
// Schema is the client for creating, migrating and dropping schema.
|
|
Schema *migrate.Schema
|
|
{{ range $_, $n := $.Nodes -}}
|
|
// {{ $n.Name }} is the client for interacting with the {{ $n.Name }} builders.
|
|
{{ $n.Name }} *{{ $n.Name }}Client
|
|
{{ end }}
|
|
}
|
|
|
|
// NewClient creates a new client configured with the given options.
|
|
func NewClient(opts ...Option) *Client {
|
|
c := config{log: log.Println}
|
|
c.options(opts...)
|
|
return &Client{
|
|
config: c,
|
|
Schema: migrate.NewSchema(c.driver),
|
|
{{ range $_, $n := $.Nodes -}}
|
|
{{ $n.Name }}: New{{ $n.Name }}Client(c),
|
|
{{ end -}}
|
|
}
|
|
}
|
|
|
|
// Tx returns a new transactional client.
|
|
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
|
if _, ok := c.driver.(*txDriver); ok {
|
|
return nil, fmt.Errorf("{{ $pkg }}: cannot start a transaction within a transaction")
|
|
}
|
|
tx, err := newTx(ctx, c.driver)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("{{ $pkg }}: starting a transaction: %v", err)
|
|
}
|
|
cfg := config{driver: tx, log: c.log, debug: c.debug}
|
|
return &Tx{
|
|
config: cfg,
|
|
{{ range $_, $n := $.Nodes -}}
|
|
{{ $n.Name }}: New{{ $n.Name }}Client(cfg),
|
|
{{ end -}}
|
|
}, nil
|
|
}
|
|
|
|
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
|
|
//
|
|
// client.Debug().
|
|
// {{ (index $.Nodes 0).Name }}.
|
|
// Query().
|
|
// Count(ctx)
|
|
//
|
|
func (c *Client) Debug() *Client {
|
|
if c.debug {
|
|
return c
|
|
}
|
|
cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true}
|
|
return &Client{
|
|
config: cfg,
|
|
Schema: migrate.NewSchema(cfg.driver),
|
|
{{ range $_, $n := $.Nodes -}}
|
|
{{ $n.Name }}: New{{ $n.Name }}Client(cfg),
|
|
{{ end -}}
|
|
}
|
|
}
|
|
|
|
{{ range $_, $n := $.Nodes -}}
|
|
{{ $client := print $n.Name "Client" }}
|
|
// {{ $client }} is a client for the {{ $n.Name }} schema.
|
|
type {{ $client }} struct {
|
|
config
|
|
}
|
|
|
|
{{ $rec := $n.Receiver }}{{ if eq $rec "c" }}{{ $rec = printf "%.2s" $n.Name | lower }}{{ end }}
|
|
|
|
// New{{ $client }} returns a client for the {{ $n.Name }} from the given config.
|
|
func New{{ $client }}(c config) *{{ $client }} {
|
|
return &{{ $client }}{config: c}
|
|
}
|
|
|
|
// Create returns a create builder for {{ $n.Name }}.
|
|
func (c *{{ $client }}) Create() *{{ $n.Name }}Create {
|
|
return &{{ $n.Name }}Create{config: c.config}
|
|
}
|
|
|
|
// Update returns an update builder for {{ $n.Name }}.
|
|
func (c *{{ $client }}) Update() *{{ $n.Name }}Update {
|
|
return &{{ $n.Name }}Update{config: c.config}
|
|
}
|
|
|
|
// UpdateOne returns an update builder for the given entity.
|
|
func (c *{{ $client }}) UpdateOne({{ $rec }} *{{ $n.Name }}) *{{ $n.Name }}UpdateOne {
|
|
return c.UpdateOneID({{ $rec }}.ID)
|
|
}
|
|
|
|
// UpdateOneID returns an update builder for the given id.
|
|
func (c *{{ $client }}) UpdateOneID(id {{ $n.ID.Type }}) *{{ $n.Name }}UpdateOne {
|
|
return &{{ $n.Name }}UpdateOne{config: c.config, id: id}
|
|
}
|
|
|
|
// Delete returns a delete builder for {{ $n.Name }}.
|
|
func (c *{{ $client }}) Delete() *{{ $n.Name }}Delete {
|
|
return &{{ $n.Name }}Delete{config: c.config}
|
|
}
|
|
|
|
// DeleteOne returns a delete builder for the given entity.
|
|
func (c *{{ $client }}) DeleteOne({{ $rec }} *{{ $n.Name }}) *{{ $n.Name }}DeleteOne {
|
|
return c.DeleteOneID({{ $rec }}.ID)
|
|
}
|
|
|
|
// DeleteOneID returns a delete builder for the given id.
|
|
func (c *{{ $client }}) DeleteOneID(id {{ $n.ID.Type }}) *{{ $n.Name }}DeleteOne {
|
|
return &{{ $n.Name }}DeleteOne{c.Delete().Where({{ $n.Package }}.ID(id))}
|
|
}
|
|
|
|
// Create returns a query builder for {{ $n.Name }}.
|
|
func (c *{{ $client }}) Query() *{{ $n.Name }}Query {
|
|
return &{{ $n.Name }}Query{config: c.config}
|
|
}
|
|
|
|
// Get returns a {{ $n.Name }} entity by its id.
|
|
func (c *{{ $client }}) Get(ctx context.Context, id {{ $n.ID.Type }}) (*{{ $n.Name }}, error) {
|
|
return c.Query().Where({{ $n.Package }}.ID(id)).Only(ctx)
|
|
}
|
|
|
|
// GetX is like Get, but panics if an error occurs.
|
|
func (c *{{ $client }}) GetX(ctx context.Context, id {{ $n.ID.Type }}) *{{ $n.Name }} {
|
|
{{ $rec }}, err := c.Get(ctx, id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return {{ $rec }}
|
|
}
|
|
|
|
{{ range $_, $e := $n.Edges }}
|
|
{{ $builder := print (pascal $e.Type.Name) "Query" }}
|
|
// Query{{ pascal $e.Name }} queries the {{ $e.Name }} edge of a {{ $n.Name }}.
|
|
func (c *{{ $client }}) Query{{ pascal $e.Name }}({{ $rec }} *{{ $n.Name }}) *{{ $builder }} {
|
|
query := &{{ $e.Type.Name }}Query{config: c.config}
|
|
{{ if gt (len $.Storage) 1 -}}
|
|
switch c.driver.Dialect() {
|
|
{{- range $_, $storage := $.Storage }}
|
|
case {{ join $storage.Dialects ", " }}:
|
|
{{- with extend $n "Receiver" $rec "Edge" $e -}}
|
|
{{ $tmpl := printf "dialect/%s/query/from" $storage }}
|
|
{{- xtemplate $tmpl . -}}
|
|
{{ end -}}
|
|
{{- end }}
|
|
}
|
|
{{- else -}}
|
|
{{- with extend $n "Receiver" $rec "Edge" $e -}}
|
|
{{ $tmpl := printf "dialect/%s/query/from" (index $.Storage 0) }}
|
|
{{- xtemplate $tmpl . -}}
|
|
{{ end -}}
|
|
{{- end }}
|
|
return query
|
|
}
|
|
{{ end }}
|
|
|
|
{{ end }}
|
|
{{ end }}
|
|
|