@efebia/pino-mongodb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@efebia/pino-mongodb",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "dependencies": {
6
+ "mongodb": "^4.8.1",
7
+ "pino-abstract-transport": "^1.0.0"
8
+ },
9
+ "devDependencies": {
10
+ "@types/node": "18",
11
+ "typescript": "^4.7.4"
12
+ },
13
+ "files": [
14
+ "/lib"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc"
18
+ },
19
+ "main": "./lib"
20
+ }
@@ -0,0 +1,13 @@
1
+ import { type MongoClientOptions } from 'mongodb';
2
+ import { type Options as QueueOptions } from './queue.js';
3
+ export type Options = {
4
+ uri?: string;
5
+ database?: string;
6
+ collection?: string;
7
+ unified?: boolean;
8
+ clientOptions?: MongoClientOptions;
9
+ queueOptions?: Omit<QueueOptions<unknown>, 'callback'>;
10
+ };
11
+ export declare const defaultOption: Options;
12
+ declare const bulkTransport: (opts: Options) => Promise<import("stream").Transform & import("pino-abstract-transport").OnUnknown>;
13
+ export default bulkTransport;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.defaultOption = void 0;
7
+ const mongodb_1 = require("mongodb");
8
+ const pino_abstract_transport_1 = __importDefault(require("pino-abstract-transport"));
9
+ const stream_1 = require("stream");
10
+ const package_json_1 = require("../package.json");
11
+ const log_js_1 = __importDefault(require("./log.js"));
12
+ const queue_js_1 = require("./queue.js");
13
+ exports.defaultOption = {
14
+ uri: 'mongodb://localhost:27017/logs',
15
+ collection: 'logs',
16
+ unified: false,
17
+ queueOptions: {
18
+ cache: [],
19
+ interval: 1000,
20
+ maxMessages: 10
21
+ }
22
+ };
23
+ const bulkTransport = async (opts) => {
24
+ const { uri, database: databaseName, collection: collectionName, clientOptions, queueOptions } = Object.assign(Object.assign({}, exports.defaultOption), opts);
25
+ if (!uri)
26
+ throw new Error(`${package_json_1.name}: URI_NOT_SPECIFIED`);
27
+ if (!collectionName)
28
+ throw new Error(`${package_json_1.name}: COLLECTION_NAME_NOT_SPECIFIED`);
29
+ const client = new mongodb_1.MongoClient(uri, clientOptions);
30
+ await client.connect();
31
+ const db = client.db(databaseName);
32
+ const collection = db.collection(collectionName);
33
+ const messageQueue = (0, queue_js_1.createMessageQueue)(Object.assign({ callback: (messages, errorCallback) => collection.bulkWrite(messages.map(e => ({
34
+ insertOne: {
35
+ document: (0, log_js_1.default)(e)
36
+ }
37
+ })), { forceServerObjectId: true }).catch((e) => {
38
+ if (e instanceof Error) {
39
+ errorCallback === null || errorCallback === void 0 ? void 0 : errorCallback(e);
40
+ }
41
+ }) }, queueOptions));
42
+ const stream = new stream_1.Writable({
43
+ objectMode: true,
44
+ autoDestroy: true,
45
+ write(chunk, encoding, callback) {
46
+ messageQueue.push(chunk, callback);
47
+ },
48
+ destroy(err, cb) {
49
+ client.close((closeErr) => {
50
+ cb(err || closeErr || null);
51
+ });
52
+ }
53
+ });
54
+ return (0, pino_abstract_transport_1.default)((source) => {
55
+ source.pipe(stream);
56
+ }, {
57
+ close(err, cb) {
58
+ stream.end();
59
+ stream.once('close', cb.bind(null, err));
60
+ }
61
+ });
62
+ };
63
+ exports.default = bulkTransport;
@@ -0,0 +1 @@
1
+ export default function (data: any): any;
package/lib/src/log.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function default_1(data) {
4
+ let log;
5
+ try {
6
+ log = typeof data === 'string' ? JSON.parse(data) : data;
7
+ if (log.time) {
8
+ log.time = new Date(log.time);
9
+ }
10
+ }
11
+ catch (e) {
12
+ log = {
13
+ msg: data
14
+ };
15
+ }
16
+ return log;
17
+ }
18
+ exports.default = default_1;
@@ -0,0 +1,13 @@
1
+ import { AnyError } from 'mongodb';
2
+ type ErrorCallback = (error?: AnyError) => void;
3
+ export interface Options<TItem> {
4
+ callback: (messages: TItem[], bufferCallback?: ErrorCallback) => any | Promise<any>;
5
+ cache?: TItem[];
6
+ interval?: number;
7
+ maxMessages?: number;
8
+ }
9
+ export declare const createMessageQueue: <TItem>({ callback, cache, interval, maxMessages }: Options<TItem>) => {
10
+ push: (message: TItem, bufferCallback: ErrorCallback) => void;
11
+ stop: () => void;
12
+ };
13
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMessageQueue = void 0;
4
+ const createMessageQueue = ({ callback, cache = [], interval = 1000, maxMessages = 10 }) => {
5
+ const sendMessages = (bufferCallback) => {
6
+ const messages = cache.splice(0, maxMessages);
7
+ if (messages.length === 0)
8
+ return;
9
+ return callback(messages, bufferCallback);
10
+ };
11
+ const push = (message, bufferCallback) => {
12
+ cache.push(message);
13
+ if (cache.length === maxMessages) {
14
+ sendMessages(bufferCallback);
15
+ }
16
+ else {
17
+ bufferCallback();
18
+ }
19
+ };
20
+ const intervalID = setInterval(sendMessages, interval);
21
+ const stop = () => clearInterval(intervalID);
22
+ return {
23
+ push,
24
+ stop
25
+ };
26
+ };
27
+ exports.createMessageQueue = createMessageQueue;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@efebia/pino-mongodb",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "dependencies": {
6
+ "mongodb": "^4.8.1",
7
+ "pino-abstract-transport": "^1.0.0"
8
+ },
9
+ "devDependencies": {
10
+ "@types/node": "18",
11
+ "typescript": "^4.7.4"
12
+ },
13
+ "files": [
14
+ "/lib"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc"
18
+ },
19
+ "main": "./lib"
20
+ }