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.

Mongo: name in columnOptions is not being passed to find query

See original GitHub issue

Issue type:

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

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

[ ] latest [ ] @next [x] 0.1.20 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have an entity that has a name db name mapping to property name using ColumnOptions:

 @Column({
    name: 'node_id',
  })
  nodeId!: string;

I have a query using a repository for this model

  async getPageAssociationsForNodeId(nodeId: string) {
    const pageAssociations = await this.pageAssociationRepository.find({
      nodeId, //typed to Model so this cant be "node_id"
    });

    return pageAssociations;
  }

When I issue this query, looking at the logs in mongo, nodeId is being passed in the query, not node_id (name provided in column options):

{
	"op" : "query",
	"ns" : "<myDb>.PageAssociation",
	"command" : {
		"find" : "PageAssociation",
		"filter" : {
			"nodeId" : "12345"
		},
		"$db" : "slingshot"
	},

nodeId is not the column name in the collection so no results are being returned.

My questions are:

  1. am I using find appropriately?
  2. is it wrong to assume that the ColumnOptions.name will be used instead of the property name of the class
  3. if the answers to the above are “yes” and “no” respectively, is this a bug?

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
TakeshiDaveaucommented, Jul 21, 2020

Why don’t make this behaviour configurable ?

The ability to decorrelate the technical database name of an attribute from its object representation in programmation language is a key feature of an ORM. The “don’t use name” doesn’t match my conception of a professional ORM.

I disagree with the fact that 99% of the query are made over query in real app. When you create a CRUD API you need this simple find query. Moreover for me it’s more dangerous to apply an unpredictable behaviour for simple things than for complexe things.

4reactions
matthijncommented, Feb 26, 2019

I’m more confused about not having this feature actually. In our system, I want to move from a homemade ‘orm’ to typeorm. But our database fields are all already snake_cased. While in typescript I’d like all my properties to be camelCased.

I’m confused about why it is ‘confusing’ to have this feature on mongo because on other databases it is supported. I’m failing to see why mongo is different in this regard, or why that is wanted.

Implementing a NamingStrategyInterface doesn’t seem to work either, it does work for the table names, and the columnName method in the naming strategy is called, but it’s result are ignored.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongodb query result without field name - Stack Overflow
Is there a way to get mongodb query results with only the values and not the field names. My query gives me the...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
The quickest way to get started with TypeORM is to use its CLI commands ... Where name is the name of your project...
Read more >
Using SQL Runner to create queries and Explores | Looker
Looker passes your query to your database just as you have written it, so be sure that the syntax of your SQL query...
Read more >
M001 Query Syntax - MongoDB Basics
See BSON Types. For example, in the mongo shell I created a document in collection called as persons : db.persons.insertOne( { name ......
Read more >
Model Basics | Sequelize
This name does not have to be the same name of the table it represents in the database. Usually, models have singular names...
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