npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amj7-typed-pocketbase

v0.0.8

Published

Add types to the PocketBase JavaScript SDK

Downloads

5

Readme

typed-pocketbase

npm GitHub top language GitHub Workflow Status (with branch)

Add types to the PocketBase JavaScript SDK.

Installation

# npm
npm i typed-pocketbase

# pnpm
pnpm i typed-pocketbase

# yarn
yarn add typed-pocketbase

Usage

Generate the types:

npx typed-pocketbase --email [email protected] --password supersecretpassword -o Database.d.ts

The codegen tool will look for POCKETBASE_EMAIL and POCKETBASE_PASSWORD environment variables if the email or password are not passed using cli options.

Create a PocketBase client:

import PocketBase from 'pocketbase';
import { TypedPocketBase } from 'typed-pocketbase';
import { Schema } from './Database';

const db = new PocketBase('http://localhost:8090') as TypedPocketBase<Schema>;

Enjoy full type-safety:

import { neq, sort, fields } from 'typed-pocketbase';

db.collection('posts').getList(1, 10, {
	fields: fields('id', 'title', 'content'),
	sort: sort('-date'),
	filter: neq('content', '')
});

Supported methods

  • getFullList
  • getList
  • getFirstListItem
  • getOne
  • create
  • update
  • subscribe

Selecting fields

Use the fields function to select the properties:

Note: Don´t use expand when selecting fields

import { fields } from 'typed-pocketbase';

db.collection('posts').getFullList({
	fields: fields('id', 'title', 'content')
});

// conditionally select fields
// falsy values are excluded
db.collection('posts').getFullList({
	fields: fields(shouldIncludeId && 'id', 'title', 'content')
});

Filtering columns

Use the and, or and other utility functions to filter rows:

import { and, or, eq } from 'typed-pocketbase';

// get all posts created in 2022
db.collection('posts').getFullList({
	// a "manual" filter is a tuple of length 3
	filter: and(['date', '<', '2023-01-01'], ['data', '>=', '2022-01-01'])
});

// get all posts expect for those created in 2022
db.collection('posts').getFullList({
	filter: or(['date', '>=', '2023-01-01'], ['data', '<', '2022-01-01'])
});

// get all posts that were create at '2023-01-01'
db.collection('posts').getFullList({
	filter: eq('date', '2023-01-01')
});

// combine or/and with helpers and manual filters
db.collection('posts').getFullList({
	filter: or(
		//
		['date', '>=', '2023-01-01'],
		lt('date', '2022-01-01')
	)
});

// conditionally filter rows
// falsy values are excluded
db.collection('posts').getFullList({
	filter: and(
		//
		gte('date', '2022-01-01'),
		!untilNow && lt('date', '2023-01-01')
	)
});

// filter for columns in relations
// works up to 6 levels deep, including the top level
db.collection('posts').getFullList({
	filter: eq('owner.name', 'me')
});

Most filter operators are available as short hand function.

Visit the pocketbase documentation to find out about all filters in the List/Search records section.

Sorting rows

Use sort, asc and desc to sort the rows:

import { sort, asc, desc } from 'typed-pocketbase';

db.collection('posts').getFullList({
	// sort by descending 'date'
	sort: desc('date')
});

db.collection('posts').getFullList({
	// sort by descending 'date' and ascending 'title'
	sort: sort('-date', '+title')
});

db.collection('posts').getFullList({
	// sort by descending 'date' and ascending 'title'
	sort: sort(desc('date'), asc('title'))
});

// you can mix functions with +/- prefixes
db.collection('posts').getFullList({
	// sort by descending 'date' and ascending 'title'
	sort: sort(desc('date'), '+title')
});

// conditionally sort rows
// falsy values are excluded
db.collection('posts').getFullList({
	sort: sort(
		//
		desc('date'),
		sortTitle && asc('title')
	)
});

Expanding

Use the expand function to expand relations:

Note: Don´t use fields when expanding as fields only works for the top level and expand would end up as an empty object

import { expand } from 'typed-pocketbase';

db.collection('posts').getFullList({
	expand: expand({
		user: true
	})
});

// nested expand
db.collection('posts').getFullList({
	expand: expand({
		user: {
			profile: true
		}
	})
});

License

MIT