Giter Site home page Giter Site logo

4lessandrodev / neode-nestjs Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 659 KB

A neode module for nestjs

Home Page: https://www.npmjs.com/package/neode-nestjs

TypeScript 91.63% JavaScript 5.21% Shell 3.17%
nestjs-module typescript neo4j nestjs schemas neode

neode-nestjs's Introduction

Neode with NestJS

A module to connect Neode, Neo4j and NestJS.

npm i neode-nestjs or yarn add neode-nestjs

Variables

.env

If your connection fail check if you are reading your .env settings before start your application You must add env-cmd before your script

{
     "start:dev": "env-cmd nest start --watch"
}
NEO4J_DATABASE=neo4j
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your-password
NEO4J_HOST=localhost
NEO4J_PORT=7687
NEO4J_PROTOCOL=neo4j
NEO4J_ENCRYPTION=ENCRYPTION_OFF

Main module with .env settings

import { NeodeModule } from 'neode-nestjs';

app.module.ts

@Module({
     imports: [NeodeModule.forRoot(), UserModule],
     controllers: [AppController],
     providers: [],
})
export class AppModule {}

Main module with connection settings

@Module({
 imports: [
  NeodeModule.forRoot (
   { host: 'bolt://localhost', port: 7689, username: 'name', password: '1234' }
 ), UserModule],
     controllers: [UserController],
     providers: [UserService],
})
export class UserModule {}

Entities or Schemas

Your schema to inject on module

Important when you import Neode if throw types error you should use import like example bellow.

import * as Neode from 'neode';

Schema

import { SchemaObject } from 'neode';

const UserSchema: SchemaObject = {
     id: {
          type: 'uuid',
          primary: true,
          required: true,
     },
     name: {
          type: 'string',
          required: true,
     },
     email: {
          type: 'string',
          unique: true,
          required: true,
     },
     password: {
          type: 'string',
          required: true,
     },
     avatar: {
          type: 'string',
     },
     isFirstAuth: {
          type: 'boolean',
     },
};

export default UserSchema;

Specific module with .env settings

@Module({
     imports: [NeodeModule.forFeature({ User: UserSchema })],
     controllers: [UserController],
     providers: [UserService],
})
export class UserModule {}

Specific module with connection settings

@Module({
 imports: [
  NeodeModule.forFeature (
   { User: UserSchema }, 
   { host: 'bolt://localhost', port: 7689, username: 'name', password: '1234' }
 )],
     controllers: [UserController],
     providers: [UserService],
})
export class UserModule {}

User interface

Example User interface

export interface UserInterface {
     id: string;
     name: string;
     email: string;
     password: string;
     avatar: string;
     isFirstAuth: boolean;
}

Create user dto

Dto example if you are using rest api. The decorator validation @IsEmail refer to class-validator

export class CreateUserDto implements Partial<UserInterface> {
     @IsNotEmpty()
     avatar!: string;

     @IsEmail()
     @IsNotEmpty()
     email!: string;

     @IsString()
     @IsNotEmpty()
     name!: string;

     @IsNotEmpty()
     @IsString()
     password!: string;
}

Service class

You can get connection by injected Connection. It returns a Neode instance

The word 'User' is the same you have used to inject on { User: UserSchema } if you use same different word as not User you must to use it.

@Injectable()
export class UserService {
     constructor(@Inject('Connection') private readonly neode: Neode) {}

     async createUser(dto: CreateUserDto): Promise<void> {
          // note that on example my interface has id and isFirstAuth attributes, and my Dto not.
          // Generate id
          const id: string = uuid();
          // Set isFirstAuth
          const isFirstAuth: boolean = true;

          await this.neode.merge('User', { ...dto, id, isFirstAuth });
     }

     async getUsers(): Promise<UserInterface[]> {
          const users = await this.neode.all('User');
          return (await users.toJson()) as UserInterface[];
     }
}

References

neode-nestjs's People

Contributors

4lessandrodev avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

neode-nestjs's Issues

TypeError: Neode.fromEnv is not a function

On start application the error bellow throw on terminal

TypeError: Neode.fromEnv is not a function
    at Function.<anonymous> (/home/node_modules/neode-nestjs/dist/index.js:51:56)
    at Generator.next (<anonymous>)
    at /home/neode-tests/node_modules/neode-nestjs/dist/index.js:33:71
    at new Promise (<anonymous>)
    at __awaiter (/home/neode-tests/node_modules/neode-nestjs/dist/index.js:29:12)
    at InstanceWrapper.useFactory [as metatype] (/home/neode-tests/node_modules/neode-nestjs/dist/index.js:50:39)
    at Injector.instantiateClass (/home/neode-tests/node_modules/@nestjs/core/injector/injector.js:289:55)
    at callback (/home/neode-tests/node_modules/@nestjs/core/injector/injector.js:42:41)
    at async Injector.resolveConstructorParams (/home/neode-tests/node_modules/@nestjs/core/injector/injector.js:114:24)
    at async Injector.loadInstance (/home/neode-tests/node_modules/@nestjs/core/injector/injector.js:46:9)

Connecting to Neo4j via NeodeModule.forRoot()

Hi @4lessandrodev!

In neode driver there is an option to connect to neo4j, by connecting URL, w/o .env variables.

So does it option also present in neode-nestjs? According to static forRoot(): DynamicModule;, the forRoot method have empty args and it's unable to pass the uri string. So is connecting via .env is the only option? Or I could pass somewhere else my variables via *.yml file or config module w/o of using .env?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.