A
A
Adel Khalitov2019-04-25 10:40:42
Node.js
Adel Khalitov, 2019-04-25 10:40:42

How to export an ACL module?

I have been suffering for 2 days with the export of the ACL module , the search engine is silent in solving these problems. There are similar topics on other forums, but no solution has been found, although I'm sure it's possible!
Essence of the question:
According to the ACL instruction, it needs to be initialized inside mongoose.conect:

const mongoose          = require('mongoose');
var node_acl = require('acl');

exports.connect = function (done) {
  mongoose.connect(url, {useCreateIndex: true, useNewUrlParser: true}, function (err, db) {
    if (err) {
      console.log('Ошибка запуска базы')
      return done(err);
    }
    var acl = new node_acl( new node_acl.mongodbBackend(mongoose.connection.db, 'acl_') );
    console.log('Подключились к базе данных');
    done();
  });
};

With this connection, everything works great, inside mongoose.connect all ACL methods are executed. But here's how you can export the ACL itself to another module?
If you initialize acl after mongoose.connect:
exports.connect = function (done) {
  mongoose.connect(url, {useCreateIndex: true, useNewUrlParser: true}, function (err, db) {
    if (err) {
      console.log('Ошибка запуска базы')
      return done(err);
    }
    var acl = new node_acl( new node_acl.mongodbBackend(mongoose.connection.db, 'acl_') );
    console.log('Подключились к базе данных');
    done();
  });
};
var conn    = mongoose.connection;
var acl = new node_acl( new node_acl.mongodbBackend(conn.db, 'acl_') );

Then, for natural reasons, conn.db === undefined, and therefore ACL initialization will fail.
Reading all sorts of forums with the ACL tag, I came across a possible solution to this problem:
Wrap acl in a function if you are calling the poll results or wrap the connection in a Promise that is exported

It seems clear, but in practice I could not implement it all.
If anyone has encountered this problem, please let me know how you solved it. If someone has an understanding of how to help me, please help with pieces of code, maybe I can understand from the words but I can’t implement it.
How can this module be exported?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adel Khalitov, 2019-04-30
@SaveLolliPoP

module.exports = new Promise( (resolve, reject) => {
    mongoose.connect(url, {useCreateIndex: true, useNewUrlParser: true}, function (err, db) {
        var acl = new node_acl(new node_acl.mongodbBackend(mongoose.connection.db, 'acl_'));
        resolve(acl);
    });
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question