question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeORM Mongo ObjectIdColumn JSON output

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [x] 0.2.0

Steps to reproduce or a small repository showing the problem:

I tried using TypeORM with Mongo as a part of a REST api. I created a simple endpoint which should return the data as a JSON. I however do get quite a weird results:

Please note that I’m using routing-controllers for handling the endpoint

Controller

import {Body, Delete, Get, JsonController, NotFoundError, Param, Post, Put} from 'routing-controllers'
import { createConnection } from 'typeorm'
import { User } from './entity/User'

@JsonController()
export class Routes {

  @Get('/')
  public async index() {
    try {
      const conn = await createConnection()

      const user = new User()
      user.username = 'test'
      await conn.mongoManager.save(user)

      return await conn.mongoManager.find(User)
    } catch (e) {
      console.log(e)
    }
  }
}

Entity:

import {Column, Entity, ObjectID, ObjectIdColumn} from 'typeorm'

@Entity()
export class User {
  @ObjectIdColumn()
  id: ObjectID

  @Column()
  username: string
}

GET /

[
    {
        "id": {
            "_bsontype": "ObjectID",
            "id": {
                "0": 90,
                "1": 222,
                "2": 23,
                "3": 59,
                "4": 109,
                "5": 180,
                "6": 239,
                "7": 212,
                "8": 20,
                "9": 157,
                "10": 13,
                "11": 1
            }
        },
        "username": "test"
    },
    ...
]

I have a feeling this is due a way underlying MongoDB works under TypeORM, but I couldn’t find anything by Googling, I assume this should be quite a conventional use case, but for some reason I am unable to get it work

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Kalhamacommented, Apr 24, 2018

Thanks a lot for your response! Serialization was a good keyword and finally I found a sleek solution based on which had already been discussed in class-transformer repo: https://github.com/typestack/class-transformer/issues/87

Solution was to edit entity file like this:

import {Transform} from 'class-transformer'
import {Column, Entity, ObjectID, ObjectIdColumn} from 'typeorm'

@Entity()
export class User {

  @ObjectIdColumn()
  @Transform((id: ObjectID) => id.toHexString(), {toPlainOnly: true})
  id: ObjectID

  @Column()
  username: string
}
2reactions
Kalhamacommented, Sep 23, 2018

@jedmao Hi!

If I can recall correctly I was able to do dirty fix for this issue by re-implementing toHexString from mongoose in @Transform from class-transformer. I presented the dirty fix in TypeORM’s repository where I had ticket for this issue.

OT: We decided to later migrate away from using Mongo with Typeorm since many of the features we had to have in our database were not implemented in Mongo at all or then are just not that mature yet they would be working properly with TypeORM. (Such as transactions, rich JOIN queries or strict data models / schemas).

Nowadays we’re happily using PostgreSQL and TypeORM in multiple production applications and haven’t encountered any problems. I’d recommend you to go with another database than Mongo unless you’re going to contribute on Mongo’s TypeORM development or just playing around.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB | TypeORM Docs
Creates a cursor for a query that can be used to iterate over results from MongoDB. This returns a modified version of the...
Read more >
TypeORM - Working with MongoDB
TypeORM - Working with MongoDB, This chapter explains the extensive ... Let's configure MongoDB host, port and database options in ormconfig.json file as ......
Read more >
MongoDB - typeorm - GitBook
import { Entity, ObjectID, ObjectIdColumn, Column } from "typeorm" ... Creates a cursor for a query that can be used to iterate over...
Read more >
MongoDB with TypeORM integration - Typeix
Fast, unopinionated, minimalist typescript framework for building efficient and scalable applications.
Read more >
TypeORM MongoDB driver return NULL for child relations
@ObjectType() @Entity() export class User extends BaseEntity { @Field(() => String) @ObjectIdColumn() _id: ObjectID; @Field(() => String) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found