ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 使用存储器 ``` import { User } from "./entity/User" const userRepository = dataSource.getRepository(User) ``` ## 基本 ``` userRepository.find({ select: { firstName: true, lastName: true, }, }) ``` sql ``` SELECT "firstName", "lastName" FROM "user" ``` ## where and ``` userRepository.find({ where: { firstName: "Timber", lastName: "Saw", }, }) ``` or ``` userRepository.find({ where: [ { firstName: "Timber", lastName: "Saw" }, { firstName: "Stan", lastName: "Lee" }, ], }) ``` ## order ``` userRepository.find({ order: { name: "ASC", id: "DESC", }, }) ``` ## skip,take分页 **skip** ``` userRepository.find({ skip: 5, }) ``` sql ``` SELECT * FROM "user" OFFSET 5 ``` **take** ``` userRepository.find({ take: 10, }) ``` sql ``` userRepository.find({ take: 10, }) ``` ## cache ``` userRepository.find({ cache: true, }) ``` ## 高级选项 Not ``` import { Not } from "typeorm" const loadedPosts = await dataSource.getRepository(Post).findBy({ title: Not("About #1"), }) ``` other ``` LessThan(10) LessThanOrEqual(10) MoreThan(10) MoreThanOrEqual(10) Equal("About #2") Like("%out #%") ILike("%out #%") In(["About #2", "About #3"]) Between(1,2) Any(["About #2", "About #3"]) IsNull() Raw("dislikes - 4") ``` ## 完成示例 ``` userRepository.find({ select: { firstName: true, lastName: true, }, relations: { profile: true, photos: true, videos: true, }, where: { firstName: "Timber", lastName: "Saw", profile: { userName: "tshaw", }, }, order: { name: "ASC", id: "DESC", }, skip: 5, take: 10, cache: true, }) ```