Node.js multitenancy guide
Learn how to implement secure, multitenant search in your Node.js applications.
This guide will walk you through implementing search in a multitenant Node.js application handling sensitive medical data.
What is multitenancy?
In Meilisearch, you might have one index containing data belonging to many distinct tenants. In such cases, your tenants must only be able to search through their own documents. You can implement this using tenant tokens.
Requirements
- Node.js and a package manager like
npm
,yarn
, orpnpm
- Meilisearch JavaScript SDK
- A Meilisearch server running — see our quick start
- A search API key — available in your Meilisearch dashboard
- A search API key UID — retrieve it using the keys endpoints
Prefer self-hosting? Read our installation guide.
Data models
This guide uses a simple data model to represent medical appointments. The documents in the Meilisearch index will look like this:
For the purpose of this guide, we assume documents are stored in an appointments
index.
Creating a tenant token
The first step is generating a tenant token that will allow a given patient to search only for their appointments. To achieve this, you must first create a tenant token that filters results based on the patient’s ID.
Create a search.js
file and use the following code to generate a tenant token:
When Meilisearch gets a search query with a tenant token, it decodes it and applies the search rules to the search request. In this example, the results are filtered by the patient
field. This means that a patient can only search for their own appointments.
Using the tenant token
Now that you have a tenant token, use it to perform searches. To achieve this, you will need to:
- On the server: create an endpoint to send the token to your front-end
- On the client: retrieve the token and use it to perform searches
Serving the tenant token
This guide uses Express.js to create the server. You can install express
by running:
Then, add the following code in a server.js
file:
This code creates an endpoint at http://localhost:3000/token
that accepts an id
query parameter and returns a tenant token.
Making a search
Now that we have an endpoint, you will use it to retrieve the tenant token in your front-end application. This guide uses InstantSearch.js to create a search interface. You will use CDN links to include InstantSearch.js and the Meilisearch InstantSearch.js connector in your HTML file.
Create client.html
file and insert this code:
Ta-da! You have successfully implemented a secure, multitenant search in your Node.js application. Users will only be able to search for documents that belong to them.
Conclusion
In this guide, you saw how to implement secure, multitenant search in a Node.js application. You then created an endpoint to generate tenant tokens for each user. You also built a search interface with InstantSearch to make searches using the tenant token.
All the code in this guide is a taken from our multitenacy example application. The code is available on GitHub.
Was this page helpful?