typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.19k stars 6.3k forks source link

Mongodb typing #9037

Open OlivierLardenois opened 2 years ago

OlivierLardenois commented 2 years ago

Issue Description

MongoRepository find and extend typing fail if passing specific mongodb operator.

Expected Behavior

No typing error throw

Actual Behavior

extend error:

Type '{ firstName: { $eq: string; }; }' is not assignable to type 'FindOptionsWhere | FindOptionsWhere[] | undefined'. Types of property 'firstName' are incompatible. Type '{ $eq: string; }' is not assignable to type 'string | FindOperator | undefined'. Object literal may only specify known properties, and '$eq' does not exist in type 'FindOperator'.ts(2322)

find error:

Type '{ firstName: { $eq: string; }; }' is not assignable to type '((FindOptionsWhere | FindOptionsWhere[]) & ObjectLiteral) | undefined'. Type '{ firstName: { $eq: string; }; }' is not assignable to type 'undefined'.ts(2322)

Steps to Reproduce

import { ObjectID } from 'mongodb';
import { Column, DataSource, Entity, ObjectIdColumn } from 'typeorm';

@Entity()
export class User {
  @ObjectIdColumn() id: ObjectID;
  @Column() firstName: string;
}

const myDataSource = new DataSource({
  type: 'mongodb',
  host: 'localhost',
  port: 27017,
  database: 'test',
});

const userRepository = myDataSource.getMongoRepository(User).extend({
  findByName() {
    return this.find({ where: { firstName: { $eq: 'Timber' } } });
  },
});

const timber = myDataSource.getMongoRepository(User).find({
  where: { firstName: { $eq: 'Timber' } },
});

My Environment

Dependency Version
Operating System
Node.js version 16.14.2
Typescript version 4.3.5
TypeORM version 0.3.6

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb yes
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

tevaum commented 1 year ago

I'm also facing this issue and I would add that, on my tests, operators like MoreThan are also not working with MongoDB. I'm searching for entries with timestamp >= 0 and it's returning an empty array when I use MoreThan, so I tried to use Mongo operators and got to this same issue. 😞

Any info or guidance on how this needs to be fixed? I can also help with the PR.

My typeorm version is 0.3.11

tevaum commented 1 year ago

@OlivierLardenois, FYI: I just tested this with TypeOrm 0.3.15 and it's working fine.