From 68e073bc07281ebbfe0cef4008a3c2ff0006d953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Fri, 10 Feb 2023 18:21:29 +0100 Subject: [PATCH] feat(core): optionally log if entity is managed or not ```ts process.env.MIKRO_ORM_LOG_EM_ID = '1'; // prints something like `User [managed by 3] { ... }` console.log(user); ``` --- packages/core/src/entity/EntityHelper.ts | 10 ++++++++++ tests/EntityHelper.mongo.test.ts | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/core/src/entity/EntityHelper.ts b/packages/core/src/entity/EntityHelper.ts index 9b5f5b1c9131..3874167ae7b5 100644 --- a/packages/core/src/entity/EntityHelper.ts +++ b/packages/core/src/entity/EntityHelper.ts @@ -124,6 +124,16 @@ export class EntityHelper { const ret = inspect(object, { depth }); let name = (this as object).constructor.name; + const showEM = ['true', 't', '1'].includes(process.env.MIKRO_ORM_LOG_EM_ID?.toString().toLowerCase() ?? ''); + + if (showEM) { + if (helper(this).__em) { + name += ` [managed by ${helper(this).__em.id}]`; + } else { + name += ` [not managed]`; + } + } + // distinguish not initialized entities if (!helper(this).__initialized) { name = `(${name})`; diff --git a/tests/EntityHelper.mongo.test.ts b/tests/EntityHelper.mongo.test.ts index 6e7f7543530f..68b04974906e 100644 --- a/tests/EntityHelper.mongo.test.ts +++ b/tests/EntityHelper.mongo.test.ts @@ -205,6 +205,16 @@ describe('EntityHelperMongo', () => { " baz: (FooBaz) { _id: ObjectId('5b0ff0619fbec620008d2414') }\n" + '}'); + process.env.MIKRO_ORM_LOG_EM_ID = '1'; + actual = inspect(bar); + process.env.MIKRO_ORM_LOG_EM_ID = '0'; + + expect(actual).toBe('FooBar [not managed] {\n' + + ' meta: { onCreateCalled: false, onUpdateCalled: false },\n' + + " name: 'bar',\n" + + " baz: (FooBaz [managed by 1]) { _id: ObjectId('5b0ff0619fbec620008d2414') }\n" + + '}'); + const god = new Author('God', 'hello@heaven.god'); const bible = new Book('Bible', god); bible.createdAt = new Date('2020-07-18T17:31:08.535Z');