【发布时间】: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}]});
我正在尝试使用查找与条件匹配的所有交易并对它们进行排序(所以我将最新的放在第一个)并获得第一个实体...!!
我该如何解决?
【问题讨论】:
-
最好是让 DB 去努力工作:在查询级别上,您已经可以 ORDER BY,然后 LIMIT 1。很抱歉,我无法准确告诉您这是如何工作的现在在 TypeORM 中,但欢迎您分享用于执行此操作的 TS 代码。 w3schools.com/sqL/sql_top.aspw3schools.com/SQL/sql_orderby.asp
标签: typescript express typeorm