simple-mongodb-helper2
v1.0.2
Published
This helper expose native entries from mongodb driver and add some elements to simplify some actions. A lot of old commands are purged from v1 helper.
Downloads
7
Readme
simple-mongo-helper-2
Synopsis
This helper expose native entries from mongodb driver and add some elements to simplify some actions. A lot of old commands are purged from v1 helper.
Installation
npm i simple-mongodb-helper2
Usage
init in your code
with import
import { helper as db } from './dist/index.js'
with require
const myMongoHelperClass = require('simple-mongodb-helper-2'); const db = myMongoHelperClass.helper;
connect
db.connect(param)
param = {
"uri": "mongodb://ip:port/dbname", <-- MongoDb uri with options
"dbName": "dbname",
"options": { <-- MongoDB drivers options
"auth": {
"password": "xxxx",
"username": "yyyy",
"authSource": "dbauth"
}
},
keepConnected : true <-- if set to true, check connexion every 2s
};
db.connect(param).then(() => {
//...
}).catch((e: any) => {
//...
});
This method create db.client witch is active db connexion (the connected MongoClient) db.usedDB : dbname db.config : all configuration parameters db.connected : last connection check result
Default parameters if not overxwrited are :
defaultParameters = {
authSource: (helper.config.dbName !== undefined) ? helper.config.dbName : 'admin',
keepAlive: true,
tlsAllowInvalidCertificates: true,
tlsAllowInvalidHostnames: true,
tlsInsecure: true,
sslValidate: false
}
All parameters could be overwrited
example with native function call
db.client.db().collection('colname').find({}).toArray().then((list) => {
return Promise.map(list, (el) => {
console.log(el)
}, { concurrency: 4 });
})
find
db.find(collection, filter, projection, sort, limit)
db.find('test',{_id:'test'},{_id:0,order:1,info:1},{order:-1},10).then((result) => {
//...
}).catch((e: any) => {
//...
});
aggregate
db.aggregate(collection, AggResquests)
db.aggregate('test',[{...},{...}]).then((result) => {
//...
}).catch((e: any) => {
//...
});
insert
db.insert(collection, documents)
db.insert('test',[{"a":"b"},{"c":"d"}]).then((result) => {
//...
}).catch((e: any) => {
//...
});
update
db.update(collection, filter, values, upsert)
db.update('test',{_id:'test'},{order:2,info:"new test"},true).then((result) => {
//...
}).catch((e: any) => {
//...
});
eval raw request
db.eval(rawScript, inputs)
// inputs is an object
let inputs = { database : "exemple", collection" : "demo" }
// rawscript must return a promise
let rawScript = "db.db(inputs.database).collection(inputs.collection).find({}).toArray()";
db.eval(rawScript,inputs).then((result) => {
//...
}).catch((e: any) => {
//...
});
replace
db.replace(collection, filter, to)
db.replace('test',{_id:'test'},{ 'element':'test'}).then((result) => {
//...
}).catch((e: any) => {
//...
});
more informations
db.count = (collection: string, filter: any)
count documents associated with filter
db.purgeBefore = (collection: string, filterKey: string, olderThanSecond: number)
purge elements where filterKey key is older than olderThanSecond parameters
db.delete = (collection: string, filter: any)
delete elements corresponding to filter
db.eval = (rawScript: string, inputs: any)
Allow to run script on server side with inputs as parameters, db as database connection and rawscript the script to eval. waring about security issue on bad use.
db.aggregate = (collection: string, AggResquests: any)
the aggregate request
db.find = (collection: string, filter: any, projection: any, sort: any, limit: any)
the find request. If filter is an array, run all filter and return an array of all results If not define, limit is 100
db.insert = (collection: string, documents: any)
the insert command, single or array of documents
db.replace = (collection: string, filter: any, to: any)
The replace command, as find filter is selector (single or array of selectors) and to the set of values
db.update = (collection: string, filter: any, values: any, upsert: any)
The update command, as find filter is selector (single or array of selectors)
db.collections = ()
get list of collections