UNPKG

papr

Version:

MongoDB TypeScript-aware Models

74 lines (73 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serializeArguments = exports.logHook = void 0; const util_1 = require("util"); /** * @module intro * @description * * All the methods of the `Model` which call native MongoDB driver methods have hooks support. * The only exception is the custom `upsert` method. * * ## `Hook<TArgs>` * * ``` * export type Hook<TArgs, TContext = Record<string, unknown>> = (params: { * args: TArgs[]; * collectionName: string; * context: TContext; * error?: Error; * methodName: HookMethodsNames; * result?: unknown; * }) => Promise<void>; * ``` * * The `context` parameter is an object which allows the `before` and `after` hooks to share * custom data between each other for a given operation (e.g. tracing ID, etc.). * * The `result` parameter is only populated in the `after` hooks, and only for operations * which have a return value. */ /** * Papr provides a basic logging hook creator, which returns a hook method, which in turn will print * the methods called on a collection with all its arguments. * * This hook creator method takes in a single argument consisting of a basic logger function * which accepts one string argument, similar to `console.log`. * * @name logHook * * @param log {Log} * * @returns {Hook<TArgs>} * * @example * import Papr, { logHook } from 'papr'; * * const papr = new Papr({ * hooks: { * before: [logHook(console.log)] * } * }); */ function logHook(log) { // eslint-disable-next-line @typescript-eslint/require-await return async function logHookMethod(params) { const flatArgs = serializeArguments(params.args); const message = `${params.collectionName}.${params.methodName}(${flatArgs})`; log(message); }; } exports.logHook = logHook; function serializeArguments(args, colors = true) { return args .filter((arg) => !!arg) .map((arg) => (0, util_1.inspect)(arg, { breakLength: Infinity, colors, compact: true, depth: 5, })) .join(', '); } exports.serializeArguments = serializeArguments;