How to Add a Field to ALL DocTypes?

We are developing an experimental feature that requires us to have a new field available on all doctypes.

What code changes would we need to make this happen? I’ve thought of the following issues:

  • When a new doctype is created in the UI, it needs the new field.
  • When documents are retrieved from the database, the new field needs to be populated on the object.
  • When the document is updated in the database, the field needs to be updated.
  • API requests would also need the new field so that client javascript can work with the field on any document.

Without deep knowledge about Frappe, it’s difficult for me to discern all the impacts and where to look to make these code changes.

Can anyone direct me to the code surface touchpoints that would need to be changed to add a new field to all doctypes everywhere?

Sidenote: this is in regards to the issue on document-membership for User Groups as a way to share documents among users:

Hi there,

For automatically adding a field to new doctypes, I believe you could just add normal on_insert or on_save lifecycle hooks to the DocType definition.

I’m not aware of any way to run arbitrary logic every time a document is read, and if such a thing were added you’d have to be careful about performance bottlenecks. Read is assumed to be a computationally “cheap” process. It might be possible to do this using Virtual Fields, but in any case it’s probably better to find a solution that doesn’t require schema changes on every read.

For catching all updates to the database, allows you to attach a CRUD method to all doctypes using "*" (see here). With this, it’s fairly straightforward to run logic on all before_save events, etc., before anything gets updated in the database.

If you’re storing this data in a field, there’s no need to do anything special to make it available to client api methods. It will be there automatically.

(Friendly addition to the above: I’m skeptical that adding a new field to all docs is the right way to get group permissions. Wouldn’t extending extending the existing User Permissions mechanism to use group definitions be easier and cleaner?)

Edit: forgot that allows "*" on doc_events