|
| 1 | +# jQuery DataTables API for Go with Gorm |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Go 1.24 or higher |
| 8 | +- Gorm 1.26 or higher |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +To install the Request package, run the following command: |
| 13 | + |
| 14 | +```bash |
| 15 | +go get github.com/ZihxS/golang-gorm-datatables |
| 16 | +``` |
| 17 | + |
| 18 | +## Simple Usage |
| 19 | + |
| 20 | +```go |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + // ... |
| 25 | + |
| 26 | + "github.com/ZihxS/golang-gorm-datatables" // [👉🏼 FOCUS HERE] |
| 27 | + |
| 28 | + // ... |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + // ... |
| 33 | + |
| 34 | + type User struct { |
| 35 | + ID int |
| 36 | + Name string |
| 37 | + Age int |
| 38 | + } |
| 39 | + |
| 40 | + // ... |
| 41 | + |
| 42 | + // example using mux router |
| 43 | + r.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { |
| 44 | + req, err := datatables.ParseRequest(r) // parse the request [👉🏼 FOCUS HERE] |
| 45 | + if err != nil { |
| 46 | + http.Error(w, fmt.Sprintf("Error processing request: %v", err), http.StatusInternalServerError) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + tx := db.Model(&User{}) // gorm query [👉🏼 FOCUS HERE] |
| 51 | + response, err := datatables.New(tx).Req(*req).Make() // make datatables [👉🏼 FOCUS HERE] |
| 52 | + if err != nil { |
| 53 | + http.Error(w, fmt.Sprintf("Error processing request: %v", err), http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + w.Header().Set("Content-Type", "application/json") |
| 58 | + _ = json.NewEncoder(w).Encode(response) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +// ... |
| 63 | +``` |
| 64 | + |
| 65 | +## More Example & Documentation |
| 66 | + |
| 67 | +You can visit this link to see more example and documentation: |
| 68 | +- Documentation: ? |
| 69 | +- Example on Server Side: ? |
| 70 | +- Example on Client Side: ? |
| 71 | + |
| 72 | +## Contributing |
| 73 | + |
| 74 | +Contributions are welcome! If you'd like to contribute to the Request package, please follow these steps: |
| 75 | + |
| 76 | +1. Fork the repository to your own GitHub account. |
| 77 | +2. Clone the repository to your local machine. |
| 78 | +3. Run `make deps` to install dependencies. |
| 79 | +4. Make changes to the code, following the guidelines below. |
| 80 | +5. Run `make test` to ensure your changes pass the test suite. |
| 81 | +6. Run `make fmt` to ensure your code is formatted correctly. |
| 82 | +7. Run `make lint` to ensure your code lint is clean. |
| 83 | +8. Commit your changes with a meaningful commit message. |
| 84 | +9. Push your changes to your forked repository. |
| 85 | +10. Submit a pull request to the original repository. |
| 86 | + |
| 87 | +### Makefile Commands |
| 88 | + |
| 89 | +The Makefile provides several commands to help with development and testing: |
| 90 | + |
| 91 | +* `make deps`: Installs dependencies for the package. |
| 92 | +* `make test`: Runs the test suite for the package. |
| 93 | +* `make test-coverage`: Runs the test suite with code coverage analysis. |
| 94 | +* `make view-coverage`: Opens the test coverage report in your web browser. |
| 95 | +* `make fmt`: Formats the code according to the Go standard. |
| 96 | +* `make lint`: Runs the linter to check for coding style issues. |
| 97 | +* `make build`: Builds the package and its dependencies. |
| 98 | +* `make clean`: Removes any build artifacts and temporary files. |
| 99 | + |
| 100 | +### Guidelines |
| 101 | + |
| 102 | +When contributing to the Request package, please follow these guidelines: |
| 103 | + |
| 104 | +* Use the Go standard coding style. |
| 105 | +* Write comprehensive tests for any new functionality. |
| 106 | +* Keep commits small and focused on a single change. |
| 107 | +* Use meaningful commit messages that describe the change. |
| 108 | + |
| 109 | +## License |
| 110 | + |
| 111 | +The Request package is licensed under the MIT License. |
| 112 | + |
| 113 | +## Authors |
| 114 | + |
| 115 | +* [Muhammad Saleh Solahudin](https://github.com/ZihxS) |
| 116 | + |
| 117 | +## Contributors |
| 118 | + |
| 119 | +## Credits |
| 120 | + |
| 121 | +- [DataTables](https://datatables.net) |
| 122 | +- [Gorm](https://gorm.io) |
0 commit comments