Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate typed PocketBase type #82

Merged
merged 7 commits into from
Oct 17, 2023

Conversation

scriptcoded
Copy link
Contributor

In the latest release of the PocketBase JavaScript SDK there was added support for providing a typed instance of the client using type assertion. This PR adds a new exported type that fits this shape and can be used directly with PocketBase like this:

import { TypedPocketBase } from "./pocketbase-types";

const pb = new PocketBase('...') as TypedPocketBase

To make this work I had to add an import statement to the generated file to get the base types from PocketBase. The resulting file looks something like this:

/**
* This file was @generated using pocketbase-typegen
*/

import PocketBase, { RecordService } from 'pocketbase'

// ...

// Type for usage with type asserted Pocketbase instance
// https://github.com/pocketbase/js-sdk#specify-typescript-definitions

export type TypedPocketBase = PocketBase & {
	collection(idOrName: string): RecordService
	collection(idOrName: 'posts'): RecordService<PostsResponse>
	collection(idOrName: 'users'): RecordService<UsersResponse>
}

An alternative if you don't like having this import would be to use generics and have the user pass down the PocketBase and RecordService types, but I think the user experience would be a lot worse.

Closes #81

@patmood
Copy link
Owner

patmood commented Oct 16, 2023

I love this, thanks for the contribution. Really glad pocketbase added this and I think it'll make this library much easier to use going forward.

I think there should be a --no-sdk command line option for users that just want pure types (maybe they are using the REST api).

Before merging I want to test it out and check some edge cases

@patmood
Copy link
Owner

patmood commented Oct 16, 2023

Im not sure how expanded relation types and the generic fields for JSON objects will work with this setup. That's not a blocker though

@scriptcoded
Copy link
Contributor Author

I think there should be a --no-sdk command line option for users that just want pure types (maybe they are using the REST api).

I agree this would be a good addition. I'll make sure to add it. I also realize I completely forget to update the documentation so I'll make sure that gets added as well.

As for expanded relations I totally didn't think of that so I'll have to read up on exactly how the types behave in that case before I give my thoughts 😄 In the worst case you should still be able to pass generic types to the collection methods (like .getOne) even though you're using the typed SDK.

src/collections.ts Outdated Show resolved Hide resolved
src/lib.ts Show resolved Hide resolved
Copy link
Owner

@patmood patmood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I'm traveling at the moment so I appreciate the update for the new SDK. Could you also bump the version in package.json to 1.1.14?

src/constants.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@scriptcoded
Copy link
Contributor Author

@patmood I don't know what versioning strategy you're using, but seeing as this adds additional functionality, should this maybe me a minor bump i.e. 1.2.0?

@patmood
Copy link
Owner

patmood commented Oct 17, 2023

Thanks again @scriptcoded - Looks good! 🎉 I'll update to 1.2.0 before I release

@patmood patmood merged commit df1344f into patmood:main Oct 17, 2023
@scriptcoded scriptcoded deleted the 81-typed-pocketbase branch October 25, 2023 11:36
@anbraten
Copy link

anbraten commented Nov 8, 2023

Just noticed this one when updating to the latest release. It's such a useful nifty feature Thanks ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typed PocketBase client
3 participants