#MoreThan(new Date()) doesn't work

1 messages · Page 1 of 1 (latest)

quiet moon
#

Hello, I'm working on my database finding products and I want to filter it by stage, there are 3 stages, DRAFT, PUBLISHED, and ARCHIVED, and e.g. the stage for PUBLISHED product is when new Date() is between published_at and archived_at field, which are timestamp type in TypeORM postgres, but it doesn't work... bots MoreThan/LessThan operators don't find anything. I wrote a function to detect stage and return an object which returns statements to { where } object of find function from repository

export const getStage = (
    stage: Stage
): { published_at?: FindOperator<Date> | null; archived_at?: FindOperator<Date> | null } => {
    switch (stage) {
        case Stage.DRAFT:
            return { published_at: LessThan(new Date()) || null, archived_at: LessThan(new Date()) || null }
        case Stage.PUBLISHED:
            return { published_at: MoreThanOrEqual(new Date()) }
        case Stage.ARCHIVED:
            return { archived_at: MoreThan(new Date()) }
    }
}```
Any advices?
rain linden
#

which version are you using of typeorm. it was a known bug before 0.3.6

quiet moon
#

typeorm - 0.3.10 with @nestjs/typeorm - 9.0.1

#

just updated to 0.3.11, still not working

rain linden
#

which database driver are you using?

quiet moon
#

postgres, here is my config

            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => ({
                type: 'postgres',
                url: configService.get('DATABASE_URL'),
                entities: [__dirname + '/**/*.entity{.ts,.js}'],
                synchronize: true,
                ssl: {
                    rejectUnauthorized: configService.get('NODE_ENV') === 'production'
                },
                namingStrategy: new SnakeNamingStrategy()
            })
        }),
rain linden
#

also take a look at github typeorm issues, a lot of people are complaining about the moreThan bug

#

we used a workaround to use the query builder instead

quiet moon
#

I see people saying to use date-fns or similar library to transform date to postgres timestamp format, but doesn't work for me too

#

ok I'll try using query builder

#

nah still doesnt find anything

#
            .createQueryBuilder()
            .select('product')
            .from(Product, 'product')
            .where('product.published_at > :date', { date: new Date() })
            .getMany()

I tried that and replacing new Date() with DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss')

quiet moon
#

ok soooo... I found out when I change MoreThanOrEqual to LessThanOrEqual it returns list of products...