Skip to content

Commit 49c23fc

Browse files
authored
Merge pull request #4 from bashleigh/refactor/cleanup
refactor and clean up of abstract repository. Also added extra tests
2 parents 4a15109 + d97c690 commit 49c23fc

10 files changed

+167
-114
lines changed

.env.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
MYSQL_ROOT_PASSWORD=root
21
MYSQL_DATABASE=polymorphic
32
MYSQL_USER=root
4-
MYSQL_PASSWORD=root
3+
MYSQL_PASSWORD=
4+
MYSQL_ROOT_PASSWORD=
5+
MYSQL_ALLOW_EMPTY_PASSWORD=true
56

67
TYPEORM_CONNECTION=mysql
78
TYPEORM_HOST=localhost
89
TYPEORM_PORT=3306
910
TYPEORM_DATABASE=polymorphic
1011
TYPEORM_USERNAME=root
11-
TYPEORM_PASSWORD=root
12+
TYPEORM_PASSWORD=
1213
TYPEORM_ENTITIES=src/__tests__/**/*.entity.ts
1314
TYPEORM_SYNCHRONIZE=true
1415
TYPEORM_LOGGING=true

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ node_js:
88
services:
99
- mysql
1010
before_install:
11+
- cp .env.dist .env
1112
- mysql -e 'CREATE DATABASE IF NOT EXISTS polymorphic;'
1213
- npm i -g npm@latest
1314
- npm i -g yarn

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# typeorm-polymorphic
22
<a href="https://www.npmjs.com/package/typeorm-polymorphic"><img src="https://img.shields.io/npm/v/typeorm-polymorphic.svg"/></a>
3-
<img src="https://github.com/bashleigh/typeorm-polymorphic/workflows/tests/badge.svg"/>
3+
<img src="https://github.com/bashleigh/typeorm-polymorphic/workflows/Tests/badge.svg"/>
44
<img src="https://camo.githubusercontent.com/a34cfbf37ba6848362bf2bee0f3915c2e38b1cc1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265" />
55

