【问题标题】:TypeORM find/findOne options(to find lastest one entitiy)TypeORM find/findOne 选项(查找最新的一个实体)
【发布时间】:2020-12-09 12:06:06
【问题描述】:

现在我正在使用 Typecript、Express.js、TypeORM

我想使用 Repository.find(或 Repository.findOne) 在符合条件的实体中查找最新的 ONE 实体。

这是我的实体


import {
    Entity,
    CreateDateColumn,
    UpdateDateColumn,
    PrimaryGeneratedColumn,
    Column,
    BaseEntity,
    ManyToOne,
} from "typeorm";

import { User } from "./user";
import { Product } from "./product";


@Entity()
export class Transaction extends BaseEntity {
    @PrimaryGeneratedColumn('increment', { type: "int" })
    id: number;

    @Column()
    user_id: number;

    @Column()
    product_id: number;

    @Column({
        type: "enum",
        enum: PaymentMethod,
        default: PaymentMethod.EL
    })
    paymentMethod: PaymentMethod;

    @Column()
    paymentValue: number;

    @Column()
    totalValue: number;

    @CreateDateColumn({ type: 'timestamp' })
    createdAt: Date;

    @UpdateDateColumn({ type: 'timestamp' })
    updatedAt: Date;
}

这是我找到最新的代码(根据 createdAt)

let alltransactions = await transactionRepository.find({ where: [ {user_id : ownership.user_id}, {PaymentMethod: ownership.paymentMethod}]});
            

我正在尝试使用查找与条件匹配的所有交易并对它们进行排序(所以我将最新的放在第一个)并获得第一个实体...!!

我该如何解决?

【问题讨论】:

标签: typescript express typeorm


【解决方案1】:

因此,您希望返回与您的 AND 条件匹配的最后插入的行。

let alltransactions = await transactionRepository.findOne({
    where: [ {user_id : ownership.user_id}, {PaymentMethod: ownership.paymentMethod}],
    order: { id: 'DESC' }
});

【讨论】:

  • 这是 OR 条件。
猜你喜欢
  • 2020-10-02
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2018-09-10
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多