knaadh / nestjs-drizzle

A NestJS module for integrating DrizzleORM with Postgres, MySQL, SQLite, Turso and Planetscale drivers
MIT License
114 stars 6 forks source link

Query field is an empty object #3

Closed msyavuz closed 6 months ago

msyavuz commented 6 months ago

drizzle object has a query property but its an empty object. Typescript shows query as having users but in runtime i get an undefined error for findFirst method. All other methods including select work, just the query field is the problem.

// users.service.ts
import { LibSQLDatabase } from "drizzle-orm/libsql";
import * as schema from "./db/users.schema";

export class UsersService {
    constructor(@Inject("DB") private drizzle: LibSQLDatabase<typeof schema>) {}
    async findOne(id: number) {
      console.log(this.drizzle);
      return await this.drizzle.query.users.findFirst({
          where: (users) => eq(users.id, id),
    });
}

I am not sure if my schema definition or injection has anything to do with it but here they are:

// users.schema.ts

import {
    sqliteTable,
    text,
    integer,
    uniqueIndex,
} from "drizzle-orm/sqlite-core";

export const users = sqliteTable(
    "users",
    {
        id: integer("id").primaryKey(),
        email: text("email").notNull(),
        username: text("username").notNull(),
        hashedPass: text("hashed_password").notNull(),
    },
    (users) => ({ emailIdx: uniqueIndex("email_index").on(users.email) }),
);

And this is the imported part:

// app.module.ts

DrizzleTursoModule.register({
    tag: "DB",
    turso: {
        config: {
            url: process.env.DB_URL,
            authToken: process.env.DB_TOKEN,
        },
    },
    config: {
        schema: [
            "./users/db/users.schema.ts",
        ],
    },
}),
mithleshjs commented 6 months ago

Try passing schema in the config as shown below

// app.module.ts
import * as schema from "./db/users.schema";

DrizzleTursoModule.register({
    tag: "DB",
    turso: {
        config: {
            url: process.env.DB_URL,
            authToken: process.env.DB_TOKEN,
        },
    },
    config: {
        schema: { ...schema },
      },
}),
msyavuz commented 6 months ago

Thank you, this works