Skip to content

Commit

Permalink
New Crowdin updates (#718)
Browse files Browse the repository at this point in the history
* New translations write_plugins.md (Russian)

* New translations advanced_query.md (Turkish)

* New translations associations.md (Turkish)

* New translations write_plugins.md (Turkish)

* New translations advanced_query.md (Chinese Simplified)

* New translations associations.md (Chinese Simplified)

* New translations write_plugins.md (Chinese Simplified)

* New translations advanced_query.md (Portuguese, Brazilian)

* New translations associations.md (Portuguese, Brazilian)

* New translations write_plugins.md (Portuguese, Brazilian)

* New translations advanced_query.md (Indonesian)

* New translations associations.md (Indonesian)

* New translations write_plugins.md (Indonesian)

* New translations advanced_query.md (Persian)

* New translations associations.md (Persian)

* New translations write_plugins.md (Persian)

* New translations advanced_query.md (Azerbaijani)

* New translations associations.md (Azerbaijani)

* New translations write_plugins.md (Azerbaijani)

* New translations advanced_query.md (Hindi)

* New translations associations.md (Hindi)

* New translations write_plugins.md (Hindi)

* Update source file advanced_query.md

* Update source file associations.md

* Update source file write_driver.md

* Update source file write_plugins.md

* New translations associations.md (French)

* New translations write_plugins.md (French)

* New translations associations.md (Spanish)

* New translations write_plugins.md (Spanish)

* New translations associations.md (Arabic)

* New translations write_plugins.md (Arabic)

* New translations associations.md (German)

* New translations write_plugins.md (German)

* New translations associations.md (Italian)

* New translations write_plugins.md (Italian)

* New translations associations.md (Japanese)

* New translations write_plugins.md (Japanese)

* New translations associations.md (Korean)

* New translations write_plugins.md (Korean)

* New translations associations.md (Polish)

* New translations write_plugins.md (Polish)

* New translations associations.md (Russian)

* New translations write_plugins.md (Russian)

* New translations associations.md (Turkish)

* New translations write_plugins.md (Turkish)

* New translations associations.md (Chinese Simplified)

* New translations write_plugins.md (Chinese Simplified)

* New translations associations.md (Portuguese, Brazilian)

* New translations write_plugins.md (Portuguese, Brazilian)

* New translations associations.md (Indonesian)

* New translations write_plugins.md (Indonesian)

* New translations associations.md (Persian)

* New translations write_plugins.md (Persian)

* New translations associations.md (Azerbaijani)

* New translations write_plugins.md (Azerbaijani)

* New translations associations.md (Hindi)

* New translations write_plugins.md (Hindi)

* New translations write_driver.md (French)

* New translations write_driver.md (Spanish)

* New translations write_driver.md (Arabic)

* New translations write_driver.md (German)

* New translations write_driver.md (Italian)

* New translations write_driver.md (Japanese)

* New translations write_driver.md (Korean)

* New translations write_driver.md (Polish)

* New translations write_driver.md (Russian)

* New translations write_driver.md (Turkish)

* New translations write_driver.md (Chinese Simplified)

* New translations write_driver.md (Portuguese, Brazilian)

* New translations write_driver.md (Indonesian)

* New translations write_driver.md (Persian)

* New translations write_driver.md (Azerbaijani)

* New translations write_driver.md (Hindi)

* New translations write_plugins.md (French)

* New translations write_plugins.md (Spanish)

* New translations write_plugins.md (Arabic)

* New translations write_plugins.md (German)

* New translations write_plugins.md (Italian)

* New translations write_plugins.md (Japanese)

* New translations write_plugins.md (Korean)

* New translations write_plugins.md (Polish)

* New translations write_plugins.md (Russian)

* New translations write_plugins.md (Turkish)

* New translations write_plugins.md (Chinese Simplified)

* New translations write_plugins.md (Portuguese, Brazilian)

* New translations write_plugins.md (Indonesian)

* New translations write_plugins.md (Persian)

* New translations write_plugins.md (Azerbaijani)

* New translations write_plugins.md (Hindi)

* New translations context.md (French)

* New translations error_handling.md (French)

* New translations method_chaining.md (French)

* New translations write_driver.md (French)

* New translations context.md (Spanish)

* New translations error_handling.md (Spanish)

* New translations method_chaining.md (Spanish)

* New translations write_driver.md (Spanish)

* New translations context.md (Arabic)

* New translations error_handling.md (Arabic)

* New translations method_chaining.md (Arabic)

* New translations write_driver.md (Arabic)

* New translations context.md (German)

* New translations error_handling.md (German)

* New translations method_chaining.md (German)

* New translations write_driver.md (German)

* New translations context.md (Italian)

* New translations error_handling.md (Italian)

* New translations method_chaining.md (Italian)

* New translations write_driver.md (Italian)

* New translations context.md (Japanese)

* New translations error_handling.md (Japanese)

* New translations method_chaining.md (Japanese)

* New translations write_driver.md (Japanese)

* New translations context.md (Korean)

* New translations error_handling.md (Korean)

* New translations method_chaining.md (Korean)

* New translations write_driver.md (Korean)

* New translations context.md (Polish)

* New translations error_handling.md (Polish)

* New translations method_chaining.md (Polish)

* New translations write_driver.md (Polish)

* New translations context.md (Russian)

* New translations error_handling.md (Russian)

* New translations method_chaining.md (Russian)

* New translations write_driver.md (Russian)

* New translations context.md (Turkish)

* New translations error_handling.md (Turkish)

* New translations method_chaining.md (Turkish)

* New translations write_driver.md (Turkish)

* New translations context.md (Chinese Simplified)

* New translations error_handling.md (Chinese Simplified)

* New translations method_chaining.md (Chinese Simplified)

* New translations write_driver.md (Chinese Simplified)

* New translations context.md (Portuguese, Brazilian)

* New translations error_handling.md (Portuguese, Brazilian)

* New translations method_chaining.md (Portuguese, Brazilian)

* New translations write_driver.md (Portuguese, Brazilian)

* New translations context.md (Indonesian)

* New translations error_handling.md (Indonesian)

* New translations method_chaining.md (Indonesian)

* New translations write_driver.md (Indonesian)

* New translations context.md (Persian)

* New translations error_handling.md (Persian)

* New translations method_chaining.md (Persian)

* New translations write_driver.md (Persian)

* New translations context.md (Azerbaijani)

* New translations error_handling.md (Azerbaijani)

* New translations method_chaining.md (Azerbaijani)

* New translations write_driver.md (Azerbaijani)

* New translations context.md (Hindi)

* New translations error_handling.md (Hindi)

* New translations method_chaining.md (Hindi)

* New translations write_driver.md (Hindi)

* Update source file context.md

* Update source file error_handling.md

* Update source file method_chaining.md

* Update source file write_driver.md

* Update source file write_plugins.md

* New translations models.md (French)

* New translations models.md (Spanish)

* New translations models.md (Arabic)

* New translations models.md (German)

* New translations models.md (Italian)

* New translations models.md (Japanese)

* New translations models.md (Korean)

* New translations models.md (Polish)

* New translations models.md (Russian)

* New translations models.md (Turkish)

* New translations models.md (Chinese Simplified)

* New translations models.md (Portuguese, Brazilian)

* New translations models.md (Indonesian)

* New translations models.md (Persian)

* New translations models.md (Azerbaijani)

* New translations models.md (Hindi)

* Update source file models.md

* New translations dbresolver.md (French)

* New translations dbresolver.md (Spanish)

* New translations dbresolver.md (Arabic)

* New translations dbresolver.md (German)

* New translations dbresolver.md (Italian)

* New translations dbresolver.md (Japanese)

* New translations dbresolver.md (Korean)

* New translations dbresolver.md (Polish)

* New translations dbresolver.md (Russian)

* New translations dbresolver.md (Turkish)

* New translations dbresolver.md (Chinese Simplified)

* New translations dbresolver.md (Portuguese, Brazilian)

* New translations dbresolver.md (Indonesian)

* New translations dbresolver.md (Persian)

* New translations dbresolver.md (Azerbaijani)

* New translations dbresolver.md (Hindi)

* Update source file dbresolver.md

* New translations connecting_to_the_database.md (Chinese Simplified)

* New translations conventions.md (French)

* New translations many_to_many.md (French)

* New translations security.md (French)

* New translations conventions.md (Spanish)

* New translations many_to_many.md (Spanish)

* New translations security.md (Spanish)

* New translations conventions.md (Arabic)

* New translations many_to_many.md (Arabic)

* New translations security.md (Arabic)

* New translations conventions.md (German)

* New translations many_to_many.md (German)

* New translations security.md (German)

* New translations conventions.md (Italian)

* New translations many_to_many.md (Italian)

* New translations security.md (Italian)

* New translations conventions.md (Japanese)

* New translations many_to_many.md (Japanese)

* New translations security.md (Japanese)

* New translations conventions.md (Korean)

* New translations many_to_many.md (Korean)

* New translations security.md (Korean)

* New translations conventions.md (Polish)

* New translations many_to_many.md (Polish)

* New translations security.md (Polish)

* New translations conventions.md (Russian)

* New translations many_to_many.md (Russian)

* New translations security.md (Russian)

* New translations conventions.md (Turkish)

* New translations many_to_many.md (Turkish)

* New translations security.md (Turkish)

* New translations conventions.md (Chinese Simplified)

* New translations many_to_many.md (Chinese Simplified)

* New translations security.md (Chinese Simplified)

* New translations conventions.md (Portuguese, Brazilian)

* New translations many_to_many.md (Portuguese, Brazilian)

* New translations security.md (Portuguese, Brazilian)

* New translations conventions.md (Indonesian)

* New translations many_to_many.md (Indonesian)

* New translations security.md (Indonesian)

* New translations conventions.md (Persian)

* New translations many_to_many.md (Persian)

* New translations security.md (Persian)

* New translations conventions.md (Azerbaijani)

* New translations many_to_many.md (Azerbaijani)

* New translations security.md (Azerbaijani)

* New translations conventions.md (Hindi)

* New translations many_to_many.md (Hindi)

* New translations security.md (Hindi)

* Update source file conventions.md

* Update source file many_to_many.md

* Update source file security.md

* New translations gorm_config.md (Korean)

* New translations gorm_config.md (Korean)

* New translations advanced_query.md (Turkish)

* New translations community.md (Persian)

* New translations advanced_query.md (Persian)
  • Loading branch information
jinzhu authored Jan 18, 2024
1 parent 7ea3f83 commit bf0b9f5
Show file tree
Hide file tree
Showing 281 changed files with 12,181 additions and 6,713 deletions.
327 changes: 208 additions & 119 deletions pages/ar_SA/docs/advanced_query.md

Large diffs are not rendered by default.

259 changes: 153 additions & 106 deletions pages/ar_SA/docs/associations.md

Large diffs are not rendered by default.

54 changes: 23 additions & 31 deletions pages/ar_SA/docs/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ title: Context
layout: page
---

GORM provides Context support, you can use it with method `WithContext`
GORM's context support, enabled by the `WithContext` method, is a powerful feature that enhances the flexibility and control of database operations in Go applications. It allows for context management across different operational modes, timeout settings, and even integration into hooks/callbacks and middlewares. Let's delve into these various aspects:

## Single Session Mode
### Single Session Mode

Single session mode usually used when you want to perform a single operation
Single session mode is appropriate for executing individual operations. It ensures that the specific operation is executed within the context's scope, allowing for better control and monitoring.

```go
Find(&users)
Expand All @@ -21,19 +21,19 @@ r. Get("/user", func(w http. Value("DB").(*gorm.DB)
db.
```

## Continuous session mode
### Continuous Session Mode

Continuous session mode is usually used when you want to perform a group of operations, for example:
Continuous session mode is ideal for performing a series of related operations. It maintains the context across these operations, which is particularly useful in scenarios like transactions.

```go
tx := db. WithContext(ctx)
tx. First(&user, 1)
tx. Model(&user). Update("role", "admin")
```

## Context timeout
### Context Timeout

You can pass in a context with a timeout to `db.WithContext` to set timeout for long running queries, for example:
Setting a timeout on the context passed to `db.WithContext` can control the duration of long-running queries. This is crucial for maintaining performance and avoiding resource lock-ups in database interactions.

```go
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
Expand All @@ -42,23 +42,21 @@ defer cancel()
db.WithContext(ctx).Find(&users)
```

## Context in Hooks/Callbacks
### Context in Hooks/Callbacks

You can access the `Context` object from the current `Statement`, for example:
The context can also be accessed within GORM's hooks/callbacks. This enables contextual information to be used during these lifecycle events.

```go
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
ctx := tx.Statement.Context
// ...
// ... use context
return
}
```

## Chi Middleware Example
### Integration with Chi Middleware

Continuous session mode which might be helpful when handling API requests, for example, you can set up `*gorm.DB` with Timeout Context in middlewares, and then use the `*gorm.DB` when processing all requests

Following is a Chi middleware example:
GORM's context support extends to web server middlewares, such as those in the Chi router. This allows setting a context with a timeout for all database operations within the scope of a web request.

```go
func SetDBMiddleware(next http.Handler) http.Handler {
Expand All @@ -69,32 +67,26 @@ func SetDBMiddleware(next http.Handler) http.Handler {
})
}

// Router setup
r := chi.NewRouter()
r.Use(SetDBMiddleware)

// Route handlers
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
db, ok := ctx.Value("DB").(*gorm.DB)

var users []User
db.Find(&users)

// lots of db operations
db, ok := r.Context().Value("DB").(*gorm.DB)
// ... db operations
})

r.Get("/user", func(w http.ResponseWriter, r *http.Request) {
db, ok := ctx.Value("DB").(*gorm.DB)

var user User
db.First(&user)

// lots of db operations
db, ok := r.Context().Value("DB").(*gorm.DB)
// ... db operations
})
```

{% note %}
**NOTE** Setting `Context` with `WithContext` is goroutine-safe, refer [Session](session.html) for details
{% endnote %}
**Note**: Setting the `Context` with `WithContext` is goroutine-safe. This ensures that database operations are safely managed across multiple goroutines. For more details, refer to the [Session documentation](session.html) in GORM.

### Logger Integration

## Logger
GORM's logger also accepts `Context`, which can be used for log tracking and integrating with existing logging infrastructures.

Logger accepts `Context` too, you can use it for log tracking, refer [Logger](logger.html) for details
Refer to [Logger documentation](logger.html) for more details.
2 changes: 1 addition & 1 deletion pages/ar_SA/docs/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Check out [From SubQuery](advanced_query.html#from_subquery) for how to use SubQ

### <span id="naming_strategy">NamingStrategy</span>

GORM allows users change the default naming conventions by overriding the default `NamingStrategy`, which is used to build `TableName`, `ColumnName`, `JoinTableName`, `RelationshipFKName`, `CheckerName`, `IndexName`, Check out [GORM Config](gorm_config.html#naming_strategy) for details
GORM allows users to change the default naming conventions by overriding the default `NamingStrategy`, which is used to build `TableName`, `ColumnName`, `JoinTableName`, `RelationshipFKName`, `CheckerName`, `IndexName`, Check out [GORM Config](gorm_config.html#naming_strategy) for details

## Column Name

Expand Down
2 changes: 1 addition & 1 deletion pages/ar_SA/docs/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ db.Omit("Name", "Age", "CreatedAt").Create(&user)

## <span id="batch_insert">Batch Insert</span>

To efficiently insert large number of records, pass a slice to the `Create` method. GORM will generate a single SQL statement to insert all the data and backfill primary key values, hook methods will be invoked too. It will begin a **transaction** when records can be splited into multiple batches.
To efficiently insert large number of records, pass a slice to the `Create` method. GORM will generate a single SQL statement to insert all the data and backfill primary key values, hook methods will be invoked too. It will begin a **transaction** when records can be split into multiple batches.

```go
var users = []User{{Name: "jinzhu1"}, {Name: "jinzhu2"}, {Name: "jinzhu3"}}
Expand Down
6 changes: 3 additions & 3 deletions pages/ar_SA/docs/dbresolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ But you can specifies which DB to use before starting a transaction, for example

```go
// Start transaction based on default replicas db
tx := DB.Clauses(dbresolver.Read).Begin()
tx := db.Clauses(dbresolver.Read).Begin()

// Start transaction based on default sources db
tx := DB.Clauses(dbresolver.Write).Begin()
tx := db.Clauses(dbresolver.Write).Begin()

// Start transaction based on `secondary`'s sources
tx := DB.Clauses(dbresolver.Use("secondary"), dbresolver.Write).Begin()
tx := db.Clauses(dbresolver.Use("secondary"), dbresolver.Write).Begin()
```

## Load Balancing
Expand Down
90 changes: 71 additions & 19 deletions pages/ar_SA/docs/error_handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,101 @@ title: Error Handling
layout: page
---

In Go, error handling is important.
Effective error handling is a cornerstone of robust application development in Go, particularly when interacting with databases using GORM. GORM's approach to error handling, influenced by its chainable API, requires a nuanced understanding.

You are encouraged to do error check after any [Finisher Methods](method_chaining.html#finisher_method)
## Basic Error Handling

## Error Handling
GORM integrates error handling into its chainable method syntax. The `*gorm.DB` instance contains an `Error` field, which is set when an error occurs. The common practice is to check this field after executing database operations, especially after [Finisher Methods](method_chaining.html#finisher_method).

Error handling in GORM is different than idiomatic Go code because of its chainable API.

If any error occurs, GORM will set `*gorm.DB`'s `Error` field, you need to check it like this:
After a chain of methods, it's crucial to check the `Error` field:

```go
// Check if returns RecordNotFound error
err := db. First(&user, 100). Error
errors.
if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
// Handle error...
}
```

Or
Or alternatively:

```go
// Check if returns RecordNotFound error
err := db. First(&user, 100). Error
errors.
if result := db.Where("name = ?", "jinzhu").First(&user); result.Error != nil {
// Handle error...
}
```

## ErrRecordNotFound
## `ErrRecordNotFound`

GORM returns `ErrRecordNotFound` when failed to find data with `First`, `Last`, `Take`, if there are several errors happened, you can check the `ErrRecordNotFound` error with `errors. Is`, for example:
GORM returns `ErrRecordNotFound` when no record is found using methods like `First`, `Last`, `Take`.

```go
// Check if returns RecordNotFound error
err := db.First(&user, 100).Error
errors.Is(err, gorm.ErrRecordNotFound)
if errors.Is(err, gorm.ErrRecordNotFound) {
// Handle record not found error...
}
```

## Handling Error Codes

Many databases return errors with specific codes, which can be indicative of various issues like constraint violations, connection problems, or syntax errors. Handling these error codes in GORM requires parsing the error returned by the database and extracting the relevant code

- **Example: Handling MySQL Error Codes**

```go
import (
"github.com/go-sql-driver/mysql"
"gorm.io/gorm"
)

// ...

result := db.Create(&newRecord)
if result.Error != nil {
if mysqlErr, ok := result.Error.(*mysql.MySQLError); ok {
switch mysqlErr.Number {
case 1062: // MySQL code for duplicate entry
// Handle duplicate entry
// Add cases for other specific error codes
default:
// Handle other errors
}
} else {
// Handle non-MySQL errors or unknown errors
}
}
```

## Dialect Translated Errors

If you would like to be able to use the dialect translated errors(like ErrDuplicatedKey), then enable the TranslateError flag when opening a db connection.
GORM can return specific errors related to the database dialect being used, when `TranslateError` is enabled, GORM converts database-specific errors into its own generalized errors.

```go
db, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{TranslateError: true})
```

- **ErrDuplicatedKey**

This error occurs when an insert operation violates a unique constraint:

```go
result := db.Create(&newRecord)
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
// Handle duplicated key error...
}
```

- **ErrForeignKeyViolated**

This error is encountered when a foreign key constraint is violated:

```go
result := db.Create(&newRecord)
if errors.Is(result.Error, gorm.ErrForeignKeyViolated) {
// Handle foreign key violation error...
}
```

By enabling `TranslateError`, GORM provides a more unified way of handling errors across different databases, translating database-specific errors into common GORM error types.

## Errors

[Errors List](https://github.com/go-gorm/gorm/blob/master/errors.go)
For a complete list of errors that GORM can return, refer to the [Errors List](https://github.com/go-gorm/gorm/blob/master/errors.go) in GORM's documentation.
2 changes: 1 addition & 1 deletion pages/ar_SA/docs/many_to_many.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Please checkout [Association Mode](associations.html#Association-Mode) for worki
type Person struct {
ID int
Name string
Addresses []Address `gorm:"many2many:person_addressses;"`
Addresses []Address `gorm:"many2many:person_addresses;"`
}

type Address struct {
Expand Down
Loading

0 comments on commit bf0b9f5

Please sign in to comment.