Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Column Aliases ignored on insert #7620

Open
2 of 21 tasks
glister opened this issue May 5, 2021 · 2 comments
Open
2 of 21 tasks

MongoDB Column Aliases ignored on insert #7620

glister opened this issue May 5, 2021 · 2 comments

Comments

@glister
Copy link

glister commented May 5, 2021

Issue Description

When using column aliases with MongoDB, TypeORM does not use the alias when inserting/updating records, it only uses it when querying data.

Expected Behavior

When declaring a class like so

@Entity({ name: 'users' })
export class User {
   @ObjectIdColumn() readonly id: ObjectID;
   @Column({ name: "first_name" }) firstName: string;
}

and inserting a new record with a MongoRepository like so

const newUser = this.usersRepository.create({ firstName: 'Joe' });
return await this.usersRepository.save(newUser);

I expect the fields in then record in the database to be snake case not camel case

Actual Behavior

firstName column is not mapped to first_name
image

Steps to Reproduce

Create an entity like the following and use the type orm MongoRepository to save a new record.

@Entity({ name: 'users' })
export class User {
   @ObjectIdColumn() readonly id: ObjectID;
   @Column({ name: "first_name" }) firstName: string;
}

My Environment

Dependency Version
Operating System macOS 11.2.3
Node.js version 12.19.0
Typescript version 4.2.3
TypeORM version 0.2.32
MongoDB version 4.4.5

Additional Context

None

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@azerpas
Copy link

azerpas commented Aug 26, 2021

Any update on this issue @glister ?

Getting the same issue with my project.

@imnotjames any idea ? Same result with MongoRepository.

@krissh-the-dev
Copy link

krissh-the-dev commented Dec 30, 2021

But it works for auto-generated columns (properties) like 'CreateDateColumn' & 'UpdateDateColumn'.

import { Column, CreateDateColumn, Entity, ObjectIdColumn, UpdateDateColumn } from 'typeorm';
import { ObjectId } from 'mongodb';

@Entity()
export class User {
  @ObjectIdColumn()
  id: ObjectId;

  @Column({ unique: true, name: 'provider_id', nullable: true })
  providerId?: string;

  @Column()
  displayName: string;

  @Column()
  email: string;

  @Column({ name: 'avatar_url' })
  avatarUrl: string;

  @Column({ nullable: true })
  projects?: Array<ObjectId>;

  @Column({ nullable: true })
  teams?: Array<ObjectId>;

  @CreateDateColumn({ name: 'created_at', type: 'time with time zone' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'time with time zone' })
  updatedAt: Date;
}
tss2-dev> db.user.find({})
[
  {
    _id: ObjectId("61cd7b4abc2f0f654b960739"),
    providerId: 'well... this is censored',
    displayName: 'Krishna',
    email: 'akrishnamoorthy007@gmail.com',
    avatarUrl: 'also this',
    created_at: ISODate("2021-12-30T09:26:34.134Z"),
    updated_at: ISODate("2021-12-30T09:26:34.134Z")
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants