All Projects → owl1n → typeorm-factories

owl1n / typeorm-factories

Licence: other
Create factories for your TypeORM entities. Useful for NestJS applications

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to typeorm-factories

nestjs-starter
🚀 Nest framework starter
Stars: ✭ 30 (-30.23%)
Mutual labels:  typeorm, nestjs
nestjs-api-example
NestJS Example
Stars: ✭ 60 (+39.53%)
Mutual labels:  typeorm, nestjs
Crud
NestJs CRUD for RESTful APIs
Stars: ✭ 2,709 (+6200%)
Mutual labels:  typeorm, nestjs
microservice-template
📖 Nest.js based microservice repository template
Stars: ✭ 131 (+204.65%)
Mutual labels:  typeorm, nestjs
sf-nest-admin
🚀 基于NestJs + TypeScript + TypeORM + Redis + MySql + Vue2 + Element-UI编写的一款简单高效的前后端分离的权限管理系统
Stars: ✭ 125 (+190.7%)
Mutual labels:  typeorm, nestjs
Nestjs Realworld Example App
Exemplary real world backend API built with NestJS + TypeORM / Prisma
Stars: ✭ 1,838 (+4174.42%)
Mutual labels:  typeorm, nestjs
nestjs-api-mongoose
Collection example apps with NestJS and Typeorm, Sequelize, Mongodb, PostgreSQL, MySQL, GraphQL, Mercurius, etc. for the NestJS community 😻
Stars: ✭ 153 (+255.81%)
Mutual labels:  typeorm, nestjs
nest-typeorm-auth-boilerplate
A NestJS boilerplate with TypeORM and authentication
Stars: ✭ 15 (-65.12%)
Mutual labels:  typeorm, nestjs
finance-project-ddd
Projeto financeiro usando domain driven design, tdd, arquitetura hexagonal e solid
Stars: ✭ 67 (+55.81%)
Mutual labels:  entities, nestjs
serverless-nestjs-typeorm
Example how to nestjs using the serverless framework with TypeORM
Stars: ✭ 99 (+130.23%)
Mutual labels:  typeorm, nestjs
Mili
mili 是一个开源的社区系统,界面优雅,功能丰富😛
Stars: ✭ 2,875 (+6586.05%)
Mutual labels:  typeorm, nestjs
nest-convoy
[WIP] An opinionated framework for building distributed domain driven systems using microservices architecture
Stars: ✭ 20 (-53.49%)
Mutual labels:  typeorm, nestjs
nest-boilerplate
Nest.js boilerplate with CircleCI, Commitizen, Commitlint, Docker-Compose, ESLint, GitHub Actions, Husky, Lint-staged, OpenAPI, Prettier, PostGreSQL, Travis CI, TypeORM
Stars: ✭ 16 (-62.79%)
Mutual labels:  typeorm, nestjs
Domain Driven Hexagon
Guide on Domain-Driven Design, software architecture, design patterns, best practices etc.
Stars: ✭ 4,417 (+10172.09%)
Mutual labels:  typeorm, nestjs
nestjs-auth-starter-kit
NestJS Auth Starter Kit (typescript / typeorm / swagger / passport / bcrypt)
Stars: ✭ 37 (-13.95%)
Mutual labels:  typeorm, nestjs
MyAPI
A template to create awesome APIs easily ⚡️
Stars: ✭ 117 (+172.09%)
Mutual labels:  typeorm, nestjs
mom
Proof of concept for Message-Oriented-Middleware based architecture.
Stars: ✭ 39 (-9.3%)
Mutual labels:  typeorm, nestjs
node-nestjs-structure
Node.js framework NestJS project structure
Stars: ✭ 258 (+500%)
Mutual labels:  typeorm, nestjs
lynx
Opinionated Framework built on top of NestJS and TypeORM
Stars: ✭ 44 (+2.33%)
Mutual labels:  typeorm, nestjs
uni-pushy-server
upushy 热更新后端。https://upushy.yoouu.cn/
Stars: ✭ 30 (-30.23%)
Mutual labels:  typeorm, nestjs

TypeORM Entity factory

The library allows you to create factories for your entities. Useful when unit-testing your NestJS project.

Faker

Library using faker.js for provide fake-data in you factories.

Library has peer dependecy for faker and faker-types libraries, but make sure you have them installed. If you want take help about functions in your IDE, just install @types/faker and faker libraries for yourself.

How to

  1. Install library:

    yarn add typeorm-factories or npm install typeorm-factories

  2. Library can find factory file everywhere in project folder. But could be better if you can create folder for them:

    Create folder in project root: mkdir factories

  3. Create your first factory:

    import * as Faker from 'faker';
    import { define } from 'typeorm-factories';
    import { Task } from '../src/tasks/task.entity';
    
    define(Task, (faker: typeof Faker) => {
      const task = new Task();
    
      task.id = faker.random.uuid();
      task.title = faker.lorem.word();
    
      return task;
    });

    Here we have factory for Task entity. Entity has this interface:

     @Entity({ name: 'tasks' })
     export class Task {
       @PrimaryGeneratedColumn('uuid')
       id: string;
     
       @Column()
       title: string;
    }
  4. Use it everywhere:

    In my case i wanted to create unit-testing of my project without database hitting. Just test it on mock data. How?

    Look at my test file for my tasks controller in project:

    For first we need to create mockFactory for repositories. Place this code everywhere you want:

    export type MockType<T> = {
        [P in keyof T]: jest.Mock<{}>;
    };
    
    // @ts-ignore
    export const repositoryMockFactory: () => MockType<Repository<any>> = jest.fn(() => ({
        findOne: jest.fn(entity => entity),
        findOneOrFail: jest.fn(entity => entity),
        // there u can implement another functions of your repositories
    }));

    And lookup to code of test file for my controller

    describe('TasksController', () => {
        let controller: TasksController;
        let repository: MockType<Repository<Task>>;
        
        beforeEach(async () => {
            const module = await Test.createTestingModule({
            imports: [FactoryModule],
            controllers: [TasksController],
            providers: [
               TasksService,
               { provide: getRepositoryToken(Task), useFactory: repositoryMockFactory },
               ],
            }).compile();
            await module.init(); // we are need to wait for module creating, but we inject our factory module to testing module
            
            controller = module.get<TasksController>(TasksController);
            repository = module.get(getRepositoryToken(Task));
        });
            
        describe('getOne', () => {
            it('should return entity', async () => {
                const task = await factory(Task).make();
                repository.findOneOrFail.mockReturnValue(task);
    
                expect(await repository.findOneOrFail(task.id)).toEqual(task);
                expect(repository.findOneOrFail).toBeCalledWith(task.id);
            })
        })
    });

    As you can see, we are create entity by factory via factory() function. After call this we have object of type EntityFactory.

    EnityFactory provide 3 functions:

    • map(callback) - callback will be called for every item in array or solo item when we call make or makeMany functions.
    • makeMany(count, params) - create many objects of your entity. Override default params in original object by passed params from variable.
    • make(params) - create one one entity object and override. About params see above.
Few words

Detailed instructions for use in development. I have not told even half of all the possibilities of this library. If you have a question about the library’s work, you can create Issue. If you have a desire to help me and make the documentation better, contact me. I have some problems with the narration and I think there are people who do it better than me.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].