This commit is contained in:
Umed Khudoiberdiev 2016-12-02 20:34:37 +05:00
parent 102856540c
commit 044e215bfd
4 changed files with 104 additions and 2 deletions

View File

@ -25,13 +25,14 @@ export class PlainObjectToNewEntityTransformer {
* we need to group our result and we must have some unique id (primary key in our case)
*/
private groupAndTransform(entity: any, object: ObjectLiteral, metadata: EntityMetadata): void {
// copy regular column properties from the given object
metadata.columns
metadata.allColumns
.filter(column => object.hasOwnProperty(column.propertyName))
.forEach(column => entity[column.propertyName] = object[column.propertyName]); // todo: also need to be sure that type is correct
// if relation is loaded then go into it recursively and transform its values too
metadata.relations
metadata.allRelations
.filter(relation => object.hasOwnProperty(relation.propertyName))
.forEach(relation => {
const relationMetadata = relation.inverseEntityMetadata;

View File

@ -0,0 +1,11 @@
import {Column} from "../../../../src/decorator/columns/Column";
import {Document} from "./Document";
import {ClassTableChild} from "../../../../src/decorator/tables/ClassTableChild";
@ClassTableChild()
export class DeliveryNote extends Document {
@Column()
invoice: string = "";
}

View File

@ -0,0 +1,42 @@
import {Table} from "../../../../src/decorator/tables/Table";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
import {TableInheritance} from "../../../../src/decorator/tables/TableInheritance";
import {CreateDateColumn} from "../../../../src/decorator/columns/CreateDateColumn";
import {UpdateDateColumn} from "../../../../src/decorator/columns/UpdateDateColumn";
@Table()
@TableInheritance("class-table")
export class Document {
@PrimaryGeneratedColumn()
id: number;
@Column()
dollarRate: number = 0;
@Column()
orderBy: string = "";
@Column()
comments: string = "";
@Column()
subTotal: number = 0;
@Column()
vat: number = 0;
@Column()
total: number = 0;
@Column()
createdBy: string = "";
@CreateDateColumn()
createdAt: string;
@UpdateDateColumn()
updatedAt: string;
}

View File

@ -0,0 +1,48 @@
import "reflect-metadata";
import {setupTestingConnections, closeConnections, reloadDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {DeliveryNote} from "./entity/DeliveryNote";
import {expect} from "chai";
describe("github issues > #78 repository 'create' is skipping inherited fields", () => {
let connections: Connection[];
before(async () => connections = await setupTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
}));
beforeEach(() => reloadDatabases(connections));
after(() => closeConnections(connections));
it("should persist successfully and return persisted entity", () => Promise.all(connections.map(async connection => {
const repository = connection.getRepository(DeliveryNote);
const deliveryNoteEntity = repository.create({
id: 1,
dollarRate: 0.5,
orderBy: "money",
comments: "this is comment",
subTotal: 10,
vat: 50,
total: 60,
createdBy: "Amir",
invoice: "Total Invoice: 60"
});
expect(deliveryNoteEntity).not.to.be.empty;
deliveryNoteEntity.should.be.instanceof(DeliveryNote);
deliveryNoteEntity.should.be.eql({
id: 1,
dollarRate: 0.5,
orderBy: "money",
comments: "this is comment",
subTotal: 10,
vat: 50,
total: 60,
createdBy: "Amir",
invoice: "Total Invoice: 60"
});
})));
});