9ecbf / pocketbase-typescript-hooks-template

A Bun template that supports building TypeScript hooks for your PocketBase application.
MIT License
3 stars 0 forks source link
bun devcontainer pocketbase typescript

PocketBase TypeScript hooks

A Bun template that supports building TypeScript hooks for your PocketBase application.

Requirements

This template is designated for developing with Dev Containers. You only need to add its associated extension to your Visual Studio Code along with ensuring that Docker is available in your machine.

If you just want to go on your way, please make sure that the following dependencies are available:

  1. Bun: Bun is a fast JavaScript bundler (we only use its bundling feature here). At the moment I was writing this README, Bun was supported on macOS, Linux, and Windows Subsystem for Linux (WSL). Feel free to check out the installation guide here.
  2. PocketBase: PocketBase is an open-source back-end for your next SaaS and mobile application in a single file (yes, I took this slogan from its home page). It supports being extended via its Hooks. Though we can work with PocketBase Hooks by using either Go or JavaScript, for the Node-family developers, the JavaScript way seems to be easier. To install PocketBase, please follow this documentation.

Usage

Add dependencies

For adding a new dependency, we can use bun install command. For example, if we want to use the functionalities of the npm package is-even, run the following command:

bun install is-even

For installing all dependencies defined in package.json, run:

bun install

Add TypeScript hooks

To add a new TypeScript hook, create a .pb.ts file (it is mandatory that a PocketBase hook must end with .pb.js) inside the src directory. For type hints, we can add a reference triple-slash directive at the beginning of the file:

/// <reference path="../types/pocketbase.d.ts" />

For reference, please take a look at this overview of Extending with JavaScript.

Build

Building is a step to compile our TypeScript files into JavaScript and move the built ones to the right place for our PocketBase application. To do that, run:

bun run build

If the building process succeeds, a directory pb_data will be created in the current workspace.

Start

After building, we can start our PocketBase application via:

pocketbase serve --dir=pb_data --hooksDir=pb_data/pb_hooks

The application will listen on port 8090 and, of course, be integrated with our added JavaScript hooks.

For more convenience in local development, we can use bun dev to start the application right after building.

And...

For more other helpful scripts for development, take a look at bun run help command.

Contribution

This template is made for personal uses. Please use it at your own risk and feel free to create a pull request if you have and suggestion to make it better.