66
An extension package for polymorphic relationship management, declaration and repository queries for [typeorm](https://typeorm.io/)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"type": "git",
1313
"url": "https://github.com/bashleigh/typeorm-polymorphic"
1414
},
15-
"description": "A simple pagination function to build a pagination object with types",
15+
"description": "A repository for building typed polymorphic relationships",
1616
"keywords": [
1717
"nestjs",
1818
"typeorm",

src/__tests__/polymorphic.repository.spec.ts

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { UserEntity } from './entities/user.entity';
44
import { config } from 'dotenv';
55
import { resolve } from 'path';
66
import { AdvertRepository } from './repository/advert.repository';
7-
import { AbstractPolymorphicRepository } from '../../dist';
7+
import { AbstractPolymorphicRepository } from '../';
88

99
describe('AbstractPolymorphicRepository', () => {
1010
let connection: Connection;
@@ -41,38 +41,118 @@ describe('AbstractPolymorphicRepository', () => {
4141
]);
4242
});
4343

44-
describe('child', () => {
45-
it('Can create with parent', async () => {
46-
const repository = connection.getCustomRepository(AdvertRepository);
44+
describe('Childen', () => {
45+
describe('create', () => {
46+
it('Can create with parent', async () => {
47+
const repository = connection.getCustomRepository(AdvertRepository);
4748

48-
const user = new UserEntity();
49+
const user = new UserEntity();
4950

50-
const result = repository.create({
51-
owner: user,
51+
const result = repository.create({
52+
owner: user,
53+
});
54+
55+
expect(result).toBeInstanceOf(AdvertEntity);
56+
expect(result.owner).toBeInstanceOf(UserEntity);
57+
});
58+
});
59+
60+
describe('save', () => {
61+
it('Can save cascade parent', async () => {
62+
const repository = connection.getCustomRepository(AdvertRepository);
63+
const userRepository = connection.getRepository(UserEntity);
64+
65+
const user = await userRepository.save(new UserEntity());
66+
67+
const result = await repository.save(
68+
repository.create({
69+
owner: user,
70+
}),
71+
);
72+
73+
expect(result).toBeInstanceOf(AdvertEntity);
74+
expect(result.owner).toBeInstanceOf(UserEntity);
75+
expect(result.id).toBeTruthy();
76+
expect(result.owner.id).toBeTruthy();
77+
expect(result.entityType).toBe(UserEntity.name);
78+
expect(result.entityId).toBe(result.owner.id);
5279
});
5380

54-
expect(result).toBeInstanceOf(AdvertEntity);
55-
expect(result.owner).toBeInstanceOf(UserEntity);
81+
it('Can save many with cascade parent', async () => {
82+
const repository = connection.getCustomRepository(AdvertRepository);
83+
const userRepository = connection.getRepository(UserEntity);
84+
85+
const user = await userRepository.save(new UserEntity());
86+
87+
const result = await repository.save([
88+
repository.create({
89+
owner: user,
90+
}),
91+
repository.create({
92+
owner: user,
93+
}),
94+
]);
95+
96+
result.forEach((res) => {
97+
expect(res).toBeInstanceOf(AdvertEntity);
98+
expect(res.owner).toBeInstanceOf(UserEntity);
99+
expect(res.id).toBeTruthy();
100+
expect(res.owner.id).toBeTruthy();
101+
expect(res.entityType).toBe(UserEntity.name);
102+
expect(res.entityId).toBe(res.owner.id);
103+
});
104+
});
56105
});
57106

58-
it('Can save cascade parent', async () => {
59-
const repository = connection.getCustomRepository(AdvertRepository);
60-
const userRepository = connection.getRepository(UserEntity);
107+
describe('findOne', () => {
108+
it('Can find entity with parent', async () => {
109+
const repository = connection.getCustomRepository(AdvertRepository);
110+
const userRepository = connection.getRepository(UserEntity);
61111

62-
const user = await userRepository.save(new UserEntity());
112+
const user = await userRepository.save(new UserEntity());
63113

64-
const result = await repository.save(
65-
repository.create({
66-
owner: user,
67-
}),
68-
);
69-
70-
expect(result).toBeInstanceOf(AdvertEntity);
71-
expect(result.owner).toBeInstanceOf(UserEntity);
72-
expect(result.id).toBeTruthy();
73-
expect(result.owner.id).toBeTruthy();
74-
expect(result.entityType).toBe(UserEntity.name);
75-
expect(result.entityId).toBe(result.owner.id);
114+
const advert = await repository.save(
115+
repository.create({
116+
owner: user,
117+
}),
118+
);
119+
120+
const result = await repository.findOne(advert.id);
121+
122+
expect(result).toBeInstanceOf(AdvertEntity);
123+
expect(result.owner).toBeInstanceOf(UserEntity);
124+
expect(result.owner.id).toBe(result.entityId);
125+
expect(result.entityType).toBe(UserEntity.name);
126+
});
127+
});
128+
129+
describe('find', () => {
130+
it('Can find entities with parent', async () => {
131+
const repository = connection.getCustomRepository(AdvertRepository);
132+
const userRepository = connection.getRepository(UserEntity);
133+
134+
const user = await userRepository.save(new UserEntity());
135+
136+
await repository.save([
137+
repository.create({
138+
owner: user,
139+
}),
140+
repository.create({
141+
owner: user,
142+
}),
143+
]);
144+
145+
const result = await repository.find();
146+
147+
result.forEach((res) => {
148+
expect(res).toBeInstanceOf(AdvertEntity);
149+
expect(res.owner).toBeInstanceOf(UserEntity);
150+
expect(res.id).toBeTruthy();
151+
expect(res.owner.id).toBeTruthy();
152+
expect(res.entityType).toBe(UserEntity.name);
153+
expect(res.entityId).toBe(res.owner.id);
154+
});
155+
});
76156
});
77157
});
78158
});

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const POLYMORPHIC_OPTIONS = 'POLYMORPHIC_OPTIONS';
2+
export const POLYMORPHIC_KEY_SEPARATOR = '::';

src/decorators.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { POLYMORPHIC_OPTIONS } from './constants';
1+
import { POLYMORPHIC_KEY_SEPARATOR, POLYMORPHIC_OPTIONS } from './constants';
22
import {
3-
PolymorphicChildrenDecoratorOptionsInterface,
4-
PolymorphicParentDecoratorOptionsInterface,
3+
PolymorphicDecoratorOptionsInterface,
54
PolymorphicMetadataOptionsInterface,
65
} from './polymorphic.interface';
76

@@ -10,7 +9,7 @@ const polymorphicPropertyDecorator = (
109
): PropertyDecorator => (target: Object, propertyKey: string) => {
1110
Reflect.defineMetadata(POLYMORPHIC_OPTIONS, true, target);
1211
Reflect.defineMetadata(
13-
`${POLYMORPHIC_OPTIONS}::${propertyKey}`,
12+
`${POLYMORPHIC_OPTIONS}${POLYMORPHIC_KEY_SEPARATOR}${propertyKey}`,
1413
{
1514
propertyKey,
1615
...options,
@@ -21,7 +20,7 @@ const polymorphicPropertyDecorator = (
2120

2221
export const PolymorphicChildren = (
2322
classType: () => Function[] | Function,
24-
options: PolymorphicChildrenDecoratorOptionsInterface = {},
23+
options: PolymorphicDecoratorOptionsInterface = {},
2524
): PropertyDecorator =>
2625
polymorphicPropertyDecorator({
2726
type: 'children',
@@ -35,7 +34,7 @@ export const PolymorphicChildren = (
3534

3635
export const PolymorphicParent = (
3736
classType: () => Function[] | Function,
38-
options: PolymorphicParentDecoratorOptionsInterface = {},
37+
options: PolymorphicDecoratorOptionsInterface = {},
3938
): PropertyDecorator =>
4039
polymorphicPropertyDecorator({
4140
type: 'parent',

src/polymorphic.interface.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,7 @@ export interface PolymorphicMetadataInterface extends PolymorphicInterface {
2424
propertyKey: string;
2525
}
2626

27-
export interface PolymorphicChildrenDecoratorOptionsInterface {
28-
primaryColumn?: string;
29-
hasMany?: boolean;
30-
cascade?: boolean;
31-
eager?: boolean;
32-
deleteBeforeUpdate?: boolean;
33-
entityTypeColumn?: string;
34-
entityTypeId?: string;
35-
}
36-
37-
export interface PolymorphicParentDecoratorOptionsInterface {
27+
export interface PolymorphicDecoratorOptionsInterface {
3828
deleteBeforeUpdate?: boolean;
3929
primaryColumn?: string;
4030
hasMany?: boolean;

0 commit comments

Comments
 (0)