{"id":22312251,"url":"https://github.com/willsoto/nestjs-objection","last_synced_at":"2025-04-12T03:52:42.613Z","repository":{"id":34174212,"uuid":"168998251","full_name":"willsoto/nestjs-objection","owner":"willsoto","description":"NestJS module for Objection","archived":false,"fork":false,"pushed_at":"2025-04-07T12:11:44.000Z","size":8692,"stargazers_count":141,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T03:52:36.062Z","etag":null,"topics":["database","knex","knexjs","nestjs","objection-orm","objectionjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willsoto.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["willsoto"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-02-03T21:57:55.000Z","updated_at":"2025-04-07T12:11:46.000Z","dependencies_parsed_at":"2023-12-17T23:25:28.984Z","dependency_job_id":"6d245686-af08-4c4e-a2f4-d1bf5da9bcd7","html_url":"https://github.com/willsoto/nestjs-objection","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-objection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-objection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-objection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-objection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willsoto","download_url":"https://codeload.github.com/willsoto/nestjs-objection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514209,"owners_count":21116899,"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":["database","knex","knexjs","nestjs","objection-orm","objectionjs"],"created_at":"2024-12-03T21:35:57.411Z","updated_at":"2025-04-12T03:52:42.589Z","avatar_url":"https://github.com/willsoto.png","language":"TypeScript","funding_links":["https://github.com/sponsors/willsoto"],"categories":[],"sub_categories":[],"readme":"# NestJS Objection\n\n[![npm version](https://badge.fury.io/js/%40willsoto%2Fnestjs-objection.svg)](https://badge.fury.io/js/%40willsoto%2Fnestjs-objection)\n[![NPM downloads](https://img.shields.io/npm/dt/@willsoto/nestjs-objection.svg)](https://www.npmjs.com/package/@willsoto/nestjs-objection)\n![](https://github.com/willsoto/nestjs-objection/workflows/tests/badge.svg)\n\n\u003c!-- prettier-ignore-start --\u003e\n\n\u003c!-- toc --\u003e\n\n- [Description](#description)\n- [Installation](#installation)\n- [API](#api)\n    + [`ObjectionModule.register`](#objectionmoduleregister)\n    + [`ObjectionModule.registerAsync`](#objectionmoduleregisterasync)\n- [Configuration](#configuration)\n- [Examples](#examples)\n  * [Injecting the connection](#injecting-the-connection)\n  * [Injecting an objection model](#injecting-an-objection-model)\n  * [Multiple connections](#multiple-connections)\n\n\u003c!-- tocstop --\u003e\n\n\u003c!-- prettier-ignore-end --\u003e\n\n## Description\n\nIntegrates [Objection.js](https://vincit.github.io/objection.js/) and [Knex](https://knexjs.org/) with [Nest](https://nestjs.com/)\n\n## Installation\n\n```bash\npnpm add @willsoto/nestjs-objection\n```\n\n_Note that Knex and Objection are `peerDependencies` to make version management easier, so those must be installed separately_\n\n```bash\npnpm add knex objection\n```\n\n## API\n\n#### `ObjectionModule.register`\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { ObjectionModule } from \"@willsoto/nestjs-objection\";\nimport { BaseModel } from \"./base\";\nimport { User } from \"./user\";\n\n@Module({\n  imports: [\n    ObjectionModule.register({\n      // You can specify a custom BaseModel\n      // If none is provided, the default Model will be used\n      // https://vincit.github.io/objection.js/#models\n      Model: BaseModel,\n      config: {\n        client: \"sqlite3\",\n        useNullAsDefault: true,\n        connection: {\n          filename: \"./example.sqlite\",\n        },\n      },\n    }),\n\n    //Register your objection models so it can be provided when needed.\n    ObjectionModule.forFeature([User]),\n  ],\n  exports: [ObjectionModule],\n})\nexport class DatabaseModule {}\n```\n\n#### `ObjectionModule.registerAsync`\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { ObjectionModule } from \"@willsoto/nestjs-objection\";\nimport knex from \"knex\";\nimport { knexSnakeCaseMappers } from \"objection\";\nimport { ConfigModule, ConfigService } from \"../config\";\nimport { BaseModel } from \"./base\";\nimport { User } from \"./user\";\n\n@Module({\n  imports: [\n    ObjectionModule.registerAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory(config: ConfigService) {\n        return {\n          // You can specify a custom BaseModel\n          // If none is provided, the default Model will be used\n          // https://vincit.github.io/objection.js/#models\n          Model: BaseModel,\n          config: {\n            ...config.get\u003cknex.Config\u003e(\"database\"),\n            ...knexSnakeCaseMappers(),\n          },\n        };\n      },\n    }),\n    //Register your objection models so it can be provided when needed.\n    ObjectionModule.forFeature([User]),\n  ],\n  exports: [ObjectionModule],\n})\nexport class DatabaseModule {}\n```\n\n## Configuration\n\n| Name     | Type     | Required | Default                 | Notes                                                           |\n| -------- | -------- | -------- | ----------------------- | --------------------------------------------------------------- |\n| `name`   | `string` | `false`  | `KNEX_CONNECTION` token | This is **required** only if you are using multiple connections |\n| `Model`  | `Object` | `false`  | `objection.Model`       |                                                                 |\n| `config` | `Object` | `true`   |                         |                                                                 |\n\n## Examples\n\n### Injecting the connection\n\n```ts\nimport { Inject, Injectable } from \"@nestjs/common\";\nimport {\n  HealthCheckError,\n  HealthIndicator,\n  HealthIndicatorResult,\n} from \"@nestjs/terminus\";\nimport { Connection, KNEX_CONNECTION } from \"@willsoto/nestjs-objection\";\n\n@Injectable()\nexport class PrimaryDatabaseHealthIndicator extends HealthIndicator {\n  constructor(@Inject(KNEX_CONNECTION) public connection: Connection) {}\n\n  async ping(key: string = \"db-primary\"): Promise\u003cHealthIndicatorResult\u003e {\n    try {\n      await this.connection.raw(\"SELECT 1\");\n      return super.getStatus(key, true);\n    } catch (error) {\n      const status = super.getStatus(key, false, { message: error.message });\n      throw new HealthCheckError(\"Unable to connect to database\", status);\n    }\n  }\n}\n```\n\n### Injecting an objection model\n\n```ts\nimport { Inject, Injectable } from \"@nestjs/common\";\nimport { User } from \"./user\";\n\n@Injectable()\nexport class UserService {\n  constructor(@Inject(User) private readonly userModel: typeof User) {}\n\n  async getUsers(): Promise\u003cUser[]\u003e {\n    return await this.userModel.query();\n  }\n}\n```\n\n### Multiple connections\n\nWhen using multiple connections, you must **name** each connection when registering it.\nOtherwise subsequent connections will override the previous ones.\n\n```ts\n@Module({\n  imports: [\n    ObjectionModule.registerAsync({\n      // You must provide a name for the connection\n      name: \"connection1\",\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory(config: ConfigService) {\n        return {\n          // You must provide a name for the connection here as well...\n          name: \"connection1\",\n          Model: BaseModel1,\n          config: {\n            client: \"sqlite3\",\n            useNullAsDefault: true,\n            connection: {\n              filename: \"./testing1.sqlite\",\n            },\n          },\n        };\n      },\n    }),\n    ObjectionModule.register({\n      // You must provide a name for the connection\n      name: \"connection2\",\n      Model: BaseModel2,\n      config: {\n        client: \"sqlite3\",\n        useNullAsDefault: true,\n        connection: {\n          filename: \"./testing2.sqlite\",\n        },\n      },\n    }),\n  ],\n})\nexport class DatabaseModule {}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsoto%2Fnestjs-objection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillsoto%2Fnestjs-objection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsoto%2Fnestjs-objection/lists"}