Created
August 3, 2017 15:14
-
-
Save claustres/fe82996530fde70617b48f9d169cb3bb to your computer and use it in GitHub Desktop.
Organisation hooks to manage a segregated DB
This file contains hidden or 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
import mongodb from 'mongodb' | |
import mongoManager from 'feathers-mongodb-management' | |
// Connect to the DB at startup | |
app.db = await mongodb.connect(app.get('dbUrl')) | |
// Create the service to manage DBs | |
app.use('/databases', mongoManager.database({ db: app.db }) | |
... | |
// Hooks to manage DB linked to an organisation | |
function createOrganisationDB (hook) { | |
return hook.app.service('databases').create({ | |
name: hook.result._id.toString() | |
}) | |
.then(db => { | |
debug('DB created for organisation ' + hook.result.name) | |
return hook | |
}) | |
} | |
function removeOrganisationDB (hook) { | |
return hook.app.service('databases').remove(hook.result._id.toString()) | |
.then(db => { | |
debug('DB removed for organisation ' + hook.result.name) | |
return hook | |
}) | |
} | |
... | |
app.service.('organisations').hooks({ | |
after: { | |
create: createOrganisationDB, | |
remove: removeOrganisationDB, | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
app.service.('organisations').hooks is not correct => app.service('organisations').hooks 😄