vaphes / pocketbase

PocketBase client SDK for python
https://pypi.org/project/pocketbase/
MIT License
331 stars 38 forks source link

[help-wanted] How can I create a Collection API hook? #81

Closed xtonousou closed 7 months ago

xtonousou commented 7 months ago

Brief

While reading the PocketBase docs, I stumbled upon collection hooks mechanism and I'm very curious, how do you implement this with the python sdk?

Use case

For example, a base collection has the field password of type text and when a record is inserted, you have to "call" an event hook that hashes the password and then saves it to PocketBase. And vice versa, when a record from that collection is listed/read then you have to "call" an event hook to decrypt it in order to show the plain text.

I know auth collection exists, but I need that field in a base collection.

Conclude

It is a matter of time until PocketBase adds the password type field for base collections, but until then I think this is a nice feature to have with an alternative solution using "encrypting" hooks. I spotted the package pluggy in poetry but nothing uses it so I assume that it's WIP ?

m29h commented 7 months ago

You may have a misconception here. The collection hooks are part of the Go code inside the pocketbase server library. They are not accessible from any web API. If you want to use them you need to compile your own pocketbase server with the relevant modifications.

The web API supports what is called "realtime service". Examples can be found in our codebase here tests/integration/test_record_event.py. these are however only notifications that are received once an record was changed, they do not allow to place middleware inside the record modification action (or even stop it from happening) like the Go server library can do.

xtonousou commented 7 months ago

Thanks for clarifying this. I may have to use the realtime service to listen for the events (triggered natively by pocketbase) that I need and then implement the logic there.