express mongodb connection pool

Because although there are tutorials on the web mongodb nodejs connection pool, but use the outdated api, so we come up with this method, share

 

Need-based express knowledge http://www.expressjs.com.cn/

 

Using official mongodb drive and generic-pool

npm install --save mongodb generic-pool

 

Of course, I also wrote the official asked to write the file, if head comparison of iron can also go directly to (funny), here is the link, after reading the article can also look at official documents

https://www.npmjs.com/package/generic-pool

https://docs.mongodb.com/ecosystem/drivers/node/

After all, I wrote a blog is Chinese thing. . .

 

Create a connection pooling code:

the require MongodbClient = const ( 'MongoDB' ) .MongoClient; 
const GenericPool = the require ( 'the pool-Generic' ); 


const Factory = { 

    Create: function () {// create a link 
    return MongodbClient.connect ( "MongoDB: //" + "localhost" + ":" + "27017" , {useUnifiedTopology: to true });
    // return a client link object of mongodb }, the destroy:
function (client) {// link destruction client.close (); // Close link to note here, client parameter is above objects we created } } // const the opts = { max: 10 , the maximum number of links // min:2 // minimum. . } Const myPool = GenericPool.createPool (Factory's, the opts); // create a connection pooling is here module.exports = myPool; // Export to other modules

 Create factory objects and opt objects, respectively, to create and destroy methods and options

 

To use, where I used the route express, creating a routing user, of course, this is the express knowledge, not to discuss:

Express the require = const ( ' Express ' ); 
const Router = express.Router (); 
const myPool = the require ( ' ./db_pool ' ); // this is the above code file db_pool 

Router.get ( ' / User ' , (REQ, RES, Next) => { 
    var name = req.query.name || '' ; // get query parameters 

    var queryObject = {}
     IF (name =! '' ) { 
        queryObject.name = name; 
    } 

    resoursePro var =myPool.acquire (); // where a connection pool request, it returns a promise object do not understand if the link to: https://developer.mozilla.org/zh-CN/docs/Web / the JavaScript / Reference / Global_Objects / Promise 
    resoursePro. the then ((Client) => {// Client above we create here is the return value of the object in the factory 
        the let Cursor = client.db ( ' dbname ' ) .collection ( ' User ' ). the Find (queryObject); // use the following operations it is conventional, as the main speaker connection pool, too lazy to write ... 
        the let somethign = cursor.toArray (); 
        . somethign the then ((the Result) => {
             // the console.log (Result);
             res.json (result); // query response result
            . myPool.release (Client) the then (() => {// use this link will be returned over a ah 
                the console.log ( ' Release ' ) 
            }); 
        }) the catch ((ERR). => { 
            myPool.release (Client) .catch ((ERR) => { 
                the console.log (ERR) 
            }) 
        }) 
    }) 
})

 

Guess you like

Origin www.cnblogs.com/incredible-x/p/11924768.html