@efebia/pino-mongodb 0.0.1 → 0.0.3
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.
- package/lib/{src/index.d.ts → index.d.ts} +4 -1
- package/lib/index.js +103 -0
- package/package.json +1 -1
- package/lib/package.json +0 -20
- package/lib/src/index.js +0 -63
- /package/lib/{src/log.d.ts → log.d.ts} +0 -0
- /package/lib/{src/log.js → log.js} +0 -0
- /package/lib/{src/queue.d.ts → queue.d.ts} +0 -0
- /package/lib/{src/queue.js → queue.js} +0 -0
|
@@ -6,8 +6,11 @@ export type Options = {
|
|
|
6
6
|
collection?: string;
|
|
7
7
|
unified?: boolean;
|
|
8
8
|
clientOptions?: MongoClientOptions;
|
|
9
|
+
} & ({
|
|
9
10
|
queueOptions?: Omit<QueueOptions<unknown>, 'callback'>;
|
|
10
|
-
}
|
|
11
|
+
} | {
|
|
12
|
+
immediate: true;
|
|
13
|
+
});
|
|
11
14
|
export declare const defaultOption: Options;
|
|
12
15
|
declare const bulkTransport: (opts: Options) => Promise<import("stream").Transform & import("pino-abstract-transport").OnUnknown>;
|
|
13
16
|
export default bulkTransport;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
9
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.defaultOption = void 0;
|
|
14
|
+
const mongodb_1 = require("mongodb");
|
|
15
|
+
const pino_abstract_transport_1 = __importDefault(require("pino-abstract-transport"));
|
|
16
|
+
const stream_1 = require("stream");
|
|
17
|
+
const log_js_1 = __importDefault(require("./log.js"));
|
|
18
|
+
const queue_js_1 = require("./queue.js");
|
|
19
|
+
exports.defaultOption = {
|
|
20
|
+
uri: 'mongodb://localhost:27017/logs',
|
|
21
|
+
collection: 'logs',
|
|
22
|
+
unified: false,
|
|
23
|
+
queueOptions: {
|
|
24
|
+
cache: [],
|
|
25
|
+
interval: 1000,
|
|
26
|
+
maxMessages: 10
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const bulkTransport = async (opts) => {
|
|
30
|
+
const { uri, database: databaseName, collection: collectionName, clientOptions, queueOptions, } = Object.assign(Object.assign({}, exports.defaultOption), opts);
|
|
31
|
+
const isImmediate = 'immediate' in opts && opts.immediate;
|
|
32
|
+
if (!uri)
|
|
33
|
+
throw new Error(`@efebia/pino-mongodb: URI_NOT_SPECIFIED`);
|
|
34
|
+
if (!collectionName)
|
|
35
|
+
throw new Error(`@efebia/pino-mongodb: COLLECTION_NAME_NOT_SPECIFIED`);
|
|
36
|
+
const client = new mongodb_1.MongoClient(uri, clientOptions);
|
|
37
|
+
await client.connect();
|
|
38
|
+
const db = client.db(databaseName);
|
|
39
|
+
const collection = db.collection(collectionName);
|
|
40
|
+
if (isImmediate) {
|
|
41
|
+
return (0, pino_abstract_transport_1.default)(async (source) => {
|
|
42
|
+
var _a, e_1, _b, _c;
|
|
43
|
+
try {
|
|
44
|
+
for (var _d = true, source_1 = __asyncValues(source), source_1_1; source_1_1 = await source_1.next(), _a = source_1_1.done, !_a;) {
|
|
45
|
+
_c = source_1_1.value;
|
|
46
|
+
_d = false;
|
|
47
|
+
try {
|
|
48
|
+
let obj = _c;
|
|
49
|
+
await collection.insertOne((0, log_js_1.default)(obj), { forceServerObjectId: true }).catch((e) => {
|
|
50
|
+
if (e instanceof Error) {
|
|
51
|
+
console.error(e);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
_d = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
61
|
+
finally {
|
|
62
|
+
try {
|
|
63
|
+
if (!_d && !_a && (_b = source_1.return)) await _b.call(source_1);
|
|
64
|
+
}
|
|
65
|
+
finally { if (e_1) throw e_1.error; }
|
|
66
|
+
}
|
|
67
|
+
}, {
|
|
68
|
+
async close(_err) {
|
|
69
|
+
await client.close();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const messageQueue = (0, queue_js_1.createMessageQueue)(Object.assign({ callback: (messages, errorCallback) => collection.bulkWrite(messages.map(e => ({
|
|
74
|
+
insertOne: {
|
|
75
|
+
document: (0, log_js_1.default)(e)
|
|
76
|
+
}
|
|
77
|
+
})), { forceServerObjectId: true }).catch((e) => {
|
|
78
|
+
if (e instanceof Error) {
|
|
79
|
+
errorCallback === null || errorCallback === void 0 ? void 0 : errorCallback(e);
|
|
80
|
+
}
|
|
81
|
+
}) }, queueOptions));
|
|
82
|
+
const stream = new stream_1.Writable({
|
|
83
|
+
objectMode: true,
|
|
84
|
+
autoDestroy: true,
|
|
85
|
+
write(chunk, encoding, callback) {
|
|
86
|
+
messageQueue.push(chunk, callback);
|
|
87
|
+
},
|
|
88
|
+
destroy(err, cb) {
|
|
89
|
+
client.close((closeErr) => {
|
|
90
|
+
cb(err || closeErr || null);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return (0, pino_abstract_transport_1.default)((source) => {
|
|
95
|
+
source.pipe(stream);
|
|
96
|
+
}, {
|
|
97
|
+
close(err, cb) {
|
|
98
|
+
stream.end();
|
|
99
|
+
stream.once('close', cb.bind(null, err));
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
exports.default = bulkTransport;
|
package/package.json
CHANGED
package/lib/package.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|
package/lib/src/index.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
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;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|