6

Is there a way to query child databases by data properties i.e. via index? In multitenancy scenarios we can end up with a ton of child databases and it would be really nice to be able to query them e.g. (child dbs on this version, child dbs that are pending closure etc)

Thanks

MToTheA
  • 37
  • 5
  • 1
    @TheGeneral I'm open to suggestions around this. I wouldn't use multiple databases if I was working with an RDBMS or even some other NoSql databases. FaunaDB seems to make the child databases much easier to work (it's data) although the management concerns still exist. Should also point out that, security isn't the only reason for using child databases. An example alternative reason might be to aggregate resource usage per tenant. How does your client id approach work? – MToTheA Jun 19 '19 at 23:45

1 Answers1

6

Like other objects in FaunaDB, databases can be created with arbitrary user data, i.e CreateDatabase({name: "bob", data:{prop: "cool"}}). This user data can also be indexed just like anything else. An example shell session:

myDb> Get(Index("by_prop"))
{ ref: Index("by_prop"),
  ts: 1560970634960000,
  active: true,
  partitions: 1,
  name: 'by_prop',
  source: Databases(),
  terms: [ { field: [ 'data', 'prop' ] } ] }
myDb> Get(Database("bob"))
{ ref: Database("bob"),
  ts: 1560970374730000,
  name: 'bob',
  data: { prop: 'cool' } }
myDb> Paginate(Match(Index("by_prop"), "cool"))
{ data: [ Database("bob") ] }

Using an index that has terms over data.prop, I can match the database I am interested in. The choice of term / value is arbitrary and can be whatever makes sense for your application.

dano
  • 5,640
  • 5
  • 27
  • 27
benjumanji
  • 1,395
  • 7
  • 15