Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
  • Philipps & Byrne GmbH
  • Berlin
  • 01:50 (UTC +02:00)
  • LinkedIn in/nhirschfeld
View GitHub Profile
@Goldziher
Goldziher / User.ts
Created August 1, 2021 06:31
Example TypeORM entities
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
@Goldziher
Goldziher / some-test.spec.ts
Created August 1, 2021 06:33
Example test
import { User } from '@backend/db/entities/User';
import { getConnection } from 'typeorm';
describe('Some Test', () => {
const connection = getConnection('default');
let user: User;
beforeEach(async () => {
const userRepository = connection.getRepository(User);
const user = userRepository.create({
@Goldziher
Goldziher / factories.ts
Created August 1, 2021 06:35
Example Interface Forge Factories
import * as faker from 'faker';
import { TypeFactory } from 'interface-forge';
import { User, UserProfile } from '@backend/db/entities/User';
export const UserProfileFactory = new TypeFactory<
Omit<UserProfile, 'user' | 'id' | 'createdDate' | 'updatedDate'>
>(() => ({
location: faker.address.city(),
employer: faker.company.companyName(),
profession: TypeFactory.iterate([
@Goldziher
Goldziher / some-test.spec.ts
Created August 1, 2021 06:36
Re-written test using factories example
import { User } from '@backend/db/entities/User';
import { UserFactory } from '@backend/db/factories/User.factory';
import { getConnection } from 'typeorm';
describe('Some Test', () => {
const connection = getConnection('default');
let user: User;
beforeEach(async () => {
const userRepository = connection.getRepository(User);
@Goldziher
Goldziher / some-test.spec.ts
Created August 1, 2021 06:37
Re-written test example with batch
import { User } from '@backend/db/entities/User';
import { UserFactory } from '@backend/db/factories/User.factory';
import { getConnection } from 'typeorm';
describe('Some Other Test', () => {
const connection = getConnection('default');
let users: User[];
beforeEach(async () => {
const userRepository = connection.getRepository(User);
@Goldziher
Goldziher / User.ts
Created August 1, 2021 06:38
Example of abstract class inheriting from BaseEntity
export abstract class AbstractUUIDPrimaryKeyEntity extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
}
@Goldziher
Goldziher / Types.ts
Created August 1, 2021 06:39
EntityInterface helper type
export type EntityInterface<Entity, Base> = {
[K in keyof Omit<Entity, keyof Base>]: K extends Base
? Omit<Entity, keyof Base>
: Entity[K];
};
@Goldziher
Goldziher / factories.ts
Created August 1, 2021 06:40
Re-written factory using EntityInterface helper
export const UserFactory = new TypeFactory<
EntityInterface<User, AbstractUUIDPrimaryKeyEntity>
>(() => ({
...
}));
@Goldziher
Goldziher / User.dto.ts
Created August 1, 2021 06:42
Example UserDTO class
export class UserDTO {
id: string;
firstName: string;
lastName: string;
email: string;
isActive: boolean;
location: string;
employer: string;
profession: string;
phoneNumber: string;
@Goldziher
Goldziher / User.dto.factory.ts
Created August 1, 2021 06:43
Example UserDTO factory
import * as faker from 'faker';
import { FixtureFactory } from 'interface-forge';
import { UserDTO } from '@shared/dto/User.dto';
export const UserDTOFactory = new FixtureFactory<UserDTO>(() => ({
id: faker.datatype.uuid(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
isActive: true,
email: faker.internet.email(),