{"id":16465787,"url":"https://github.com/tony133/nestjs-knexjs","last_synced_at":"2025-05-16T06:00:25.245Z","repository":{"id":37050181,"uuid":"341333558","full_name":"Tony133/nestjs-knexjs","owner":"Tony133","description":"Knexjs module for Nest framework (node.js) 😻","archived":false,"fork":false,"pushed_at":"2025-05-12T23:50:54.000Z","size":4485,"stargazers_count":109,"open_issues_count":1,"forks_count":11,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-13T00:31:16.721Z","etag":null,"topics":["javascript","knex","knexjs","mariadb","mysql","nest","nestjs","nestjs-knexjs","nodejs","postgresql","sqlite","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,"zenodo":null}},"created_at":"2021-02-22T20:50:25.000Z","updated_at":"2025-05-12T23:50:56.000Z","dependencies_parsed_at":"2023-11-07T03:30:10.196Z","dependency_job_id":"2ba43218-1b31-456f-b0b9-09836352b275","html_url":"https://github.com/Tony133/nestjs-knexjs","commit_stats":{"total_commits":1166,"total_committers":7,"mean_commits":"166.57142857142858","dds":"0.42881646655231564","last_synced_commit":"762b948fe4c7cf03383c943085f71b1213bb9156"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-knexjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-knexjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-knexjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tony133%2Fnestjs-knexjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tony133","download_url":"https://codeload.github.com/Tony133/nestjs-knexjs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253851080,"owners_count":21973674,"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","knex","knexjs","mariadb","mysql","nest","nestjs","nestjs-knexjs","nodejs","postgresql","sqlite","typescript"],"created_at":"2024-10-11T11:35:13.683Z","updated_at":"2025-05-16T06:00:25.212Z","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\nKnexjs 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-knexjs knex mysql     # for mysql/mariadb\n  $ npm i --save nest-knexjs knex pg        # for postgresql\n  $ npm i --save nest-knexjs knex sqlite    # for sqlite\n  $ npm i --save nest-knexjs knex oracledb  # for oracledb\n```\nor\n\n```bash\n  $ yarn add nest-knexjs knex mysql     # for mysql/mariadb\n  $ yarn add nest-knexjs knex pg        # for postgresql\n  $ yarn add nest-knexjs knex sqlite    # for sqlite\n  $ yarn add nest-knexjs knex oracledb  # for oracledb\n```\nor \n\n```bash\n  $ pnpm add nest-knexjs knex mysql     # for mysql/mariadb\n  $ pnpm add nest-knexjs knex pg        # for postgresql\n  $ pnpm add nest-knexjs knex sqlite    # for sqlite\n  $ pnpm add nest-knexjs knex oracledb  # for oracledb\n```\n\n## Table of Contents\n\n- [Description](#description)\n- [Table of Contents](#table-of-contents)\n- [Usage](#usage)\n  - [KnexModule](#knexmodule)\n  - [ExampleOfUse](#example-of-use)\n  - [MultiConnectionsDatabase](#multi-connections-database)\n  - [CreateMigrations](#create-migrations)\n  - [RunMigrations](#run-migrations)\n  - [CreateSeeds](#create-seeds) \n  - [RunSeeds](#run-seeds)\n## Usage\n\n### KnexModule\n\nKnexModule is the primary entry point for this package and can be used synchronously\n\n```typescript\n@Module({\n  imports: [\n    KnexModule.forRoot({\n      config: {\n        client: 'mysql',\n        version: '5.7',\n        connection: {\n          host: '127.0.0.1',\n          user: 'root',\n          password: 'root',\n          database: 'test',\n        },\n      },\n    }),\n  ],\n})\n```\nor asynchronously\n\n```typescript\n@Module({\n  imports: [\n    KnexModule.forRootAsync({\n      useFactory: () =\u003e ({\n        config: {\n          client: 'mysql',\n          version: '5.7',\n          connection: {\n            host: '127.0.0.1',\n            user: 'root',\n            password: 'root',\n            database: 'test',\n          },\n        },\n      }),\n    }),\n  ],\n})\n```\n\n## Example of use\n\nUsersService:\n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { Knex } from 'knex';\nimport { InjectConnection } from 'nest-knexjs';\n\n@Injectable()\nexport class UsersService {\n  constructor(@InjectConnection() private readonly knex: Knex) {}\n\n  async findAll() {\n    const users = await this.knex.table('users');\n    return { users };\n  }\n}\n```\n\nUsersController:\n\n```typescript\nimport { Controller, Get } from '@nestjs/common';\nimport { UsersService } from './users.service';\n\n@Controller()\nexport class UsersController {\n  constructor(private readonly usersService: UsersService) {}\n\n  @Get()\n  async getAllUsers() {\n    return await this.usersService.findAll();\n  }\n}\n```\n\n## Multi Connections Database\n\n```typescript\n@Module({\n  imports: [\n    KnexModule.forRootAsync(\n      {\n        useFactory: () =\u003e ({\n          config: {\n            client: 'mysql',\n            version: '5.7',\n            useNullAsDefault: true,\n            connection: {\n              host: '127.0.0.1',\n              user: 'root',\n              port: 3306,\n              password: 'root',\n              database: 'test1',\n            },\n            pool: {\n              min: 2,\n              max: 10,\n            },\n          },\n        }),\n      },\n      'db1Connection',\n    ),\n    KnexModule.forRootAsync(\n      {\n        useFactory: () =\u003e ({\n          config: {\n            client: 'mysql',\n            version: '5.7',\n            useNullAsDefault: true,\n            connection: {\n              host: '127.0.0.1',\n              user: 'root',\n              port: 3307,\n              password: 'root',\n              database: 'test2',\n            },\n            pool: {\n              min: 2,\n              max: 10,\n            },\n          },\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\n@Injectable()\nexport class PostService {\n  constructor(\n    @InjectConnection('db2Connection')\n    private knexConnection: Knex,\n  ) {}\n\n  async create(createPostDto: CreatePostDto) {\n    try {\n      const post = await this.knexConnection.table('posts').insert({\n        title: createPostDto.title,\n        description: createPostDto.description,\n      });\n\n      return post;\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n\n  async findAll() {\n    const posts = await this.knexConnection.table('posts');\n    return { posts };\n  }\n}\n```\n\nUsersService:\n\n```typescript\n@Injectable()\nexport class UsersService {\n  constructor(\n    @InjectConnection('db1Connection')\n    private knexConnection: Knex,\n  ) {}\n\n  async findAll() {\n    const users = await this.knexConnection.table('users');\n    return { users };\n  }\n\n  async create(createUserDto: CreateUserDto) {\n    try {\n      const users = await this.knexConnection.table('users').insert({\n        firstName: createUserDto.firstName,\n        lastName: createUserDto.lastName,\n      });\n\n      return { users };\n    } catch (err) {\n      throw new HttpException(err, HttpStatus.BAD_REQUEST);\n    }\n  }\n}\n```\n\n\nIn `knexfile.js` update with your database configuration settings:\n\n```javascript\nmodule.exports = {\n  development: {\n    client: 'mysql',\n    version: '5.7',\n    connection: {\n      name: 'db1Connection',\n      host: '127.0.0.1',\n      user: 'root',\n      port: 3306,\n      password: 'root',\n      database: 'test1',\n    },\n    migrations: {\n      directory: './migrations/users',\n      tableName: '[:name_file_migrations_users]',\n    },\n  },\n  developmentTwo: {\n    client: 'mysql',\n    version: '5.7',\n    connection: {\n      name: 'db2Connection',\n      host: '127.0.0.1',\n      user: 'root',\n      port: 3307,\n      password: 'root',\n      database: 'test2',\n    },\n    migrations: {\n      directory: './migrations/posts',\n      tableName: '[:name_file_migrations_posts]',\n    },\n  },\n};\n```\n\nRun Migrations:\n\n```bash\n$ npx knex migrate:latest --env development\n\n$ npx knex migrate:latest --env developmentTwo\n```\n\n## Create Migrations\n\n```bash\n  $ npx knex init\n  $ npx knex migrate:make [name_migrations]\n```\n\nExample file migrations:\n\n```typescript\nexports.up = function(knex) {\n  return knex.schema\n    .createTable('users', function(table) {\n      table.increments('id');\n      table.string('firstName', 255).notNullable();\n      table.string('lastName', 255).notNullable();\n    })\n    .createTable('products', function(table) {\n      table.increments('id');\n      table.string('name', 255).notNullable();\n      table.decimal('price').notNullable();\n    });\n};\n\nexports.down = function (knex) {\n  return knex.schema.dropTable('products').dropTable('users');\n};\n```\n\n## Run Migrations\n\nIn `knexfile.js` update with your config settings:\n\n```javascript\nmodule.exports = {\n  development: {\n    client: 'mysql',\n    connection: {\n      host: '127.0.0.1',\n      user: 'root',\n      password: 'root',\n      database: 'test',\n    },\n  },\n}\n```\n\nthen we run the following command from the terminal \n\n```bash\n  $ npx knex migrate:latest \n```\n\n## Create Seeds\n\n```bash\n  $ npx knex seed:make [name_seed]\n```\n\nExample file seeds\n\n```javascript\nexports.seed = function (knex) {\n  return knex('users')\n    .del()\n    .then(function () {\n      return knex('users').insert([\n        { id: 1, firstName: 'firstName#1', lastName: 'lastName#1' },\n        { id: 2, firstName: 'firstName#2', lastName: 'lastName#2' },\n        { id: 3, firstName: 'firstName#3', lastName: 'lastName#3' },\n      ]);\n    });\n};\n```\n\n## Run Seeds\n\n```\n  $ npx knex seed:run \n```\n\nFor more information on `Knex.js` see [here](http://knexjs.org/)\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-knexjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftony133%2Fnestjs-knexjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftony133%2Fnestjs-knexjs/lists"}