pino-mongodb
Advanced tools
Comparing version 2.1.2 to 3.0.0
@@ -7,3 +7,3 @@ 'use strict' | ||
try { | ||
log = JSON.parse(data) | ||
log = typeof data === 'string' ? JSON.parse(data) : data | ||
@@ -10,0 +10,0 @@ if (log.time) { |
{ | ||
"name": "pino-mongodb", | ||
"version": "2.1.2", | ||
"version": "3.0.0", | ||
"description": "Insert JSON from stdin into MongoDB", | ||
@@ -9,3 +9,4 @@ "files": [ | ||
"scripts": { | ||
"test": "tap test", | ||
"test": "tap test/*.js", | ||
"test:end2end": "npm test && tap --no-check-coverage test/end-to-end/*.js", | ||
"lint": "eslint .", | ||
@@ -39,15 +40,17 @@ "lint:fix": "npm run lint -- --fix", | ||
"carrier": "0.3", | ||
"commander": "6.1", | ||
"mongodb": "3.6", | ||
"muri": "1.3" | ||
"commander": "8.2", | ||
"mongodb": "4.1", | ||
"muri": "1.3", | ||
"pino-abstract-transport": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "7.9", | ||
"eslint-config-standard": "14.1", | ||
"eslint-plugin-import": "2.22", | ||
"eslint": "7.32", | ||
"eslint-config-standard": "16.0", | ||
"eslint-plugin-import": "2.24", | ||
"eslint-plugin-node": "11.1", | ||
"eslint-plugin-promise": "4.2", | ||
"eslint-plugin-standard": "4.0", | ||
"tap": "14.10" | ||
"eslint-plugin-promise": "5.1", | ||
"eslint-plugin-standard": "5.0", | ||
"pino": "^7.0.0-rc.4", | ||
"tap": "15.0" | ||
} | ||
} |
@@ -11,43 +11,51 @@ #!/usr/bin/env node | ||
const makeInsert = require('./lib/makeInsert') | ||
const transport = require('./lib/pino-transport') | ||
program | ||
.version(pkg.version) | ||
.description(pkg.description) | ||
.arguments('[mongo-url]') | ||
.option('-c, --collection <name>', 'database collection', 'logs') | ||
.option('-o, --stdout', 'output inserted documents into stdout', false) | ||
.option('-e, --errors', 'output insertion errors into stderr', false) | ||
.option('-u, --unified', 'use mongodb unified topology', false) | ||
.parse(process.argv) | ||
module.exports = transport | ||
const mongoUrl = (program.args[0] || 'mongodb://localhost:27017/logs') | ||
if (require.main === module) { | ||
// used as cli | ||
cli() | ||
} | ||
function handleConnection (e, mClient) { | ||
if (e) { | ||
throw e | ||
} | ||
function cli () { | ||
program | ||
.version(pkg.version) | ||
.description(pkg.description) | ||
.arguments('[mongo-url]') | ||
.option('-c, --collection <name>', 'database collection', transport.defaultOption.collection) | ||
.option('-o, --stdout', 'output inserted documents into stdout', false) | ||
.option('-e, --errors', 'output insertion errors into stderr', false) | ||
.parse(process.argv) | ||
const dbName = parseMongoUrl(mongoUrl).db | ||
const mongoUrl = (program.args[0] || transport.defaultOption.uri) | ||
const db = mClient.db(dbName) | ||
const emitter = carrier.carry(process.stdin) | ||
const collection = db.collection(program.collection) | ||
const insert = makeInsert(program.errors, program.stdout) | ||
function handleConnection (e, mClient) { | ||
if (e) { | ||
throw e | ||
} | ||
emitter.on('line', (line) => { | ||
insert(collection, log(line)) | ||
}) | ||
const dbName = parseMongoUrl(mongoUrl).db | ||
process.on('SIGINT', () => { | ||
mClient.close(process.exit) | ||
}) | ||
} | ||
const db = mClient.db(dbName) | ||
const emitter = carrier.carry(process.stdin) | ||
const collection = db.collection(program.collection) | ||
const insert = makeInsert(program.errors, program.stdout) | ||
const options = { useNewUrlParser: true } | ||
if (program.unified) { options.useUnifiedTopology = true } | ||
emitter.on('line', (line) => { | ||
insert(collection, log(line)) | ||
}) | ||
MongoClient.connect( | ||
mongoUrl, | ||
options, | ||
handleConnection | ||
) | ||
process.on('SIGINT', () => { | ||
mClient.close(process.exit) | ||
}) | ||
} | ||
const options = {} | ||
MongoClient.connect( | ||
mongoUrl, | ||
options, | ||
handleConnection | ||
) | ||
} |
# pino-mongodb | ||
[![npm version](https://img.shields.io/npm/v/pino-mongodb)](https://www.npmjs.com/package/pino-mongodb) | ||
[![Build Status](https://img.shields.io/github/workflow/status/pinojs/pino-mongodb/CI)](https://github.com/pinojs/pino-mongodb/actions) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/pinojs/pino-mongodb/badge.svg)](https://snyk.io/test/github/pinojs/pino-mongodb) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
> Insert JSON from stdin into MongoDB | ||
@@ -13,7 +15,45 @@ | ||
```bash | ||
$ npm i -g pino-mongodb | ||
$ npm i pino-mongodb | ||
``` | ||
## Get started | ||
## Usage as Pino Transport | ||
You can use this module as a [pino transport](https://getpino.io/#/docs/transports?id=v7-transports) like so: | ||
```js | ||
const pino = require('pino') | ||
const transport = pino.transport({ | ||
uri: 'mongodb://localhost:27017/', | ||
database: 'logs', | ||
collection: 'log-collection', | ||
mongoOptions: { | ||
auth: { | ||
username: 'one', | ||
password: 'two' | ||
} | ||
} | ||
}) | ||
pino(transport) | ||
``` | ||
The `mongoOptions` is provided to the the standard mongodb client. All the available options are described on [its official documentation](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/MongoClientOptions.html). | ||
Note that you may encouter missing logs in special cases: it dependes on data and mongo's version. Please checkout the [mongodb limitation](https://docs.mongodb.com/manual/reference/limits/) official documentation. | ||
For example on MongoDB 4: | ||
```js | ||
// IT DOES NOT WORK: | ||
log.info({ $and: [{ a: 1 }, { b: 2 }] }, 'my query is') | ||
// IT WORKS: | ||
log.info({ query: { $and: [{ a: 1 }, { b: 2 }]} }, 'my query is') | ||
``` | ||
## Usage as Pino Legacy Transport | ||
Pino supports a [legacy transport interface](https://getpino.io/#/docs/transports?id=legacy-transports) | ||
that is still supported by this module. | ||
### Get started | ||
```bash | ||
@@ -31,3 +71,3 @@ $ echo '{"name": "Viktor"}' | pino-mongodb [options] [mongo-url] | ||
## Usage | ||
### CLI Options | ||
@@ -42,6 +82,6 @@ ``` | ||
-c, --collection <name> database collection (default: "logs") | ||
-o, --stdout output inserted documents into stdout | ||
-e, --errors output insertion errors into stderr | ||
-u, --unified use mongodb unified topology | ||
-h, --help output usage information | ||
-o, --stdout output inserted documents into stdout (default: | ||
false) | ||
-e, --errors output insertion errors into stderr (default: false) | ||
-h, --help display help for command | ||
``` | ||
@@ -48,0 +88,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
194648
8
5267
106
5
8
1
+ Added@types/node@22.10.2(transitive)
+ Added@types/webidl-conversions@7.0.3(transitive)
+ Added@types/whatwg-url@8.2.2(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbson@4.7.2(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedcommander@8.2.0(transitive)
+ Addeddenque@2.1.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedmongodb@4.1.4(transitive)
+ Addedmongodb-connection-string-url@2.6.0(transitive)
+ Addedpino-abstract-transport@0.2.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsplit2@3.2.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedtr46@3.0.0(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwebidl-conversions@7.0.0(transitive)
+ Addedwhatwg-url@11.0.0(transitive)
- Removedbl@2.2.1(transitive)
- Removedbson@1.1.6(transitive)
- Removedcommander@6.1.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddenque@1.5.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removedmongodb@3.6.12(transitive)
- Removedoptional-require@1.1.8(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedrequire-at@1.0.6(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
Updatedcommander@8.2
Updatedmongodb@4.1