pocketbase-typegen icon indicating copy to clipboard operation
pocketbase-typegen copied to clipboard

Add generation types for Create and Update requests

Open ivanpajon opened this issue 1 year ago • 5 comments

#110 This PR adds the generation of types for Create and Update requests. Also, using the new generated types for create/update I think #65 can be solved due to the proper typing of "file" fields.

(Collections _authOrigins,_externalAuths,_mfas and _otps were excluded from this PR as they are internal and I think it is very rare to create/update them via API)

ivanpajon avatar Dec 24 '24 15:12 ivanpajon

Any hope on getting this merged in?

wh1337 avatar Mar 29 '25 05:03 wh1337

Hey @ivanpajon! Thanks for the contribution and sorry for the delay. There's a lot of duplication going on with the new output. Could this issue be solved with utility functions? Something like this:

type BooksRecord = {
	title: string
}

type Update<T> = Partial<T> & {
    oldPassword?: string
    password?: string
    passwordConfirm?: string
}
type Create<T> = Omit<T, 'id'>

const newBook: Create<BooksRecord> = { title: 'hello' }
const myUpdate: Update<BooksRecord> = { title: 'hello'}

patmood avatar Mar 31 '25 02:03 patmood

@patmood I have done like this to not introduce breaking changes to the library (and because it was easier than a refactor), but definitely using utility types would be a cleaner approach

ivanpajon avatar Apr 01 '25 14:04 ivanpajon

@patmood I updated the PR to implement utility types and reduce duplicated code.

Changes:

  • Could be used through Collections enum or Record type
  • Utility types now can be used with any collection (including _authOrigins,_externalAuths,_mfas and _otps)
  • Handle file fields in Record definitions (string | File)
  • Any Autodate fields omitted from Create and Update
  • Improved TypedPocketBase definition to be more generic and type-safe
  • Tests updated

Usage example:

export type PostsRecord = {
  id: string
  active: boolean
  creator: string
  created?: IsoAutoDateString
  updated?: IsoAutoDateString
}

const post: CreateBase<PostsRecord> = {
  id: 'UNIQUE_ID',
  active: true,
  creator: 'user',
}
// OR
const post: Create<Collections.Posts> = {
  id: 'UNIQUE_ID',
  active: true,
  creator: 'user',
}

// Same for update with UpdateBase<> and Update<> types
// For Auth collections also can be used CreateAuth<> and UpdateAuth<> types

ivanpajon avatar Apr 01 '25 20:04 ivanpajon

@patmood I fixed the tests

ivanpajon avatar Apr 03 '25 23:04 ivanpajon

@patmood I didn't update the readme yet, when I have a moment to write some good examples I will make a commit with them

ivanpajon avatar Apr 11 '25 00:04 ivanpajon

@patmood I updated the README

ivanpajon avatar Apr 12 '25 20:04 ivanpajon

Thanks there are some merge conflicts, so the branch needs to be updated. Resolve the source file conflicts first, then just run npm run build to update the dist file.

Could you also replace AUTODATE_STRING_TYPE_NAME with DATE_STRING_TYPE_NAME (the unresolved comment).

patmood avatar Apr 14 '25 09:04 patmood

I just updated the branch.

Regarding the AUTODATE_STRING_TYPE_NAME, I left a comment in the conversation explaining why I can't remove it:

I needed to do this "trick" in order to be able to exclude any autodate field (without knowing its name in the collection) from the create/update types, as in latest pocketbase versions you can create multiple autodate fields with any name.

ivanpajon avatar Apr 14 '25 10:04 ivanpajon

Just adding a comment to say that i would love to see this PR Land. Been a while since last update is there anything that can be done to wrap it up?

sean-at-heady avatar Aug 20 '25 14:08 sean-at-heady

I’m about to start using pocketbase on a new project and will update this library at the same time

On Thu, 21 Aug 2025 at 12:22 am, sean-at-heady @.***> wrote:

sean-at-heady left a comment (patmood/pocketbase-typegen#111) https://github.com/patmood/pocketbase-typegen/pull/111#issuecomment-3206636049

Just adding a comment to say that i would love to see this PR Land. Been a while since last update is there anything that can be done to wrap it up?

— Reply to this email directly, view it on GitHub https://github.com/patmood/pocketbase-typegen/pull/111#issuecomment-3206636049, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6YGPGHDWP6IBRJ25BEBQ33OSAEHAVCNFSM6AAAAABUE3Z6OKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTEMBWGYZTMMBUHE . You are receiving this because you were mentioned.Message ID: @.***>

patmood avatar Aug 20 '25 21:08 patmood

I’m about to start using pocketbase on a new project and will update this

library at the same time

On Thu, 21 Aug 2025 at 12:22 am, sean-at-heady @.***>

wrote:

sean-at-heady left a comment (patmood/pocketbase-typegen#111)

https://github.com/patmood/pocketbase-typegen/pull/111#issuecomment-3206636049

Just adding a comment to say that i would love to see this PR Land. Been a

while since last update is there anything that can be done to wrap it up?

Reply to this email directly, view it on GitHub

https://github.com/patmood/pocketbase-typegen/pull/111#issuecomment-3206636049,

or unsubscribe

https://github.com/notifications/unsubscribe-auth/AA6YGPGHDWP6IBRJ25BEBQ33OSAEHAVCNFSM6AAAAABUE3Z6OKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTEMBWGYZTMMBUHE

.

You are receiving this because you were mentioned.Message ID:

@.***>

I'd be open to collaborating on that.

SeanDunford avatar Aug 20 '25 21:08 SeanDunford

With my last commit the local tests were passing, and since I couldn't find out why and what those integration tests are I left them as they were.

ivanpajon avatar Aug 25 '25 21:08 ivanpajon

going to merge this and fix the outstanding issues + tests

patmood avatar Oct 01 '25 10:10 patmood