{"id":16465779,"url":"https://github.com/tony133/nestjs-mysql","last_synced_at":"2025-03-16T18:31:26.968Z","repository":{"id":40778185,"uuid":"507087337","full_name":"Tony133/nestjs-mysql","owner":"Tony133","description":"Mysql module for Nest framework (node.js) 😻","archived":false,"fork":false,"pushed_at":"2025-03-10T22:42:02.000Z","size":2380,"stargazers_count":15,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-12T00:34:32.802Z","etag":null,"topics":["javascript","mysql","mysql2","nest","nest-module","nest-mysql","nestjs","nestjs-mysql","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tony133.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-24T17:08:30.000Z","updated_at":"2025-03-10T22:42:05.000Z","dependencies_parsed_at":"2024-05-17T23:24:41.682Z","dependency_job_id":"df5741de-c354-4933-81c1-89b678bd2684","html_url":"https://github.com/Tony133/nestjs-mysql","commit_stats":{"total_commits":889,"total_committers":3,"mean_commits":296.3333333333333,"dds":0.07199100112485934,"last_synced_commit":"a32f120dcd939ee893d99b7b0a8b00a242c6b00c"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tony133","download_url":"https://codeload.github.com/Tony133/nestjs-mysql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","mysql","mysql2","nest","nest-module","nest-mysql","nestjs","nestjs-mysql","typescript"],"created_at":"2024-10-11T11:35:11.934Z","updated_at":"2025-03-16T18:31:26.216Z","avatar_url":"https://github.com/Tony133.png","language":"TypeScript","funding_links":["https://opencollective.com/nest","https://paypal.me/kamilmysliwiec"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"200\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456\n[circleci-url]: https://circleci.com/gh/nestjs/nest\n\n  \u003cp align=\"center\"\u003eA progressive \u003ca href=\"http://nodejs.org\" target=\"_blank\"\u003eNode.js\u003c/a\u003e framework for building efficient and scalable server-side applications.\u003c/p\u003e\n    \u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@nestjs/core.svg\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@nestjs/core.svg\" alt=\"Package License\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@nestjs/common.svg\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://circleci.com/gh/nestjs/nest\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/circleci/build/github/nestjs/nest/master\" alt=\"CircleCI\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://coveralls.io/github/nestjs/nest?branch=master\" target=\"_blank\"\u003e\u003cimg src=\"https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9\" alt=\"Coverage\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://discord.gg/G7Qnnhy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/discord-online-brightgreen.svg\" alt=\"Discord\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#backer\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/nest/backers/badge.svg\" alt=\"Backers on Open Collective\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#sponsor\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/nest/sponsors/badge.svg\" alt=\"Sponsors on Open Collective\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://paypal.me/kamilmysliwiec\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Donate-PayPal-ff3f59.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://opencollective.com/nest#sponsor\"  target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg\" alt=\"Support us\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://twitter.com/nestframework\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/twitter/follow/nestframework.svg?style=social\u0026label=Follow\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n  \u003c!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)\n  [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)--\u003e\n\n## Description\n\nMySQL module for Nest framework (node.js) 😻\n\n## Installation\n\nFirst install the module via `yarn` or `npm` or `pnpm` and do not forget to install the driver package as well:\n\n\n```bash\n    $ npm i --save nest-mysql mysql2\n```\nor\n\n```bash\n    $ yarn add nest-mysql mysql2\n```\n\nor\n\n```bash\n    $ pnpm add nest-mysql mysql2\n```\n\n## Table of Contents\n\n- [Description](#description)\n- [Table of Contents](#table-of-contents)\n- [Usage](#usage)\n  - [MySqlModule](#mysqlmodule)\n  - [MultiConnectionsDatabase](#multi-connections-database)\n  - [ExampleOfUse](#example-of-use)\n\n## Usage\n\n### MySqlModule\n\nMySqlModule is the primary entry point for this package and can be used synchronously\n\n```typescript\n@Module({\n  imports: [\n    MysqlModule.forRoot({\n        host: 'localhost',\n        database: 'test2',\n        password: 'root',\n        user: 'root',\n        port: 3306,      \n    }),\n  ],\n})\n```\n\nor asynchronously\n\n```typescript\n@Module({\n  imports: [\n    MysqlModule.forRootAsync({\n      useFactory: () =\u003e ({\n        host: 'localhost',\n        database: 'test',\n        password: 'root',\n        user: 'root',\n        port: 3306,      \n      }),\n    }),\n  ],\n})\n```\n\n## Example of use\n\nUsersService:\n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { InjectClient } from 'nest-mysql';\nimport { Connection } from 'mysql2';\nimport { User } from '../interfaces/user.interface';\n\n@Injectable()\nexport class UsersService {\n  constructor(@InjectClient() private readonly connection: Connection) {}\n\n  async findAll(): Promise\u003cUser[]\u003e {\n    const users = await this.connection.query('SELECT * FROM users');\n    const results = Object.assign([{}], users[0]);\n\n    return results;\n  }\n}\n```\n\nUsersController:\n\n```typescript\nimport { Controller, Get } from '@nestjs/common';\nimport { UsersService } from './users.service';\nimport { User } from '../interfaces/user.interface';\n\n@Controller()\nexport class UsersController {\n  constructor(private readonly usersService: UsersService) {}\n\n  @Get()\n  async getAllUsers(): Promise\u003cUser[]\u003e {\n    return await this.usersService.findAll();\n  }\n}\n```\n\n## Multi Connections Database\n\n```typescript\n@Module({\n  imports: [\n    MysqlModule.forRootAsync(\n      {\n        useFactory: () =\u003e ({\n          host: 'localhost',\n          database: 'test1',\n          password: 'root',\n          user: 'root',\n          port: 3306,      \n        }),\n      },\n      'db1Connection',\n    ),\n    MysqlModule.forRootAsync(\n      {\n        useFactory: () =\u003e ({\n          host: 'localhost',\n          database: 'test2',\n          password: 'root',\n          user: 'root',\n          port: 3307,      \n        }),\n      },\n      'db2Connection',\n    ),\n  ],\n  controllers: [],\n  providers: [],\n})\nexport class AppModule {}\n```\n\nUsage example with Multi Connection\n\nPostService:\n\n```typescript\nimport { Pool } from 'mysql2';\nimport { InjectConnection } from 'nest-mysql';\nimport { CreatePostDto } from './dto/create-post.dto';\nimport { Post } from '../interfaces/post.interface';\n\n@Injectable()\nexport class PostService {\n  constructor(\n    @InjectConnection('db2Connection')\n    private dbConnection: Pool,\n  ) {}\n\n  public async findAll(): Promise\u003cPost[]\u003e {\n    const posts = await this.dbConnection.query('SELECT * FROM posts');\n    const results = Object.assign([{}], posts[0]);\n\n    return results;\n  }\n\n  public async create(createPostDto: CreatePostDto): Promise\u003cPost[]\u003e {\n    try {\n      const post = await this.dbConnection.query(\n        'INSERT INTO posts (title, description) VALUES (?, ?)',\n        [createPostDto.title, createPostDto.description],\n      );\n      return post;\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n}\n```\n\nUsersService:\n\n```typescript\nimport { Pool } from 'mysql2';\nimport { InjectConnection } from 'nest-mysql';\nimport { CreateUserDto } from './dto/create-user.dto';\nimport { User } from '../interfaces/user.interface';\n\n@Injectable()\nexport class UsersService {\n  constructor(\n    @InjectConnection('db1Connection')\n    private dbConnection: Pool,\n  ) {}\n\n  public async findAll(): Promise\u003cUser[]\u003e {\n    const users = await this.dbConnection.query('SELECT * FROM users');\n    const results = Object.assign([{}], users[0]);\n\n    return results;\n  }\n\n  public async create(createUserDto: CreateUserDto): Promise\u003cUser[]\u003e {\n    try {\n      const user = await this.dbConnection.query(\n        'INSERT INTO users (firstName, lastName)  VALUES (?, ?)',\n        [createUserDto.firstName, createUserDto.lastName],\n      );\n      return user;\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n}\n```\n\nFor more information on `node-mysql` see [here](https://github.com/sidorares/node-mysql2)\n\n## Contribute\nFeel free to help this library, I'm quite busy with also another Nestjs packages, but the community will appreciate the effort of improving this library. Make sure you follow the guidelines\n\n## Stay in touch\n\n- Author - [Tony133](https://github.com/Tony133)\n- Framework - [https://nestjs.com](https://nestjs.com/)\n\n## License\n\n [MIT licensed](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftony133%2Fnestjs-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftony133%2Fnestjs-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftony133%2Fnestjs-mysql/lists"}