0.8.9 • Published 1 month ago
nestjs-drizzle v0.8.9
Nest.js Drizzle
Todo List
- mysql2
- postgresjs
- node-postgres
- supabase
- sqlite
- planetscale
- neon
- vercel postgres
- turso
npm install nestjs-drizzle
For schema
// drizzle/schemas/users.ts
import { pgTable, varchar, uuid, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: uuid('id').unique().primaryKey().defaultRandom(),
username: varchar('name', { length: 255 }).notNull(),
password: varchar('password', { length: 255 }).notNull(),
// more schema
});
// drizzle/schema.ts
export * from './schemas/users.ts'
app.module.ts
import { DrizzleModule } from 'nestjs-drizzle/mysql';
import * as schema from 'src/drizzle/schema'
@Module({
imports: [
DrizzleModule.forRoot({
isGlobal: true,
schema,
pool: {
host: process.env.HOST,
port: process.env.PORT,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
}
}),
]
})
for async registeration
import { DrizzleModule, registerAsync } from 'nestjs-drizzle/postgres';
import * as schema from 'src/drizzle/schema'
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
DrizzleModule.forAsyncRoot({
isGlobal: true,
useFactory: async (config: ConfigService) => ({
connection: config.get('DATABASE_URL'),
schema,
}),
inject: [ConfigService]
}),
]
})
I recomend to use
environment.d.ts
file for env type safety.
declare namespace NodeJS {
interface ProcessEnv {
[key: string]: string | undefined;
DATABASE_URL: string;
// add more environment variables and their types here
}
}
any.service.ts
import { Injectable } from "@nestjs/common";
import { DrizzleService } from "nestjs-drizzle/mysql";
import { users } from "./drizzle";
import { isNull } from "drizzle-orm";
@Injectable()
export class AppService {
constructor(private readonly drizzle: DrizzleService) {}
async getManyUsers() {
const users = await this.drizzle.get(users, {
id: users.id,
username: users.username,
});
return users;
}
async getOneUser(id: string) {
const [user] = await this.drizzle
.get(users, {
id: users.id,
username: users.username,
})
.where(eq(users.id, id));
return user;
}
}
Other helper functions
// values is basicly set
this.drizzle.insert(users, values);
this.drizzle.update(users, values).where(eq(users.id, 10));
// Increment | Decrement
this.drizzle.update(users, {
age: increment(users.age, 20)
}).where(eq(users.id, 10));
this.drizzle.delete(users).where(eq(users.id, 10));
this.drizzle.query("users").findFirst();
if you need to other features
this.drizzle.db; // main db
this.drizzle.delete(users).where(eq(users.id, 10)).prepare();
this.drizzle.insert(users, values).$dynamic;
Using query
// first make DrizzleService to type safe
import * as schema from "src/drizzle/schema";
import { DrizzleService } from "nestjs-drizzle/mysql";
@Injectable()
export class AppService {
constructor(
private readonly drizzle: DrizzleService<typeof schema> // <- put here <typeof schema>
) {}
getUsers() {
this.drizzle.query("users").findMany({
columns: {
id: true,
name: true,
},
limit: 10,
});
}
}
Bugs showcase
npm ERR! Found: reflect-metadata@0.2.1
in package.json reflect-metadata
change version to ^0.1.14
0.8.9
1 month ago
0.8.8
1 month ago
0.8.6
3 months ago
0.8.5
3 months ago
0.8.4
3 months ago
0.7.7
3 months ago
0.8.1
3 months ago
0.8.0
3 months ago
0.8.3
3 months ago
0.8.2
3 months ago
0.7.5
4 months ago
0.7.2
4 months ago
0.7.0
4 months ago
0.6.0
5 months ago
0.5.7
5 months ago
0.5.6
5 months ago
0.5.4
5 months ago
0.5.2
5 months ago
0.0.4
5 months ago