Replies: 11 comments 1 reply
-
Oh! You mean the ORM bindings to their entities/repository classes. I agree that would be incredibly neat. |
Beta Was this translation helpful? Give feedback.
-
Yes right? Anyway I had issues even with the first answer, I don't remember the error honestly. Would you like to share the code? It may be helpful for some people, including me! |
Beta Was this translation helpful? Give feedback.
-
I just wrote a very simple provider. I gave this some more thought and I actually would not prefer to have my ORM tied to my entities. I develop in monorepos so I can have import { Injectable, OnModuleInit } from '@nestjs/common';
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';
import * as schema from './schemas';
import { Pool } from 'pg';
@Injectable()
export class DrizzleProvider implements OnModuleInit {
db: NodePgDatabase<typeof schema>;
constructor() {}
async onModuleInit() {
const client = new Pool({
connectionString: process.env.DATABASE_URL,
});
await client.connect();
const db = drizzle(client, { schema });
this.db = db;
// migrate(this.db, { migrationsFolder: './src/db/migrations' });
}
} What im doing is just extending each Entity to implement the Drizzle table class. import { User } from 'src/database/schemas';
export class UserEntity implements User {
id: string;
username: string;
usernameId: string;
displayName: string;
account?: AccountEntity;
createdAt: Date;
updatedAt: Date;
} |
Beta Was this translation helpful? Give feedback.
-
Definately wanted. nestJs is used a lot |
Beta Was this translation helpful? Give feedback.
-
Dear @Rykuno , can you provide more details on how you got drizzle to work with nestjs and sqlite? can you provide a minimal reproduction repository? All the best and thanks for your help, |
Beta Was this translation helpful? Give feedback.
-
+1 |
Beta Was this translation helpful? Give feedback.
-
My setup is as follows @johannesschobel
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import * as schema from './schemas';
import { customAlphabet } from 'nanoid';
@Injectable()
export class DrizzleService implements OnApplicationBootstrap {
db: NodePgDatabase<typeof schema>;
async onApplicationBootstrap() {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
const client = await pool.connect();
const db = drizzle(client, { schema });
this.db = db;
}
createId() {
const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 12);
return nanoid();
}
} The |
Beta Was this translation helpful? Give feedback.
-
For anyone coming here using mysql2. The docs state you initiate a connection like this: import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
const poolConnection = mysql.createPool({
host: "host",
user: "user",
database: "database",
...
});
const db = drizzle(poolConnection); That did not work for me in combination with nestjs. I kept getting // use either
import { createPool } from "mysql2/promise"
const connection = createPool({
host: "host",
user: "user",
database: "database",
...
});
// or
import * as mysql from "mysql2/promise"
const connection = mysql.createPool({
host: "host",
user: "user",
database: "database",
...
}); |
Beta Was this translation helpful? Give feedback.
-
Hello, @Rykuno! I have one question: when creating a query with leftJoin and putting the response in the If I am doing something wrong, please point out my mistake. Or is there any advice on how to resolve the problem? I am using the NodePgDatabase type when defining the db variable. But if I change type to Here is my query:
My schema:
|
Beta Was this translation helpful? Give feedback.
-
The thing that stops me from trying out drizzle is lack of official documentation/package about nest integration (either by nest or drizzle itself) |
Beta Was this translation helpful? Give feedback.
-
i working on this package nestjs-drizzle right now only supports mysql and postgres. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe want to want
it would be great to have a package like "nestjs-keysely" for drizzle
Beta Was this translation helpful? Give feedback.
All reactions