PocketBase : The Ultimate All-in-One Open Source Solution for Your Backend Revolution!
Tags: backend, go, javascript, webdev
Published: 2023-12-02 15:41:02

[![Substack Newsletter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbg4qlzlytxbzbvj97ia.png)](https://benoitpetit.substack.com/subscribe)

> 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](https://pocketbase.io/docs/js-database/)) 
- 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](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.

```typescript
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();
```

### 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.

```typescript
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');
```

### 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.

```typescript
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,
});
```

### 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.

```typescript
// 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!");
});
```

## 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/](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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohkf7lrymedkort4w9iz.png)](https://donate.stripe.com/00g6pJ6R19MJ15S7ss)

--- 

👀 See me here : 

- [Substack](https://benoitpetit.substack.com/subscribe)
- [Mastodon](https://piaille.fr/@codingben) 
- [(X) Twitter](https://twitter.com/codingben_)
- [Github](https://github.com/benoitpetit)
- [NPM Packages](https://www.npmjs.com/~codingben)
- [benoitpetit.dev](https://benoitpetit.dev)
- [Dev.to](https://dev.to/benoitpetit)