Skip to content

gooodev/pocketbase-typegen

 
 

Repository files navigation

Pocketbase Typegen

Generate typescript definitions from your pocketbase.io schema.

Quickstart

npx pocketbase-typegen --db ./pb_data/data.db --out pocketbase-types.ts

This will produce types for all your PocketBase collections to use in your frontend typescript codebase.

Versions

When using PocketBase v0.8.x, use pocketbase-typegen v1.1.x

Users of PocketBase v0.7.x should use pocketbase-typegen v1.0.x

Usage

Options:
  -V, --version          output the version number
  -d, --db <char>        path to the pocketbase SQLite database
  -j, --json <char>      path to JSON schema exported from pocketbase admin UI
  -u, --url <char>       URL to your hosted pocketbase instance. When using this options you must also provide email and password options.
  -e, --email <char>     email for an admin pocketbase user. Use this with the --url option
  -p, --password <char>  password for an admin pocketbase user. Use this with the --url option
  -o, --out <char>       path to save the typescript output file (default: "pocketbase-types.ts")
  -h, --help             display help for command

DB example:

npx pocketbase-typegen --db ./pb_data/data.db

JSON example (export JSON schema from the pocketbase admin dashboard):

npx pocketbase-typegen --json ./pb_schema.json

URL example:

npx pocketbase-typegen --url https://myproject.pockethost.io --email [email protected] --password 'secr3tp@ssword!'

Example output

The output is a typescript file pocketbase-types.ts (example) which will contain:

  • Collections An enum of all collections/
  • [CollectionName]Record One type for each collection (eg ProfilesRecord)/
  • [CollectionName]Response One response type for each collection (eg ProfilesResponse) which includes system fields. This is what is returned from the PocketBase API.
    • [CollectionName][FieldName]Options If the collection contains a select field with set values, an enum of the options will be generated.
  • CollectionRecords A type mapping each collection name to the record type.

Example usage

In PocketBase SDK v0.8 you can use generic types when fetching records, eg:

import { Collections, TasksResponse } from "./pocketbase-types"

pb.collection(Collections.Tasks).getOne<TasksResponse>("RECORD_ID") // -> results in Promise<TaskResponse>

About

Typescript generation for pocketbase records

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 91.8%
  • JavaScript 5.1%
  • Dockerfile 1.9%
  • Shell 1.2%