1.0.0 • Published 11 months ago
@thisux/pocketbase-typegene v1.0.0
pocketbase-typegen
A TypeScript type generator for PocketBase collections. This tool automatically generates TypeScript interfaces and types from your PocketBase schema.
Features
- Generates TypeScript interfaces for all PocketBase collections
- Supports all PocketBase field types
- Generates union types for select fields
- Handles optional fields
- Supports file fields (single and multiple)
- Excludes system collections by default
- Generates a Collections interface for type-safe access
Installation
# Using bun
bun add pocketbase-typegen
# Using npm
npm install pocketbase-typegen
# Using yarn
yarn add pocketbase-typegenUsage
Generate types from a schema file
import { pbTypegenFromFile, pbTypegenToFile } from 'pocketbase-typegen';
// Generate types and get as string
const types = await pbTypegenFromFile('./schema.json');
console.log(types);
// Generate types and save to file
await pbTypegenToFile('./schema.json', './types.ts');Generate types from schema object
import { pbTypegen } from 'pocketbase-typegen';
const schema = [/* your PocketBase schema */];
const types = pbTypegen(schema);Output Example
The generator creates TypeScript interfaces and types like this:
export type CategoryOptions =
'web' |
'mobile' |
'design';
export interface ProjectRecord {
id: string;
title: string;
description?: string;
category?: CategoryOptions;
files?: string[];
created?: string;
updated?: string;
}
export interface Collections {
projects: ProjectRecord;
}Type Mappings
text,email,url,editor→stringnumber→numberbool→booleandate→stringselect→ Custom union typejson→anyfile(single) →stringfile(multiple) →string[]relation→stringpassword→never
Development
This project uses Bun for development. Make sure you have Bun installed.
# Install dependencies
bun install
# Run tests
bun test
# Generate types from sample schema
bun run generateLicense
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1.0.0
11 months ago