1.1.2 • Published 1 year ago

lazy-mongo-log v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Lazy Mongo Log

Write to MongoDB as you print!

Installation

npm i lazy-mongo-log

How to Use

import { MongoClient } from 'mongodb';
import { newLazyMongoLog } from 'lazy-mongo-log';

const mongo = new MongoClient('mongodb://localhost:27017/');
const database = mongo.db('my-database');
const collection = database.collection('my-collection');

const print = newLazyMongoLog({
    collection
});

print('Hello World!'); // Hello World!

MongoDB

{
    "type": "info",
    "keyword": null,
    "message": "Hello World!",
    "date_created": 2023-01-07T06:42:01.003+00:00
}

Configuration

import { MongoClient } from 'mongodb';
import { newLazyMongoLog } from 'lazy-mongo-log';

const mongo = new MongoClient('mongodb://localhost:27017/');
const database = mongo.db('my-database');
const collection = database.collection('my-collection');

const print = newLazyMongoLog({
    // The MongoDB collection.
    collection: collection,

    // The default type when using the `print(...)`.
    // Other stuff like `print.error(...)` are not affected.
    type: 'super cool info',

    // Keyword to be included in the log.
    keyword: 'my cool keyword',

    // If this should also print on the console.
    useConsole: true,

    // Don't like the log document schema?
    // You can change it here!
    logSelector(document) {
        return {
            super_message: document.message,
            secret_type: document.type,
            // I don't want your damn keywords!
            hello: 'world!'
        };
    }
});

Other Fun Stuff

Changing Configurations

If you want to set the configurations later, you can do it like so:

import { MongoClient } from 'mongodb';
import { newLazyMongoLog } from 'lazy-mongo-log';

const print = newLazyMongoLog({
    // We can set the collection later.
    keyword: 'unicorns'
});

print('Hello World!'); // Won't write to MongoDB...

const mongo = new MongoClient('mongodb://localhost:27017/');

const database = mongo.db('my-collection');
const collection = database.collection('my-collection');

print.set({
    collection,
    keyword: 'dragons' // I want dragons instead.
});

print('Hello World!'); // Now it does!

Branching Configurations

Do you only want to change the configuration for one specific thing? Here is how you do it:

import { MongoClient } from 'mongodb';
import { newLazyMongoLog } from 'lazy-mongo-log';

const print = newLazyMongoLog({
    // We can set the collection later.
    keyword: 'unicorns'
});

print.using({
    keyword: 'dragons'
})('This is a dragon'); // keyword = 'dragons'

print('This is a unicorn.'); // keyword = 'unicorns'

print.using({
    type: 'my lair'
})('Welcome!'); // type = 'my lair'

print('Welcome back!'); // type = 'info'

// You can also do this!
print.using({
    keyword: 'snakes',
    type: 'sneaky'
}).using({
    keyword: 'bears'
}).using({
    collection: mySuperCoolCollection,
    keyword: 'why are you doing this?!'
}).warn('Because, why not?');

Others

More tools to play with:

// Print using `info` type.
print.info('Hello %s!', 'World'); // MongoDB: type = 'info'

// Print using `warning` type. Uses `console.warn(...)`.
print.warn('Tread lightly...'); // MongoDB: type = 'warning'

// Print using `error` type. Uses `console.error(...)`.
print.error('Something bad happened!'); // MongoDB: type = 'error'

// You can wait for it to finish.
const ok = await print.info('It takes time to insert a document.');

console.log(ok); // `true` if successful, otherwise `false`.
1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago