Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/clean up #4

Merged
merged 15 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=polymorphic
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
MYSQL_ALLOW_EMPTY_PASSWORD=true

TYPEORM_CONNECTION=mysql
TYPEORM_HOST=localhost
TYPEORM_PORT=3306
TYPEORM_DATABASE=polymorphic
TYPEORM_USERNAME=root
TYPEORM_PASSWORD=root
TYPEORM_PASSWORD=
TYPEORM_ENTITIES=src/__tests__/**/*.entity.ts
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=true
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ node_js:
services:
- mysql
before_install:
- cp .env.dist .env
- mysql -e 'CREATE DATABASE IF NOT EXISTS polymorphic;'
- npm i -g npm@latest
- npm i -g yarn
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# typeorm-polymorphic
<a href="https://www.npmjs.com/package/typeorm-polymorphic"><img src="https://img.shields.io/npm/v/typeorm-polymorphic.svg"/></a>
<img src="https://github.com/bashleigh/typeorm-polymorphic/workflows/tests/badge.svg"/>
<img src="https://github.com/bashleigh/typeorm-polymorphic/workflows/Tests/badge.svg"/>
<img src="https://camo.githubusercontent.com/a34cfbf37ba6848362bf2bee0f3915c2e38b1cc1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265" />

An extension package for polymorphic relationship management, declaration and repository queries for [typeorm](https://typeorm.io/)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "git",
"url": "https://github.com/bashleigh/typeorm-polymorphic"
},
"description": "A simple pagination function to build a pagination object with types",
"description": "A repository for building typed polymorphic relationships",
"keywords": [
"nestjs",
"typeorm",
Expand Down
130 changes: 105 additions & 25 deletions src/__tests__/polymorphic.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UserEntity } from './entities/user.entity';
import { config } from 'dotenv';
import { resolve } from 'path';
import { AdvertRepository } from './repository/advert.repository';
import { AbstractPolymorphicRepository } from '../../dist';
import { AbstractPolymorphicRepository } from '../';

describe('AbstractPolymorphicRepository', () => {
let connection: Connection;
Expand Down Expand Up @@ -41,38 +41,118 @@ describe('AbstractPolymorphicRepository', () => {
]);
});

