wdb-go is a Go Client library for wunderDB.
To use wdb-go, install it using go get
go get github.com/TanmoySG/wdb-go
Then in your go code, import it as
import wdbgo "github.com/TanmoySG/wdb-go"
Initialize a new wdb client using NewClient()
method passing the URL of wdb instance, username and password of the authenticating user.
wdb, err := wdbgo.NewClient(uname, pword, wdbAddress, nil)
if err != nil {
log.Fatal(err)
}
You an also pass in a custom application name to be included as the user-agent. If you do not need app name you can pass nil
, as above. NewWdbClient
checks if the connection can be eshtablished, otherwise returns error. To skip the first time check you can pass wdbgo.SkipConnectionCheck
as the last argument.
wdb, err := wdbgo.NewWdbClient(uname, pword, wdbAddress, nil, wdbgo.SkipConnectionCheck)
To crete a user, use the CreateUser()
function.
err := wdb.CreateUser(username, password)
It returns error if no user was created, else returns nil error.
To create a role, use CreateRole()
method - passing the name
of role to create, and the lists of Allowed and Denied Privileges.
err := wdb.CreateRole(roleName, allowedPrivileges, deniedPrivileges)
Use the privileges available in the github.com/TanmoySG/wdb-go/privileges
sub-package as privileges.PrivilegeName
, refer to this for more
Once user and role are created use the GrantRole()
method to grant the role to the user - passing the username, role-name and database to grant the role on. In addition to the database, a role can also be granted on a collection by passing the collection name as the last argument, which is an option argument.
// role granted on collection
err := wdb.GrantRole(username, roleName, database, collection)
// collection is an optional argument, role granted only on database
err := wdb.GrantRole(username, roleName, database)
The collection has to be a child of the database, if the role needs to be granted on the collection.
Use the ListRoles()
method to list the roles in wunderdb. Returns map of roles and error.
roles, err := wdb.ListRoles()