How To Query (Not List) Child Databases By Data Properties

Viewed 542

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

1 Answers

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.

Related