Nestjs repository pattern config challenge












2















I'm trying to figure out how the "Repository Pattern" of TypeOrm in Nest works. I would like to have a resources file in which a local and a remote file hold the different entites, controllers, modules etc. See screenshot



enter image description here



When the app is building I`m getting the following error:



[Nest] 3186   - 11/19/2018, 10:44:43 PM   [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms


From the Nest and TypeORM documentation I can triangulate, that I have to tell the application where it can find the entities or at least this is what I believe the error is trying to tell me.



I'm using a .env to pull the config for TypeORM in:



TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}


app.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'

@Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: ,
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}


project.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';

@Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}


project.service.ts



import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';

@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}

async findAll(): Promise<Project> {
return await this.projectRepository.find();
}
}


project.entity.ts



import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Project {
@PrimaryGeneratedColumn()
id: number;

@Column({ length: 500 })
name: string;
}


project.controller.ts



import { Controller, Get } from '@nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';

@Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}

@Get()
findAll(): Promise<Project> {
return this.projectService.findAll();
}
}









share|improve this question























  • You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

    – zenbeni
    Nov 23 '18 at 9:34
















2















I'm trying to figure out how the "Repository Pattern" of TypeOrm in Nest works. I would like to have a resources file in which a local and a remote file hold the different entites, controllers, modules etc. See screenshot



enter image description here



When the app is building I`m getting the following error:



[Nest] 3186   - 11/19/2018, 10:44:43 PM   [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms


From the Nest and TypeORM documentation I can triangulate, that I have to tell the application where it can find the entities or at least this is what I believe the error is trying to tell me.



I'm using a .env to pull the config for TypeORM in:



TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}


app.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'

@Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: ,
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}


project.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';

@Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}


project.service.ts



import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';

@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}

async findAll(): Promise<Project> {
return await this.projectRepository.find();
}
}


project.entity.ts



import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Project {
@PrimaryGeneratedColumn()
id: number;

@Column({ length: 500 })
name: string;
}


project.controller.ts



import { Controller, Get } from '@nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';

@Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}

@Get()
findAll(): Promise<Project> {
return this.projectService.findAll();
}
}









share|improve this question























  • You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

    – zenbeni
    Nov 23 '18 at 9:34














2












2








2








I'm trying to figure out how the "Repository Pattern" of TypeOrm in Nest works. I would like to have a resources file in which a local and a remote file hold the different entites, controllers, modules etc. See screenshot



enter image description here



When the app is building I`m getting the following error:



[Nest] 3186   - 11/19/2018, 10:44:43 PM   [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms


From the Nest and TypeORM documentation I can triangulate, that I have to tell the application where it can find the entities or at least this is what I believe the error is trying to tell me.



I'm using a .env to pull the config for TypeORM in:



TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}


app.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'

@Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: ,
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}


project.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';

@Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}


project.service.ts



import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';

@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}

async findAll(): Promise<Project> {
return await this.projectRepository.find();
}
}


project.entity.ts



import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Project {
@PrimaryGeneratedColumn()
id: number;

@Column({ length: 500 })
name: string;
}


project.controller.ts



import { Controller, Get } from '@nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';

@Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}

@Get()
findAll(): Promise<Project> {
return this.projectService.findAll();
}
}









share|improve this question














I'm trying to figure out how the "Repository Pattern" of TypeOrm in Nest works. I would like to have a resources file in which a local and a remote file hold the different entites, controllers, modules etc. See screenshot



enter image description here



When the app is building I`m getting the following error:



[Nest] 3186   - 11/19/2018, 10:44:43 PM   [ExceptionHandler] No repository for "Project" was found. Looks like this entity is not registered in current "default" connection? +1ms


From the Nest and TypeORM documentation I can triangulate, that I have to tell the application where it can find the entities or at least this is what I believe the error is trying to tell me.



I'm using a .env to pull the config for TypeORM in:



TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_USERNAME = xyz
TYPEORM_PASSWORD = xyz
TYPEORM_DATABASE = xyz
TYPEORM_PORT = 5432
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/server/**/**.entity{.ts,.js}


