npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@willsoto/nestjs-objection

v8.1.2

Published

Objection module for NestJS

Downloads

4,841

Readme

NestJS Objection

npm version NPM downloads

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 {}