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

@webstyle/knex

v1.0.2

Published

<h1 align="center"></h1>

Downloads

2

Readme

About

This module provides a thin wrapper around Knex.js. Knex.js is primarily a Query Builder that works with multiple databases.

The module was generated using @nestjsplus/dyn-schematics, a schematics package for NestJS that generates dynamic modules using the pattern described here. There's a complete tutorial on using the custom schematics here.

Installation

npm install @nestjsplus/knex

(or yarn equivalent)

Quick Start

To configure your DB connection, import the KnexModule module using the familiar register() / registerAsync() pattern. See the example repo 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.

Once configured, inject the SINGLETON knex api interface object into any service using the KNEX_CONNECTION injection token.

For example, your AppModule might look like this (full example in the sample repo):

// src/app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { KnexModule } from '@nestjsplus/knex';

@Module({
  imports: [
    KnexModule.registerAsync({
      useExisting: ConfigService,
    }),
    ConfigModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Now 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:

// src/app.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { KNEX_CONNECTION } from '@nestjsplus/knex';

@Injectable()
export class AppService {
  constructor(@Inject(KNEX_CONNECTION) private readonly knex) {}

  async getCats() {
    return await this.knex('cats')
      .select('*')
      .from('cats');
  }
  ...

Here, 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).

Configuring connection options

I'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:

{
  client: 'pg',
  debug: true,
  connection: {
    host: 'localhost',
    user: 'john',
    password: 'password',
    database: 'nest',
    port: 5432,
  },
}

You can use any of the following methods to provide the KnexOptions to the module. These follow the usual patterns for custom providers:

  • register(): pass a plain JavaScript object
  • registerAsync(): pass a dynamic object via:
    • useFactory: supply a factory function to return the object; the factory should implement the appropriate options factory interface
    • useClass: bind to a provider/service that supplies the object; that service should implement the appropriate options factory interface
    • useExisting: bind to an existing (provided elsewhere) provider/service to supply the object; that service should implement the appropriate options factory interface

Connection availability on application startup

The KNEX_CONNECTION is an asynchronous provider. 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).

Working Example

See 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.

About @nestjsplus/dyn-schematics

Nest Dynamic Package Generator 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 for the full set of available schematics, and documentation. Read these articles for more background:

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

John Biundo (Y Prospect on Discord)

License

Licensed under the MIT License - see the LICENSE file for details.