Skip to content

Commit

Permalink
fix(mapping): remove deprecated fk option from 1:m and m:1 decorato…
Browse files Browse the repository at this point in the history
…rs (#87)
  • Loading branch information
B4nan committed Sep 19, 2019
1 parent 6d79e57 commit 99b436a
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/decorators/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export type EntityData<T extends IEntityType<T>> = { [P in keyof T]?: T[P] | IPr

export interface EntityProperty<T extends IEntityType<T> = any> {
name: string & keyof T;
fk: string;
entity: () => EntityName<T>;
type: string;
primary: boolean;
Expand Down
6 changes: 4 additions & 2 deletions lib/decorators/ManyToOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import { Cascade, ReferenceType } from '../entity';

export function ManyToOne(options: ManyToOneOptions = {}): Function {
return function (target: IEntity, propertyName: string) {
if ((options as any).fk) {
throw new Error(`@ManyToOne({ fk })' is deprecated, use 'inversedBy' instead in '${target.constructor.name}.${propertyName}'`);
}

const meta = MetadataStorage.getMetadata(target.constructor.name);
Utils.lookupPathFromDecorator(meta);
const property = { name: propertyName, reference: ReferenceType.MANY_TO_ONE, cascade: [Cascade.PERSIST, Cascade.MERGE] };
const prop = Object.assign(property, options) as EntityProperty;
prop.nullable = !prop.cascade.includes(Cascade.REMOVE) && !prop.cascade.includes(Cascade.ALL);
Utils.renameKey(prop, 'fk', 'inversedBy');
meta.properties[propertyName] = prop;
};
}

export interface ManyToOneOptions extends PropertyOptions {
entity?: () => string | Function;
fk?: string;
inversedBy?: string;
cascade?: Cascade[];
}
8 changes: 6 additions & 2 deletions lib/decorators/OneToMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ export function OneToMany(options: OneToManyOptions): Function {
throw new Error(`'@OneToMany({ entity: string | Function })' is required in '${target.constructor.name}.${propertyName}'`);
}

if ((options as any).fk) {
throw new Error(`@OneToMany({ fk })' is deprecated, use 'mappedBy' instead in '${target.constructor.name}.${propertyName}'`);
}

const prop = { name: propertyName, reference: ReferenceType.ONE_TO_MANY, cascade: [Cascade.PERSIST, Cascade.MERGE] };
Object.assign(prop, options);
Utils.renameKey(prop, 'fk', 'mappedBy');
meta.properties[propertyName] = prop as EntityProperty;
};
}
Expand All @@ -29,4 +32,5 @@ export type OneToManyOptions = PropertyOptions & {
joinColumn?: string;
inverseJoinColumn?: string;
referenceColumnName?: string;
} & ({ fk: string } | { mappedBy: string });
mappedBy: string;
};
6 changes: 0 additions & 6 deletions lib/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,6 @@ export class MetadataDiscovery {

private processEntity(meta: EntityMetadata): EntityMetadata[] {
this.defineBaseEntityProperties(meta);

// BC with 1:m `fk` option
Object.values(meta.properties)
.filter(prop => prop.reference === ReferenceType.ONE_TO_MANY)
.forEach(prop => Utils.renameKey(prop, 'fk', 'mappedBy'));

this.validator.validateEntityDefinition(this.metadata, meta.name);
Object.values(meta.properties).forEach(prop => {
this.applyNamingStrategy(meta, prop);
Expand Down
2 changes: 2 additions & 0 deletions tests/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('decorators', () => {
});

test('ManyToOne', () => {
expect(() => ManyToOne({ entity: () => Test, fk: 'test' } as any)(new Test3(), 'test1')).toThrowError(`@ManyToOne({ fk })' is deprecated, use 'inversedBy' instead in 'Test3.test1`);
const storage = MetadataStorage.getMetadata();
ManyToOne({ entity: () => Test })(new Test3(), 'test1');
expect(storage.Test3.properties.test1).toMatchObject({
Expand All @@ -47,6 +48,7 @@ describe('decorators', () => {

test('OneToMany', () => {
expect(() => OneToMany({} as any)(new Test(), 'test')).toThrowError(`@OneToMany({ entity: string | Function })' is required in 'Test.test`);
expect(() => OneToMany({ entity: () => Test, fk: 'test' } as any)(new Test(), 'test')).toThrowError(`@OneToMany({ fk })' is deprecated, use 'mappedBy' instead in 'Test.test`);

const storage = MetadataStorage.getMetadata();
OneToMany({ entity: () => Test, mappedBy: 'test' })(new Test4(), 'test2');
Expand Down
4 changes: 2 additions & 2 deletions tests/entities-js/Book3.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const schema = {
author: {
reference: 'm:1',
type: 'Author3',
fk: 'id',
mappedBy: 'id',
},
publisher: {
reference: 'm:1',
type: 'Publisher3',
fk: 'id',
mappedBy: 'id',
},
tags: {
reference: 'm:n',
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-js/Publisher3.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const schema = {
},
books: {
reference: '1:m',
fk: 'publisher',
mappedBy: 'publisher',
type: 'Book3',
},
tests: {
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-sql/Publisher2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Publisher2 extends BaseEntity2 {
@Property()
name: string;

@OneToMany({ entity: () => Book2, fk: 'publisher' })
@OneToMany({ entity: () => Book2, mappedBy: 'publisher' })
books: Collection<Book2>;

@ManyToMany({ entity: () => Test2, owner: true })
Expand Down
2 changes: 1 addition & 1 deletion tests/entities/Author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Author extends BaseEntity {
@Property()
born?: Date;

@OneToMany({ entity: () => Book, fk: 'author', referenceColumnName: '_id', cascade: [Cascade.PERSIST], orphanRemoval: true })
@OneToMany({ entity: () => Book, mappedBy: 'author', referenceColumnName: '_id', cascade: [Cascade.PERSIST], orphanRemoval: true })
books = new Collection<Book>(this);

@ManyToMany({ entity: () => Author, owner: true })
Expand Down
2 changes: 1 addition & 1 deletion tests/entities/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Publisher {
@Property()
name: string;

@OneToMany({ entity: () => Book.name, fk: 'publisher' })
@OneToMany({ entity: () => Book.name, mappedBy: 'publisher' })
books = new Collection<Book>(this);

@ManyToMany({ entity: () => Test.name, owner: true })
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ debug@^3.1.0, debug@^3.2.6:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down

0 comments on commit 99b436a

Please sign in to comment.