/
generator.go
165 lines (135 loc) · 4.57 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package generator
import (
"encoding/json"
"os"
"path"
"strings"
"github.com/steebchen/prisma-client-go/generator/ast/dmmf"
"github.com/steebchen/prisma-client-go/generator/ast/transform"
"github.com/steebchen/prisma-client-go/generator/types"
"github.com/steebchen/prisma-client-go/logger"
)
// Root describes the generator output root.
type Root struct {
Generator Generator `json:"generator"`
OtherGenerators []Generator `json:"otherGenerators"`
SchemaPath string `json:"schemaPath"`
// Version contains the version hash of the Prisma engine
Version string `json:"version"`
DMMF dmmf.Document `json:"DMMF"`
Datasources []Datasource `json:"datasources"`
// Datamodel provides the raw string of the Prisma datamodel.
Datamodel string `json:"datamodel"`
// BinaryPaths (optional)
BinaryPaths BinaryPaths `json:"binaryPaths"`
AST *transform.AST `json:"ast"`
}
func (r *Root) EscapedDatamodel() string {
return strings.ReplaceAll(r.Datamodel, "`", "'")
}
func (r *Root) GetDatasourcesJSON() string {
ds := r.Datasources[0]
data, err := json.Marshal([]Datasource{ds})
if err != nil {
panic(err)
}
return string(data)
}
func (r *Root) GetEngineType() string {
if str := os.Getenv("PRISMA_CLIENT_ENGINE_TYPE"); str != "" {
return str
}
if str := r.Generator.Config.EngineType; str != "" {
return str
}
return "binary"
}
// Config describes the options for the Prisma Client Go generator
type Config struct {
EngineType string `json:"engineType"`
Package types.String `json:"package"`
DisableGitignore string `json:"disableGitignore"`
DisableGoBinaries string `json:"disableGoBinaries"`
}
// Generator describes a generator defined in the Prisma schema.
type Generator struct {
// Output holds the file path of where the client gets generated in.
Output *Value `json:"output"`
Name types.String `json:"name"`
Provider *Value `json:"provider"`
Config Config `json:"config"`
BinaryTargets []BinaryTarget `json:"binaryTargets"`
// PinnedBinaryTarget (optional)
PinnedBinaryTarget string `json:"pinnedBinaryTarget"`
}
type BinaryTarget struct {
FromEnvVar string `json:"fromEnvVar"`
Value string `json:"value"`
}
type Value struct {
FromEnvVar string `json:"fromEnvVar"`
Value string `json:"value"`
}
// Provider describes the Database of this datasource.
type Provider string
// Provider values
//
//goland:noinspection GoUnusedConst
const (
ProviderMySQL Provider = "mysql"
ProviderMongo Provider = "mongo"
ProviderSQLite Provider = "sqlite"
ProviderPostgreSQL Provider = "postgresql"
)
// Datasource describes a Prisma data source of any database type.
type Datasource struct {
Name types.String `json:"name"`
Provider Provider `json:"provider"`
ActiveProvider Provider `json:"activeProvider"`
URL EnvValue `json:"url"`
Config interface{} `json:"config"`
}
// EnvValue contains a string value and optionally information if, and if yes from where, an env var is used for this value.
type EnvValue struct {
// FromEnvVar (optional)
FromEnvVar string `json:"fromEnvVar"`
Value string `json:"value"`
}
func (r *Root) GetSanitizedDatasourceURL() string {
ds := r.Datasources[0]
url := ds.URL.Value
if ds.ActiveProvider != ProviderSQLite {
return url
}
url = strings.ReplaceAll(url, "file:", "")
url = strings.ReplaceAll(url, "sqlite:", "")
if path.IsAbs(url) {
return "file:" + url
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
// get prisma schema path from prisma schema file
schemaPath := path.Dir(r.SchemaPath)
// trim /private as it is some kind of symlink on macOS
schemaPath = strings.Replace(schemaPath, "/private", "", 1)
// replace /schema.prisma as we need just the directory
schemaPath = strings.Replace(schemaPath, "schema.prisma", "", 1)
// use the schema path to locate the sqlite file (as the path is relative to the schema)
url = path.Join(schemaPath, url)
// replace absolute URL to relative
url = strings.Replace(url, wd, "", 1)
url = strings.Trim(url, "/")
// prefix with sqlite: to make it a valid connection string again
url = "file:" + url
logger.Debug.Printf("sanitizing relative sqlite path %s\n", url)
return url
}
// BinaryPaths holds the information of the paths to the Prisma binaries.
type BinaryPaths struct {
// MigrationEngine (optional)
MigrationEngine map[string]string `json:"migrationEngine"` // key target, value path
// QueryEngine (optional)
QueryEngine map[string]string `json:"queryEngine"`
}