-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generics for expand fields (#30)
* add generics for expand fields * update naming
- Loading branch information
Showing
10 changed files
with
170 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,9 @@ This will produce types for all your PocketBase collections to use in your front | |
|
||
## Versions | ||
|
||
When using PocketBase v0.8.x, use `pocketbase-typegen` v1.1.x | ||
When using PocketBase > v0.8.x, use `pocketbase-typegen` v1.1.x | ||
|
||
Users of PocketBase v0.7.x should use `pocketbase-typegen` v1.0.x | ||
Users of PocketBase < v0.7.x should use `pocketbase-typegen` v1.0.x | ||
|
||
## Usage | ||
|
||
|
@@ -40,7 +40,15 @@ URL example: | |
|
||
`npx pocketbase-typegen --url https://myproject.pockethost.io --email [email protected] --password 'secr3tp@ssword!'` | ||
|
||
## Example output | ||
Add it to your projects `package.json`: | ||
|
||
``` | ||
"scripts": { | ||
"typegen": "pocketbase-typegen --db ./pb_data/data.db", | ||
}, | ||
``` | ||
|
||
## Example Output | ||
|
||
The output is a typescript file `pocketbase-types.ts` ([example](./test/pocketbase-types-example.ts)) which will contain: | ||
|
||
|
@@ -50,12 +58,40 @@ The output is a typescript file `pocketbase-types.ts` ([example](./test/pocketba | |
- `[CollectionName][FieldName]Options` If the collection contains a select field with set values, an enum of the options will be generated. | ||
- `CollectionRecords` A type mapping each collection name to the record type. | ||
|
||
## Example usage | ||
## Example Usage | ||
|
||
In [PocketBase SDK](https://github.com/pocketbase/js-sdk) v0.8 you can use generic types when fetching records, eg: | ||
In [PocketBase SDK](https://github.com/pocketbase/js-sdk) v0.8+ you can use generic types when fetching records, eg: | ||
|
||
```typescript | ||
import { Collections, TasksResponse } from "./pocketbase-types" | ||
|
||
pb.collection(Collections.Tasks).getOne<TasksResponse>("RECORD_ID") // -> results in Promise<TaskResponse> | ||
``` | ||
|
||
## Example Advanced Usage | ||
|
||
You can provide types for JSON fields and [expanded relations](https://pocketbase.io/docs/expanding-relations/) by passing generic arguments to the Response types: | ||
|
||
```typescript | ||
import { Collections, CommentsResponse, UserResponse } from "./pocketbase-types" | ||
|
||
/** | ||
type CommentsRecord<Tmetadata = unknown> = { | ||
text: string | ||
metadata: null | Tmetadata | ||
user: RecordIdString | ||
} | ||
*/ | ||
type Tmetadata = { | ||
likes: number | ||
} | ||
type Texpand = { | ||
user: UsersResponse | ||
} | ||
const result = await pb | ||
.collection(Collections.Comments) | ||
.getOne<CommentsResponse<Tmetadata, Texpand>>("RECORD_ID", { expand: "user" }) | ||
|
||
// Now you can access the expanded relation with type safety and hints in your IDE | ||
result.expand?.user.username | ||
``` |
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
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
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
Oops, something went wrong.