feathers-mongodb-management
Feathers service adapters for managing MongoDB databases, users and collections
Installation
With NPM npm install feathers-mongodb-management --save
or Yarn yarn add feathers-mongodb-management
v1.x is expected to work with Feathers v3 (a.k.a Buzzard) and NodeJS v12
v2.x is expected to work with Feathers v5 (a.k.a Dove) and NodeJS v16
Documentation
If a large number of users require it a more complete feathers-mongodb-management documentation will be created. For now as the API is pretty straigth forward the example and tests should be sufficient to understand how it works.
The initial use case was to simplify data segregation for a SaaS application where each user could belong to different organisations like in GitHub. Indeed, one key aspect of access control is to filter the resources a user can reach. In this specific use case the users always work in the context of an organisation acting as a filter applied on the manipulated resources. This issue is often tackled by applying a client-side filter on a global service like /users?org=orgId
and storing on each resouce the organisation it belongs to. We consider this approach as more harder to maintain in the long-term due to additional filtering and relations in the data model. As a consequence we created this plugin to "physically" separate the data of each organisation in a dedicated DB instead of doing this "logically" using filtering.
You can find more information by reading this article.
Complete Example
Here's an example of a Feathers server that uses feathers-mongodb-management
.
const feathers = require('feathers');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const bodyParser = require('body-parser');
const errorHandler = require('feathers-errors/handler');
const mongodb = require('mongodb');
const plugin = require('feathers-mongodb-management');
const app = feathers()
.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.use(errorHandler());
const client = await mongodb.MongoClient.connect('mongodb://127.0.0.1:27017')
app.use('/mongo/databases', plugin.database({ adminDb: client.db('feathers-test').admin(), client }));
let dbService = app.service('/mongo/databases');
let db = await dbService.create({ name: 'test-db' })
db = client.db('test-db');
app.use('/mongo/test-db/collections', plugin.collection({ db }));
let collectionService = app.service('/mongo/test-db/collections');
const collection = await collectionService.create({ name: 'test-collection' })
app.use('/mongo/test-db/users', plugin.user({ db }));
let userService = app.service('/mongo/test-db/users');
const user = await userService.create({ name: 'test-user', password: 'test-password', roles: ['readWrite'] })
...
app.listen(3030);
console.log('Feathers app started on 127.0.0.1:3030');
License
Copyright (c) 2016
Licensed under the MIT license.