Using neo4j-driver with nest js

Hello,

I have a code using nest js to acces the database through the bolt protocol. The tutorial I followed (Nestjs + Neo4j Integration | by Faaiz SHAH | Medium) installed the driver version 1.7.6, when I install that, i get the error: [ExceptionsHandler] Client network socket disconnected before secure TLS connection was established. If I update the driver, an error related with v1 shows up, and I don't know exactly what to do to change the v1 into the newer version of the driver in the code. Thank you!

Code: import { Injectable, Inject } from '@ nestjs/common';
//import { v1 } from 'neo4j-driver';
var neo4j = require('neo4j-driver');
@ Injectable()
export class Neo4jService {
constructor(@ Inject('Neo4j') private readonly neo4j: v1.Driver) {}
async findAll(): Promise {
return this.neo4j.session().run('MATCH (n) RETURN n');
}
}

Hey @ziadtaleb97,

Sorry it's a bit late but I've been experimenting with NestJS and Neo4j for the past few days and I've put together a module that you can use to get an instance of the Driver in your services. You register Neo4jModule as a dynamic module with your database connection details, then you can inject the Neo4jService into your modules

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Neo4jModule } from 'nest-neo4j'

@Module({
  imports: [
    Neo4jModule.forRoot({
      scheme: 'neo4j',
      host: 'localhost',
      port: 7687,
      username: 'neo4j',
      password: 'neo'
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Then to inject:

import { Neo4jService } from 'nest-neo4j'

@Controller()
export class AppController {
  constructor(private readonly neo4jService: Neo4jService){}
  // ...
}

Right now it's got methods for:

  • Reading read(cypher: string, params: object)
  • Writing write(cypher: string, params: object)
  • Getting a read/write session - getReadSession(database?: string) or getWriteSession(database?: string)

Let me know if it's useful. I'm also looking into how we can integrate with packages like TypeORM so if you use anything like that or have any preference then let me know.

@ziadtaleb97 If you would like to use Neo4j with NestJS, you can use DRIVINE.ORG. Drivine provides ready-made NestJS integration and has been battle tested in production.

You can find out more via the website or sample app, but quick start steps are as follows:

Register one or more Neo4j Databases

DATABASE_TYPE=NEO4J
DATABASE_USER='neo4j'
DATABASE_PASSWORD='h4ckM3'
DATABASE_HOST='localhost'
DATABASE_PORT='7687'
# This property is optional and defaults to the 'neo4j' database.
DATABASE_NAME='neo4j'

Declare a module that imports DrivineModule:

@Module({
    imports: [
        DrivineModule.withOptions(<DrivineModuleOptions>{
            connectionProviders: [DatabaseRegistry.buildOrResolveFromEnv()]
        }),
        HealthModule,
        MovieModule,
        TrafficModule
    ],
    controllers: [],
    providers: []
})
export class AppModule {}

Start Creating Repositories

@Injectable()
export class ActorRepository {

    constructor(@InjectPersistenceManager() readonly persistenceManager: PersistenceManager,
                @InjectCypher(__dirname, 'moviesForActor') readonly moviesForActor: CypherStatement,
                @InjectCypher(__dirname, 'coActorsForActor') readonly coActorsForActor: CypherStatement) {
    }

    @Transactional()
    async findByName(name: string): Promise<any> {
        const spec = new QuerySpecification().withStatement(this.moviesForActor).bind({name: name});
        return this.persistenceManager.maybeGetOne(spec);
    }

    async listCoActors(name: string): Promise<any> {
        const spec = new QuerySpecification().withStatement(this.coActorsForActor).bind({name: name});
        return this.persistenceManager.maybeGetOne(spec);
    }

}

Drivine Provides:

  • PersistenceManager that you can use to compose repositories.
  • Decorator driven transaction context.
  • Streaming without back-pressure. (We provided feedback to Neo4j driver team in this regard).
  • Community on StackOverflow.

To find out more visit the website or check out the sample app.

@adam_cowley Looking forward to coming on the program to show talk more about Drivine.