Typeorm: Feature Request - Support moment as Date parameter

Created on 24 Oct 2017  路  3Comments  路  Source: typeorm/typeorm

moment has toDate() to get Date object and also has toJSON() to get timestamp string

All 3 comments

I think its something where you need to use value transformers

Not like that, I'm not saving moment in Entity.
moment is easy to manipulate date, so the use case is

    async countPlayerDailyRecord(activityId: number, player: string, day?: Date | moment.Moment) {
        let start = moment(day).startOf("d").toDate(); // because moment is not support
        let end = moment(day).endOf("d").toDate();

        return this.createQueryBuilder("pr")
            .where("pr.activity.id = :activityId", {activityId})
            .andWhere("pr.player = :player", {player})
            .andWhere("pr.createdAt BETWEEN :start AND :end", {start, end})
            .getCount();
    }

I don't see anything bad here:

        return this.createQueryBuilder("pr")
            .where("pr.activity.id = :activityId", {activityId})
            .andWhere("pr.player = :player", {player})
            .andWhere("pr.createdAt BETWEEN :start AND :end")
            .setParameter("start", moment(day).startOf("d").toDate())
            .setParameter("end", moment(day).endOf("d").toDate())
            .getCount();

Sorry but I don't think typeorm needs to be integrated with third party modules this way.

Was this page helpful?
0 / 5 - 0 ratings