--- title: Plugins keywords: fastai sidebar: home_sidebar nb_path: "nbs/plugin.pluginbase.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %}

Creating a plugin

The memri pod uses a plugin system to add features to the backend memri backend. Plugins, can import your data (importers), change your data (indexers), or call other serivces. Users can define their own plugins to add new behaviour to their memri app. Let's use the following plugin as an example of how we can start plugins.

Memri plugins need to define at least 2 methods: .run() and .add_to_schema(). .run() defines the logic of the plugin. .add_to_schema() defines the schema for the plugin in the pod. Note that currently, add_to_schema requires all item to have all properties defined that are used in the plugin. In the future, we might replace add_to_schema, to be done automatically, based on a declarative schema defined in the plugin.

{% raw %}
MyPlugin()
MyPlugin (#None)
{% endraw %}
class MyItem(Item):
    properties = Item.properties + ["name", "age"]
    edges = Item.edges
    def __init__(self, name=None, age=None, **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.age = age

class MyPlugin(PluginBase):
    """"""
    properties = PluginBase.properties
    edges= PluginBase.edges

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.pluginPackage="pymemri.plugin.pluginbase"

    def run(self, run, client):
        print("running")
        client.create(MyItem("some person", 20))

    def add_to_schema(self, client):
        client.add_to_schema(MyItem("my name", 10))
{% raw %}
from pymemri.pod.client import PodClient
client = PodClient()
{% endraw %} {% raw %}
assert client.add_to_schema(MyPlugin(name="abc", data_query="abc"))
assert client.add_to_schema(PluginRun())
{% endraw %} {% raw %}
plugin = MyPlugin(name="abc", data_query="abc")
run = PluginRun()
run.add_edge("plugin", plugin)
{% endraw %} {% raw %}
client.create(run)
client.create(plugin)
client.create_edge(run.get_edges("plugin")[0]);
{% endraw %} {% raw %}
run = client.get(run.id)
{% endraw %}

Running your plugin

Plugins can be started using the pymemri run_plugin CLI. To use the CLI, you can either pass your run arguments as parameters, or set them as environment variables. If both are set, the CLI will prefer the passed arguments.

{% raw %}
{% endraw %}

CLI

{% raw %}

run_plugin[source]

run_plugin(pod_full_address:Param object at 0x7fad54cdaa10>=None, plugin_run_id:Param object at 0x7fad54cdaa50>=None, database_key:Param object at 0x7fad54cdacd0>=None, owner_key:Param object at 0x7fad54cdaa90>=None)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
!run_plugin --pod_full_address=$DEFAULT_POD_ADDRESS --plugin_run_id=$run.id --owner_key=$client.owner_key \
            --database_key=$client.database_key
Used arguments passed to `run_plugin()` (ignoring environment)
pod_full_address=http://localhost:3030
plugin_run_id=50a298368a040592adfd8c1c801af959
database_key=6184197375534391316725372949916181999760839224725764742871635308
owner_key=3780693072719641989650646548306482314355897960660743337420186235

running
{% endraw %}