DEV Community

Nicol Acosta
Nicol Acosta

Posted on

API Prototypes with dbb: Another step to better prototypes

Second Post to know the other features in dbb

Introduction

In the previous post i added more features that i added to dbb and in this time, its not different, i added more stuff to build even more good stuff

Admin UI

This stuff are inside since the last post hehe, but i forgot to mention in the post so here is the missing part

It's really simple, just open your server on the / route and will load the admin page, the API it's on the /api/v1 route so will not conflict with the other routes

Will show obviously the schemas, basic CRUD for any of them, with cool create/edit forms based on the datatypes, the basic that any schema scan admin needs to have

New datatype

In this case it's just one type, that it's list of values, you can define just one type or more, and will be saved in database in the same way

To define in your schema, just add mark your type inside the list brackets ["string"]

{
  "schemas": [
    {
      "name": "some_schema_name",
      "fields": {
        "name": "string",
        "flags": ["string"],
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

In the Admin UI will show a input with the elements on text separated by comma, so ["some", "value"] will be displayed as "some, value" and in the same format will save as list

Image description

Value generator

Maybe in some case you need generate some key or some like that, so you can define this generator format in the generate key in the json

{
  "schemas": [
    {
      "name": "some_schema_name",
      "fields": {
        "name": "string",
        "sku": "string",
      },
      "generate": {
        "sku": "$str(4)$-$num(4)$-$sym(4)$-$str_num(4)$-$any(4)$"
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

The way to create a generator it's simple, just define in the generator your format with the function with $ on start and end of the function, will works in this format $str(4)$ and you can use this generator functions str, num, sym, str_num, any

In the Admin UI just just with keep empty to generate the value

Relations

You can relate one schema to another using the in the relations key in the json

{
  "schemas": [
    {
      "name": "orders",
      "fields": {
        "estimated_delivery_time": "datetime"
      }
    },
    {
      "name": "order_product",
      "fields": {
        "order_id": "string",
        "product_id": "string"
      },
      "relations": {
        "order_id": "orders",
        "product_id": "products"
      }
    },
    {
      "name": "products",
      "fields": {
        "name": "string",
        "description": "string",
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

You can define also if some relation is mandatory adding :mandatory to your key name

Load relations in query

Why have relations if you can't load this related data in the same call, so of course you can, adding relations as query param with your relations to load (yes, allow multiple)

GET /api/v1/:schema?relations=relation_1,relation_2
Enter fullscreen mode Exit fullscreen mode

Pretty simple to use

Admin UI basic auth

The last is a basic auth to Admin UI, simple to configure, with env vars, not much secure it's just to not have unsecured page in the system

ADMIN_AUTH_USERNAME: user
ADMIN_AUTH_PASSWORD: pass
Enter fullscreen mode Exit fullscreen mode

Next steps

The next to complete it's create a more complete experience on the admin, add functions like operations on query calls, some seeds to populate the system as demo and more features to create a complete solution as prototype

Top comments (0)