DBCore

Generate applications powered by your database.

This software is in ALPHA development.

What makes DBCore special?

DBCore reads your database and generates high-quality, statically typed Go code (for the API) and TypeScript (for the UI) based on templates.

Not happy with the built-in templates? Write your own to generate any kind of application you want.

Screenshot of the examples/todo application.

Generating a Go API

In your project root folder create a dbcore.yml:

project: $project-name
database:
  # Or 'mysql'
  dialect: postgres

  # Omit if localhost
  host: $host-or-ip

  # Omit if 5432 for postgres or 3306 for mysql
  port: $port

  database: $database-name
  username: $username
  password: $password

api:
  template: go
  outDir: $outDir

  # e.g. "v1/", for URLs
  routerPrefix: $router-prefix

  audit:
    # Disabled by default
    enabled: true
    createdAt: $createdAtColumn
    updatedAt: $updatedAtColumn
    deletedAt: $deletedAtColumn

  auth:
    # Disabled by default
    enabled: true
    table: $users
    # Column for username field
    username: $username
    # Column for password field, bcrypt hash is stored
    password: $password

  extra:
    repo: $your-repo

The dbcore.yml file controls code generation. You'll also need an api.yml file for runtime behavior:

database:
  # Or use 'mysql'
  dialect: postgres
  # Or use '$username:$password@tcp($host:$port)/$database?sql_mode=ANSI', sql_mode=ANSI is required
  dsn: postgres://$username:$password@$host:$port/$database?sslmode=disable

address: :9090

session:
  duration: 2hr
  secret: $my-secret-signing-key

Clone the repo and run make && make install within the repo root. You will need Docker, only.

Then go to your project directory and run dbcore . to generate the project. Finally run go run cmd/main.go to start the server.

API Specification

Authentication

When authentication is enabled, make a JSON POST request with {"username": "$your-username", "password": "$your-password"} to /$version/session/start to generate a token. It is valid for 2 hours by default.

Passwords are stored as BCrypt hashes.

You can store this token in the au cookie or you can submit it as bearer token by setting the Authorization header to BEARER $your-token.

Example

$ curl -X POST -d '{"username": "alex", "password": "alex"}' localhost:9090/v1/session/start
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTA3MjkyNjMsImlhdCI6MTU5MDcyMjA2MywibmJmIjoxNTkwNzIyMDYzLCJ1c2VybmFtZSI6InBoaWwifQ.4AAveeFRpXckn3cRFyCQew2V7jmcU4OOYH68wcv6afI"}

Get many rows from a table

Make a GET request to /$version/$table.

Query parameters

Parameter Definition Example
limit Number of rows to return limit=25
offset Number of rows to skip offset=0
sortColumn Column to sort on sortColumn=id
sortOrder Order to sort (one of asc or desc) sortOrder=desc
filter SQL where filter to eliminate results filter=id>3

Example

$ curl 'localhost:9090/v1/users?limit=25&offset=0&sortColumn=id&sortOrder=desc&filter=id=1'
{
  "total": 1,
  "data": [
    {
      "id": 1,
      "username": "alex",
      "password": "<REDACTED>",
      "name": "Alex"
    },
  ]
}

Create a new row

Make a POST request to /$version/$table.

Example

$ curl -X POST -d '{"username": "alex", "password": "alex", "name": "Alex"}' localhost:9090/v1/users
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex"}

Get a row

Make a GET request to /$version/$table/$id.

This endpoint is only available if the table has a primary key.

Example

$ curl localhost:9090/v1/users/1
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex"}

Update a row

Make a PUT request to /$version/$table/$id.

This endpoint is only available if the table has a primary key.

Example

$ curl -X PUT -d '{"id": 1, "username": "alex", "password": "alex", "name": "Alex K"}' localhost:9090/v1/users/1
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex K"}

Delete a row

Make a DELETE request to /$version/$table/$id.

This endpoint is only available if the table has a primary key.

Example

$ curl -X DELETE localhost:9090/v1/users/1

Generating a TypeScript/React UI

Using the same configuration as for the API, after running dbcore . you can run yarn start in browser/ to start the application at http://localhost:9091.

Use browser.defaultRoute to override the default home page.