Skip to content

Commit

Permalink
Merge pull request #7 from david-plugge/next
Browse files Browse the repository at this point in the history
  • Loading branch information
david-plugge authored Jun 27, 2023
2 parents 0ab5bf3 + 41f9e63 commit 50e92ad
Show file tree
Hide file tree
Showing 23 changed files with 1,243 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"printWidth": 80,
"tabWidth": 4
}
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# typed-pocketbase

## 0.0.3

### Patch Changes

- dd52f65: rework codegen cli and support all field types and modifiers
- 4201a28: export TypedRecord
- 5b60b0c: fix entrypoints and file thumbs type when none are specified
- 376dc88: Add expand and nested filter support

## 0.0.3-next.3

### Patch Changes

- rework codegen cli and support all field types and modifiers

## 0.0.3-next.2

### Patch Changes

- export TypedRecord

## 0.0.3-next.1

### Patch Changes

- fix entrypoints and file thumbs type when none are specified

## 0.0.3-next.0

### Patch Changes

- Add expand and nested filter support

## 0.0.2

### Patch Changes
Expand Down
63 changes: 52 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@ Add types to the [PocketBase JavaScript SDK](https://github.com/pocketbase/js-sd

```bash
# npm
npm i typed-pocketbase
npm i typed-pocketbase@next

# pnpm
pnpm i typed-pocketbase
pnpm i typed-pocketbase@next

# yarn
yarn add typed-pocketbase
yarn add typed-pocketbase@next
```

## Usage

Generate the PocketBase types using [pocketbase-typegen](https://github.com/patmood/pocketbase-typegen):
Generate the types:

```bash
npx pocketbase-typegen --db ./pb_data/data.db --out pocketbase-types.ts
npx typed-pocketbase --email [email protected] --password supersecretpassword -o Database.d.ts
```

Create a PocketBase client and add types:
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:

```ts
import PocketBase from 'pocketbase';
import { TypedPocketBase } from 'typed-pocketbase';
import { CollectionRecords } from './pocketbase-types';
import { Schema } from './Database';

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

Enjoy full type-safety:
Expand Down Expand Up @@ -63,6 +65,8 @@ Supported methods

Use the `fields` function to select the properties:

**Note:** Don´t use `expand` when selecting fields

```ts
import { fields } from 'typed-pocketbase';

Expand All @@ -79,13 +83,14 @@ db.collection('posts').getFullList({

## Filtering columns

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

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

Expand Down Expand Up @@ -117,19 +122,30 @@ db.collection('posts').getFullList({
!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 a short hand.
Most filter operators are available as short hand function.

Visit the [pocketbase documentation](https://pocketbase.io/docs/api-records/) to find out about all filters in the `List/Search records` section.

## Sorting rows

Use the `sort` function to sort the rows:
Use `sort`, `asc` and `desc` to sort the rows:

```ts
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')
Expand Down Expand Up @@ -157,6 +173,31 @@ db.collection('posts').getFullList({
});
```

## 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

```ts
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](https://github.com/david-plugge/typed-pocketbase/blob/main/LICENSE)
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3.7"

services:
pocketbase:
image: ghcr.io/muchobien/pocketbase:latest
container_name: pocketbase
restart: unless-stopped
environment:
ENCRYPTION: example #optional
ports:
- "8090:8090"
volumes:
- pb_data:/pb_data
healthcheck: #optional (recommended) since v0.10.0
test: wget --no-verbose --tries=1 --spider http://localhost:8090/api/health || exit 1
interval: 5s
timeout: 5s
retries: 5

volumes:
pb_data:
149 changes: 149 additions & 0 deletions example/Database.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* This file was @generated using typed-pocketbase
*/

// https://pocketbase.io/docs/collections/#base-collection
type BaseCollectionRecord = {
id: string;
created: string;
updated: string;
};

// https://pocketbase.io/docs/collections/#auth-collection
type AuthCollectionRecord = {
id: string;
created: string;
updated: string;
username: string;
email: string;
emailVisibility: boolean;
verified: boolean;
};

// https://pocketbase.io/docs/collections/#view-collection
type ViewCollectionRecord = {
id: string;
};

// utilities

type MaybeArray<T> = T | T[];

// ===== users =====

export type UsersResponse = {
name?: string;
avatar?: string;
} & AuthCollectionRecord;

export type UsersCreate = {
name?: string;
avatar?: string;
};

export type UsersUpdate = {
name?: string;
avatar?: string;
};

export type UsersCollection = {
type: 'auth';
collectionId: '_pb_users_auth_';
collectionName: 'users';
response: UsersResponse;
create: UsersCreate;
update: UsersUpdate;
relations: {
'posts(owner)': PostsCollection[];
};
};

// ===== posts =====

export type PostsResponse = {
title: string;
slug: string;
date?: string;
content?: string;
published?: boolean;
owner?: string;
} & BaseCollectionRecord;

export type PostsCreate = {
title: string;
slug: string;
date?: string;
content?: string;
published?: boolean;
owner?: string;
};

export type PostsUpdate = {
title?: string;
slug?: string;
date?: string;
content?: string;
published?: boolean;
owner?: string;
};

export type PostsCollection = {
type: 'base';
collectionId: 'sbrth2mzfnqba9e';
collectionName: 'posts';
response: PostsResponse;
create: PostsCreate;
update: PostsUpdate;
relations: {
owner: UsersCollection;
};
};

// ===== usis =====

export type UsisResponse = {
avatar?: string;
} & ViewCollectionRecord;

export type UsisCollection = {
type: 'view';
collectionId: 'bubx07xyejsas8a';
collectionName: 'usis';
response: UsisResponse;
relations: {};
};

// ===== test =====

export type TestResponse = {
options?: ('a' | 'b' | 'c' | 'd')[];
} & BaseCollectionRecord;

export type TestCreate = {
options?: MaybeArray<'a' | 'b' | 'c' | 'd'>;
};

export type TestUpdate = {
options?: MaybeArray<'a' | 'b' | 'c' | 'd'>;
'options+'?: MaybeArray<'a' | 'b' | 'c' | 'd'>;
'options-'?: MaybeArray<'a' | 'b' | 'c' | 'd'>;
};

export type TestCollection = {
type: 'base';
collectionId: '800ro086vmm2fbj';
collectionName: 'test';
response: TestResponse;
create: TestCreate;
update: TestUpdate;
relations: {};
};

// ===== Schema =====

export type Schema = {
users: UsersCollection;
posts: PostsCollection;
usis: UsisCollection;
test: TestCollection;
};
Loading

0 comments on commit 50e92ad

Please sign in to comment.