With Orama v2.0.0-beta.5
, we introduced the new plugin system. This allows you to write your own plugins and use them in your Orama project.
The Orama plugin system is meant to replace the old hooks system, which is now deprecated.
If you had any custom hook, it would be incredibly easy to migrate it to a plugin, as the APIs are 100% backward compatible.
Creating a plugin
A plugin is essentially a JavaScript object with a name
and a series of hook functions.
An example plugin could look like this:
And you can then use it in your Orama instance like this:
Every plugin should have:
- A
name
property, which is a string (mandatory)
- Any hook function you want to use (optional)
Plugin hooks
With v2.0.0-beta.5
, we essentially moved the hooks from the components
property of the Orama instance to the plugins
property.
The available hooks are:
beforeInsert
Runs before an insert operation. Receives the Orama instance, the document ID, and the entire document to be inserted as arguments.
afterInsert
Runs after an insert operation. Receives the Orama instance, the document ID, and the entire document as arguments.
beforeRemove
Runs before a remove operation. Receives the Orama instance and the document ID to be removed as arguments.
afterRemove
Runs after a remove operation. Receives the Orama instance and the document ID removed as arguments.
beforeUpdate
Runs before an update operation. Receives the Orama instance and the document ID to be updated as arguments.
afterUpdate
Runs after an update operation. Receives the Orama instance and the document ID as arguments.
beforeSearch
Runs before a search operation. Receives the Orama instance and the query object as arguments.
afterSearch
Runs after a search operation. Receives the Orama instance, the query object, and the search results as arguments.
beforeInsertMultiple
Runs before an insertMultiple
operation. Receives the Orama instance and the documents to be inserted as arguments.
afterInsertMultiple
Runs after an insertMultiple
operation. Receives the Orama instance and the documents inserted as arguments.
beforeRemoveMultiple
Runs before a removeMultiple
operation. Receives the Orama instance and the IDs of the documents to be removed as arguments.
afterRemoveMultiple
Runs after a removeMultiple
operation. Receives the Orama instance and the IDs of the documents removed as arguments.
beforeUpdateMultiple
Runs before an updateMultiple
operation. Receives the Orama instance and the IDs of the documents to be updated as arguments.
afterUpdateMultiple
Runs after an updateMultiple
operation. Receives the Orama instance and the IDs of the documents updated as arguments.
Migrating from hooks to plugins
If you were using the old hooks system, you can easily migrate to the new plugin system by moving your hooks to a single plugin.
For instance, if you had a beforeInsert
hook, you can migrate it to a plugin like this:
Types, behavior, and arguments are 100% backward compatible, so you don’t need to change anything else.