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

@ockonor/nest-drizzle

v0.2.1

Published

Integration of Drizzle ORM for Nestjs Framework

Downloads

4

Readme

This module was created to integrate Drizzle ORM with NestJS with all serverfull drivers:

'postgres-js' | 'mysql2' | 'supabase' | 'neon' | 'planetscale' | 'sqlite3'

Installation

To install this project:

npm i @ockonor/nest-drizzle 
npm i -D drizzle-kit

Create the drizzle-kit configuration in your project root folder

// <your project root>/drizzle.config.ts
import type { Config } from "drizzle-kit";
 
export default {
  schema: "./src/schema.ts",
  out: "./drizzle",
} satisfies Config;

Create your schema in your project src folder

See documentation here

// <your project root>/src/schema.ts
import { serial, text, timestamp, pgTable } from 'drizzle-orm/pg-core';

export const user = pgTable('user', {
  id: serial('id'),
  name: text('name'),
  email: text('email'),
  password: text('password'),
  role: text('role').$type<'admin' | 'customer'>(),
  createdAt: timestamp('created_at'),
  updatedAt: timestamp('updated_at'),
});

Add generate script in package.json and execute drizzle-kit documentation here

"scripts": {
    "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
    "build": "tsc",
    "prepare": "npm run build",
    "format": "prettier --write \"src/**/*.ts\"",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:e2e": "jest --config ./test/jest-e2e.json",
  + "generate": "drizzle-kit generate:pg"
  },
# Update you schema
npm run generate

Configuration

Modules

Methods:

  • register()
  • registerAsync()
  • forRoot()
  • forRootAsync()
// in your module
import { NestDrizzleModule } from '@ockonor/nest-drizzle';

@Module({
  controllers: [NestDrizzleClientController],
  imports: [
    NestDrizzleModule.registerAsync({
      useFactory: () => {
        return {
          driver: 'postgres-js', // 'postgres-js'|'mysql2'|'supabase'|'neon'|'planetscale' |'sqlite3'
          url: 'postgres://<user>:<password>@<host>:<port>/<database>', // postgres://<user>:<password>@<host>:<port>/<database>, ./<your file>.sqlite
          ...
        };
      },
    }),
  ],
})
export class ...

Types

Don't forget to import the good types in your controllers/services

// if you have driver 'postgres-js'|'supabase'|'neon'
import { PostgresJsDb, DRIZZLE_ORM } from '@ockonor/nest-drizzle';
// or if you have driver 'mysql2'|'planetscale'
import { MySql2Db, DRIZZLE_ORM } from '@ockonor/nest-drizzle';
// or if you have the driver 'sqlite3'
import { SQLite3Db, DRIZZLE_ORM } from '@ockonor/nest-drizzle';

Controllers / Services

See documentation here

// in your controller or service
import { Controller, Get, Inject } from '@nestjs/common';
import { user } from '../../schema';
import { PostgresJsDb, DRIZZLE_ORM } from '@ockonor/nest-drizzle';

@Controller()
export class NestDrizzleClientController {
  // private readonly db: PostgresJsDb or MySql2Db or SQLite3Db
  constructor(@Inject(DRIZZLE_ORM) private readonly db: PostgresJsDb) {}

  @Get()
  async index() {
    const allUsers = await this.db.select().from(user);
    return allUsers;
  }
}

TODO

  • [ ] Command for migration
  • [ ] Support another serverless databases + sqlite providers
  • [X] Add forRoot and forRootAsync