Skip to content

Database

Learn how to use databases within your HTTP app.

Notice

Mantis currently only supports SQLite databases.

Configuration

Configure your database connection in your HTTP App:

v
module main

import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response
import khalyomede.mantis.database { Database, DatabaseConnection } 

struct Post {
  id int
  title string
}

fn main() {
  app := create_app(
    database: Database{ 
      connection: DatabaseConnection{
        driver: .sqlite
        database: 'database.sqlite'
      }
    }
    routes: [
      route.get(
        name: "post.show"
        path: "/post/{id}"
        callback: fn (app App) Response {
          // The ? indicates this might not find an ID
          id := app.route_parameter("id") or {
            return response.html(
              content: "Post not found",
              status: .not_found
            )
          }

          mut app_ref := app

          posts := app_ref.database.all[Post]("SELECT id, title FROM posts") or { 
            return response.html(
              content: "Database error: ${err.msg()}",
              status: .server_error
            )
          }

          post := posts[0] or {
            return response.html(
              content: "Post not found",
              status: .not_found
            )
          }

          return response.html(content: "Post (id ${id}): ${post.title}")
        }
      )
    ]
  )

  app.serve() or { panic(err) }
}