Hello PocketBase

PocketBase - open source backend in 1 file

Introduction

Recently I have build several backends using FastAPI, Django, And sometimes with Java Spring. And I planned to build some using Swift for Server Backend frameworks like Vapor. I like django for many things, and have been using django from it’s 1.8 version. Later, the FastAPI came out and I started working with FastAPI, tried to built several micro-servers with that.

Django was and still (until the date that I am writing this post ) my first option for commercial projects and other Freelancing project, it is easy to use and very robust framework with batteries included, like Authentication, Authorization, Role management, User, Admin interface, Middlewares ( like caching ..) and many more, it is something that already defined, and you need to fill the blanks to make it run, there is no headache for selecting databases, you probabily just go for PostgreSQL (why not? ) and you will use DRF (Or Django Ninja recently) for API endpoitns, you can use hundreds and thousand of already available great apps for your projects, like Django OAuth Toolkit which I used in many projects, and it is still serving my company Authentication and Authorization system called “iAM”.

Django is already very well, why i switched to FastAPI or other backends then ? (I am not gonna talk about java, it is a whole different story), well, Django is so well that sometimes it dose more than i want, for many modern projects, what i want to build is just a simle backend for my commercial projects, (like National Parks mobile apps or Trading Eagle backend or B2B business management systems), I am not using django templates for years by now, cause we are nowadays all working with ‘Backend-JSON-Frontend’ mode, while frontend can be one of:

  • A website frontend with:
    • React and Next.js (B2B – Commercial Project)
    • Vue and Nuxt.js (Trading Eangle – Commercial Joint Project)
    • Svelte and Svelte Kit (Journalim Frontend – Open Source Project)
    • Flutter Web (UniSat DataHub – Educational, NGO, Non-Profit Joint Project)
  • A desktop GUI application:
    • Electron (iMartingle Bot —Personal Commercial Project, once built for Binary Options )
  • A mobile application:
    • Android or iOS Application (includes flutter apps like National Parks KZ – A commercial App for Gov)
  • A CLI application:
    • Brain (Personal Algo-trading App – Brain, the CLI endpoint)

One of core Django Idea is MVC but we are not using V with templates anymore, and for later projects that I used Django Ninja (for example the National Parks Backend), I did not even write any view classes, so django becoming some how not necessary and very heavy for me and my company, also it is becoming much more harder for my small startup to activly maintaing our django based code base, (cause dockernizing django apps are much harder thant dockernizing a FastAPI app, you know it), and there are less and less people with mastering Django proficiency, so I some how decided to try something other, and i selected those:

  • Java & Spring
  • Swift & Vapor
  • Node & Express
  • Python & FastAPI

Until today, there is not meaninfgull progress about this thing, and I am still in the process of ‘trying out’ all different stacks and consider all their pros and cons.

So, here i am today, to try out PocketBase.

PocketBace at first glance

PocketBase is an open source backend consisting of embedded database (SQLite) with realtime subscriptions, built-in auth management, convenient dashboard UI and simple REST-ish API.1

PocketBase doc describes it as a backend with:

  1. Embedded database
  2. Real-time
  3. Auth management
  4. dashboard UI
  5. REST API

Let’ analyze now,

It is embedded database, first idea come to my mind at this point is, how i am gonna scale it?

It is real-time, the second question come to me is, dose it fast enough ?

It has auth management, no question until now,

It has a dashboard UI, i probabily dont need it and will not use it also,

It has a REST API, it is the core functions we want, with all those questions, let’s continue descover it’s documentations,

The easiest way to get started is to download the prebuilt minimal PocketBase app:

Oh, i have to admit that, i love how it works, this is a pro obviously, by using a Go program,

Nice GIFsTenor

Once you’ve downloaded and extracted the archive, you could start the application by running the following console command in the extracted directory: ./pocketbase serve.

This seems easy, let’s first discover all of it’s cli commands, so i tried to run ./pocketbase --help, but there i god an error from the OS:

2023-02-02 at 15.26.42

Obviously it is not a hard thing to handle, just opened the directory, right click on pocketbase executive then run

Let’s try again, this time we got what we want,

./pocketbase --help
PocketBase CLI

Usage:
  pocketbase [command]

Available Commands:
  migrate     Executes app DB migration scripts
  serve       Starts the web server (default to 127.0.0.1:8090)
  upgrade     Upgrades your existing pb_data to be compatible with the v0.8.x changes