app.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AngularUniversalModule } from './modules/angular-universal/angular-universal.module';
import { JiraService } from './services/jira.service'
// modules
import { ProjectModule } from './resources/local/project/project.module'
// sync
import {ProjectsSync} from './sync/projects.sync'

@Module({
imports: [
ProjectModule,
TypeOrmModule.forRoot(),
AngularUniversalModule.forRoot(),
],
controllers: ,
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}


project.module.ts



import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectService } from './project.service';
import { ProjectController } from './project.controller';
import { Project } from './project.entity';

@Module({
imports: [TypeOrmModule.forFeature([Project])],
providers: [ProjectService],
controllers: [ProjectController],
})
export class ProjectModule {}


project.service.ts



import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Project } from './project.entity';

@Injectable()
export class ProjectService {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {}

async findAll(): Promise<Project> {
return await this.projectRepository.find();
}
}


project.entity.ts



import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Project {
@PrimaryGeneratedColumn()
id: number;

@Column({ length: 500 })
name: string;
}


project.controller.ts



import { Controller, Get } from '@nestjs/common';
import { ProjectService } from './project.service';
import { Project } from './project.entity';

@Controller('project')
export class ProjectController {
constructor(private readonly projectService: ProjectService) {}

@Get()
findAll(): Promise<Project> {
return this.projectService.findAll();
}
}






node.js typescript nestjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 21:59









TinoTino

1,27831948




1,27831948













  • You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

    – zenbeni
    Nov 23 '18 at 9:34



















  • You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

    – zenbeni
    Nov 23 '18 at 9:34

















You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

– zenbeni
Nov 23 '18 at 9:34





You won't be able to run correctly with node (not ts-node) as you need to parse entities within dist and not src. Personally I prefer using a factory method to instanciate the TypeOrm connection so I can change this path depending on env (and parsing other props from env files within code).

– zenbeni
Nov 23 '18 at 9:34












1 Answer
1






active

oldest

votes


















0














Just a wild guess, have you tried:



TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}


And/or switching the imports in app.module.ts putting the TypeOrmModule first:



@Module({
imports: [
TypeOrmModule.forRoot(),
ProjectModule,
AngularUniversalModule.forRoot(),
],
controllers: ,
providers:[JiraService, ProjectsSync],
})
export class ApplicationModule {}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383242%2fnestjs-repository-pattern-config-challenge%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Just a wild guess, have you tried:



    TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}


    And/or switching the imports in app.module.ts putting the TypeOrmModule first:



    @Module({
    imports: [
    TypeOrmModule.forRoot(),
    ProjectModule,
    AngularUniversalModule.forRoot(),
    ],
    controllers: ,
    providers:[JiraService, ProjectsSync],
    })
    export class ApplicationModule {}





    share|improve this answer






























      0














      Just a wild guess, have you tried:



      TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}


      And/or switching the imports in app.module.ts putting the TypeOrmModule first:



      @Module({
      imports: [
      TypeOrmModule.forRoot(),
      ProjectModule,
      AngularUniversalModule.forRoot(),
      ],
      controllers: ,
      providers:[JiraService, ProjectsSync],
      })
      export class ApplicationModule {}





      share|improve this answer




























        0












        0








        0







        Just a wild guess, have you tried:



        TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}


        And/or switching the imports in app.module.ts putting the TypeOrmModule first:



        @Module({
        imports: [
        TypeOrmModule.forRoot(),
        ProjectModule,
        AngularUniversalModule.forRoot(),
        ],
        controllers: ,
        providers:[JiraService, ProjectsSync],
        })
        export class ApplicationModule {}





        share|improve this answer















        Just a wild guess, have you tried:



        TYPEORM_ENTITIES = ./server/**/**.entity{.ts,.js}


        And/or switching the imports in app.module.ts putting the TypeOrmModule first:



        @Module({
        imports: [
        TypeOrmModule.forRoot(),
        ProjectModule,
        AngularUniversalModule.forRoot(),
        ],
        controllers: ,
        providers:[JiraService, ProjectsSync],
        })
        export class ApplicationModule {}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 15:02

























        answered Nov 20 '18 at 10:38









        LewsmithLewsmith

        959




        959
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383242%2fnestjs-repository-pattern-config-challenge%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)