diff --git a/pages/ar_SA/community.md b/pages/ar_SA/community.md index f5af60c1032..c1870d5b190 100644 --- a/pages/ar_SA/community.md +++ b/pages/ar_SA/community.md @@ -42,6 +42,7 @@ layout: page * [gorm-cache - GORM query cache plugin](https://github.com/liyuan1125/gorm-cache) * [deepgorm - Nested map filtering](https://github.com/survivorbat/gorm-deep-filtering) * [oracle - GORM Oracle driver](https://github.com/CengSin/oracle) +* [Go ORM Helper - GORM Code Completion Goland Plugin](https://github.com/maiqingqiang/go-orm-helper) ## Contribute to this page diff --git a/pages/ar_SA/docs/create.md b/pages/ar_SA/docs/create.md index 13677bb8a8e..fe1c6c09480 100644 --- a/pages/ar_SA/docs/create.md +++ b/pages/ar_SA/docs/create.md @@ -23,6 +23,10 @@ result.Error // returns error result.RowsAffected // returns inserted records count ``` +{% note warn %} +**NOTE** You cannot pass a struct to 'create', so you should pass a pointer to the data. +{% endnote %} + ## Create Record With Selected Fields Create a record and assign a value to the fields specified. diff --git a/pages/ar_SA/docs/dbresolver.md b/pages/ar_SA/docs/dbresolver.md index 2d67fe05322..2fe2aa9db18 100644 --- a/pages/ar_SA/docs/dbresolver.md +++ b/pages/ar_SA/docs/dbresolver.md @@ -33,7 +33,7 @@ db.Use(dbresolver.Register(dbresolver.Config{ // sources/replicas load balancing policy Policy: dbresolver.RandomPolicy{}, // print sources/replicas mode in logger - ResolverModeReplica: true, + TraceResolverMode: true, }).Register(dbresolver.Config{ // use `db1` as sources (DB's default connection), `db5` as replicas for `User`, `Address` Replicas: []gorm.Dialector{mysql.Open("db5_dsn")}, diff --git a/pages/ar_SA/docs/migration.md b/pages/ar_SA/docs/migration.md index 0600a6ddfe1..b330c334e9e 100644 --- a/pages/ar_SA/docs/migration.md +++ b/pages/ar_SA/docs/migration.md @@ -264,9 +264,26 @@ db.Migrator().RenameIndex(&User{}, "idx_name", "idx_name_2") GORM creates constraints when auto migrating or creating table, see [Constraints](constraints.html) or [Database Indexes](indexes.html) for details +## Atlas Integration + +[Atlas](https://atlasgo.io) is an open-source database migration tool that has an official integration with GORM. + +While GORM's `AutoMigrate` feature works in most cases, at some point you many need to switch to a [versioned migrations](https://atlasgo.io/concepts/declarative-vs-versioned#versioned-migrations) strategy. + +Once this happens, the responsibility for planning migration scripts and making sure they are in line with what GORM expects at runtime is moved to developers. + +Atlas can automatically plan database schema migrations for developers using the official [GORM Provider](https://github.com/ariga/atlas-provider-gorm). After configuring the provider you can automatically plan migrations by running: +```bash +atlas migrate diff --env gorm +``` + +To learn how to use Atlas with GORM, check out the [official documentation](https://atlasgo.io/guides/orms/gorm). + + + ## Other Migration Tools -GORM's AutoMigrate works well for most cases, but if you are looking for more serious migration tools, GORM provides a generic DB interface that might be helpful for you. +To use GORM with other Go-based migration tools, GORM provides a generic DB interface that might be helpful for you. ```go // returns `*sql.DB` diff --git a/pages/ar_SA/gen/database_to_structs.md b/pages/ar_SA/gen/database_to_structs.md index 89e82f8a3da..8fd89393272 100644 --- a/pages/ar_SA/gen/database_to_structs.md +++ b/pages/ar_SA/gen/database_to_structs.md @@ -247,3 +247,49 @@ Specify datatype mapping between field type and db column type. g.WithDataTypeMap(dataMap) ``` +## Generate From Sql + +Gen supports generate structs from sql following GORM conventions, it can be used like: + +```go +package main + +import ( + "gorm.io/gorm" + "gorm.io/rawsql" +) + +func main() { + g := gen.NewGenerator(gen.Config{ + OutPath: "../query", + Mode: gen.WithoutContext|gen.WithDefaultQuery|gen.WithQueryInterface, // generate mode + }) + // https://github.com/go-gorm/rawsql/blob/master/tests/gen_test.go + gormdb, _ := gorm.Open(rawsql.New(rawsql.Config{ + //SQL: rawsql, //create table sql + FilePath: []string{ + //"./sql/user.sql", // create table sql file + "./test_sql", // create table sql file directory + }, + })) + g.UseDB(gormdb) // reuse your gorm db + + // Generate basic type-safe DAO API for struct `model.User` following conventions + + g.ApplyBasic( + // Generate struct `User` based on table `users` + g.GenerateModel("users"), + + // Generate struct `Employee` based on table `users` + g.GenerateModelAs("users", "Employee"), + + ) +g.ApplyBasic( +// Generate structs from all tables of current database +g.GenerateAllTable()..., +) + // Generate the code + g.Execute() +} + +``` diff --git a/pages/ar_SA/gen/query.md b/pages/ar_SA/gen/query.md index 2bc6ba1bfec..fd26cd256fa 100644 --- a/pages/ar_SA/gen/query.md +++ b/pages/ar_SA/gen/query.md @@ -505,6 +505,7 @@ u.WithContext(ctx).Assign(field.Attrs(map[string]interface{}{"age": 20})).Where( // Found user with `name` = `gen`, update it with Assign attributes u.WithContext(ctx).Assign(field.Attrs(&model.User{Name: "gen_assign"}).Select(dal.User.ALL)).Where(u.Name.Eq("gen")).FirstOrInit() + // SELECT * FROM USERS WHERE name = gen' ORDER BY id LIMIT 1; // user -> User{ID: 111, Name: "gen", Age: 20} ``` @@ -516,7 +517,7 @@ Get first matched record or create a new one with given conditions (only works w ```go // Found user with `name` = `gen` -result := u.WithContext(ctx).Where(u.Name.Eq(jinzhu)).FirstOrCreate(&user) +result := u.WithContext(ctx).Where(u.Name.Eq(jinzhu)).FirstOrCreate() // user -> User{ID: 111, Name: "gen", "Age": 18} // result.RowsAffected // => 0 ``` @@ -558,6 +559,7 @@ u.WithContext(ctx).Assign(u.Age.Value(20)).Where(u.Name.Eq("gen")).FirstOrCreate // user -> User{ID: 111, Name: "gen", Age: 20} ``` + ### Struct & Map Conditions ```go diff --git a/pages/ar_SA/gen/rawsql_driver.html b/pages/ar_SA/gen/rawsql_driver.html index f66bc540bcb..1a2a0d75540 100644 --- a/pages/ar_SA/gen/rawsql_driver.html +++ b/pages/ar_SA/gen/rawsql_driver.html @@ -3,8 +3,8 @@
- - + +