Skip to content

Commit

Permalink
feat(core): optionally log if entity is managed or not
Browse files Browse the repository at this point in the history
```ts
process.env.MIKRO_ORM_LOG_EM_ID = '1';
// prints something like `User [managed by 3] { ... }`
console.log(user);
```
  • Loading branch information
B4nan committed Feb 10, 2023
1 parent c101d51 commit 68e073b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/entity/EntityHelper.ts
Expand Up @@ -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})`;
Expand Down
10 changes: 10 additions & 0 deletions tests/EntityHelper.mongo.test.ts
Expand Up @@ -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');
Expand Down

0 comments on commit 68e073b

Please sign in to comment.