mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
renamed query builder methods
This commit is contained in:
parent
3e4e0cc161
commit
8de023d45c
@ -46,8 +46,8 @@ createConnection(options).then(connection => {
|
||||
.createQueryBuilder("p")
|
||||
.leftJoinAndSelect("p.author", "author")
|
||||
.leftJoinAndSelect("p.categories", "categories")
|
||||
.setFirstResult(5)
|
||||
.setMaxResults(10);
|
||||
.skip(5)
|
||||
.take(10);
|
||||
|
||||
Promise.all(posts.map(post => postRepository.persist(post)))
|
||||
.then(savedPosts => {
|
||||
|
||||
@ -7,11 +7,8 @@ export interface FindManyOptions<Entity> extends FindOneOptions<Entity> {
|
||||
|
||||
/**
|
||||
* Offset (paginated) where from entities should be taken.
|
||||
*
|
||||
* todo: this should be renamed. maybe startFrom ?
|
||||
* or just rename limit/offset to rawLimit, rawOffset
|
||||
*/
|
||||
from?: number;
|
||||
skip?: number;
|
||||
|
||||
/**
|
||||
* Limit (paginated) - max number of entities should be taken.
|
||||
|
||||
@ -31,7 +31,7 @@ export class FindOptionsUtils {
|
||||
possibleOptions.where instanceof Object ||
|
||||
possibleOptions.join instanceof Object ||
|
||||
possibleOptions.order instanceof Object ||
|
||||
typeof possibleOptions.from === "number" ||
|
||||
typeof possibleOptions.skip === "number" ||
|
||||
typeof possibleOptions.take === "number"
|
||||
);
|
||||
}
|
||||
@ -95,11 +95,11 @@ export class FindOptionsUtils {
|
||||
if (options.where)
|
||||
this.applyConditions(qb, options.where);
|
||||
|
||||
if ((options as FindManyOptions<T>).from)
|
||||
qb.setFirstResult((options as FindManyOptions<T>).from!);
|
||||
if ((options as FindManyOptions<T>).skip)
|
||||
qb.skip((options as FindManyOptions<T>).skip!);
|
||||
|
||||
if ((options as FindManyOptions<T>).take)
|
||||
qb.setMaxResults((options as FindManyOptions<T>).take!);
|
||||
qb.take((options as FindManyOptions<T>).take!);
|
||||
|
||||
if (options.order)
|
||||
Object.keys(options.order).forEach(key => {
|
||||
|
||||
@ -76,8 +76,8 @@ export class QueryBuilder<Entity> {
|
||||
protected parameters: ObjectLiteral = {};
|
||||
protected limit: number;
|
||||
protected offset: number;
|
||||
protected firstResult: number;
|
||||
protected maxResults: number;
|
||||
protected skipNumber: number;
|
||||
protected takeNumber: number;
|
||||
protected ignoreParentTablesJoins: boolean = false;
|
||||
|
||||
/**
|
||||
@ -764,18 +764,18 @@ export class QueryBuilder<Entity> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set's maximum number of entities to be selected.
|
||||
* Sets maximal number of entities to take.
|
||||
*/
|
||||
setMaxResults(maxResults: number): this {
|
||||
this.maxResults = maxResults;
|
||||
take(take: number): this {
|
||||
this.takeNumber = take;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set's offset of entities to be selected.
|
||||
* Sets number of entities to skip
|
||||
*/
|
||||
setFirstResult(firstResult: number): this {
|
||||
this.firstResult = firstResult;
|
||||
skip(skip: number): this {
|
||||
this.skipNumber = skip;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -908,7 +908,7 @@ export class QueryBuilder<Entity> {
|
||||
|
||||
const mainAliasName = this.fromTableName ? this.fromTableName : this.aliasMap.mainAlias.name;
|
||||
let rawResults: any[];
|
||||
if (this.firstResult || this.maxResults) {
|
||||
if (this.skipNumber || this.takeNumber) {
|
||||
// we are skipping order by here because its not working in subqueries anyway
|
||||
// to make order by working we need to apply it on a distinct query
|
||||
const [sql, parameters] = this.getSqlWithParameters({ skipOrderBy: true });
|
||||
@ -938,17 +938,17 @@ export class QueryBuilder<Entity> {
|
||||
|
||||
if (this.connection.driver instanceof SqlServerDriver) { // todo: temporary. need to refactor and make a proper abstraction
|
||||
|
||||
if (this.firstResult || this.maxResults) {
|
||||
idsQuery += ` OFFSET ${this.firstResult || 0} ROWS`;
|
||||
if (this.maxResults)
|
||||
idsQuery += " FETCH NEXT " + this.maxResults + " ROWS ONLY";
|
||||
if (this.skipNumber || this.takeNumber) {
|
||||
idsQuery += ` OFFSET ${this.skipNumber || 0} ROWS`;
|
||||
if (this.takeNumber)
|
||||
idsQuery += " FETCH NEXT " + this.takeNumber + " ROWS ONLY";
|
||||
}
|
||||
} else {
|
||||
|
||||
if (this.maxResults)
|
||||
idsQuery += " LIMIT " + this.maxResults;
|
||||
if (this.firstResult)
|
||||
idsQuery += " OFFSET " + this.firstResult;
|
||||
if (this.takeNumber)
|
||||
idsQuery += " LIMIT " + this.takeNumber;
|
||||
if (this.skipNumber)
|
||||
idsQuery += " OFFSET " + this.skipNumber;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -1214,8 +1214,8 @@ export class QueryBuilder<Entity> {
|
||||
if (!options || !options.skipOffset)
|
||||
qb.setOffset(this.offset);
|
||||
|
||||
qb.setFirstResult(this.firstResult)
|
||||
.setMaxResults(this.maxResults);
|
||||
qb.skip(this.skipNumber)
|
||||
.take(this.takeNumber);
|
||||
|
||||
return qb;
|
||||
}
|
||||
|
||||
@ -75,16 +75,6 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
|
||||
throw new Error(`Query Builder is not supported by MongoDB.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new entity from the given plan javascript object. If entity already exist in the database, then
|
||||
* it loads it (and everything related to it), replaces all values with the new ones from the given object
|
||||
* and returns this new entity. This new entity is actually a loaded from the db entity with all properties
|
||||
* replaced from the new object.
|
||||
async preload(object: DeepPartial<Entity>): Promise<Entity> {
|
||||
// todo: implement
|
||||
return {} as any;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Finds entities that match given find options or conditions.
|
||||
*/
|
||||
@ -92,8 +82,8 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
|
||||
const query = this.convertFindManyOptionsOrConditionsToMongodbQuery(optionsOrConditions);
|
||||
const cursor = await this.createEntityCursor(query);
|
||||
if (FindOptionsUtils.isFindManyOptions(optionsOrConditions)) {
|
||||
if (optionsOrConditions.from)
|
||||
cursor.skip(optionsOrConditions.from);
|
||||
if (optionsOrConditions.skip)
|
||||
cursor.skip(optionsOrConditions.skip);
|
||||
if (optionsOrConditions.take)
|
||||
cursor.limit(optionsOrConditions.take);
|
||||
if (optionsOrConditions.order)
|
||||
@ -111,8 +101,8 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
|
||||
const query = this.convertFindManyOptionsOrConditionsToMongodbQuery(optionsOrConditions);
|
||||
const cursor = await this.createEntityCursor(query);
|
||||
if (FindOptionsUtils.isFindManyOptions(optionsOrConditions)) {
|
||||
if (optionsOrConditions.from)
|
||||
cursor.skip(optionsOrConditions.from);
|
||||
if (optionsOrConditions.skip)
|
||||
cursor.skip(optionsOrConditions.skip);
|
||||
if (optionsOrConditions.take)
|
||||
cursor.limit(optionsOrConditions.take);
|
||||
if (optionsOrConditions.order)
|
||||
@ -135,8 +125,8 @@ export class MongoRepository<Entity extends ObjectLiteral> extends Repository<En
|
||||
|
||||
const cursor = await this.createEntityCursor(query);
|
||||
if (FindOptionsUtils.isFindManyOptions(optionsOrConditions)) {
|
||||
if (optionsOrConditions.from)
|
||||
cursor.skip(optionsOrConditions.from);
|
||||
if (optionsOrConditions.skip)
|
||||
cursor.skip(optionsOrConditions.skip);
|
||||
if (optionsOrConditions.take)
|
||||
cursor.limit(optionsOrConditions.take);
|
||||
if (optionsOrConditions.order)
|
||||
|
||||
@ -129,7 +129,7 @@ describe("mongodb > basic repository actions", () => {
|
||||
|
||||
// assert find method
|
||||
const loadedPosts1 = await postRepository.find({
|
||||
from: 10,
|
||||
skip: 10,
|
||||
take: 10
|
||||
});
|
||||
loadedPosts1.length.should.be.equal(10);
|
||||
@ -142,7 +142,7 @@ describe("mongodb > basic repository actions", () => {
|
||||
|
||||
// assert find method
|
||||
const [loadedPosts2, loadedPosts2Count] = await postRepository.findAndCount({
|
||||
from: 5,
|
||||
skip: 5,
|
||||
take: 5
|
||||
});
|
||||
loadedPosts2.length.should.be.equal(5);
|
||||
|
||||
@ -214,7 +214,7 @@ describe("repository > find methods", () => {
|
||||
categoryName: "even",
|
||||
isNew: true
|
||||
},
|
||||
from: 1,
|
||||
skip: 1,
|
||||
take: 2,
|
||||
order: {
|
||||
id: "ASC"
|
||||
@ -233,7 +233,7 @@ describe("repository > find methods", () => {
|
||||
categoryName: "even",
|
||||
isNew: true
|
||||
},
|
||||
from: 1,
|
||||
skip: 1,
|
||||
take: 2,
|
||||
order: {
|
||||
id: "ASC"
|
||||
|
||||
@ -26,7 +26,7 @@ describe("github issues > #190 too many SQL variables when using setMaxResults i
|
||||
const loadedPosts = await connection.entityManager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.leftJoinAndSelect("post.categories", "categories")
|
||||
.setMaxResults(1000)
|
||||
.take(1000)
|
||||
.getMany();
|
||||
|
||||
loadedPosts.length.should.be.equal(1000);
|
||||
|
||||
@ -41,7 +41,7 @@ describe("other issues > using limit in conjunction with order by", () => {
|
||||
const loadedPosts1 = await connection.entityManager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.innerJoinAndSelect("post.categories", "categories")
|
||||
.setMaxResults(10)
|
||||
.take(10)
|
||||
.orderBy("post.id", "DESC")
|
||||
.getMany();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user