{"id":19435425,"url":"https://github.com/nestjsplus/knex","last_synced_at":"2025-04-24T21:30:30.849Z","repository":{"id":44069931,"uuid":"210061506","full_name":"nestjsplus/knex","owner":"nestjsplus","description":"A Module for Utilizing Knex.js with NestJS","archived":false,"fork":false,"pushed_at":"2023-01-11T01:45:56.000Z","size":513,"stargazers_count":33,"open_issues_count":22,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-03-03T18:24:59.529Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/nestjsplus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-21T22:37:31.000Z","updated_at":"2023-02-22T11:03:50.000Z","dependencies_parsed_at":"2023-02-08T21:45:17.733Z","dependency_job_id":null,"html_url":"https://github.com/nestjsplus/knex","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsplus%2Fknex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsplus%2Fknex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsplus%2Fknex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjsplus%2Fknex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nestjsplus","download_url":"https://codeload.github.com/nestjsplus/knex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223968411,"owners_count":17233445,"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":[],"created_at":"2024-11-10T15:06:23.916Z","updated_at":"2024-11-10T15:06:24.358Z","avatar_url":"https://github.com/nestjsplus.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://nestjs.com/img/logo_text.svg\" width=\"150\" alt=\"Nest Logo\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003ch3 align=\"center\"\u003eA module for Utilizing Knex.js with NestJS\u003c/h3\u003e\n\u003ch4 align=\"center\"\u003eGenerated by @nestjsplus/dyn-schematics\u003c/h4\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://nestjs.com\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/built%20with-NestJs-red.svg\" alt=\"Built with NestJS\"\u003e\n  \u003c/a\u003e\n  \u003cimg src=\"https://badge.fury.io/js/%40nestjsplus%2Fknex.svg\" alt=\"npm version\" height=\"18\"\u003e\n  \u003ca href=\"https://github.com/nestjsplus/dyn-schematics\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Built%20with-%40nestjsplus%2Fdyn--schematics-brightgreen\" alt=\"Built with @nestjsplus/dyn-schematics\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n### About\n\nThis module provides a thin wrapper around [Knex.js](http://knexjs.org).  Knex.js is primarily a *Query Builder* that works with multiple databases.\n\nThe module was generated using [@nestjsplus/dyn-schematics](https://github.com/nestjsplus/dyn-schematics), a schematics package for NestJS that generates *dynamic modules* using the pattern [described here](https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370). There's a complete tutorial on [using the custom schematics here](https://dev.to/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an).\n\n### Installation\n\n```bash\nnpm install @nestjsplus/knex\n```\n\n(or yarn equivalent)\n\n### Quick Start\n\nTo configure your DB connection, import the `KnexModule` module using the familiar `register()` / `registerAsync()` pattern. See the [example repo](https://github.com/nestjsplus/knex-cats) for an example. Basically, you configure the module with a Knes.js `connection` object, which maps directly to the connection options [in the Knex.js docs](http://knexjs.org/#Installation-client).\n\nOnce configured, inject the `SINGLETON` knex api interface object into any service using the `KNEX_CONNECTION` injection token.\n\nFor example, your `AppModule` might look like this (full example in the [sample repo](https://github.com/nestjsplus/knex-cats)):\n\n```typescript\n// src/app.module.ts\nimport { Module } from '@nestjs/common';\nimport { AppController } from './app.controller';\nimport { AppService } from './app.service';\nimport { ConfigModule } from './config/config.module';\nimport { ConfigService } from './config/config.service';\nimport { KnexModule } from '@nestjsplus/knex';\n\n@Module({\n  imports: [\n    KnexModule.registerAsync({\n      useExisting: ConfigService,\n    }),\n    ConfigModule,\n  ],\n  controllers: [AppController],\n  providers: [AppService],\n})\nexport class AppModule {}\n```\n\nNow you have access to a `KNEX_CONNECTION` token that is associated with the Knex.js API, which you can inject into any provider, and use the resulting Knex.js API object directly. For example, you might do this:\n\n```typescript\n// src/app.service.ts\nimport { Inject, Injectable } from '@nestjs/common';\nimport { KNEX_CONNECTION } from '@nestjsplus/knex';\n\n@Injectable()\nexport class AppService {\n  constructor(@Inject(KNEX_CONNECTION) private readonly knex) {}\n\n  async getCats() {\n    return await this.knex('cats')\n      .select('*')\n      .from('cats');\n  }\n  ...\n```\n\nHere, you've injected the connection as a local property of the service class, and can access any of the Knex.js API through that property (e.g., `return await this.knex('cats').select('*').from('cats')`, where `knex` represents your Knex.js API object).\n\n### Configuring connection options\n\nI'm not showing the `ConfigService` in the `AppModule` above, but it's just an _injectable_ that implements the `KnexOptionsFactory` interface, meaning it has methods to return a `KnexOptions` object. A `KnexOptions` object looks like:\n\n```json\n{\n  client: 'pg',\n  debug: true,\n  connection: {\n    host: 'localhost',\n    user: 'john',\n    password: 'password',\n    database: 'nest',\n    port: 5432,\n  },\n}\n```\n\nYou can use any of the following methods to provide the `KnexOptions` to the module. These follow the [usual patterns for custom providers](https://docs.nestjs.com/fundamentals/custom-providers):\n\n- `register()`: pass a plain JavaScript object\n- `registerAsync()`: pass a dynamic object via:\n  - `useFactory`: supply a factory function to return the object; the factory should implement the appropriate [options factory](https://github.com/nestjsplus/knex/blob/master/src/interfaces/knex-options-factory.interface.ts) interface\n  - `useClass`: bind to a provider/service that supplies the object; that service should implement the appropriate [options factory](https://github.com/nestjsplus/knex/blob/master/src/interfaces/knex-options-factory.interface.ts) interface\n  - `useExisting`: bind to an existing (provided elsewhere) provider/service to supply the object; that service should implement the appropriate [options factory](https://github.com/nestjsplus/knex/blob/master/src/interfaces/knex-options-factory.interface.ts) interface\n\n### Connection availability on application startup\n\nThe `KNEX_CONNECTION` is an [asynchronous provider](https://docs.nestjs.com/fundamentals/async-providers). This means that the Nest application bootstrap process (specifically, the Dependency Injection phase) won't complete until the DB connection is made. So your app, once it bootstraps, is guaranteed to have a DB connection via the `KNEX_CONNECTION` injection token. Note that asynchronous providers must be injected with the `@Inject()` decorator instead of normal constructor injection (again, see the [example](https://github.com/nestjsplus/knex-cats)).\n\n### Working Example\n\nSee [knex-cats](https://github.com/nestjsplus/knex-cats) for a full example. It shows an example of using the `KNEX_CONNECTION`, a service that uses it to access a PostgreSQL database, and includes a few of the Knex.js Query Builder features.\n\n### About @nestjsplus/dyn-schematics\n\n[Nest Dynamic Package Generator Schematics](https://github.com/nestjsplus/dyn-schematics) generates a starter template for building NestJS dynamic packages.  It uses the `@nestjs/cli` core package, and provides customized schematics for generating modular NestJS applications.  See [here](https://github.com/nestjsplus/dyn-schematics) for the full set of available schematics, and documentation.  Read these articles for more background:\n\n- [Advanced NestJS: How to build completely dynamic NestJS modules](https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370) - covers the design pattern used by this module\n- [Build a NestJS Module for Knex.js (or other resource-based libraries) in 5 Minutes](https://dev.to/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an) - covers a custom schematic that can be used to generate a module template based on this pattern\n\n### Change Log\n\nSee [Changelog](CHANGELOG.md) for more information.\n\n### Contributing\n\nContributions welcome! See [Contributing](CONTRIBUTING.md).\n\n### Author\n\n**John Biundo (Y Prospect on [Discord](https://discord.gg/G7Qnnhy))**\n\n### License\n\nLicensed under the MIT License - see the [LICENSE](LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjsplus%2Fknex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnestjsplus%2Fknex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjsplus%2Fknex/lists"}