2

I am working on a multi-tenancy architecture using FaunaDB where each team of users has a dedicated child database.

I am trying to create a collection (Users) inside the child database, where data about the team and its users is saved.

I am using Fauna's Javascript client. The following code creates a collection at the top level database, while I am trying to add it into the child database.

        var user = await faunaClient.query(
          q.Create(
            q.Collection('users'),
            { data: ctx.currentUser }
          )
        )

How can I adapt it to specify the collection inside the child database?

Do I need to pass a reference to the child database somewhere? Or should I instantiate a new Fauna client, with a key from the child database?

Anas Tiour
  • 1,344
  • 3
  • 17
  • 33

2 Answers2

3

If you have the admin key of the parent database you can access the child from a new client based on that secret. Format the secret like so admin_secret:child_db_name:role

Eigil
  • 81
  • 2
  • I confirm! Working beautifully. Hope it gets documented somewhere, and I hope it gets changed in the future, or it will break my setup for sure – Anas Tiour Mar 27 '20 at 20:38
2

In order to write on sub-databases you need to use a key that points to that database, so yes, you need a new Fauna Client

Marrony
  • 231
  • 1
  • 3
  • Thanks! So I would generate a key for each tenant/child database, save it in a collection in the top-level database. And everytime I want to access the child database in question, I would need to fetch the key from the top-level collection? Is there a recomended way to handle keys? – Anas Tiour Mar 26 '20 at 16:28