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

Cannot start application because of PersistenceManager injection #5

Closed
skelet00r opened this issue Oct 10, 2020 · 7 comments
Closed
Labels
question Further information is requested

Comments

@skelet00r
Copy link

skelet00r commented Oct 10, 2020

Local neo4j instance running at v4.1

docker run \     
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    neo4j

.env confirguration

SERVER_PORT=3000

NODE_ENV=dev

#Valid options are [VERBOSE, DEBUG, INFO, WARN, ERROR, NONE]
DRIVINE_LOG_LEVEL=VERBOSE

NEO_DATABASE_TYPE=NEO4J
NEO_DATABASE_USER=neo4j
NEO_DATABASE_PASSWORD=neo4j
NEO_DATABASE_HOST=localhost
NEO_DATABASE_PORT=7687

Logs

[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
[Nest] 37857   - 10/10/2020, 17:17:39   [NestFactory] Starting Nest application...
[Nest] 37857   - 10/10/2020, 17:17:39   [InstanceLoader] AppModule dependencies initialized +18ms
[Nest] 37857   - 10/10/2020, 17:17:39   [ExceptionHandler] Nest can't resolve dependencies of the HealthRepository (?). Please make sure that the argument PersistenceManager:default at index [0] is available in the HealthModule context.

Potential solutions:
- If PersistenceManager:default is a provider, is it part of the current HealthModule?
- If PersistenceManager:default is exported from a separate @Module, is that module imported within HealthModule?
  @Module({
    imports: [ /* the Module containing PersistenceManager:default */ ]
  })
 +3ms
Error: Nest can't resolve dependencies of the HealthRepository (?). Please make sure that the argument PersistenceManager:default at index [0] is available in the HealthModule context.

Potential solutions:
- If PersistenceManager:default is a provider, is it part of the current HealthModule?
- If PersistenceManager:default is exported from a separate @Module, is that module imported within HealthModule?
  @Module({
    imports: [ /* the Module containing PersistenceManager:default */ ]
  })

New to nest not entirely sure where the issue is, PersistenceManager isnt injected into the Health module directly.
Two injection points I think are the HealthRepository and the root instantiation of Drivine

Is there a need to inject PersistenceManager into each module?

Note

Removed the ^ from all the deps in the package.json and deleted to package-lock.json and did a clean npm i still ran into the same issue.
Wanted to make sure some other dependencies weren't causing the issue

@skelet00r skelet00r changed the title Cannot start application because of nest injection issue Cannot start application because of PersistenceManager injection Oct 10, 2020
@skelet00r
Copy link
Author

Potentially related to this issue liberation-data/drivine#47

@DanielMenke
Copy link

DanielMenke commented Oct 27, 2020

Experiencing the same problem.
app.module.ts

  imports: [
    DrivineModule.withOptions(<DrivineModuleOptions>{
      connectionProviders: [DatabaseRegistry.buildOrResolveFromEnv('NEO')],
    }),
    IngredientModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

ingredient.module.ts

import { Module } from '@nestjs/common';
import { IngredientService } from './ingredient.service';
import { IngredientRepository } from './ingredient.repository';

@Module({
  providers: [IngredientService, IngredientRepository],
  exports: [IngredientService],
})
export class IngredientModule {}

ingredient.repository.ts

import { Injectable } from '@nestjs/common';
import {
  InjectPersistenceManager,
  PersistenceManager,
  QuerySpecification,
} from '@liberation-data/drivine';
import { CreateIngredientDto, Ingredient } from './model/ingredient';

@Injectable()
export class IngredientRepository {
  constructor(
    @InjectPersistenceManager() readonly persistenceManager: PersistenceManager,
  ) {}

  public async create(dto: CreateIngredientDto): Promise<Ingredient> {
    return this.persistenceManager.getOne<Ingredient>(
      new QuerySpecification<Ingredient>(
        'CREATE (i:INGREDIENT {id:apoc.create.uuid(), name: $name}) RETURN i',
      )
        .bind([dto.name])
        .transform(Ingredient),
    );
  }
}



@jasperblues
Copy link
Member

jasperblues commented Oct 27, 2020

Remove NEO_ prefix form the DB registration, so that it looks like:

DATABASE_TYPE=NEO4J
DATABASE_USER=neo4j
DATABASE_PASSWORD=neo4j
DATABASE_HOST=localhost
DATABASE_PORT=7687

This is required to make @InjectPersistenceManager(undefined/null) to work. With the .env as is, it would be necessary to have @InjectPersistenceManager('NEO')

If this fixes the issue, let us know, so that it can be closed.

Explanation

In Drivine you can have one database defined as the default and this is the one that will be provided when using @InjectPersistenceManager() with no arguments. There can be additional named databases to use with @InjectPersistenceManger('name'). When loading from .env` files the name is the prefix part.

Interesting Points

  • It is also possible to register databases on the fly, at runtime, using DatabaseRegistry.
  • Once there are additional databases, we can have transactions spanning multiple DBs. (Though these are not true XA transactions - discussion for another day).

@jasperblues jasperblues added documentation Improvements or additions to documentation question Further information is requested and removed documentation Improvements or additions to documentation labels Oct 27, 2020
@jasperblues
Copy link
Member

All ok @ElePhontitis ? Can I close the issue?

@DanielMenke
Copy link

@jasperblues as I did not open it, I didn't feel entitled to determine that, but you have an ok from my side. Thanks a lot for your quick response!

@jasperblues
Copy link
Member

Oops, sorry! I didn't realize that there were two people having the same issue.

Let's wait for @jdelibas to confirm that the issue is solved for him too. I'm going to have to improve the docs.

@4lessandrodev
Copy link

4lessandrodev commented Mar 28, 2021

I had the same problem, but I followed the steps informed and everything went well

env file

NEO_DATABASE_TYPE=NEO4J
NEO_DATABASE_USER=neo4j
NEO_DATABASE_PASSWORD=secure_password
NEO_DATABASE_HOST=localhost
NEO_DATABASE_PORT=7687

Main module

@Module({
  imports: [
    DrivineModule.withOptions(<DrivineModuleOptions>{
      connectionProviders: [DatabaseRegistry.buildOrResolveFromEnv('NEO')],
    }),
    UserModule,
    TaskModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

User module context

@Module({
  imports: [],
  controllers: [UserController],
  providers: [UserRepository, UserService],
  exports: [],
})
export class UserModule {}

Repository

@Injectable()
export class UserRepository {
  constructor(
    @InjectPersistenceManager('NEO')
    readonly persistenceManager: PersistenceManager,
  ) {}

  async save(user: User): Promise<void> {
    await this.persistenceManager.execute(
      new QuerySpecification(
        `MERGE (u:User {ID: "${user.ID}", name: "${user.name}"} )`,
      ),
    );
  }
}

All running perfectly. I Think can close this issue
Full example follow Github repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants