2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Use case to create a system of parent child database in Arangodb for each sub domain


To create an application using Arangodb where we have requirement, To access the root user connection as default and based on the request from domain access the child database via root database.

The Problem:

If we have a blog website that gives service like user can signup to get a sub-domain and start writing blog on their own sub-domains. Now the situation happens like one of the user wants to get all his data extracted from providers database in different formats (May be in XML or JSON or any other format). Now for provider to extract data from the single database which might contains millions of user data will be a really painful job. How to to make this situation easy with less cost.

In details Requirement:

Let’s say we have requirement where we have blog website with domain.com and we are giving access to users to do signup and create sub-domains of there own and write blogs. If a user signup and creates a sub-domain, sub1.domain.com and like this sub2.domain.com, sub3.domain.com, sub4.domain.com etc. Now my database design expectation is not to merge/store all blogs in a single collection rather that to store in different databases. These different database will be created at run-time and for every sub-domain one new dedicated database will get created. If any user logged in using the sub-domain and write any blog will store in the database dedicate to the sub-domain. Now as we all know getting new database connection is always heavy and costly. So what we will do is, will get the root database connection one time and based on request from sub-domain will get the child database connection without doing authentication again and again.

Lets create the db parent and child databases using Arangodb console.

To create a new root user:

var users = require(“@arangodb/users”);
users.save(“admin1”, “mypassword”);
users.grantDatabase(“admin1”, “_system”);

login via console with root user and child databases [ — server.username admin1 — server.password mypassword — serevr.database _system]

var users = require(“@arangodb/users”);
users.save(“user1”, “user1”);
db._createDatabase(“db1”, [], [{ username: “user1”, active: true}])

var users = require(“@arangodb/users”);
users.save(“user2”, “user2”);
db._createDatabase(“db2”, [], [{ username: “user2”, active: true}])

var users = require(“@arangodb/users”);
users.save(“user3”, “user3”);
db._createDatabase(“db3”, [], [{ username: “user3”, active: true}])

So till now we saw how to create a new root user and child databases with associated new user and password for each child database using Arangodb console.

Now, when we are accessing application by default we have the root user database connection. When we try to get the access of child databases we need not to do re-authentication as we already have the root user who have access for all child databases.