Skip to content

Instantly share code, notes, and snippets.

@claustres
Created August 3, 2017 15:14
Show Gist options
  • Save claustres/fe82996530fde70617b48f9d169cb3bb to your computer and use it in GitHub Desktop.
Save claustres/fe82996530fde70617b48f9d169cb3bb to your computer and use it in GitHub Desktop.
Organisation hooks to manage a segregated DB
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,
}
})
@poupryc
Copy link

poupryc commented Aug 20, 2017

app.service.('organisations').hooks is not correct => app.service('organisations').hooks 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment