TypeORM Mongo ObjectIdColumn JSON output
See original GitHub issueIssue 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:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
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:
@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
fromclass-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.