DEV Community

Cover image for Pocketbase, readymade backend within clicks
fayismahmood
fayismahmood

Posted on • Edited on

Pocketbase, readymade backend within clicks

As for a freelance developer his most works would be something like shopping cart, or content management services. However the task in the backend would be the database, file storage, and authentication management.

In each of his work he fixes almost the same problems, while some chooses services like firebase and supabase for database management and other common backend services. But the problem comes when these services are unaffordable as it's expensive or hard to configure.

Here in this short article introduces a new service, which would be a solution for this problem.

What's Pocketbase?

enter image description here
Pocketbase is an open source backend service, written in Go programming language, which enables a realtime database, file storage, authentication system, role and permission manager and email engine with an elegant admin ui dashboard to manage databases and other services.

Pocketbase supports javascript and dart SDKs officially in order to access the backend from the client side. This makes full stack development easier than ever, as the developer shouldn't be bothered with databases, role and permission management, realtime websockets and other backend services, as Pocketbase enables these all services within clicks.

Why Pocketbase.

Here are several reasons for preferring Pocketbase over other services like firebase and others.

  • Open source, so it is hostable in cloud services like vultr free of
    cost and customisable according to your use case.

    • Single file executable. So, it is easy to host on cloud instances, just start by running the executable file, nothing to configure.
    • Cross platform support. Supports Windows, Linux and Mac operating systems.
    • Statically build application. It’s built on top of GoLang, so it’s blazing fast and memory safe.So it would be better performing even on a basic $2.5 vultr instance_Pocketbase memory usage is about 12MB in Windows task manager. _
    • Easy to Integrate. Pocketbse officially provides Javascript and Dart Sdk which is helpful to integrate with flutter mobile apps and javascript frameworks like react and vue.

Quick start with Pocketbase.

Pocketbase is easy to get started within a few steps.

Step 1: Download and Unzip

you can download pocketbase for any of Linux, WIndows, Mac platforms from pocketbase.io documentation page. then unzip the downloaded zip with your favorite zip extractor.

Step 2: Run Pocketbase

You can run pocketbase using any console application. run console application in unzipped folder and run the command

./pocketbase serve
Enter fullscreen mode Exit fullscreen mode

in Windows

pocketbase.exe serve
Enter fullscreen mode Exit fullscreen mode

That's all to run a Pocketbase backend.

The console will print out the routes for the admin panel, static server and rest api server.

Go to the admin url (usually this would be : http://localhost:8090/)

First time you visit the admin panel you have to create an admin account. and you can open the admin account using these credentials.

Working with Pocketbase Collections.

You can create and manage database collections with the Pocketbase admin panel by clicking on create New Collection in the collection section.

This will open a drawer to define your database schema, here you can add fields for your collection in the field tab and role and permissions in the Api rules tab. After setting fields and api rules you can click the create button to create your collection.You can upload files by creating file field in collection fields,

Whenever you create a collection the collection would be displayed at the left corner as a button, by clicking the button you can add, edit, delete records to the collection.

Manage Users.

In order to create users and manage you can click on the users button. Users panel allows you to create, delete, edit users and their roles as admin and user. To create a user you have to provide the user email id and password , once you created a user Pocketbase send an verification mail to the email id to user verification.

In Pocketbase every user has a basic credential modal including email id and password, and a profile model which admin can customize them.

Working with web APIs.

Pocketbase officially supports Dart and Javascript SDKs, which would be helpful to integrate with mobile apps and web application frameworks, like Flutter, Vanilla Javascript, React and Vue. This tutorial will focus on Javascript SDK.

Step 1
Firstly you have to install Pocketbase npm package using the command
npm i pocketbase

Step 2
After the installation is completed you can import this into the frontend code. and connect with the server using the Pocketbase class.

import PocketBase from  'pocketbase';
    const client =  new  PocketBase('http://127.0.0.1:8090');
Enter fullscreen mode Exit fullscreen mode

Step 3
Authenticate with your credentials, by logging in you would be allowed to access Pocketbase as the user permitted by admin.

const adminAuthData =  await client.admins.authViaEmail('test@example.com',  '123456');
Enter fullscreen mode Exit fullscreen mode

Pocketbase provides various types of authentication methods like google auth and other, refer the official docs for more details.

As you logged in you can access collections and all other services. For example you can list the collection records using Pocketbase.records.getList function

const pageResult =  await client.records.getList('demo',  1, 50,  { filter:  'created >= "2022-01-01 00:00:00"',});
Enter fullscreen mode Exit fullscreen mode

Refer the official doc for further details

Going to production

Pocketbase is self-hostable and easy to deploy, no configuration and no dependency is needed as it is completely portable, so it could deploy just by uploading and executing the file.

The example shown here is based on linux server,
Step 1
Firstly you have to allow Pocketbase ports in the server by the command

[root@dev ~]$ ufw allow 80  && ufw allow 443
Enter fullscreen mode Exit fullscreen mode

Step 2

Then run the pocketbase executable file by the command

  [root@dev ~]$ ./pocketbase serve --http="yourdomain.com:80"  --https="yourdomain.com:443"
Enter fullscreen mode Exit fullscreen mode

Pocketbase server will run on “yourdomain.com”

Adding into Service

Instead of using previous method, you have to add Pocketbase into service. By adding into service the server would be active after you restart your server. in order to add into service you have to create a service file namely pocketbase.service and edit the file as shown below.

[Unit]

Description = pocketbase



[Service]

Type = simple

User =  YOUR_USER

Group =  YOUR_GROUP

LimitNOFILE =  4096

Restart = always

RestartSec = 5s

StandardOutput = append:/your/path/to/logs/errors.log

StandardError = append:/your/path/to/logs/errors.log

ExecStart =  /your/path/to/pocketbase serve --http="yourdomain.com:80"  --https="yourdomain.com:443"



[Install]

WantedBy = multi-user.target
Enter fullscreen mode Exit fullscreen mode

Save the file and run the command

[root@dev ~]$ systemctl enable pocketbase.service

[root@dev ~]$ systemctl start pocketbase
Enter fullscreen mode Exit fullscreen mode

These commands add pocketbase into service, though pocketbase will restart when your server restarts.

Serve static files with pocketbase

You can serve static files like html, css, using pocketbase.

In order to enable static serving you have to create a folder named ‘pb_public’ in the pocketbase base directory and copy files to the pb_public . The static files would be served in the base pocketbase url ( usually in http://localhost:8090/)

Top comments (0)