What is the best practice to store custom data in a plugin?

Hi, I am currently developing a plugin to summarise notes in a plugin where I would like to store summaries either in SQLite or JSON. I think either adding a table to existing Joplin's SQLite database or creating a SQLite database in the user's plugin folder are the best way to go.

However, I would like to know what is the best practice? What contributors usually do when they try to store custom data in a plugin?

For example, I would like to store changes users made in a rich editor in a panel:

Is there also a way to send data from plugin to webview? If not then I am thinking writing a script that constantly check the data in the SQLite db for new summaries created or updated.

Yes, call webviewApi.postMessage from your webview and set the response in the plugin.

1 Like

Hmmm, what do you mean by setting responses in the plugin?

Let's have this example:

(1) When I right-click on the note, the context menu appears (in the plugin) ->
(2) I click on the one of the buttons saying "Summarise the note" (in the plugin) ->
(3) Internally that button executes the command (in the plugin) ->
(4) I get the note body and summarise the content in the body (in the plugin) ->
(5) I send the summarised content of the note to the panel (from the plugin to the web view) ->
(6) The panel displays the summarised content in the rich text editor (in the webview) -> finished!

Ah, I misunderstood the question.

You can try this.
Send a message from webview when it loads:

webviewApi.postMessage("getData")

In the plugin, return the promise and save a refernce to it

var sendData;
joplin.views.panels.onMessage(resourceSearch, async (msg: Message) => {
  return new Promise((resolve) => {
     sendData = resolve;
})

// and then later when you have the data, call
sendData(data);
1 Like

Great! I will try that! Thank you for answering my question.

I suggest the user data API. This API allows adding custom syncable metadata to individual notes.

The table of contents plugin tutorial should also have an example that shows how to use this API.

1 Like

Oh thank you so much!! I was confused what user data API meant but now it's more clear. This is exactly what I need!! (linking each summary to notes)

One more question: Does user data API persistently store custom data in the Joplin's SQLite database?

Yes, but I don't think you should try to access it directly. Instead use the provided methods userDataGet/userDataSet

1 Like