8.1.2 • Published 7 months ago
@willsoto/nestjs-objection v8.1.2
NestJS Objection
Description
Integrates Objection.js and Knex with Nest
Installation
pnpm add @willsoto/nestjs-objection
Note that Knex and Objection are peerDependencies
to make version management easier, so those must be installed separately
pnpm add knex objection
API
ObjectionModule.register
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
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<knex.Config>("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
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<HealthIndicatorResult> {
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
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<User[]> {
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.
@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 {}
8.1.2
7 months ago
8.1.1
7 months ago
7.2.0
2 years ago
8.1.0
2 years ago
8.0.0
2 years ago
6.1.0
2 years ago
6.3.0
2 years ago
6.2.0
2 years ago
7.0.0
2 years ago
7.1.0
2 years ago
6.0.0
3 years ago
6.0.0-next.1
3 years ago
5.1.0
3 years ago
5.0.0
3 years ago
4.0.2
4 years ago
4.0.1
4 years ago
4.0.0
4 years ago
3.2.0
4 years ago
3.1.0
4 years ago
3.0.0
4 years ago
2.2.0
4 years ago
2.1.0
4 years ago
2.0.0
5 years ago
1.1.0
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago
0.2.1
5 years ago