DEV Community

Cover image for PocketBase : The Ultimate All-in-One Open Source Solution for Your Backend Revolution!
Beπ ✨
Beπ ✨

Posted on • Updated on • Originally published at benoitpetit.dev

PocketBase : The Ultimate All-in-One Open Source Solution for Your Backend Revolution!

Substack Newsletter

Hello, it's Beπ ✨, and I wanted to share with you this open-source gem that I love!

Introduction:

Do you have a brilliant idea for a SaaS or mobile application? PocketBase is the ideal solution for you! This open-source platform, developed in Go, provides an all-in-one approach to managing your database, files, users, and API.

Features:

  • Integrated SQLite database to store your data. This saves you from configuring and maintaining an external database. (Option to use another database) (Option to database using extension capabilities)
  • Integrated File and User Management: Manage files and users in your application seamlessly. You can easily create, edit, and delete files, as well as manage user accounts.
  • Convenient Dashboard: A convenient dashboard to administer your application. You can view your database data, manage users and files, and monitor your application's performance.
  • Simple REST-ish API: A simple REST-ish API to access your data. This allows you to integrate your application with other services or applications.
  • JavaScript Client SDKs: PocketBase offers JavaScript client SDKs for web and mobile applications, making it easy to use the PocketBase API.

Examples of JavaScript SDK Usage:

PocketBase's JavaScript SDK is easy to use and allows for the quick and simple development of web and mobile applications. The examples below provide an overview of the capabilities offered by the SDK.

In addition to the JavaScript SDK, PocketBase also offers a Dart SDK. More information is available here: https://pub.dev/packages/pocketbase

Real-time Database

This example demonstrates how to use the JavaScript SDK to access PocketBase's real-time database. You can use methods such as getList(), getOne(), delete(), create(), and subscribe() to manage your data.

import PocketBase from 'pocketbase';

const pb = new PocketBase('http://127.0.0.1:8090');

// list and search for 'example' collection records
const list = await pb.collection('example').getList(1, 100, {
  filter: 'title != "" && created > "2022-08-01"',
  sort: '-created,title',
});

// or fetch a single 'example' collection record
const record = await pb.collection('example').getOne('RECORD_ID');

// delete a single 'example' collection record
await pb.collection('example').delete('RECORD_ID');

// create a new 'example' collection record
const newRecord = await pb.collection('example').create({
  title: 'Lorem ipsum dolor sit amet',
});

// subscribe to changes in any record from the 'example' collection
pb.collection('example').subscribe('*', function (e) {
  console.log(e.record);
});

// stop listening for changes in the 'example' collection
pb.collection('example').unsubscribe();
Enter fullscreen mode Exit fullscreen mode

Authentication:

This example demonstrates how to use the JavaScript SDK to manage user authentication. Methods like create(), authWithPassword(), authWithOAuth2(), requestVerification(), requestPasswordReset(), and requestEmailChange() can be used for authentication.

import PocketBase from 'pocketbase';

const pb = new PocketBase('http://127.0.0.1:8090');

// sign-up with username/email and password
await pb.collection('users').create({
  email: 'test@example.com',
  password: '123456',
  passwordConfirm: '123456',
  name: 'John Doe',
});

// sign-in with username/email and password
await pb.collection('users').authWithPassword('test@example.com', '123456');

// sign-in/sign-up with OAuth2 (Google, Facebook, etc.)
await pb.collection('users').authWithOAuth2({
  provider: 'google',
});

// send verification email
await pb.collection('users').requestVerification('test@example.com');

// send password reset email
await pb.collection('users').requestPasswordReset('test@example.com');

// send request email change email
await pb.collection('users').requestEmailChange('new@example.com');
Enter fullscreen mode Exit fullscreen mode

File Management:

This example demonstrates how to use the JavaScript SDK to store and manage files. Methods like create() and update() can be used for file management.

import PocketBase from 'pocketbase';

const pb = new PocketBase('http://127.0.0.1:8090');

// file input (eg. <input type="file" id="fileInput" />)
const fileInput = document.getElementById('fileInput');

const formData = new FormData();

// listen to file input changes
fileInput.addEventListener('change', function () {
  for (let file of fileInput.files) {
    formData.append('yourFileField', file);
  }
});

// set some other regular text field value
formData.append('title', 'Hello world!');

// create a new record and upload the file(s)
await pb.collection('example').create(formData);

// delete all 'yourFileField' files from a record
await pb.collection('example').update('RECORD_ID', {
  'yourFileField': null,
});
Enter fullscreen mode Exit fullscreen mode

Extension Possibility:

This example demonstrates how to use the JavaScript SDK to extend PocketBase's functionalities. Methods like onRecordAfterUpdateRequest(), onMailerBeforeRecordVerificationSend(), routerAdd(), and cronAdd() can be used to customize your application.

// pb_hooks/main.pb.js

// intercept requests
onRecordAfterUpdateRequest((e) => {
  console.log(e.record.id);
});

// intercept system emails
onMailerBeforeRecordVerificationSend((e) => {
  // send custom email
  e.mailClient.send(...);

  // stops propagation
  return false;
});

// register custom routes
routerAdd(
  "get",
  "/hello",
  (c) => {
    return c.string(200, "Hello!");
  },
  $apis.activityLogger($app),
  $apis.requireAdminAuth()
);

// jobs scheduling
cronAdd("hello", "*/2 * * * *", () => {
  // prints "Hello!" every 2 minutes
  console.log("Hello!");
});
Enter fullscreen mode Exit fullscreen mode

Conclusion:

PocketBase is a powerful and flexible open-source solution that allows you to easily create backends for your applications. If you are looking for a simple, efficient, and flexible solution to manage your backend, PocketBase is the ideal choice for you.

Advantages:

  • Simplicity: A simple and intuitive solution to use. You can create your backend in minutes without needing advanced development knowledge.
  • Efficiency: An efficient solution that saves you time and resources. You don't need to configure and maintain external servers, databases, or services.
  • Flexibility: A flexible solution that allows you to customize your backend according to your needs. You can add your business logic, plugins, migrations, and models.

To learn more about PocketBase, visit the website: https://pocketbase.io/ and click on Live demo for a preview of the dashboard.

I hope you enjoyed it! Feel free to share the article.

See you soon! Beπ ✨


🫶 If you enjoy my posts and want to support my work, you can make a donation by clicking on the Stripe Sponsor link below or scanning the QR code. Stripe Sponsor link


Stripe Sponsor


👀 See me here :

Top comments (2)

Collapse
 
su_subh1 profile image
Subh

Integrated SQLite database to store your data. This saves you from configuring and maintaining an external database. (Option to use another database)

Apart from SQLite, what other DB supported?

Collapse
 
benoitpetit profile image
Beπ ✨ • Edited

Pocketbase only offers SQLite.

When I talk about another database, I mean that it is possible to extend the functionality.

pocketbase.io/docs/js-database/

I'm going to rephrase this sentence, it's not very clear, thank you for your comment 😊