https://github.com/willsoto/nestjs-objection
NestJS module for Objection
https://github.com/willsoto/nestjs-objection
database knex knexjs nestjs objection-orm objectionjs
Last synced: 6 months ago
JSON representation
NestJS module for Objection
- Host: GitHub
- URL: https://github.com/willsoto/nestjs-objection
- Owner: willsoto
- License: apache-2.0
- Created: 2019-02-03T21:57:55.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-04-07T12:11:44.000Z (6 months ago)
- Last Synced: 2025-04-12T03:52:36.062Z (6 months ago)
- Topics: database, knex, knexjs, nestjs, objection-orm, objectionjs
- Language: TypeScript
- Homepage:
- Size: 8.29 MB
- Stars: 141
- Watchers: 2
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# NestJS Objection
[](https://badge.fury.io/js/%40willsoto%2Fnestjs-objection)
[](https://www.npmjs.com/package/@willsoto/nestjs-objection)
- [Description](#description)
- [Installation](#installation)
- [API](#api)
+ [`ObjectionModule.register`](#objectionmoduleregister)
+ [`ObjectionModule.registerAsync`](#objectionmoduleregisterasync)
- [Configuration](#configuration)
- [Examples](#examples)
* [Injecting the connection](#injecting-the-connection)
* [Injecting an objection model](#injecting-an-objection-model)
* [Multiple connections](#multiple-connections)## Description
Integrates [Objection.js](https://vincit.github.io/objection.js/) and [Knex](https://knexjs.org/) with [Nest](https://nestjs.com/)
## Installation
```bash
pnpm add @willsoto/nestjs-objection
```_Note that Knex and Objection are `peerDependencies` to make version management easier, so those must be installed separately_
```bash
pnpm add knex objection
```## API
#### `ObjectionModule.register`
```typescript
import { Module } from "@nestjs/common";
import { ObjectionModule } from "@willsoto/nestjs-objection";
import { BaseModel } from "./base";
import { User } from "./user";@Module({
imports: [
ObjectionModule.register({
// You can specify a custom BaseModel
// If none is provided, the default Model will be used
// https://vincit.github.io/objection.js/#models
Model: BaseModel,
config: {
client: "sqlite3",
useNullAsDefault: true,
connection: {
filename: "./example.sqlite",
},
},
}),//Register your objection models so it can be provided when needed.
ObjectionModule.forFeature([User]),
],
exports: [ObjectionModule],
})
export class DatabaseModule {}
```#### `ObjectionModule.registerAsync`
```typescript
import { Module } from "@nestjs/common";
import { ObjectionModule } from "@willsoto/nestjs-objection";
import knex from "knex";
import { knexSnakeCaseMappers } from "objection";
import { ConfigModule, ConfigService } from "../config";
import { BaseModel } from "./base";
import { User } from "./user";@Module({
imports: [
ObjectionModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory(config: ConfigService) {
return {
// You can specify a custom BaseModel
// If none is provided, the default Model will be used
// https://vincit.github.io/objection.js/#models
Model: BaseModel,
config: {
...config.get("database"),
...knexSnakeCaseMappers(),
},
};
},
}),
//Register your objection models so it can be provided when needed.
ObjectionModule.forFeature([User]),
],
exports: [ObjectionModule],
})
export class DatabaseModule {}
```## Configuration
| Name | Type | Required | Default | Notes |
| -------- | -------- | -------- | ----------------------- | --------------------------------------------------------------- |
| `name` | `string` | `false` | `KNEX_CONNECTION` token | This is **required** only if you are using multiple connections |
| `Model` | `Object` | `false` | `objection.Model` | |
| `config` | `Object` | `true` | | |## Examples
### Injecting the connection
```ts
import { Inject, Injectable } from "@nestjs/common";
import {
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from "@nestjs/terminus";
import { Connection, KNEX_CONNECTION } from "@willsoto/nestjs-objection";@Injectable()
export class PrimaryDatabaseHealthIndicator extends HealthIndicator {
constructor(@Inject(KNEX_CONNECTION) public connection: Connection) {}async ping(key: string = "db-primary"): Promise {
try {
await this.connection.raw("SELECT 1");
return super.getStatus(key, true);
} catch (error) {
const status = super.getStatus(key, false, { message: error.message });
throw new HealthCheckError("Unable to connect to database", status);
}
}
}
```### Injecting an objection model
```ts
import { Inject, Injectable } from "@nestjs/common";
import { User } from "./user";@Injectable()
export class UserService {
constructor(@Inject(User) private readonly userModel: typeof User) {}async getUsers(): Promise {
return await this.userModel.query();
}
}
```### Multiple connections
When using multiple connections, you must **name** each connection when registering it.
Otherwise subsequent connections will override the previous ones.```ts
@Module({
imports: [
ObjectionModule.registerAsync({
// You must provide a name for the connection
name: "connection1",
imports: [ConfigModule],
inject: [ConfigService],
useFactory(config: ConfigService) {
return {
// You must provide a name for the connection here as well...
name: "connection1",
Model: BaseModel1,
config: {
client: "sqlite3",
useNullAsDefault: true,
connection: {
filename: "./testing1.sqlite",
},
},
};
},
}),
ObjectionModule.register({
// You must provide a name for the connection
name: "connection2",
Model: BaseModel2,
config: {
client: "sqlite3",
useNullAsDefault: true,
connection: {
filename: "./testing2.sqlite",
},
},
}),
],
})
export class DatabaseModule {}
```