mirror of
https://github.com/ent/ent.git
synced 2026-03-05 19:35:23 +03:00
dialect/entsql: add entsql annotation (#876)
This commit is contained in:
27
dialect/entsql/annotation.go
Normal file
27
dialect/entsql/annotation.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
package entsql
|
||||
|
||||
import "github.com/facebook/ent/schema"
|
||||
|
||||
// Annotation is a builtin schema annotation for attaching
|
||||
// SQL metadata to schema objects for both codegen and runtime.
|
||||
type Annotation struct {
|
||||
// The Table option allows overriding the default table
|
||||
// name that is generated by ent. For example:
|
||||
//
|
||||
// entsql.Annotation{
|
||||
// Table: "Users",
|
||||
// }
|
||||
//
|
||||
Table string
|
||||
}
|
||||
|
||||
// Name describes the annotation name.
|
||||
func (Annotation) Name() string {
|
||||
return "EntSQL"
|
||||
}
|
||||
|
||||
var _ schema.Annotation = (*Annotation)(nil)
|
||||
19
ent.go
19
ent.go
@@ -46,6 +46,16 @@ type (
|
||||
// Indexes returns the indexes of the schema.
|
||||
Indexes() []Index
|
||||
// Config returns an optional config for the schema.
|
||||
//
|
||||
// Deprecated: the Config method predates the Annotations method and it
|
||||
// is planned be removed in v0.5.0. New code should use Annotations instead.
|
||||
//
|
||||
// func (T) Annotations() []schema.Annotation {
|
||||
// return []schema.Annotation{
|
||||
// entsql.Annotation{Table: "Name"},
|
||||
// }
|
||||
// }
|
||||
//
|
||||
Config() Config
|
||||
// Mixin returns an optional list of Mixin to extends
|
||||
// the schema.
|
||||
@@ -109,6 +119,15 @@ type (
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Deprecated: the Config object predates the schema.Annotation method and it
|
||||
// is planned be removed in v0.5.0. New code should use Annotations instead.
|
||||
//
|
||||
// func (T) Annotations() []schema.Annotation {
|
||||
// return []schema.Annotation{
|
||||
// entsql.Annotation{Table: "Name"},
|
||||
// }
|
||||
// }
|
||||
//
|
||||
Config struct {
|
||||
// A Table is an optional table name defined for the schema.
|
||||
Table string
|
||||
|
||||
@@ -6,6 +6,7 @@ package gen
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/token"
|
||||
"go/types"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/facebook/ent"
|
||||
"github.com/facebook/ent/dialect/entsql"
|
||||
"github.com/facebook/ent/dialect/sql/schema"
|
||||
"github.com/facebook/ent/entc/load"
|
||||
"github.com/facebook/ent/schema/field"
|
||||
@@ -222,12 +224,27 @@ func (t Type) Label() string {
|
||||
|
||||
// Table returns SQL table name of the node/type.
|
||||
func (t Type) Table() string {
|
||||
if table := t.EntSQL().Table; table != "" {
|
||||
return table
|
||||
}
|
||||
if t.schema != nil && t.schema.Config.Table != "" {
|
||||
return t.schema.Config.Table
|
||||
}
|
||||
return snake(rules.Pluralize(t.Name))
|
||||
}
|
||||
|
||||
// EntSQL returns the EntSQL annotation if exists, or an empty one.
|
||||
func (t Type) EntSQL() entsql.Annotation {
|
||||
annotate := entsql.Annotation{}
|
||||
if t.Annotations == nil || t.Annotations[annotate.Name()] == nil {
|
||||
return annotate
|
||||
}
|
||||
if buf, err := json.Marshal(t.Annotations[annotate.Name()]); err == nil {
|
||||
_ = json.Unmarshal(buf, &annotate)
|
||||
}
|
||||
return annotate
|
||||
}
|
||||
|
||||
// Package returns the package name of this node.
|
||||
func (t Type) Package() string {
|
||||
return strings.ToLower(t.Name)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/facebook/ent/dialect/entsql"
|
||||
"github.com/facebook/ent/dialect/sql"
|
||||
"github.com/facebook/ent/entc/integration/config/ent"
|
||||
"github.com/facebook/ent/entc/integration/config/ent/migrate"
|
||||
@@ -26,8 +27,8 @@ func TestSchemaConfig(t *testing.T) {
|
||||
require.NoError(t, client.Schema.Create(ctx, migrate.WithGlobalUniqueID(true)))
|
||||
client.User.Create().SaveX(ctx)
|
||||
|
||||
// check that the table was created with the given custom name.
|
||||
table := schema.User{}.Config().Table
|
||||
// Check that the table was created with the given custom name.
|
||||
table := schema.User{}.Annotations()[0].(entsql.Annotation).Table
|
||||
query, args := sql.Select().Count().
|
||||
From(sql.Table("sqlite_master")).
|
||||
Where(sql.And(sql.EQ("type", "table"), sql.EQ("name", table))).
|
||||
|
||||
@@ -4,16 +4,20 @@
|
||||
|
||||
package schema
|
||||
|
||||
import "github.com/facebook/ent"
|
||||
import (
|
||||
"github.com/facebook/ent"
|
||||
"github.com/facebook/ent/dialect/entsql"
|
||||
"github.com/facebook/ent/schema"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Config of the User.
|
||||
func (User) Config() ent.Config {
|
||||
return ent.Config{
|
||||
Table: "Users",
|
||||
// Annotations of the User schema.
|
||||
func (User) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "Users"},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user