The golang-gorm-datatables package is a Go library which provides support for jQuery DataTables server-side processing using Gorm. It contains functions for generating SQL queries for filtering, sorting and pagination, as well as functions for counting the total and filtered records.
- Go 1.24 or higher
- Gorm 1.26 or higher
To install the Request package, run the following command:
go get github.com/ZihxS/golang-gorm-datatables
package main
import (
// ...
dt "github.com/ZihxS/golang-gorm-datatables" // [FOCUS HERE]
// ...
)
func main() {
// ...
type User struct {
ID int
Name string
Age int
}
// ...
// example using mux router
r.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
req, err := dt.ParseRequest(r) // parse the request [FOCUS HERE]
if err != nil {
http.Error(w, fmt.Sprintf("Error processing request: %v", err), http.StatusInternalServerError)
return
}
tx := db.Model(&User{}) // gorm query [FOCUS HERE]
response, err := dt.New(tx).Req(*req).Make() // make datatables [FOCUS HERE]
if err != nil {
http.Error(w, fmt.Sprintf("Error processing request: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
})
}
// ...
You can visit this link to see more example and documentation:
- Documentation:
- Golang Packages Website: https://pkg.go.dev/github.com/ZihxS/golang-gorm-datatables
- Our Documentation Website: (WIP: https://github.com/ZihxS/golang-gorm-datatables-docs)
- Example on Server Side: https://github.com/ZihxS/golang-gorm-datatables-be-examples
- Example on Client Side: https://github.com/ZihxS/golang-gorm-datatables-fe-examples
- Create example on server side.
- Create example on client side.
- Create own documentation website.
- Add slice or array format for datatables response.
- Well proper for all possible complex queries.
- Better performance and memory allocation.
- Support for datatables exporting.
Contributions are welcome! If you'd like to contribute to the Request package, please follow these steps:
- Fork the repository to your own GitHub account.
- Clone the repository to your local machine.
- Run
make deps
to install dependencies. - Make changes to the code, following the guidelines below.
- Run
make test
to ensure your changes pass the test suite. - Run
make fmt
to ensure your code is formatted correctly. - Run
make lint
to ensure your code lint is clean. - Commit your changes with a meaningful commit message.
- Push your changes to your forked repository.
- Submit a pull request to the original repository.
The Makefile provides several commands to help with development and testing:
make deps
: Installs dependencies for the package.make test
: Runs the test suite for the package.make test-coverage
: Runs the test suite with code coverage analysis.make view-coverage
: Opens the test coverage report in your web browser.make fmt
: Formats the code according to the Go standard.make lint
: Runs the linter to check for coding style issues.make build
: Builds the package and its dependencies.make clean
: Removes any build artifacts and temporary files.
When contributing to the Request package, please follow these guidelines:
- Use the Go standard coding style.
- Write comprehensive tests for any new functionality.
- Keep commits small and focused on a single change.
- Use meaningful commit messages that describe the change.
The Request package is licensed under the MIT License.