mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added self referencing sample
This commit is contained in:
parent
3951852bbf
commit
3b43d2a00d
@ -16,9 +16,7 @@ usages.
|
||||
* add partial selection support
|
||||
* in query builder should we use property names or table names? (right now its mixed)
|
||||
* should all entities have a primary column?
|
||||
* check if inheritance and abstract table works fine
|
||||
* think about indices
|
||||
* make subscribers and listeners to work correctly
|
||||
* think more about cascades
|
||||
* add cascadePersist to cascades?
|
||||
* naming strategy need to be done correctly
|
||||
@ -28,8 +26,5 @@ usages.
|
||||
* what happens if owner one-to-one on both sides
|
||||
* check self referencing
|
||||
* class lifecycle callbacks?
|
||||
* query builder limit offset
|
||||
* query with count? (Paginator)
|
||||
* wrap persistment in transaction
|
||||
* array / json / date column types
|
||||
* exceptions everywhere!
|
||||
33
sample/sample8-self-referencing/app.ts
Normal file
33
sample/sample8-self-referencing/app.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import {createMysqlConnection} from "../../src/typeorm";
|
||||
import {Category} from "./entity/Category";
|
||||
|
||||
// first create a connection
|
||||
let options = {
|
||||
host: "192.168.99.100",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaCreate: true
|
||||
};
|
||||
|
||||
createMysqlConnection(options, [Category]).then(connection => {
|
||||
|
||||
let categoryRepository = connection.getRepository(Category);
|
||||
|
||||
let category1 = new Category();
|
||||
category1.name = "category #1";
|
||||
|
||||
let mainCategory = new Category();
|
||||
mainCategory.name = "main category";
|
||||
mainCategory.oneCategory = category1;
|
||||
mainCategory.manyCategories.push(category1);
|
||||
mainCategory.oneManyCategory = category1;
|
||||
|
||||
categoryRepository.persist(mainCategory)
|
||||
.then(savedCategory => {
|
||||
console.log("saved category: ", savedCategory);
|
||||
})
|
||||
.catch(error => console.log("Cannot save. Error: ", error.stack ? error.stack : error));
|
||||
|
||||
}, error => console.log("Cannot connect: ", error.stack ? error.stack : error));
|
||||
61
sample/sample8-self-referencing/entity/Category.ts
Normal file
61
sample/sample8-self-referencing/entity/Category.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {ManyToMany} from "../../../src/decorator/Relations";
|
||||
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
|
||||
import {OneToMany} from "../../../src/decorator/relations/OneToMany";
|
||||
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
|
||||
import {OneToOne} from "../../../src/decorator/relations/OneToOne";
|
||||
import {OneToOneInverse} from "../../../src/decorator/relations/OneToOneInverse";
|
||||
|
||||
@Table("sample8_category")
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@OneToOne(type => Category, category => category.oneInverseCategory, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
oneCategory: Category;
|
||||
|
||||
@OneToOneInverse(type => Category, category => category.oneCategory, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
oneInverseCategory: Category;
|
||||
|
||||
@ManyToOne(type => Category, category => category.oneManyCategories, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
oneManyCategory: Category;
|
||||
|
||||
@OneToMany(type => Category, category => category.oneManyCategory, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
oneManyCategories: Category[] = [];
|
||||
|
||||
@ManyToMany(type => Category, category => category.manyInverseCategories, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
manyCategories: Category[] = [];
|
||||
|
||||
@ManyToManyInverse(type => Category, category => category.manyCategories, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
manyInverseCategories: Category[] = [];
|
||||
|
||||
}
|
||||
@ -128,12 +128,12 @@ export class EntityMetadataBuilder {
|
||||
const column1options: ColumnOptions = {
|
||||
length: metadata.primaryColumn.length,
|
||||
type: metadata.primaryColumn.type,
|
||||
name: metadata.table.name + "_" + metadata.primaryColumn.name
|
||||
name: metadata.table.name + "_" + metadata.primaryColumn.name + "_1"
|
||||
};
|
||||
const column2options: ColumnOptions = {
|
||||
length: inverseSideMetadata.primaryColumn.length,
|
||||
type: inverseSideMetadata.primaryColumn.type,
|
||||
name: inverseSideMetadata.table.name + "_" + inverseSideMetadata.primaryColumn.name
|
||||
name: inverseSideMetadata.table.name + "_" + inverseSideMetadata.primaryColumn.name + "_2"
|
||||
};
|
||||
const columns = [
|
||||
new ColumnMetadata({
|
||||
|
||||
@ -521,8 +521,8 @@ export class QueryBuilder<Entity> {
|
||||
const junctionTable = junctionMetadata.table.name;
|
||||
const junctionAlias = join.alias.parentAliasName + "_" + join.alias.name;
|
||||
const joinAlias = join.alias.name;
|
||||
const condition1 = junctionAlias + "." + parentTable + "_" + parentTableColumn + "=" + parentAlias + "." + joinTableColumn; // todo: use column names from junction table somehow
|
||||
const condition2 = joinAlias + "." + joinTableColumn + "=" + junctionAlias + "." + joinTable + "_" + joinTableColumn;
|
||||
const condition1 = junctionAlias + "." + junctionMetadata.columns[0].name + "=" + parentAlias + "." + joinTableColumn; // todo: use column names from junction table somehow
|
||||
const condition2 = joinAlias + "." + joinTableColumn + "=" + junctionAlias + "." + junctionMetadata.columns[1].name;
|
||||
|
||||
return " " + joinType + " JOIN " + junctionTable + " " + junctionAlias + " " + join.conditionType + " " + condition1 +
|
||||
" " + joinType + " JOIN " + joinTable + " " + joinAlias + " " + join.conditionType + " " + condition2 + appendedCondition;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user