entc: add option to use entc as package

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

Reviewed By: alexsn

Differential Revision: D17783580

fbshipit-source-id: 597f124a28415fef66b0b16811ad2acac8df631d
This commit is contained in:
Ariel Mashraki
2019-10-07 06:59:24 -07:00
committed by Facebook Github Bot
parent e668326c5e
commit 7597f07912
46 changed files with 2231 additions and 154 deletions

View File

@@ -9,7 +9,7 @@ title: Introduction
`entc` run the following command:
```bash
go get github.com/facebookincubator/ent/entc/cmd/entc
go get github.com/facebookincubator/ent/cmd/entc
```
## Initialize A New Schema
@@ -33,12 +33,6 @@ the assets for working with your entities. Run the following command:
entc generate ./ent/schema
```
You should note that `goimports` is required for the codegen, and it can be installed using:
```bash
go get -u golang.org/x/tools/cmd/goimports
```
The `generate` command generates the following assets for the schemas:
- `Client` and `Tx` objects used for interacting with the graph.
@@ -47,6 +41,24 @@ The `generate` command generates the following assets for the schemas:
- Package containing constants and predicates used for interacting with the builders.
- A `migrate` package for SQL dialects. See [Migration](migrate.md) for more info.
## Version Compatibility Between `entc` And `ent`
When working with `entc` in a project, you want to make sure that the version being
used by `entc` is **identical** to the `ent` version used by your project.
One of the options for achieving this is asking `go generate` to use the version
mentioned in the `go.mod` file when running `entc`.
Add a `generate.go` file to your project under `<project>/ent`:
```go
package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate ./schema
```
And run `go generate ./ent` from your project root directory in order to run `entc` for your project.
## Code Generation Options
For more info about codegen options, run `entc generate -h`:
@@ -83,6 +95,35 @@ a file with the same name as the template.
Example of a custom template provides a `Node` API for GraphQL -
[Github](https://github.com/facebookincubator/ent/blob/master/entc/integration/template/ent/template/node.tmpl).
## Use `entc` As A Package
Another option for running `entc` is to use it as a package as follows:
```go
package main
import (
"log"
"github.com/facebookincubator/ent/entc"
"github.com/facebookincubator/ent/entc/gen"
"github.com/facebookincubator/ent/schema/field"
)
func main() {
err := entc.Generate("./schema", &gen.Config{
Header: "// Your Custom Header",
IDType: &field.TypeInfo{Type: field.TypeInt},
})
if err != nil {
log.Fatal("running ent codegen:", err)
}
}
```
The full example exists in [GitHub](https://github.com/facebookincubator/ent/tree/master/examples/entcpkg).
## Schema Description
In order to get a description of your graph schema, run:

View File

@@ -17,7 +17,7 @@ sidebar_label: Quick Introduction
## Installation
```console
go get github.com/facebookincubator/ent/entc/cmd/entc
go get github.com/facebookincubator/ent/cmd/entc
```
After installing `entc` (the code generator for `ent`), you should have it in your `PATH`.