Flags:
      --automigrate            enable/disable auto migrations (default true)
      --debug                  enable debug mode, aka. showing more detailed logs
      --dir string             the PocketBase data directory (default "pb_data")
      --encryptionEnv string   the env variable whose value of 32 characters will be used
                               as encryption key for the app settings (default none)
  -h, --help                   help for pocketbase
      --indexFallback          fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA) (default true)
      --migrationsDir string   the directory with the user defined migrations
      --publicDir string       the directory to serve static files (default "pb_public")
  -v, --version                version for pocketbase

Use "pocketbase [command] --help" for more information about a command.

There are three main commands available for us to use,

  • migrate
  • serve
  • upgrade
./pocketbase migrate --help
Supported arguments are:
- up            - runs all available migrations
- down [number] - reverts the last [number] applied migrations
- create name   - creates new blank migration template file
- collections   - creates new migration file with snapshot of the local collections configuration

Usage:
  pocketbase migrate [flags]

Flags:
  -h, --help   help for migrate

Global Flags:
      --automigrate            enable/disable auto migrations (default true)
      --debug                  enable debug mode, aka. showing more detailed logs
      --dir string             the PocketBase data directory (default "pb_data")
      --encryptionEnv string   the env variable whose value of 32 characters will be used
                               as encryption key for the app settings (default none)
      --indexFallback          fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA) (default true)
      --migrationsDir string   the directory with the user defined migrations
      --publicDir string       the directory to serve static files (default "pb_public")
./pocketbase serve --help
Starts the web server (default to 127.0.0.1:8090)

Usage:
  pocketbase serve [flags]

Flags:
  -h, --help              help for serve
      --http string       api HTTP server address (default "127.0.0.1:8090")
      --https string      api HTTPS server address (auto TLS via Let's Encrypt)
                          the incoming --http address traffic also will be redirected to this address
      --origins strings   CORS allowed domain origins list (default [*])

Global Flags:
      --automigrate            enable/disable auto migrations (default true)
      --debug                  enable debug mode, aka. showing more detailed logs
      --dir string             the PocketBase data directory (default "pb_data")
      --encryptionEnv string   the env variable whose value of 32 characters will be used
                               as encryption key for the app settings (default none)
      --indexFallback          fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA) (default true)
      --migrationsDir string   the directory with the user defined migrations
      --publicDir string       the directory to serve static files (default "pb_public")
./pocketbase upgrade --help

Upgrades your existing pb_data to be compatible with the v0.8.x changes
Prerequisites and caveats:
- already upgraded to v0.7.*
- no existing users collection
- existing profiles collection fields like email, username, verified, etc. will be renamed to username2, email2, etc.

Usage:
  pocketbase upgrade [flags]

Flags:
  -h, --help   help for upgrade

Global Flags:
      --automigrate            enable/disable auto migrations (default true)
      --debug                  enable debug mode, aka. showing more detailed logs
      --dir string             the PocketBase data directory (default "pb_data")
      --encryptionEnv string   the env variable whose value of 32 characters will be used
                               as encryption key for the app settings (default none)
      --indexFallback          fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA) (default true)
      --migrationsDir string   the directory with the user defined migrations
      --publicDir string       the directory to serve static files (default "pb_public")

From the cli params and commands only, we can find the PocketBase is very simple in design and work.

Basics - Collection

I am very much familiar to the word collection cause the previous backend api tool I used, the kinto-storage (Kinto)also uses this term, however, kinto has something more than collections and records, except from collections and recors kinto also has buckets

concepts-general

PocketBase vs SQLite

Field Example values Supported modifiers
text "", "example"  
editor "", "<p>example</p>"  
number 0, -1, 1, 1.5 + (add), - (subtract)
bool false, true  
email "", "[email protected]"  
url "", "https://example.com"  
date "", "2022-01-01 00:00:00.000Z"  
select (single) "", "optionA"  
select (multiple) [], ["optionA", "optionB"] + (append), - (remove)
relation (single) "", "JJ2YRU30FBG8MqX"  
relation (multiple) [], ["JJ2YRU30FBG8MqX", "eP2jCr1h3NGtsbz"] + (append), - (remove)
file (single) "", "example123_Ab24ZjL.png"  
file (multiple) [], ["file1_Ab24ZjL.png", "file2_Frq24ZjL.txt"] - (remove)
json any json value  

For now, I gues pocketbase enough for many things, even maybe ok to build some of mine Brain trading microservices, but it wont be enough for any gis related topic, for now, as it dose not support storing gis data, for that purpose, PostGIS maybe the best.

I also made an xmind for this.

From the basic information we got so far, it is not hard to find that, PocketBase might be an alternative to PostgREST

TL;TD : So far So good.

  1. https://pocketbase.io/docs/