-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from david-plugge/next
- Loading branch information
Showing
23 changed files
with
1,243 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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'; | ||
|
||
|
@@ -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']) | ||
}); | ||
|
||
|
@@ -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') | ||
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.