TypeError: Seems like the schema generic is missing - did you forget to add it to your DB type?

How can I solve the following generic error relating to the db.query.users in my code below?
Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?"> | { [x: string]:RelationalQueryBuilder<ExtractTablesWithRelations<any>, { ...; }>; }'.

Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">'.ts(2339)
Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?"> | { [x: string]:RelationalQueryBuilder<ExtractTablesWithRelations<any>, { ...; }>; }'.

Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">'.ts(2339)
I'm trying to create an adapter that accepts a generic schema to instantiate a PostgresJsDatabase instance, or colloquially db. I've created the schema that is needed for this adapter, but how can I satisfy typescript when running db.query.user.findFirst()?
import { PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js";
import { Sql } from "postgres";
import * as schema from "./schema";
import { eq } from "drizzle-orm";

export function postgresAdapter<T extends typeof schema = any>(
sql: Sql,
schema: T
) {
const db = drizzle(sql, { schema });

const { users } = schema;

// This function works below
async function insertUser(
user: typeof users.$inferInsert
): Promise<void> {
await db
.insert(users)
.values(user)
.onConflictDoNothing({ target: users.id });
}

// I'm getting an error here at db.query
async function getUserById(
id: string
): Promise<typeof schema.users.$inferSelect | null> {
const user = await db.query.users.findFirst({
^ // Property 'users' does
// not exist on type
where: (users: any, { eq }) => eq(users.id, id),
});
return user || null;
}
}
import { PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js";
import { Sql } from "postgres";
import * as schema from "./schema";
import { eq } from "drizzle-orm";

export function postgresAdapter<T extends typeof schema = any>(
sql: Sql,
schema: T
) {
const db = drizzle(sql, { schema });

const { users } = schema;

// This function works below
async function insertUser(
user: typeof users.$inferInsert
): Promise<void> {
await db
.insert(users)
.values(user)
.onConflictDoNothing({ target: users.id });
}

// I'm getting an error here at db.query
async function getUserById(
id: string
): Promise<typeof schema.users.$inferSelect | null> {
const user = await db.query.users.findFirst({
^ // Property 'users' does
// not exist on type
where: (users: any, { eq }) => eq(users.id, id),
});
return user || null;
}
}
3 Replies
Angelelz
Angelelz15mo ago
You are using the schema parameter passed to the postgresAdapter function. That parameter is typed as any. Are you planning to use this function to pass it different schemas? What is the purpose of passing the schama as a parameter?
Paul
PaulOP15mo ago
Yeah I was looking to allow users of this function to pass in their drizzle schema that should look a certain way for me to query. But I also created the same schema myself as a fallback. On a high level, I'm trying to create an adapter to update a user's database. But I think defining the essential elements of the schema myself may suffice instead of allowing users to pass it in. Anyways, I think it's okay to ignore this question if defining the generic of a schema is bit complicated. Thank you
Angelelz
Angelelz15mo ago
I'm not sure, but if in this line you delete the default type parameter it might work:
export function postgresAdapter<T extends typeof schema>(
...
export function postgresAdapter<T extends typeof schema>(
...
Want results from more Discord servers?
Add your server