describe('child', () => {
it('Can create with parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
describe('Childen', () => {
describe('create', () => {
it('Can create with parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);

const user = new UserEntity();
const user = new UserEntity();

const result = repository.create({
owner: user,
const result = repository.create({
owner: user,
});

expect(result).toBeInstanceOf(AdvertEntity);
expect(result.owner).toBeInstanceOf(UserEntity);
});
});

describe('save', () => {
it('Can save cascade parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
const userRepository = connection.getRepository(UserEntity);

const user = await userRepository.save(new UserEntity());

const result = await repository.save(
repository.create({
owner: user,
}),
);

expect(result).toBeInstanceOf(AdvertEntity);
expect(result.owner).toBeInstanceOf(UserEntity);
expect(result.id).toBeTruthy();
expect(result.owner.id).toBeTruthy();
expect(result.entityType).toBe(UserEntity.name);
expect(result.entityId).toBe(result.owner.id);
});

expect(result).toBeInstanceOf(AdvertEntity);
expect(result.owner).toBeInstanceOf(UserEntity);
it('Can save many with cascade parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
const userRepository = connection.getRepository(UserEntity);

const user = await userRepository.save(new UserEntity());

const result = await repository.save([
repository.create({
owner: user,
}),
repository.create({
owner: user,
}),
]);

result.forEach((res) => {
expect(res).toBeInstanceOf(AdvertEntity);
expect(res.owner).toBeInstanceOf(UserEntity);
expect(res.id).toBeTruthy();
expect(res.owner.id).toBeTruthy();
expect(res.entityType).toBe(UserEntity.name);
expect(res.entityId).toBe(res.owner.id);
});
});
});

it('Can save cascade parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
const userRepository = connection.getRepository(UserEntity);
describe('findOne', () => {
it('Can find entity with parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
const userRepository = connection.getRepository(UserEntity);

const user = await userRepository.save(new UserEntity());
const user = await userRepository.save(new UserEntity());

const result = await repository.save(
repository.create({
owner: user,
}),
);

expect(result).toBeInstanceOf(AdvertEntity);
expect(result.owner).toBeInstanceOf(UserEntity);
expect(result.id).toBeTruthy();
expect(result.owner.id).toBeTruthy();
expect(result.entityType).toBe(UserEntity.name);
expect(result.entityId).toBe(result.owner.id);
const advert = await repository.save(
repository.create({
owner: user,
}),
);

const result = await repository.findOne(advert.id);

expect(result).toBeInstanceOf(AdvertEntity);
expect(result.owner).toBeInstanceOf(UserEntity);
expect(result.owner.id).toBe(result.entityId);
expect(result.entityType).toBe(UserEntity.name);
});
});

describe('find', () => {
it('Can find entities with parent', async () => {
const repository = connection.getCustomRepository(AdvertRepository);
const userRepository = connection.getRepository(UserEntity);

const user = await userRepository.save(new UserEntity());

await repository.save([
repository.create({
owner: user,
}),
repository.create({
owner: user,
}),
]);

const result = await repository.find();

result.forEach((res) => {
expect(res).toBeInstanceOf(AdvertEntity);
expect(res.owner).toBeInstanceOf(UserEntity);
expect(res.id).toBeTruthy();
expect(res.owner.id).toBeTruthy();
expect(res.entityType).toBe(UserEntity.name);
expect(res.entityId).toBe(res.owner.id);
});
});
});
});
});
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const POLYMORPHIC_OPTIONS = 'POLYMORPHIC_OPTIONS';
export const POLYMORPHIC_KEY_SEPARATOR = '::';
11 changes: 5 additions & 6 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { POLYMORPHIC_OPTIONS } from './constants';
import { POLYMORPHIC_KEY_SEPARATOR, POLYMORPHIC_OPTIONS } from './constants';
import {
PolymorphicChildrenDecoratorOptionsInterface,
PolymorphicParentDecoratorOptionsInterface,
PolymorphicDecoratorOptionsInterface,
PolymorphicMetadataOptionsInterface,
} from './polymorphic.interface';

Expand All @@ -10,7 +9,7 @@ const polymorphicPropertyDecorator = (
): PropertyDecorator => (target: Object, propertyKey: string) => {
Reflect.defineMetadata(POLYMORPHIC_OPTIONS, true, target);
Reflect.defineMetadata(
`${POLYMORPHIC_OPTIONS}::${propertyKey}`,
`${POLYMORPHIC_OPTIONS}${POLYMORPHIC_KEY_SEPARATOR}${propertyKey}`,
{
propertyKey,
...options,
Expand All @@ -21,7 +20,7 @@ const polymorphicPropertyDecorator = (

export const PolymorphicChildren = (
classType: () => Function[] | Function,
options: PolymorphicChildrenDecoratorOptionsInterface = {},
options: PolymorphicDecoratorOptionsInterface = {},
): PropertyDecorator =>
polymorphicPropertyDecorator({
type: 'children',
Expand All @@ -35,7 +34,7 @@ export const PolymorphicChildren = (

export const PolymorphicParent = (
classType: () => Function[] | Function,
options: PolymorphicParentDecoratorOptionsInterface = {},
options: PolymorphicDecoratorOptionsInterface = {},
): PropertyDecorator =>
polymorphicPropertyDecorator({
type: 'parent',
Expand Down
12 changes: 1 addition & 11 deletions src/polymorphic.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ export interface PolymorphicMetadataInterface extends PolymorphicInterface {
propertyKey: string;
}

export interface PolymorphicChildrenDecoratorOptionsInterface {
primaryColumn?: string;
hasMany?: boolean;
cascade?: boolean;
eager?: boolean;
deleteBeforeUpdate?: boolean;
entityTypeColumn?: string;
entityTypeId?: string;
}

export interface PolymorphicParentDecoratorOptionsInterface {
export interface PolymorphicDecoratorOptionsInterface {
deleteBeforeUpdate?: boolean;
primaryColumn?: string;
hasMany?: boolean;
Expand Down
Loading