This repo shows how to use GoooQo to access a single table.
The test.db is a database of sqlite which contains a simple table t_user
with 4 rows.
id | username | nickname | memo | valid | |
---|---|---|---|---|---|
1 | f0rb | f0rb@163.com | test1 | true | |
2 | user2 | test2@qq.com | test2 | false | |
3 | user3 | test3@qq.com | test3 | memo | true |
4 | user4 | test4@qq.com | test4 | true |
With GoooQo, we only need to define the structs UserEntity
and UserQuery
in user.go,
where UserEntity
corresponds to the table t_user
and UserQuery
is used to build the query clause.
The goooqo.PageQuery
embedded in UserQuery
helps to implement sorting and paging operations.
Then we build the web APIs listened on http://localhost:9090/user/
by the following two lines:
userDataAccess := rdb.NewTxDataAccess[UserEntity](tm)
goooqo.BuildRestService[UserEntity, UserQuery]("/user/", userDataAccess)
Run the main method in demo.go and visit the the following URLs to check the response:
- http://localhost:9090/user/
- http://localhost:9090/user/3
- http://localhost:9090/user/?pageNumber=2&pageSize=2
- http://localhost:9090/user/?sort=id,desc
- http://localhost:9090/user/?sort=memo,desc%3Bemail,desc
- http://localhost:9090/user/?idIn=1,3
- http://localhost:9090/user/?idGt=2&emailContain=qq
- http://localhost:9090/user/?emailContain=qq
- http://localhost:9090/user/?emailContain=qq&memoNull=true
- http://localhost:9090/user/?emailContain=qq&memoNull=false
And there are also some sample POST/PUT/PATCH/DELETE
operations listed in user.http to take a try.
NOTE:The web module of GoooQo is for tests and demos only, not a core feature.
This project is under the Apache Licence v2.