uAadmin the Golang Web Framework #10 Schema Basics

Abdullah Alrasheed
3 min readDec 6, 2018

--

IMPORTANT (BEFORE YOU START)

If you installed uAdmin before December 4th version 0.1.0-beta.5, please update your uAdmin installation (If you are not sure, just update it anyway):

go get -u github.com/uadmin/uadmin/...

After you do that go to your project’s folder and run:

uadmin prepare

This will update uAdmin templates and static files.

Schema Basics

Each model you register in uAdmin using uadmin.Register() generates a schema in the type of a uadmin.ModelSchema. These model schema instances contain all the information the system need to know about your model and it’s fields.

These uadmin.ModelSchema instances are stored in uadmin.Schema which is a map of string that returns a uadmin.ModelSchema where the key of the map if the name of the model in small letters.

Let’s take a look at your model Todo in models/todo.go:

...
type Todo struct {
uadmin.Model
Name string `uadmin:"required;pattern:.{5,100};pattern_msg:Name length should be 5-100 letters"`
Description string `uadmin:"html"`
TargetDate *time.Time `uadmin:"required"`
Progress int `uadmin:"progress_bar:30:red,70:yellow,100:green;min:0;max:100"`
Task Task
TaskID uint
}
...

When you register this model using uadmin.Register, uAdmin generates a uadmin.ModelSchema of your model. Let’s examine this schema and see what can we do with it.

In our app so far we have two models:

  • Todo
  • Task

After registering both models using uadmin.Register and registering the inlines, we can check the schema of both models. To check first the Todo model’s schema, you can access it using uadmin.Schema["todo"]. Notice that we used todo NOT Todo because all models are registered using their name is small letters.

The schema of our Todo model stored in uadmin.Schema["todo"] is a uadmin.ModelSchema object which has the following fields:

  • Name: The name of the Model. In this case equals “Todo”
  • DisplayName: A human readable version of the name of the Model. In this “Todo. But if the model name was “TodoItem” the DisplayName would be “Todo Item”
  • ModelName: A the same as the Name but in small letters. In this case equals “todo”
  • ModelID: (Data) A place holder to store the primary key of a single row for form processing.
  • Inlines: A list of associated inlines to this model.
  • InlinesData: (Data) A place holder to store the data of the inlines.
  • Fields: A list of uadmin.F type representing the fields of the model.
  • IncludeFormJS: A list of string where you could add URLs to javascript files that uAdmin will run when a form view of this model is rendered.
  • IncludeListJS: A list of string where you could add URLs to javascript files that uAdmin will run when a list view of this model is rendered.
  • FormModifier: A function that you could pass that will allow you to modify the schema when rendering a form. It will pass to you the a pointer to the schema so you could modify it and a copy of the Model that is being rendered and the user access it to be able to customize per user (or per user group).
  • ListModifier: A function that you could pass that will allow you to modify the schema when rendering a list. It will pass to you the a pointer to the schema so you could modify it and the user access it to be able to customize per user (or per user group).

The schema also has one method which is GetFieldByName() which allows you to get any uadmin.F object representing a field by name instead of searching for it in a the Fields list.

Now we will use this method to make one the description field required:

To do that, open main.go and add this code:

import (
"github.com/twistedhardware/todo/models"
"github.com/uadmin/uadmin"
)

func main() {
uadmin.Register(
models.Todo{},
models.Task{},
)
uadmin.RegisterInlines(models.Task{}, map[string]string{
"Todo": "TaskID",
})
// Modify Model Schema
uadmin.Schema["todo"].FieldByName("Description").Required = true
// Setup Email
uadmin.EmailSMTPServer = "smtp.gmail.com"
uadmin.EmailSMTPServerPort = 587
uadmin.EmailFrom = "TODO System <myemail@gmail.com>"
uadmin.EmailUsername = "myemail@gmail.com"
uadmin.EmailPassword = "my very hard password!"

uadmin.SiteName = "Todo App"
uadmin.StartServer()
//uadmin.StartSecureServer("pub.pem", "priv.pem")
}

This will come is very handy once we go over uadmin.F in the next tutorial.

Congrats, now you understand the “magic” that makes uAdmin works which is the schema.

In the next part we will talk about field schema and explore what we can do with it.

--

--