-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Matheus Martins
committed
Oct 7, 2019
1 parent
087ebeb
commit 5351b56
Showing
8 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import knex from 'knex'; | ||
|
||
import commonDBConnection from './commonDBConnection'; | ||
import { getNamespace } from 'continuation-local-storage'; | ||
|
||
let connectionMap; | ||
|
||
export const connectAllDb = async () => { | ||
let tenants; | ||
try { | ||
tenants = await commonDBConnection.select('*').from('tenants'); | ||
} catch (e) { | ||
console.log('error', e); | ||
return; | ||
} | ||
connectionMap = | ||
tenants | ||
.map(tenant => { | ||
return { | ||
[tenant.slug]: knex(createConnectionConfig(tenant)) | ||
} | ||
}) | ||
.reduce((prev, next) => { | ||
return Object.assign({}, prev, next); | ||
}, {}); | ||
} | ||
|
||
const createConnectionConfig = (tenant) => { | ||
return { | ||
client: process.env.DB_CLIENT, | ||
connection: { | ||
host: tenant.db_host, | ||
port: tenant.db_port, | ||
user: tenant.db_username, | ||
database: tenant.db_name, | ||
password: tenant.db_password | ||
}, | ||
pool: {min: 2, max: 20} | ||
}; | ||
} | ||
|
||
export const getConnectionBySlug = (slug) => { | ||
return connectionMap ? connectionMap[slug] : null; | ||
} | ||
|
||
export const getConnection = () => { | ||
const nameSpace = getNamespace('unique context'); | ||
const conn = nameSpace.get('connection'); | ||
if (!conn) { | ||
throw 'Connection is not set for any tenant database.'; | ||
} | ||
return conn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createNamespace } from 'continuation-local-storage'; | ||
|
||
import { getConnectionBySlug } from '../infra/connectionManager'; | ||
|
||
|
||
let nameSpace = createNamespace('unique context'); | ||
|
||
|
||
export const resolve = (req, res, next) => { | ||
const slug = req.query.slug; | ||
if (!slug) { | ||
res.json({ message: `Please provide tenant's slug to connect.` }); | ||
return; | ||
} | ||
nameSpace.run(() => { | ||
nameSpace.set('connection', getConnectionBySlug(slug)); | ||
next(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {getConnection} from '../infra/connectionManager'; | ||
|
||
export const getAll = async (req, res) => { | ||
res.json({body: await getConnection().select('*').from('users')}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE users( | ||
id INT PRIMARY KEY, | ||
first_name VARCHAR(255) NOT NULL, | ||
last_name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL, | ||
address VARCHAR(400) | ||
); | ||
|
||
INSERT INTO users(id,first_name, last_name, email, address) VALUES | ||
(1,'Kevin', 'Martins', 'kevinmmartins@gmail.com', 'BR'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE users( | ||
id INT PRIMARY KEY, | ||
first_name VARCHAR(255) NOT NULL, | ||
last_name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL, | ||
address VARCHAR(400) | ||
); | ||
|
||
INSERT INTO users(id,first_name, last_name, email, address) VALUES | ||
(1,'Kauan', 'Martins', 'kauanmmartins@egmail.com', 'USA'); |