0.5.4 • Published 4 years ago

simple-mongodb-helper-2 v0.5.4

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

simple-mongo-helper-2

Synopsis

Hello!

Just a short readme about this db.

This helper expose native entries from mongodb driver and add some elements to simplify some actions.

Installation

npm i simple-mongodb-helper-2

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)

let opt = {
    "uri": "mongodb://ip:port/dbname", <-- MongoDb uri with options
    "dbName": "dbname",
    "options": { <-- MongoDB drivers options
        "auth": {
            "password": "xxxx",
            "username": "yyyy",
            "authSource": "dbauth"
        }
    },
    keepAlive: true,<-- for long time without request
    "forceReconnect" : true, <-- forced reconnection try
    "forceReconnectLimit" : 5000 <-- limit of forced reconnection try
};
db.connect(opt).then(() => {
  //...
}).catch((e: any) =>  {
  //...
});

This method create db.mDB witch is active db connexion (the connected MongoClient) db.usedDB = dbname; db.config = all configuration parameters

note, forced connection paramters are : authSource: the opt.dbName, keepAlive: true, tlsAllowInvalidCertificates: true, tlsAllowInvalidHostnames: true, tlsInsecure: true, sslValidate: false, socketTimeoutMS: 360000, connectTimeoutMS: 360000, useNewUrlParser: true, useUnifiedTopology: true,

All parameters could be overwrited

example with native function call

db.mDB.db(db.usedDB).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 = (database: any)

get list of collections

db.export = (collections: any, data: any)

serialized export

db.import = (data: any)

import serialized datas

db.createIndexes = (collection: string, indexes: any)

indexes creator

db.indexes = (collection: string)

get indexes

db.deserialize = (input: any)

used for export/import

db.serialize = (input: any)

used for export/import

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago