renamed query builder methods, removed "ON", removed parameters from inner/left joins

This commit is contained in:
Umed Khudoiberdiev 2016-11-29 14:03:49 +05:00
parent bced54dbc6
commit 2ec59ec7cc
26 changed files with 201 additions and 186 deletions

View File

@ -1,4 +1,19 @@
# 0.0.2 (in development)
# 0.0.3
* completely refactored persistence mechanism
* breaking changes in `QueryBuilder`:
* `getSingleResult()` renamed to `getOne()`
* `getResults()` renamed to `getMany()`
* `getResultsAndCount()` renamed to `getManyAndCount()`
* in the innerJoin*/leftJoin* methods now no need to specify `ON`
* in the innerJoin*/leftJoin* methods no longer supports parameters, use `addParameters` or `setParameter` instead.
* `setParameters` is removed because it confuses users
* `getOne` returns `Promise<Entity|undefined>`
* breaking changes in `Repository` and `EntityManager`:
* `findOne` and `findOneById` now return `Promise<Entity|undefined>` instead of `Promise<Entity>`
* multiple bugfixes
# 0.0.2
* lot of API refactorings
* complete support TypeScript 2

View File

@ -1,7 +1,7 @@
{
"name": "typeorm",
"private": true,
"version": "0.0.3-alpha.9",
"version": "0.0.3-alpha.10",
"description": "Data-mapper ORM for Typescript",
"license": "MIT",
"readmeFilename": "README.md",

View File

@ -46,7 +46,7 @@ createConnection(options).then(connection => {
.createQueryBuilder("post")
.where("post.title=:keyword")
.setParameter("keyword", "hello")
.getResults();
.getMany();
})
.then(post => {
console.log("Loaded post: ", post);

View File

@ -60,15 +60,15 @@ createConnection(options).then(connection => {
return postRepository
.createQueryBuilder("post")
.leftJoinAndMapMany("post.superCategories", "post.categories", "categories")
.leftJoinAndMapOne("post.author", Author, "author", "ON", "author.id=post.authorId")
.getResults();
.leftJoinAndMapOne("post.author", Author, "author", "author.id=post.authorId")
.getMany();
}).then(posts => {
console.log("Loaded posts: ", posts);
return entityManager
.createQueryBuilder(Author, "author")
.getResults();
.getMany();
}).then(authors => {
console.log("Loaded authors: ", authors);

View File

@ -51,7 +51,7 @@ createConnection(options).then(connection => {
.leftJoin("post.categories", "categories")
.leftJoin("categories.author", "author")
.where("post.id=1")
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log("loadedPosts: ", loadedPost);
@ -71,7 +71,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("post.author", "author")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: post.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log(loadedPost);
@ -88,7 +88,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("post.author", "author")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: post.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log(loadedPost);
@ -102,7 +102,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("post.author", "author")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: post.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log(loadedPost);
@ -116,7 +116,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("post.author", "author")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: post.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log(loadedPost);

View File

@ -53,7 +53,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("p.author", "author")
.leftJoinAndSelect("p.categories", "categories")
.where("p.id = :id", { id: loadedPost.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log("---------------------------");

View File

@ -52,7 +52,7 @@ createConnection(options).then(connection => {
Promise.all(posts.map(post => postRepository.persist(post)))
.then(savedPosts => {
console.log("Posts has been saved. Lets try to load some posts");
return qb.getResults();
return qb.getMany();
})
.then(loadedPost => {
console.log("post loaded: ", loadedPost);

View File

@ -52,7 +52,7 @@ createConnection(options).then(connection => {
.leftJoinAndSelect("p.author", "author")
.leftJoinAndSelect("p.categories", "categories")
.where("p.id = :id", { id: loadedPost.id })
.getSingleResult();
.getOne();
})
.then(loadedPost => {
console.log("load finished. Now lets update entity");

View File

@ -31,12 +31,12 @@ export class LazyRelationsWrapper {
} else {
qb.select(relation.propertyName)
.from(relation.type, relation.propertyName)
.innerJoin(relation.junctionEntityMetadata.table.name, relation.junctionEntityMetadata.name, "ON",
.innerJoin(relation.junctionEntityMetadata.table.name, relation.junctionEntityMetadata.name,
`${relation.junctionEntityMetadata.name}.${relation.name}=:${relation.propertyName}Id`)
.setParameter(relation.propertyName + "Id", this[relation.referencedColumnName]);
}
this[loadIndex] = qb.getResults().then(results => {
this[loadIndex] = qb.getMany().then(results => {
this[index] = results;
this[resolveIndex] = true;
delete this[loadIndex];
@ -58,12 +58,12 @@ export class LazyRelationsWrapper {
// loaded: category from post
qb.select(relation.propertyName) // category
.from(relation.type, relation.propertyName) // Category, category
.innerJoin(relation.entityMetadata.target as Function, relation.entityMetadata.name, "ON",
.innerJoin(relation.entityMetadata.target as Function, relation.entityMetadata.name,
`${relation.entityMetadata.name}.${relation.propertyName}=:${relation.propertyName}Id`) // Post, post, post.category = categoryId
.setParameter(relation.propertyName + "Id", this[relation.referencedColumnName]);
}
// console.log(qb.getSql());
this[loadIndex] = qb.getSingleResult().then(result => {
this[loadIndex] = qb.getOne().then(result => {
this[index] = result;
this[resolveIndex] = true;
delete this[loadIndex];

View File

@ -356,7 +356,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.where(qbAlias + "." + relation.joinColumn.referencedColumn.propertyName + "=:id") // todo: need to escape alias and propertyName?
.setParameter("id", relationIdInDatabaseEntity) // (example) subject.entity is a post here
.enableOption("RELATION_ID_VALUES")
.getSingleResult();
.getOne();
if (databaseEntity) {
alreadyLoadedRelatedDatabaseSubject = new Subject(valueMetadata, undefined, databaseEntity);
@ -433,7 +433,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.where(qbAlias + "." + relation.inverseSideProperty + "=:id") // todo: need to escape alias and propertyName?
.setParameter("id", relationIdInDatabaseEntity) // (example) subject.entity is a details here, and the value is details.id
.enableOption("RELATION_ID_VALUES")
.getSingleResult();
.getOne();
// add only if database entity exist - because in the case of inverse side of the one-to-one relation
// we cannot check if it was removed or not until we query the database
@ -513,11 +513,11 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
databaseEntities = await this.connection
.getRepository<ObjectLiteral>(valueMetadata.target)
.createQueryBuilder(qbAlias)
.innerJoin(relation.junctionEntityMetadata.table.name, "persistenceJoinedRelation", "ON",
.innerJoin(relation.junctionEntityMetadata.table.name, "persistenceJoinedRelation",
"persistenceJoinedRelation." + relation.joinTable.joinColumnName + "=:id") // todo: need to escape alias and propertyName?
.setParameter("id", relationIdInDatabaseEntity)
.enableOption("RELATION_ID_VALUES")
.getResults();
.getMany();
} else if (relation.isManyToManyNotOwner) {
@ -531,11 +531,11 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
databaseEntities = await this.connection
.getRepository<ObjectLiteral>(valueMetadata.target)
.createQueryBuilder(qbAlias)
.innerJoin(relation.junctionEntityMetadata.table.name, "persistenceJoinedRelation", "ON",
.innerJoin(relation.junctionEntityMetadata.table.name, "persistenceJoinedRelation",
"persistenceJoinedRelation." + relation.inverseRelation.joinTable.inverseJoinColumnName + "=:id") // todo: need to escape alias and propertyName?
.setParameter("id", relationIdInDatabaseEntity)
.enableOption("RELATION_ID_VALUES")
.getResults();
.getMany();
} else { // this case can only be a oneToMany relation
@ -552,7 +552,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.where(qbAlias + "." + relation.inverseSideProperty + "=:id") // todo: need to escape alias and propertyName?
.setParameter("id", relationIdInDatabaseEntity)
.enableOption("RELATION_ID_VALUES")
.getResults();
.getMany();
}
// add to loadMap loaded entities if some of them are missing

View File

@ -0,0 +1,3 @@
export interface JoinOptions {
limit?: number;
}

View File

@ -7,24 +7,24 @@ import {QueryRunner} from "../query-runner/QueryRunner";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {OrderByCondition} from "../find-options/OrderByCondition";
import {Connection} from "../connection/Connection";
import {JoinOptions} from "./JoinOptions";
/**
*/
export interface Join {
alias: Alias;
type: "LEFT"|"INNER";
conditionType: "ON"|"WITH";
condition?: string;
tableName: string;
mapToProperty?: string;
isMappingMany: boolean;
options?: JoinOptions;
}
export interface JoinRelationId {
alias: Alias;
type: "LEFT"|"INNER";
conditionType: "ON"|"WITH";
condition?: string;
mapToProperty?: string;
}
@ -33,7 +33,6 @@ export interface RelationCountMeta {
alias: Alias;
// property: string;
conditionType: "ON"|"WITH";
condition?: string;
mapToProperty?: string;
entities: { entity: any, metadata: EntityMetadata }[];
@ -274,29 +273,29 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoin(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoin(property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs (without selection) given entity's table.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoin(entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoin(entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs (without selection) given table.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoin(tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoin(tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs (without selection).
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoin(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters);
innerJoin(entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
return this.join("INNER", entityOrProperty, alias, condition, options);
}
/**
@ -305,29 +304,29 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoin(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoin(property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs (without selection) entity's table.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoin(entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoin(entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs (without selection) given table.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoin(tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoin(tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs (without selection).
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoin(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
return this.join("LEFT", entityOrProperty, alias, conditionType, condition, parameters);
leftJoin(entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
return this.join("LEFT", entityOrProperty, alias, condition, options);
}
/**
@ -336,30 +335,30 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndSelect(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndSelect(property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs entity and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndSelect(entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndSelect(entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs table and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndSelect(tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndSelect(tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndSelect(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
innerJoinAndSelect(entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters);
return this.join("INNER", entityOrProperty, alias, condition, options);
}
/**
@ -368,30 +367,30 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndSelect(property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndSelect(property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs entity and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndSelect(entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndSelect(entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs table and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndSelect(tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndSelect(tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs and adds all selection properties to SELECT.
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndSelect(entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
leftJoinAndSelect(entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("LEFT", entityOrProperty, alias, conditionType, condition, parameters);
return this.join("LEFT", entityOrProperty, alias, condition, options);
}
/**
@ -402,7 +401,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapMany(mapToProperty: string, property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapMany(mapToProperty: string, property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -411,7 +410,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapMany(mapToProperty: string, entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapMany(mapToProperty: string, entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -420,7 +419,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapMany(mapToProperty: string, tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapMany(mapToProperty: string, tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -429,9 +428,9 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapMany(mapToProperty: string, entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
innerJoinAndMapMany(mapToProperty: string, entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters, mapToProperty, true);
return this.join("INNER", entityOrProperty, alias, condition, options, mapToProperty, true);
}
/**
@ -442,7 +441,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapOne(mapToProperty: string, property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapOne(mapToProperty: string, property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -451,7 +450,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapOne(mapToProperty: string, entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapOne(mapToProperty: string, entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -460,7 +459,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapOne(mapToProperty: string, tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
innerJoinAndMapOne(mapToProperty: string, tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* INNER JOINs, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -469,9 +468,9 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
innerJoinAndMapOne(mapToProperty: string, entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
innerJoinAndMapOne(mapToProperty: string, entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("INNER", entityOrProperty, alias, conditionType, condition, parameters, mapToProperty, false);
return this.join("INNER", entityOrProperty, alias, condition, options, mapToProperty, false);
}
/**
@ -482,7 +481,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapMany(mapToProperty: string, property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapMany(mapToProperty: string, property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -491,7 +490,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapMany(mapToProperty: string, entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapMany(mapToProperty: string, entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -500,7 +499,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapMany(mapToProperty: string, tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapMany(mapToProperty: string, tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -509,9 +508,9 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapMany(mapToProperty: string, entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
leftJoinAndMapMany(mapToProperty: string, entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("LEFT", entityOrProperty, alias, conditionType, condition, parameters, mapToProperty, true);
return this.join("LEFT", entityOrProperty, alias, condition, options, mapToProperty, true);
}
/**
@ -522,7 +521,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapOne(mapToProperty: string, property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapOne(mapToProperty: string, property: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs entity's table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -531,7 +530,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapOne(mapToProperty: string, entity: Function|string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapOne(mapToProperty: string, entity: Function|string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs table, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -540,7 +539,7 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapOne(mapToProperty: string, tableName: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this;
leftJoinAndMapOne(mapToProperty: string, tableName: string, alias: string, condition?: string, options?: JoinOptions): this;
/**
* LEFT JOINs, SELECTs the data returned by a join and MAPs all that data to some entity's property.
@ -549,9 +548,9 @@ export class QueryBuilder<Entity> {
* You also need to specify an alias of the joined data.
* Optionally, you can add condition and parameters used in condition.
*/
leftJoinAndMapOne(mapToProperty: string, entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
leftJoinAndMapOne(mapToProperty: string, entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions): this {
this.addSelect(alias);
return this.join("LEFT", entityOrProperty, alias, conditionType, condition, parameters, mapToProperty, false);
return this.join("LEFT", entityOrProperty, alias, condition, options, mapToProperty, false);
}
/**
@ -560,8 +559,8 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
leftJoinRelationId(property: string, conditionType: "ON"|"WITH" = "ON", condition?: string, parameters?: ObjectLiteral): this {
return this.joinRelationId("LEFT", undefined, property, conditionType, condition, parameters);
leftJoinRelationId(property: string, condition?: string): this {
return this.joinRelationId("LEFT", undefined, property, condition);
}
/**
@ -570,8 +569,8 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
innerJoinRelationId(property: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral): this {
return this.joinRelationId("INNER", undefined, property, conditionType, condition, parameters);
innerJoinRelationId(property: string, condition?: string): this {
return this.joinRelationId("INNER", undefined, property, condition);
}
/**
@ -580,8 +579,8 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
leftJoinRelationIdAndMap(mapToProperty: string, property: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
return this.joinRelationId("INNER", mapToProperty, property, conditionType, condition, parameters);
leftJoinRelationIdAndMap(mapToProperty: string, property: string, condition: string = ""): this {
return this.joinRelationId("INNER", mapToProperty, property, condition);
}
/**
@ -590,8 +589,8 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
innerJoinRelationIdAndMap(mapToProperty: string, property: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
return this.joinRelationId("INNER", mapToProperty, property, conditionType, condition, parameters);
innerJoinRelationIdAndMap(mapToProperty: string, property: string, condition: string = ""): this {
return this.joinRelationId("INNER", mapToProperty, property, condition);
}
/**
@ -600,7 +599,7 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
countRelation(property: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
countRelation(property: string, condition: string = ""): this {
const [parentAliasName, parentPropertyName] = property.split(".");
const alias = parentAliasName + "_" + parentPropertyName + "_relation_count";
@ -611,14 +610,11 @@ export class QueryBuilder<Entity> {
aliasObj.parentPropertyName = parentPropertyName;
const relationCountMeta: RelationCountMeta = {
conditionType: conditionType,
condition: condition,
alias: aliasObj,
entities: []
};
this.relationCountMetas.push(relationCountMeta);
if (parameters)
this.addParameters(parameters);
return this;
}
@ -628,7 +624,7 @@ export class QueryBuilder<Entity> {
*
* @experimental
*/
countRelationAndMap(mapProperty: string, property: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral): this {
countRelationAndMap(mapProperty: string, property: string, condition: string = ""): this {
const [parentAliasName, parentPropertyName] = property.split(".");
const alias = parentAliasName + "_" + parentPropertyName + "_relation_count";
@ -639,14 +635,11 @@ export class QueryBuilder<Entity> {
const relationCountMeta: RelationCountMeta = {
mapToProperty: mapProperty,
conditionType: conditionType,
condition: condition,
alias: aliasObj,
entities: []
};
this.relationCountMetas.push(relationCountMeta);
if (parameters)
this.addParameters(parameters);
return this;
}
@ -821,7 +814,7 @@ export class QueryBuilder<Entity> {
/**
* Sets given object literal as parameters.
* Note, that it clears all previously set parameters.
*/
setParameters(parameters: ObjectLiteral): this {
this.parameters = {};
Object.keys(parameters).forEach(key => {
@ -829,7 +822,7 @@ export class QueryBuilder<Entity> {
});
return this;
}
*/
/**
* Adds all parameters from the given object.
* Unlike setParameters method it does not clear all previously set parameters.
@ -936,7 +929,7 @@ export class QueryBuilder<Entity> {
/**
* Executes sql generated by query builder and returns object with scalar results and entities created from them.
*/
async getResultsAndScalarResults(): Promise<{ entities: Entity[], scalarResults: any[] }> {
async getEntitiesAndScalarResults(): Promise<{ entities: Entity[], scalarResults: any[] }> {
if (!this.aliasMap.hasMainAlias)
throw new Error(`Alias is not set. Looks like nothing is selected. Use select*, delete, update method to set an alias.`);
@ -1122,25 +1115,25 @@ export class QueryBuilder<Entity> {
/**
* Gets all scalar results returned by execution of generated query builder sql.
*/
getScalarResults<T>(): Promise<T[]> {
getScalarMany<T>(): Promise<T[]> {
return this.execute();
}
/**
* Gets first scalar result returned by execution of generated query builder sql.
*/
getSingleScalarResult<T>(): Promise<T> {
return this.getScalarResults().then(results => results[0]);
getScalarOne<T>(): Promise<T> {
return this.getScalarMany().then(results => results[0]);
}
/**
* Gets entities and count returned by execution of generated query builder sql.
*/
getResultsAndCount(): Promise<[Entity[], number]> {
getManyAndCount(): Promise<[Entity[], number]> {
// todo: share database connection and counter
return Promise.all<any>([
this.getResults(),
this.getMany(),
this.getCount()
]);
}
@ -1148,8 +1141,8 @@ export class QueryBuilder<Entity> {
/**
* Gets entities returned by execution of generated query builder sql.
*/
getResults(): Promise<Entity[]> {
return this.getResultsAndScalarResults().then(results => {
getMany(): Promise<Entity[]> {
return this.getEntitiesAndScalarResults().then(results => {
return results.entities;
});
}
@ -1157,8 +1150,8 @@ export class QueryBuilder<Entity> {
/**
* Gets single entity returned by execution of generated query builder sql.
*/
getSingleResult(): Promise<Entity|undefined> {
return this.getResults().then(entities => entities[0]);
getOne(): Promise<Entity|undefined> {
return this.getMany().then(entities => entities[0]);
}
/**
@ -1190,7 +1183,7 @@ export class QueryBuilder<Entity> {
this.joins.forEach(join => {
const property = join.tableName || join.alias.target || (join.alias.parentAliasName + "." + join.alias.parentPropertyName);
qb.join(join.type, property, join.alias.name, join.conditionType, join.condition || "", undefined, join.mapToProperty, join.isMappingMany);
qb.join(join.type, property, join.alias.name, join.condition || "", undefined, join.mapToProperty, join.isMappingMany);
});
this.groupBys.forEach(groupBy => qb.addGroupBy(groupBy));
@ -1327,11 +1320,11 @@ export class QueryBuilder<Entity> {
.select(`${parentMetadata.name + "." + parentMetadata.primaryColumn.propertyName} AS id`)
.addSelect(`COUNT(${ this.connection.driver.escapeAliasName(relation.propertyName) + "." + this.connection.driver.escapeColumnName(relation.inverseEntityMetadata.primaryColumn.name) }) as cnt`)
.from(parentMetadata.target, parentMetadata.name)
.leftJoin(parentMetadata.name + "." + relation.propertyName, relation.propertyName, relationCountMeta.conditionType, relationCountMeta.condition)
.setParameters(this.parameters)
.leftJoin(parentMetadata.name + "." + relation.propertyName, relation.propertyName, relationCountMeta.condition)
.addParameters(this.parameters)
.where(`${parentMetadata.name + "." + parentMetadata.primaryColumn.propertyName} IN (:relationCountIds)`, { relationCountIds: ids })
.groupBy(parentMetadata.name + "." + parentMetadata.primaryColumn.propertyName)
.getScalarResults()
.getScalarMany()
.then((results: { id: any, cnt: any }[]) => {
// console.log(relationCountMeta.entities);
relationCountMeta.entities.forEach(entityWithMetadata => {
@ -1563,7 +1556,7 @@ export class QueryBuilder<Entity> {
// condition2 = joinAlias + "." + inverseJoinColumnName + "=" + junctionAlias + "." + junctionMetadata.columns[0].name;
}
return " " + join.type + " JOIN " + junctionTable + " " + this.connection.driver.escapeAliasName(junctionAlias) + " " + join.conditionType + " " + condition1;
return " " + join.type + " JOIN " + junctionTable + " " + this.connection.driver.escapeAliasName(junctionAlias) + " ON " + condition1;
// " " + joinType + " JOIN " + joinTableName + " " + joinAlias + " " + join.conditionType + " " + condition2 + appendedCondition;
// console.log(join);
// return " " + join.type + " JOIN " + joinTableName + " " + join.alias.name + " " + (join.condition ? (join.conditionType + " " + join.condition) : "");
@ -1584,7 +1577,7 @@ export class QueryBuilder<Entity> {
const parentAlias = join.alias.parentAliasName;
if (!parentAlias) {
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " " + (join.condition ? ( join.conditionType + " " + this.replacePropertyNames(join.condition) ) : "");
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " " + (join.condition ? ( "ON " + this.replacePropertyNames(join.condition) ) : "");
}
const foundAlias = this.aliasMap.findAliasByName(parentAlias);
@ -1616,18 +1609,18 @@ export class QueryBuilder<Entity> {
condition2 = this.connection.driver.escapeAliasName(joinAlias) + "." + this.connection.driver.escapeColumnName(inverseJoinColumnName) + "=" + this.connection.driver.escapeAliasName(junctionAlias) + "." + this.connection.driver.escapeColumnName(junctionMetadata.columns[0].name);
}
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(junctionTable) + " " + this.connection.driver.escapeAliasName(junctionAlias) + " " + join.conditionType + " " + condition1 +
" " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(joinAlias) + " " + join.conditionType + " " + condition2 + appendedCondition;
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(junctionTable) + " " + this.connection.driver.escapeAliasName(junctionAlias) + " ON " + condition1 +
" " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(joinAlias) + " ON " + condition2 + appendedCondition;
} else if (relation.isManyToOne || (relation.isOneToOne && relation.isOwning)) {
const joinTableColumn = relation.joinColumn.referencedColumn.name;
const condition = this.connection.driver.escapeAliasName(join.alias.name) + "." + this.connection.driver.escapeColumnName(joinTableColumn) + "=" + this.connection.driver.escapeAliasName(parentAlias) + "." + this.connection.driver.escapeColumnName(relation.name);
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " " + join.conditionType + " " + condition + appendedCondition;
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " ON " + condition + appendedCondition;
} else if (relation.isOneToMany || (relation.isOneToOne && !relation.isOwning)) {
const joinTableColumn = relation.inverseRelation.joinColumn.referencedColumn.name;
const condition = this.connection.driver.escapeAliasName(join.alias.name) + "." + this.connection.driver.escapeColumnName(relation.inverseRelation.name) + "=" + this.connection.driver.escapeAliasName(parentAlias) + "." + this.connection.driver.escapeColumnName(joinTableColumn);
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " " + join.conditionType + " " + condition + appendedCondition;
return " " + joinType + " JOIN " + this.connection.driver.escapeTableName(joinTableName) + " " + this.connection.driver.escapeAliasName(join.alias.name) + " ON " + condition + appendedCondition;
} else {
throw new Error("Unexpected relation type"); // this should not be possible
@ -1759,10 +1752,10 @@ export class QueryBuilder<Entity> {
return mappings;
}
protected join(joinType: "INNER"|"LEFT", property: string, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entity: Function, alias: string, conditionType?: "ON"|"WITH", condition?: string, parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH", condition: string, parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entityOrProperty: Function|string, alias: string, conditionType: "ON"|"WITH" = "ON", condition: string = "", parameters?: ObjectLiteral, mapToProperty?: string, isMappingMany: boolean = false): this {
protected join(joinType: "INNER"|"LEFT", property: string, alias: string, condition?: string, options?: JoinOptions, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entity: Function, alias: string, condition?: string, options?: JoinOptions, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entityOrProperty: Function|string, alias: string, condition: string, options?: JoinOptions, mapToProperty?: string, isMappingMany?: boolean): this;
protected join(joinType: "INNER"|"LEFT", entityOrProperty: Function|string, alias: string, condition: string = "", options?: JoinOptions, mapToProperty?: string, isMappingMany: boolean = false): this {
// todo: entityOrProperty can be a table name. implement if its a table
// todo: entityOrProperty can be target name. implement proper behaviour if it is.
@ -1782,13 +1775,20 @@ export class QueryBuilder<Entity> {
mapToProperty = entityOrProperty;
}
const join: Join = { type: joinType, alias: aliasObj, tableName: tableName, conditionType: conditionType, condition: condition, mapToProperty: mapToProperty, isMappingMany: isMappingMany };
const join: Join = {
type: joinType,
alias: aliasObj,
tableName: tableName,
condition: condition,
options: options,
mapToProperty: mapToProperty,
isMappingMany: isMappingMany
};
this.joins.push(join);
if (parameters) this.addParameters(parameters);
return this;
}
protected joinRelationId(joinType: "LEFT"|"INNER", mapToProperty: string|undefined, property: string, conditionType: "ON"|"WITH" = "ON", condition?: string, parameters?: ObjectLiteral): this {
protected joinRelationId(joinType: "LEFT"|"INNER", mapToProperty: string|undefined, property: string, condition?: string): this {
if (!this.isPropertyAlias(property))
throw new Error("Only entity relations are allowed in the leftJoinRelationId operation"); // todo: also check if that relation really has entityId
@ -1805,11 +1805,8 @@ export class QueryBuilder<Entity> {
type: joinType,
mapToProperty: mapToProperty,
alias: aliasObj,
conditionType: conditionType,
condition: condition
});
if (parameters)
this.addParameters(parameters);
return this;
}

View File

@ -52,7 +52,7 @@ export class PlainObjectToDatabaseEntityTransformer {
});
}
return queryBuilder.getSingleResult();
return queryBuilder.getOne();
}
// -------------------------------------------------------------------------

View File

@ -209,7 +209,7 @@ export class Repository<Entity extends ObjectLiteral> {
*/
async find(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity[]> {
return this.createFindQueryBuilder(conditionsOrFindOptions, options)
.getResults();
.getMany();
}
/**
@ -247,7 +247,7 @@ export class Repository<Entity extends ObjectLiteral> {
*/
async findAndCount(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<[ Entity[], number ]> {
return this.createFindQueryBuilder(conditionsOrFindOptions, options)
.getResultsAndCount();
.getManyAndCount();
}
/**
@ -275,7 +275,7 @@ export class Repository<Entity extends ObjectLiteral> {
*/
async findOne(conditionsOrFindOptions?: ObjectLiteral|FindOptions, options?: FindOptions): Promise<Entity|undefined> {
return this.createFindQueryBuilder(conditionsOrFindOptions, options)
.getSingleResult();
.getOne();
}
/**
@ -284,7 +284,7 @@ export class Repository<Entity extends ObjectLiteral> {
*/
async findByIds(ids: any[], options?: FindOptions): Promise<Entity[]> {
const qb = this.createFindQueryBuilder(undefined, options);
return qb.andWhereInIds(ids).getResults();
return qb.andWhereInIds(ids).getMany();
}
/**
@ -308,7 +308,7 @@ export class Repository<Entity extends ObjectLiteral> {
}
}
return this.createFindQueryBuilder(conditions, options)
.getSingleResult();
.getOne();
}
/**

View File

@ -462,7 +462,7 @@ export class SpecificRepository<Entity extends ObjectLiteral> {
if (notInIds && notInIds.length > 0)
qb.andWhere("junction." + inverseEntityColumn.name + " NOT IN (:notInIds)", { notInIds: notInIds });
return qb.getScalarResults()
return qb.getScalarMany()
.then((results: { id: any }[]) => {
results.forEach(result => ids.push(result.id)); // todo: prepare result?
});

View File

@ -22,7 +22,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
const parentPropertyName = this.metadata.treeParentRelation.propertyName;
return this.createQueryBuilder("treeEntity")
.where(`treeEntity.${parentPropertyName} IS NULL`)
.getResults();
.getMany();
}
/**
@ -31,7 +31,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
createDescendantsQueryBuilder(alias: string, closureTableAlias: string, entity: Entity): QueryBuilder<Entity> {
const joinCondition = `${alias}.${this.metadata.firstPrimaryColumn.name}=${closureTableAlias}.descendant`;
return this.createQueryBuilder(alias)
.innerJoin(this.metadata.closureJunctionTable.table.name, closureTableAlias, "ON", joinCondition)
.innerJoin(this.metadata.closureJunctionTable.table.name, closureTableAlias, joinCondition)
.where(`${closureTableAlias}.ancestor=${this.metadata.getEntityIdMap(entity)![this.metadata.firstPrimaryColumn.propertyName]}`);
}
@ -41,7 +41,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
findDescendants(entity: Entity): Promise<Entity[]> {
return this
.createDescendantsQueryBuilder("treeEntity", "treeClosure", entity)
.getResults();
.getMany();
}
/**
@ -51,7 +51,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
// todo: throw exception if there is no column of this relation?
return this
.createDescendantsQueryBuilder("treeEntity", "treeClosure", entity)
.getResultsAndScalarResults()
.getEntitiesAndScalarResults()
.then(entitiesAndScalars => {
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.scalarResults);
this.buildChildrenEntityTree(entity, entitiesAndScalars.entities, relationMaps);
@ -74,7 +74,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
createAncestorsQueryBuilder(alias: string, closureTableAlias: string, entity: Entity): QueryBuilder<Entity> {
const joinCondition = `${alias}.${this.metadata.firstPrimaryColumn.name}=${closureTableAlias}.ancestor`;
return this.createQueryBuilder(alias)
.innerJoin(this.metadata.closureJunctionTable.table.name, closureTableAlias, "ON", joinCondition)
.innerJoin(this.metadata.closureJunctionTable.table.name, closureTableAlias, joinCondition)
.where(`${closureTableAlias}.descendant=${this.metadata.getEntityIdMap(entity)![this.metadata.firstPrimaryColumn.propertyName]}`);
}
@ -84,7 +84,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
findAncestors(entity: Entity): Promise<Entity[]> {
return this
.createAncestorsQueryBuilder("treeEntity", "treeClosure", entity)
.getResults();
.getMany();
}
/**
@ -94,7 +94,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
// todo: throw exception if there is no column of this relation?
return this
.createAncestorsQueryBuilder("treeEntity", "treeClosure", entity)
.getResultsAndScalarResults()
.getEntitiesAndScalarResults()
.then(entitiesAndScalars => {
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.scalarResults);
this.buildParentEntityTree(entity, entitiesAndScalars.entities, relationMaps);

View File

@ -66,7 +66,7 @@ describe("decorators > embedded", () => {
const sortedPosts1 = await postRepository
.createQueryBuilder("post")
.orderBy("post.counters.comments", "DESC")
.getResults();
.getMany();
sortedPosts1.should.be.eql([{
id: 2,
@ -92,7 +92,7 @@ describe("decorators > embedded", () => {
const sortedPosts2 = await postRepository
.createQueryBuilder("post")
.orderBy("post.counters.favorites", "DESC")
.getResults();
.getMany();
sortedPosts2.should.be.eql([{
id: 1,

View File

@ -56,7 +56,7 @@ describe("QueryBuilder > relation-count", () => {
.leftJoinAndSelect("post.tag", "tag")
.countRelation("post.categories")
.countRelation("tag.posts")
.getResults();
.getMany();
loadedPosts[0].categoriesCount.should.be.equal(2);
loadedPosts[1].categoriesCount.should.be.equal(1);
@ -65,9 +65,9 @@ describe("QueryBuilder > relation-count", () => {
loadedPosts = await postRepository
.createQueryBuilder("post")
.leftJoinAndSelect("post.tag", "tag")
.countRelationAndMap("post.secondCategoriesCount", "post.categories", "ON", "tag IS NOT NULL")
.countRelationAndMap("post.secondCategoriesCount", "post.categories", "tag IS NOT NULL")
.countRelationAndMap("post.secondTagsCount", "tag.posts")
.getResults();
.getMany();
loadedPosts[0].secondCategoriesCount.should.be.equal(2);
loadedPosts[1].secondCategoriesCount.should.be.equal(0);

View File

@ -62,7 +62,7 @@ describe("QueryBuilder > relation-id", () => {
.createQueryBuilder("post")
.leftJoinRelationId("post.categories")
.where("post.id = :id", { id: post.id })
.getSingleResult())!;
.getOne())!;
expect(loadedPost.tagId).to.not.be.empty;
expect(loadedPost.tagId).to.be.equal(1);
@ -74,7 +74,7 @@ describe("QueryBuilder > relation-id", () => {
.createQueryBuilder("post")
.leftJoinRelationId("post.categories")
.where("post.id = :id", { id: emptyPost.id })
.getSingleResult())!;
.getOne())!;
should.not.exist(loadedEmptyPost.tagId);
should.not.exist(loadedEmptyPost.categoryIds);
@ -83,7 +83,7 @@ describe("QueryBuilder > relation-id", () => {
.createQueryBuilder("post")
.innerJoinRelationId("post.categories")
.where("post.id = :id", { id: emptyPost.id })
.getSingleResult())!;
.getOne())!;
should.not.exist(loadedEmptyPost);
@ -91,7 +91,7 @@ describe("QueryBuilder > relation-id", () => {
.createQueryBuilder("post")
.leftJoinRelationIdAndMap("post.allCategoryIds", "post.categories")
.where("post.id = :id", { id: post.id })
.getSingleResult())!;
.getOne())!;
loadedPost.allCategoryIds.should.contain(1);
loadedPost.allCategoryIds.should.contain(2);

View File

@ -124,7 +124,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -146,7 +146,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("details.post", "post")
.where("details.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -159,7 +159,7 @@ describe("one-to-one", function() {
return postRepository
.createQueryBuilder("post")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -173,7 +173,7 @@ describe("one-to-one", function() {
return postDetailsRepository
.createQueryBuilder("details")
.where("details.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -237,7 +237,7 @@ describe("one-to-one", function() {
.createQueryBuilder("post")
.leftJoinAndSelect("post.category", "category")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -284,7 +284,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
updatedPostReloaded.details.comment.should.be.equal("this is post");
});
@ -321,7 +321,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
updatedPostReloaded.details.comment.should.be.equal("this is post");
});
@ -356,7 +356,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.image", "image")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.image.url = "new-logo.png";
@ -368,7 +368,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.image", "image")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
reloadedPost.image.url.should.be.equal("new-logo.png");
@ -405,7 +405,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.metadata", "metadata")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.metadata = null;
@ -417,7 +417,7 @@ describe("one-to-one", function() {
.leftJoinAndSelect("post.metadata", "metadata")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
expect(reloadedPost.metadata).to.not.exist;

View File

@ -124,7 +124,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -149,7 +149,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("details.posts", "posts")
.where("details.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -162,7 +162,7 @@ describe("many-to-one", function() {
return postRepository
.createQueryBuilder("post")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -176,7 +176,7 @@ describe("many-to-one", function() {
return postDetailsRepository
.createQueryBuilder("details")
.where("details.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -240,7 +240,7 @@ describe("many-to-one", function() {
.createQueryBuilder("post")
.leftJoinAndSelect("post.category", "category")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -287,7 +287,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
updatedPostReloaded.details.comment.should.be.equal("this is post");
});
@ -324,7 +324,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
updatedPostReloaded.details.comment.should.be.equal("this is post");
});
@ -359,7 +359,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.image", "image")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.image.url = "new-logo.png";
@ -371,7 +371,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.image", "image")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
reloadedPost.image.url.should.be.equal("new-logo.png");
@ -408,7 +408,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.metadata", "metadata")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.metadata = null;
@ -420,7 +420,7 @@ describe("many-to-one", function() {
.leftJoinAndSelect("post.metadata", "metadata")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
expect(reloadedPost.metadata).to.be.empty;
@ -489,7 +489,7 @@ describe("many-to-one", function() {
.createQueryBuilder("details")
.leftJoinAndSelect("details.posts", "posts")
.where("details.id=:id", { id: savedDetails.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});

View File

@ -126,7 +126,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -151,7 +151,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("details.posts", "posts")
.where("details.id=:id")
.setParameter("id", savedPost.id)
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -164,7 +164,7 @@ describe("many-to-many", function() {
return postRepository
.createQueryBuilder("post")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -178,7 +178,7 @@ describe("many-to-many", function() {
return postDetailsRepository
.createQueryBuilder("details")
.where("details.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});
@ -244,7 +244,7 @@ describe("many-to-many", function() {
.createQueryBuilder("post")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedPost);
});
@ -263,7 +263,7 @@ describe("many-to-many", function() {
.createQueryBuilder("p")
.leftJoinAndSelect("p.categories", "categories")
.where("p.id=:id", { id: savedPost.id })
.getSingleResult()
.getOne()
.then(loadedPost => {
loadedPost!.categories.splice(0, 1);
return postRepository.persist(loadedPost!);
@ -308,7 +308,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
updatedPostReloaded.details[0].comment.should.be.equal("this is post");
});
@ -346,7 +346,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.details", "details")
.where("post.id=:id")
.setParameter("id", updatedPost.id)
.getSingleResult();
.getOne();
}).then(updatedPostReloaded => {
expect(updatedPostReloaded.details).to.be.empty;
@ -355,7 +355,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("details.posts", "posts")
.where("details.id=:id")
.setParameter("id", details.id)
.getSingleResult()!;
.getOne()!;
}).then(reloadedDetails => {
expect(reloadedDetails).not.to.be.empty;
expect(reloadedDetails!.posts).to.be.empty;
@ -392,7 +392,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.images", "images")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.images[0].url = "new-logo.png";
@ -404,7 +404,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.images", "images")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
reloadedPost.images[0].url.should.be.equal("new-logo.png");
@ -442,7 +442,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.metadatas", "metadatas")
.where("post.id=:id")
.setParameter("id", post.id)
.getSingleResult();
.getOne();
}).then(loadedPost => {
loadedPost.metadatas = [];
@ -454,7 +454,7 @@ describe("many-to-many", function() {
.leftJoinAndSelect("post.metadatas", "metadatas")
.where("post.id=:id")
.setParameter("id", newPost.id)
.getSingleResult();
.getOne();
}).then(reloadedPost => {
expect(reloadedPost.metadatas).to.be.empty;
@ -523,7 +523,7 @@ describe("many-to-many", function() {
.createQueryBuilder("details")
.leftJoinAndSelect("details.posts", "posts")
.where("details.id=:id", { id: savedDetails.id })
.getSingleResult()
.getOne()
.should.eventually.eql(expectedDetails);
});

View File

@ -32,13 +32,13 @@ describe("github issues > #57 cascade insert not working with OneToOne relations
const tokens = await connection.getRepository(AccessToken)
.createQueryBuilder("token")
.innerJoinAndSelect("token.user", "user")
.getResults();
.getMany();
// get from inverse side and check
const users = await connection.getRepository(User)
.createQueryBuilder("user")
.innerJoinAndSelect("user.access_token", "token")
.getResults();
.getMany();
expect(users).not.to.be.empty;
users.should.be.eql([{
@ -76,7 +76,7 @@ describe("github issues > #57 cascade insert not working with OneToOne relations
const tokens = await connection.getRepository(AccessToken)
.createQueryBuilder("token")
.innerJoinAndSelect("token.user", "user")
.getResults();
.getMany();
expect(tokens).not.to.be.empty;
tokens.should.be.eql([{
@ -91,7 +91,7 @@ describe("github issues > #57 cascade insert not working with OneToOne relations
const users = await connection.getRepository(User)
.createQueryBuilder("user")
.innerJoinAndSelect("user.access_token", "token")
.getResults();
.getMany();
expect(users).not.to.be.empty;
users.should.be.eql([{

View File

@ -49,7 +49,7 @@ describe("github issues > #58 relations with multiple primary keys", () => {
.createQueryBuilder(Post, "post")
.innerJoinAndSelect("post.categories", "postCategory")
.innerJoinAndSelect("postCategory.category", "category")
.getSingleResult();
.getOne();
expect(loadedPost!).not.to.be.empty;
loadedPost!.should.be.eql({

View File

@ -36,11 +36,11 @@ describe("github issues > #70 cascade deleting works incorrect", () => {
const loadedPost = await connection.entityManager
.createQueryBuilder(Post, "post")
.innerJoinAndSelect("post.categories", "categories")
.getSingleResult()!;
.getOne()!;
const loadedCategories = await connection.entityManager
.createQueryBuilder(Category, "category")
.getResults();
.getMany();
expect(loadedPost!).not.to.be.empty;
loadedPost!.should.be.eql({
@ -70,11 +70,11 @@ describe("github issues > #70 cascade deleting works incorrect", () => {
// load them again to make sure they are not exist anymore
const loadedPosts2 = await connection.entityManager
.createQueryBuilder(Post, "post")
.getResults();
.getMany();
const loadedCategories2 = await connection.entityManager
.createQueryBuilder(Category, "category")
.getResults();
.getMany();
expect(loadedPosts2).to.be.empty;
expect(loadedCategories2).to.be.empty;

View File

@ -34,7 +34,7 @@ describe("github issues > #71 ManyToOne relation with custom column name persist
.createQueryBuilder(Artikel, "artikel")
.innerJoinAndSelect("artikel.kollektion", "kollektion")
.where("artikel.id=:id", { id: 1 })
.getSingleResult();
.getOne();
expect(kollektion).not.to.be.empty;
expect(loadedArtikel).not.to.be.empty;