typescript - TypeORM の find/findOne オプション(最新のエンティティを見つけるために)
今、私はTypeScript、Express.js、TypeORMを使用しています
条件に合うエンティティの中で最新の1つのエンティティを見つけるために、Repository.find(またはRepository.findOne)を使いたいです。
これは私のエンティティです
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}]});
私は条件に一致するすべてのトランザクションを見つけて、それらを並べ替え(最新のものを最初に置くように)最初のエンティティを取得しようとしています…!!
それをどうやって解決できますか?
回答 #1
つまり、OR 条件に一致する最後に挿入された行を返したいということです。(編集 AND -> OR)
let alltransactions = await transactionRepository.findOne({
where: [ {user_id : ownership.user_id}, {PaymentMethod: ownership.paymentMethod}],
order: { id: 'DESC' }
});
