-
-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #738 from ebrahim354/updated-multi-tenancy
added connection manager
- Loading branch information
Showing
24 changed files
with
2,278 additions
and
144 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,17 @@ | ||
module.exports.androidFirebaseOptions = { | ||
apiKey: process.env.apiKey, | ||
appId: process.env.appId, | ||
messagingSenderId: process.env.messagingSenderId, | ||
projectId: process.env.projectId, | ||
storageBucket: process.env.storageBucket, | ||
}; | ||
|
||
module.exports.iosFirebaseOptions = { | ||
apiKey: process.env.iOSapiKey, | ||
appId: process.env.iOSappId, | ||
messagingSenderId: process.env.iOSmessagingSenderId, | ||
projectId: process.env.iOSprojectId, | ||
storageBucket: process.env.iOSstorageBucket, | ||
iosClientId: process.env.iosClientId, | ||
iosBundleId: process.env.iosBundleId, | ||
}; |
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 @@ | ||
const Tenant = require('../models/Tenant'); | ||
const Database = require('../Database/index'); | ||
const { setConnection, getConnection } = require('./connections'); | ||
|
||
module.exports = async (organizationId) => { | ||
try { | ||
const alreadyConnected = getConnection(organizationId); | ||
if (alreadyConnected) return alreadyConnected; | ||
const [tenant] = await Tenant.find({ organization: organizationId }); | ||
if (!tenant || !tenant.url) throw new Error('Organization not found!'); | ||
console.log('tenant', tenant); | ||
let connection = new Database(tenant.url); | ||
await connection.connect(); | ||
setConnection(tenant.organization, connection); | ||
return connection; | ||
} catch (e) { | ||
console.log('organization not found!'); | ||
} | ||
}; |
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 @@ | ||
const connections = {}; | ||
|
||
const setConnection = (orgId, connection) => { | ||
connections[`${orgId}`] = connection; | ||
}; | ||
|
||
const getConnection = (orgId) => { | ||
if (!connections[orgId]) return null; | ||
return connections[orgId]; | ||
}; | ||
|
||
const destroy = async () => { | ||
for (let conn in connections) { | ||
await connections[conn].disconnect(); | ||
delete connections[conn]; | ||
} | ||
}; | ||
|
||
module.exports = { setConnection, getConnection, destroy }; |
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,9 @@ | ||
const connections = require('./connections'); | ||
|
||
module.exports = async () => { | ||
try { | ||
await connections.destroy(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; |
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,9 @@ | ||
const { getConnection } = require('./connections'); | ||
|
||
module.exports = (organizationId) => { | ||
try { | ||
return getConnection(organizationId); | ||
} catch (e) { | ||
console.log('organization not found!'); | ||
} | ||
}; |
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,11 @@ | ||
const addTenantConnection = require('./addTenantConnection'); | ||
const getTenantConnection = require('./getTenantConnection'); | ||
const initTenants = require('./initTenants'); | ||
const destroyConnections = require('./destroyConnections'); | ||
|
||
module.exports = { | ||
addTenantConnection, | ||
getTenantConnection, | ||
initTenants, | ||
destroyConnections, | ||
}; |
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,16 @@ | ||
const Tenant = require('../models/Tenant'); | ||
const Database = require('../Database/index'); | ||
const { setConnection } = require('./connections'); | ||
|
||
module.exports = async () => { | ||
try { | ||
const databases = await Tenant.find(); | ||
for (let db of databases) { | ||
let connection = new Database(db.url); | ||
await connection.connect(); | ||
setConnection(db.organization, connection); | ||
} | ||
} catch (e) { | ||
console.log('connection failed'); | ||
} | ||
}; |
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,3 @@ | ||
module.exports = (tenantId, id) => { | ||
return tenantId + ' ' + id; | ||
}; |
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,4 @@ | ||
module.exports = (fullId) => { | ||
const [tenantId, id] = fullId.split(' '); | ||
return { tenantId, id }; | ||
}; |
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,31 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const tenantSchema = new Schema({ | ||
organization: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Organizaiton', | ||
}, | ||
url: { | ||
type: String, | ||
required: true, | ||
}, | ||
type: { | ||
type: String, | ||
enum: ['MONGO', 'POSTGRES'], | ||
default: 'MONGO', | ||
required: true, | ||
}, | ||
status: { | ||
type: String, | ||
required: true, | ||
default: 'ACTIVE', | ||
enum: ['ACTIVE', 'BLOCKED', 'DELETED'], | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
module.exports = mongoose.model('Tenant', tenantSchema); |
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,25 @@ | ||
const User = require('../../models/User'); | ||
const { NotFoundError } = require('errors'); | ||
const requestContext = require('talawa-request-context'); | ||
const { | ||
USER_NOT_FOUND_MESSAGE, | ||
USER_NOT_FOUND_CODE, | ||
USER_NOT_FOUND_PARAM, | ||
} = require('../../../constants'); | ||
|
||
module.exports = async (parent, args, context) => { | ||
const user = await User.findOne({ _id: context.userId }); | ||
if (!user) { | ||
throw new NotFoundError( | ||
requestContext.translate(USER_NOT_FOUND_MESSAGE), | ||
USER_NOT_FOUND_CODE, | ||
USER_NOT_FOUND_PARAM | ||
); | ||
} | ||
|
||
user.token = null; | ||
|
||
await user.save(); | ||
|
||
return true; | ||
}; |
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,25 @@ | ||
const User = require('../../models/User'); | ||
const { NotFoundError } = require('errors'); | ||
const requestContext = require('talawa-request-context'); | ||
const { | ||
USER_NOT_FOUND_MESSAGE, | ||
USER_NOT_FOUND_CODE, | ||
USER_NOT_FOUND_PARAM, | ||
} = require('../../../constants'); | ||
|
||
module.exports = async (parent, args, context) => { | ||
const user = await User.findById(context.userId); | ||
if (!user) { | ||
throw new NotFoundError( | ||
requestContext.translate(USER_NOT_FOUND_MESSAGE), | ||
USER_NOT_FOUND_CODE, | ||
USER_NOT_FOUND_PARAM | ||
); | ||
} | ||
|
||
user.token = args.token; | ||
|
||
await user.save(); | ||
|
||
return true; | ||
}; |
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
Oops, something went wrong.