this repo showcases the usage of finder for creating crud rest APIs with go & postgres.
this project utilises docker to run migration
use make help to view command list:
- copy
.env.exampleinto a.envfile and modify database connection info - run
make migrate/up/allto create thecategoriestable in the database - use air to run the application
- or use
go run .
- or use
it features 5 actions inspired by laravel:
use base url: localhost:8000/api/v1
-
index: a list of the model, can be:-
searched:
- the columns used for search are inside
meta.search_columns /categories?q=main- search can utilize relational joins fields using the join alias when defined as:
func (m *Model) SearchFields() *[]string { return &[]string{ "description", "rel:stores.name", "rel:stores.description", } }
- the columns used for search are inside
-
paginated
- pagination info are inside
metaproprty of the response /categories?page=1&paginate=12
- pagination info are inside
-
sorted:
- the columns used for sorting are inside
meta.columns - ascending or descending order, and can use multiple column sort
/categories?sorts=-created_at/categories?sorts=created_at/categories?sorts=depth,-created_at
- the columns used for sorting are inside
-
filtered:
- the columns used for filtering are inside
meta.columns /categories?filters=parent_id:null/categories?filters=parent_id:not-null- filter can use multiple
columnand multiplecriteriavalues /categories?filters=parent_id:null,is_disabled:false|true- filter can use
opsbetweencolumn:op:criteria, ops options below:eq: db equal of=eg:is_disabled:eq:truenq: db equal of!=eg:is_disabled:nq:truegt: db equal of>eg:price:gt:5gte: db equal of>=eg:price:gte:5lt: db equal of<eg:price:lt:5lte: db equal of<=eg:price:lte:5ex: special filter for single relations of model eg:roles:ex:5
- another eg:
/orders?filters=status:eq:completed|cancelled|returned
- the columns used for filtering are inside
-
mix:
- all the above can be used in combination with each other
-
note:
finder.Modelinterface can useRelations()to define relations that can be used withqorfiltersfor example if you have auserstable withrolesmany-to-many relation:
func (m *Model) Relations() *[]finder.RelationField { return &[]finder.RelationField{ { Table: "roles", Join: &finder.Join{ From: "users.id", To: "roles.id", }, Through: &finder.Through{ Table: "user_roles", Join: &finder.Join{ From: "user_roles.user_id", To: "user_roles.role_id", }, }, }, } }
-
-
show:GET /categories/:idreturns single model result -
destroy:DELETE /categories/:iddeletes single model -
store:POST /categoriescreates single model- sample main category body:
{ "id": "4db02628-50a2-4022-ac82-1949853bd728" "name": { "en":"main", "ar":"رئيسية" } }- sample sub category body:
{ "name": { "en":"branch", "ar":"فرعية" }, "is_disabled":true, "is_featured":true, "parent":{ "id": "4db02628-50a2-4022-ac82-1949853bd728" } } -
update:PUT /categories/:idupdates single model result- sample update category body:
{ "name": { "en":"branch name" }, "is_disabled":false, "is_featured":true }