4.0.1 • Published 1 year ago

@syukurilexs/nestjs-mariadb v4.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

Description

This module is Global type of module

Installation

npm install @syukurilexs/nestjs-mariadb

Usage Single Database

Import MariadbModule:

import { MariadbModule } from '@syukurilexs/nestjs-mariadb';

@Module({
  imports: [
    MariadbModule.forRoot({
      host: 'localhost',
      user: 'root',
      password: 'yourpassword',
      connectionLimit: 5
    })
  ],
  providers: [...],
})
export class AppModule {}

Inject Pool:

@Injectable()
export class YourService {
  constructor(@Mariadb() private readonly pool: Pool) {}

  method1() {
    // For single query just use this method, it will auto release
    // after use
    return this.pool.query('select * from mydb.mytable');
  }

  async method2() {
    // Create connection
    const conn = await this.pool.getConnection();

    // Use created connection
    const rows = await conn.query('select * from syukur.mytable');

    // Re-use existing connection
    const rows2 = await conn.query('select * from syukur.mytable');

    // Release connection to pool after use
    conn.release();

    return [rows, rows2];
  }
}

Usage Multiple Databases

Import MariadbModule:

import { MariadbModule } from '@syukurilexs/nestjs-mariadb';

@Module({
  imports: [
    MariadbModule.forRoot(
      {
        host: 'server01',
        user: 'root',
        password: 'yourpassword',
        connectionLimit: 5
      },
      'DATABASE_ONE'
    ),
    MariadbModule.forRoot(
      {
        host: 'server02',
        user: 'root',
        password: 'yourpassword',
        connectionLimit: 5
      },
      'DATABASE_TWO'
    )
  ],
  providers: [...],
})
export class AppModule {}

Inject Pool:

@Injectable()
export class YourService {
  constructor(
    @Mariadb('DATABASE_ONE') private readonly poolOne: Pool,
    @Mariadb('DATABASE_TWO') private readonly poolTwo: Pool
  ) {}

  method1() {
    // For single query just use this method, it will auto release
    // after use
    const one$ = this.poolOne.query('select * from mydb.mytable');
    const two$ = this.poolTwo.query('select * from mydb.mytable');

    return Promise.all([one$, two$]);
  }

  async method2() {
    // Create connection
    const conn = await this.poolOne.getConnection();

    // Use created connection
    const rows = await conn.query('select * from syukur.mytable');

    // Re-use existing connection
    const rows2 = await conn.query('select * from syukur.mytable');

    // Release connection to pool after use
    conn.release();

    return [rows, rows2];
  }
}

Usage Multiple Databases Asyn

Import MariadbModule:

import { MariadbModule } from '@syukurilexs/nestjs-mariadb';

@Module({
  imports: [
    MariadbModule.forRoot(
      {
        imports: [ConfigModule],
        useFactory: (config: ConfigService) => ({
          host: config.get('HOST_ONE'),
          user: 'admin',
          password: 'password',
          connectionLimit: 5,
          port: Number(config.get('PORT_ONE')),
        }),
        inject: [ConfigService],
      },
      'DATABASE_ONE'
    ),
    MariadbModule.forRoot(
      {
        imports: [ConfigModule],
        useFactory: (config: ConfigService) => ({
          host: config.get('HOST_TWO'),
          user: 'admin',
          password: 'password',
          connectionLimit: 5,
          port: Number(config.get('PORT_ONE')),
        }),
        inject: [ConfigService],
      },
      'DATABASE_TWO'
    )
  ],
  providers: [...],
})
export class AppModule {}

Inject Pool:

@Injectable()
export class YourService {
  constructor(
    @Mariadb('DATABASE_ONE') private readonly poolOne: Pool,
    @Mariadb('DATABASE_TWO') private readonly poolTwo: Pool
  ) {}

  method1() {
    // For single query just use this method, it will auto release
    // after use
    const one$ = this.poolOne.query('select * from mydb.mytable');
    const two$ = this.poolTwo.query('select * from mydb.mytable');

    return Promise.all([one$, two$]);
  }

  async method2() {
    // Create connection
    const conn = await this.poolOne.getConnection();

    // Use created connection
    const rows = await conn.query('select * from syukur.mytable');

    // Re-use existing connection
    const rows2 = await conn.query('select * from syukur.mytable');

    // Release connection to pool after use
    conn.release();

    return [rows, rows2];
  }
}

This module is Gloabl Module

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

Syukur Kassim (Syukurilexs on Linkedin)

License

Licensed under the MIT License - see the LICENSE file for details.

4.0.1

1 year ago

3.0.0

3 years ago

2.0.0

3 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago