├── .gitignore ├── README.md ├── package.json ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fauna-multitenancy-js -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "faun", 3 | "version": "1.0.0", 4 | "description": "trial ", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "faunadb": "^2.0.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Flevian Kanaiza 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var faunadb = require("faunadb"), 2 | q = faunadb.query; 3 | var client = new faunadb.Client({ secret: 'YOUR_FAUNADB_ADMIN_SECRET' }); 4 | var top_db = ["production", "internal", "staging"]; 5 | var parent_db = "staging"; 6 | var child_db = ["people_department", "it_department"]; 7 | var top_db_role = "admin"; 8 | var child_db_role = "server"; 9 | 10 | 11 | // create Top level databases 12 | var top_db_creation = client.query( 13 | q.Map( 14 | top_db, 15 | function(name) { 16 | return q.CreateDatabase({ name: name }); 17 | })); 18 | 19 | // Generate fauna databases array for the top level databases 20 | top_db_creation.then(function(data){ 21 | var new_top_db = top_db.map(function(value, index){ 22 | return q.Database(value) 23 | }); 24 | 25 | // generate keys for top level databases 26 | var top_db_key_creaction = client.query( 27 | q.Map( 28 | new_top_db, 29 | function(db) { 30 | return q.CreateKey({ role: top_db_role, database: db }); 31 | })); 32 | 33 | return top_db_key_creaction.then(function(data) { 34 | // Generate an object of top database names and their keys 35 | var top_db_secrets = {}; 36 | var top_db_keys = Object.values(data).map(function(element){ 37 | return element.secret; 38 | }); 39 | top_db.forEach(function(key, index){ 40 | top_db_secrets[key] = top_db_keys[index] 41 | }); 42 | console.log("---------------Top database secrets-----------"); 43 | console.log(top_db_secrets); 44 | console.log("----------------------------------------------"); 45 | var parent_db_key; 46 | Object.keys(top_db_secrets).map(function(key, index) { 47 | if (key == parent_db){ 48 | parent_db_key = top_db_secrets[key]; 49 | } 50 | }); 51 | 52 | // Create parent database instance 53 | var client = new faunadb.Client({ secret: parent_db_key }); 54 | 55 | // Create child databases 56 | var child_db_creation = client.query( 57 | q.Map( 58 | child_db, 59 | function(name) { 60 | return q.CreateDatabase({ name: name }); 61 | })); 62 | 63 | return child_db_creation.then(function(data){ 64 | // Generate fauna databases array for the low level databases 65 | var new_child_db = [] 66 | child_db.forEach(function(value, index){ 67 | return new_child_db.push(q.Database(value)) 68 | }) 69 | 70 | // Generate child databases keys 71 | var child_db_key_creation = client.query( 72 | q.Map( 73 | new_child_db, 74 | function(db) { 75 | return q.CreateKey({ role: child_db_role, database: db }); 76 | })); 77 | 78 | return child_db_key_creation.then(function(data) { 79 | // Generate an object of child database names and their keys 80 | var child_db_secrets = {}; 81 | var child_db_keys = [] 82 | Object.values(data).forEach(function(element){ 83 | child_db_keys.push(element.secret); 84 | }) 85 | child_db.forEach(function(key, index){ 86 | child_db_secrets[key] = child_db_keys[index] 87 | }); 88 | console.log("-------------Child database secrets-----------"); 89 | console.log(child_db_secrets); 90 | console.log("----------------------------------------------"); 91 | }); 92 | }); 93 | }); 94 | }).catch(function(error){console.error(error)}); 95 | --------------------------------------------------------------------------------