What doe's this library? Honestly nothing special. It's just connecting to "mongodb" database for you, and additionally gives you some pattern, that you can deside to use or not. That's it. It's just connect to db, and give you a client instance on which you can perform same stuff as you was before. And in turn, It's still your responsability to configure this connection. And now you think, why i need this package, if I still responsible for all this routine stuff, and this how I answere to this question -
IF (
- You dont want to run again and again through same steps of diving in to the mongod nodejs driver documentation to see the examples of how they do that stuff, and implement same thing one to one, maybe with some variable name changes in your project.
- Or you simply want to start learn mongodb with native driver, immediately starting from CRUD Opperations for example.
- You want to fetch some data from somewhere as soon as posible, and store it somewhere, and you allready now how to use mongodb nodejs driver, then just install this package in your project, give it a url, and immediately get client instance to perform opperartions...
- You can not make a choice on how it is more convenient to pass your client instance to your functions, maybe alongside to your regular arguments, or maybe differently, well, I'll give you a pattern, I think is not bad, at least its good for fast projects, for which I designed this package originally.
) Then this library may be usefull for you.
- Navigate to your project folder. cmd:
cd myprojectDir
- If your project still not initialized by npm, i.e. theres no package.json file, initialize It, for simplicity you can just type cmd:
npm init -y
- Install the package locally. cmd:
npm i -S connrun
( Same as cmd:npm install --save connrun
)
Let me descibe It's simplest usage. Just look at the code below:
const { connRun } = require('connrun')
const MONGODBURI = 'mongodb://localhost:8000'
const connRunCallback = ({ client }) => () => client
const yourApp = async () => {
const client = await ConnRun({ url: MONGODBURI }, connRunCallback)
// // Do Some stuff...
// // 1. For example:
// const db = client.db('mydb')
// const collection = db.collection('mycollection')
// // and soo on...
// // 2. Or call your own functions
// await myfunction(client, ...args)
await client.close()
}
Here we simply pass to ConnRun function "options" object with url property which contains url of database - as first parameter, and a function which returns another function, which are will be executed by connRun automaticaly - as second parameter, and we get MongoClient instance after promise returned by connRun function resolves, and thereafter we do anything what wee need.
But now lets consider the case when we have to write some functions which are rely on client instance, then we might deside how to pass that client instance to that functions to get most convenient and most consistant way to stick with afterwardes in our project when writing such a functions... For that reasone I introduce a pattern, which can help you on that way, Lets write all our functions which expect client instance, following this pattern: ({ client }) => () => //mystuff
Benefits of this pattern are following:
- In this case arguments of your base function will not interfere with arguments, that ConnRun will pass to you. and in case when you deside to change signiture of your base function, It will be truly simple.
- You can reuse this functions without ConnRun, as any function you write with this pattern In the end has all nessesery arguments to internally call another functions with the same pattern.
- And of course afterwards you can follow this pattern to very end of your project. See The Examples
That's It, hop this will be useful for you
See typedefs
You Can Get ConnRun either as default export or named export... And if writing with vanila javascript, using const ConnRun = require('connrun')
you not getting features like intelliSence, try to use named exports: const { ConnRun } = require('connrun')