You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

typed-pocketbase

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-pocketbase

Add types to the PocketBase JavaScript SDK

0.0.2
Source
npmnpm
Version published
Weekly downloads
23
-14.81%
Maintainers
1
Weekly downloads
 
Created
Source

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 PocketBase types using pocketbase-typegen:

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

Create a PocketBase client and add types:

import PocketBase from 'pocketbase';
import { TypedPocketBase } from 'typed-pocketbase';
import { CollectionRecords } from './pocketbase-types';

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

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:

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 some other utility function to filter rows:

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

// get all posts created in 2022
db.collection('posts').getFullList({
	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')
	)
});

Most filter operators are available as a short hand.

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

Sorting rows

Use the sort function to sort the rows:

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

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')
	)
});

License

MIT

Keywords

pocketbase

FAQs

Package last updated on 19 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts