1.0.2 • Published 7 years ago

who-can-mongodb v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

WhoCan MongoDB

Who can do what? MongoDB style

Install

yarn install who-can-mongodb
npm install --save who-can-mongodb

API

const WhoCan = require('who-can-mongodb'),
    MongoDB = require('mongodb');
    
MongoDB.connect('mongodb://localhost/database', async function(error, database) {
    if (error) {
        return console.error(error);
    }
    
    const can = new WhoCan(database);
    
    async function purchaseBook(user, book) {
        await can.allow(user.id, 'take', book.id);
    }
    
    async function takeBook(user, book) {
        if (!await can.can(user.id, 'take', book.id)) {
            throw new Error('Please puchase the book');
        }
        return book;
    }
    
    const user = {
            id: '0d399b15-cad4-4c85-b892-15a14fd40a48'
        },
        book = {
            id: 'b3111978-3a1a-416f-979c-d21a8b4365c6'
        }
    
    await purchaseBook(user, book)
        .then(() => takeBook(user, book))
        .then(() => console.log('I have the book!'))
        .catch(() => console.error('Should have brought the book first!')); 
});
1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago