mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed bug in query builder and added not working yet tests for relation count
This commit is contained in:
parent
a83093910f
commit
24352ef52d
@ -574,11 +574,13 @@ export class QueryBuilder<Entity> {
|
||||
.filter(join => this.selects.indexOf(join.alias.name) !== -1)
|
||||
.forEach(join => {
|
||||
const joinMetadata = this.aliasMap.getEntityMetadataByAlias(join.alias);
|
||||
if (!joinMetadata)
|
||||
throw new Error("Cannot get entity metadata for the given alias " + join.alias.name);
|
||||
joinMetadata.columns.forEach(column => {
|
||||
allSelects.push(join.alias.name + "." + column.name + " AS " + join.alias.name + "_" + column.propertyName);
|
||||
});
|
||||
if (joinMetadata) {
|
||||
joinMetadata.columns.forEach(column => {
|
||||
allSelects.push(join.alias.name + "." + column.name + " AS " + join.alias.name + "_" + column.propertyName);
|
||||
});
|
||||
} else {
|
||||
allSelects.push(join.alias.name);
|
||||
}
|
||||
});
|
||||
|
||||
// add all other selects
|
||||
@ -775,9 +777,8 @@ export class QueryBuilder<Entity> {
|
||||
if (entityOrProperty instanceof Function) {
|
||||
aliasObj.target = entityOrProperty;
|
||||
|
||||
} else if (typeof entityOrProperty === "string" && entityOrProperty.indexOf(".") !== -1) {
|
||||
aliasObj.parentAliasName = entityOrProperty.split(".")[0];
|
||||
aliasObj.parentPropertyName = entityOrProperty.split(".")[1];
|
||||
} else if (this.isPropertyAlias(entityOrProperty)) {
|
||||
[aliasObj.parentAliasName, aliasObj.parentPropertyName] = entityOrProperty.split(".");
|
||||
|
||||
} else if (typeof entityOrProperty === "string") {
|
||||
tableName = entityOrProperty;
|
||||
@ -791,4 +792,24 @@ export class QueryBuilder<Entity> {
|
||||
return this;
|
||||
}
|
||||
|
||||
private isPropertyAlias(str: any): str is string {
|
||||
if (!(typeof str === "string"))
|
||||
return false;
|
||||
if (str.indexOf(".") === -1)
|
||||
return false;
|
||||
|
||||
const aliasName = str.split(".")[0];
|
||||
const propertyName = str.split(".")[1];
|
||||
|
||||
if (!aliasName || !propertyName)
|
||||
return false;
|
||||
|
||||
const aliasNameRegexp = /^[a-zA-Z0-9_-]+$/;
|
||||
const propertyNameRegexp = aliasNameRegexp;
|
||||
if (!aliasNameRegexp.test(aliasName) || !propertyNameRegexp.test(propertyName))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
import {Table} from "../../../../../src/decorator/tables/Table";
|
||||
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Post} from "./Post";
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {ManyToOne} from "../../../../../src/decorator/relations/ManyToOne";
|
||||
|
||||
@Table()
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Post, post => post.categories)
|
||||
post: Post;
|
||||
|
||||
}
|
||||
23
test/functional/query-builder/relation-count/entity/Post.ts
Normal file
23
test/functional/query-builder/relation-count/entity/Post.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import {Category} from "./Category";
|
||||
import {Table} from "../../../../../src/decorator/tables/Table";
|
||||
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {RelationsCountColumn} from "../../../../../src/decorator/columns/RelationsCountColumn";
|
||||
import {OneToMany} from "../../../../../src/decorator/relations/OneToMany";
|
||||
|
||||
@Table()
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@OneToMany(type => Category, category => category.post)
|
||||
categories: Category[]|null;
|
||||
|
||||
@RelationsCountColumn((post: Post) => post.categories)
|
||||
categoriesCount: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
import "reflect-metadata";
|
||||
import * as chai from "chai";
|
||||
import {expect} from "chai";
|
||||
import {Connection} from "../../../../src/connection/Connection";
|
||||
import {Repository} from "../../../../src/repository/Repository";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
import {CreateConnectionOptions} from "../../../../src/connection-manager/CreateConnectionOptions";
|
||||
import {createConnection} from "../../../../src/index";
|
||||
|
||||
chai.should();
|
||||
chai.use(require("sinon-chai"));
|
||||
chai.use(require("chai-as-promised"));
|
||||
|
||||
describe("query builder > relation count", function() {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Configuration
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
const parameters: CreateConnectionOptions = {
|
||||
driver: "mysql",
|
||||
connection: {
|
||||
host: "192.168.99.100",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaCreate: true,
|
||||
logging: {
|
||||
logFailedQueryError: true
|
||||
}
|
||||
},
|
||||
entities: [Post, Category]
|
||||
};
|
||||
// connect to db
|
||||
let connection: Connection;
|
||||
before(function() {
|
||||
return createConnection(parameters)
|
||||
.then(con => connection = con)
|
||||
.catch(e => {
|
||||
console.log("Error during connection to db: " + e);
|
||||
throw e;
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
connection.close();
|
||||
});
|
||||
|
||||
// clean up database before each test
|
||||
function reloadDatabase() {
|
||||
return connection.syncSchema(true)
|
||||
.catch(e => console.log("Error during schema re-creation: ", e));
|
||||
}
|
||||
|
||||
let postRepository: Repository<Post>;
|
||||
let categoryRepository: Repository<Category>;
|
||||
before(function() {
|
||||
postRepository = connection.getRepository(Post);
|
||||
categoryRepository = connection.getRepository(Category);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Specifications
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
describe("add exist element to exist object with empty one-to-many relation and save it", function() {
|
||||
let newPost: Post, newCategory1: Category, newCategory2: Category, loadedPosts: Post[];
|
||||
|
||||
before(reloadDatabase);
|
||||
|
||||
// save a new category
|
||||
before(function () {
|
||||
newCategory1 = categoryRepository.create();
|
||||
newCategory1.name = "Animals";
|
||||
return categoryRepository.persist(newCategory1);
|
||||
});
|
||||
|
||||
// save a new category
|
||||
before(function () {
|
||||
newCategory2 = categoryRepository.create();
|
||||
newCategory2.name = "Kids";
|
||||
return categoryRepository.persist(newCategory2);
|
||||
});
|
||||
|
||||
// save a new post
|
||||
before(function() {
|
||||
newPost = postRepository.create();
|
||||
newPost.title = "Super post";
|
||||
newPost.categories = [newCategory1, newCategory2];
|
||||
return postRepository.persist(newPost);
|
||||
});
|
||||
|
||||
// load a post, want to have categories count
|
||||
before(function() {
|
||||
// todo...
|
||||
});
|
||||
|
||||
it("should contain a new category", function () {
|
||||
// todo...
|
||||
// console.log(loadedPosts);
|
||||
// expect(loadedPosts).not.to.be.empty;
|
||||
// expect(loadedPost.categories).not.to.be.empty;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user