Error using the query param

So I'm getting a '''Property 'staff' does not exist on type 'DrizzleTypeError<...>'" when i try to run the query
''' const user = await db.query.staff.findMany({ where: eq(staff.email, email.toString()) }) '''' , my staff table exists in the schema and its is impoeted in the file
8 Replies
Angelelz
Angelelz•11mo ago
Can you show how how you're instantiating the db object?
sten
sten•10mo ago
@Angelelz, I am seeing many posts of this for some reason.
import * as schema from '$lib/db/schema';
import { connectD1 } from 'wrangler-proxy';

export const cloudflare: Handle = async ({ event, resolve }) => {
if (dev) {
event.locals.D1 = drizzle(connectD1('D1', { hostname }), { schema });
} else if (event?.platform?.env && !dev) {
event.locals.D1 = drizzle(event.platform?.env?.D1, { schema });
}
return await resolve(event);
};

export const users = createTable('user', {
id: text('id')
.notNull()
.$defaultFn(() => uuidv4())
.primaryKey(),
export const usersRelations = relations(users, ({ one }) => ({
applications: one(applications)
}));


const user = await db.query.users.findFirst({
where: eq(users.id, user_id)
});
import * as schema from '$lib/db/schema';
import { connectD1 } from 'wrangler-proxy';

export const cloudflare: Handle = async ({ event, resolve }) => {
if (dev) {
event.locals.D1 = drizzle(connectD1('D1', { hostname }), { schema });
} else if (event?.platform?.env && !dev) {
event.locals.D1 = drizzle(event.platform?.env?.D1, { schema });
}
return await resolve(event);
};

export const users = createTable('user', {
id: text('id')
.notNull()
.$defaultFn(() => uuidv4())
.primaryKey(),
export const usersRelations = relations(users, ({ one }) => ({
applications: one(applications)
}));


const user = await db.query.users.findFirst({
where: eq(users.id, user_id)
});
This is essentially my code. Importing the schema to drizzle, exporting a user and its relationship. For some reason vscode is complaining about this
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) any
This is not a big problem since the code work itself, it's just typescript that complains, but it would be nice to see that everything is properly setup 🤓
Angelelz
Angelelz•10mo ago
I see I don't use cloudflare. I think your problem is that you don't have your locals type declared properly
sten
sten•10mo ago
You mean what I export from $lib/db/schema?
Angelelz
Angelelz•10mo ago
event.locals.D1 Is that type defined somewhere?
sten
sten•10mo ago
export const cloudflare: Handle = async ({ event, resolve }) => {
if (dev) {
event.locals.D1 = drizzle(connectD1('D1', { hostname }), { schema });
} else if (event?.platform?.env && !dev) {
event.locals.D1 = drizzle(event.platform?.env?.D1, { schema });
}
return await resolve(event);
};
export const cloudflare: Handle = async ({ event, resolve }) => {
if (dev) {
event.locals.D1 = drizzle(connectD1('D1', { hostname }), { schema });
} else if (event?.platform?.env && !dev) {
event.locals.D1 = drizzle(event.platform?.env?.D1, { schema });
}
return await resolve(event);
};
This is in one of the sequences in hooks.server.ts in sveltekit event.locals.D1 is defined in my app.d.ts file under Platform and Locals interface Defined as D1: DrizzleD1Database; But I am missing the schema... so I guess I need to add it there
import * as schema from '$lib/db/schema';

D1: DrizzleD1Database<schema>;
import * as schema from '$lib/db/schema';

D1: DrizzleD1Database<schema>;
drizzle return type is : DrizzleD1Database<typeof schema> I'm not type expert here
Angelelz
Angelelz•10mo ago
How does it get from locals to the db object you're using for your query? Are the relations being imported from the schema too?
sten
sten•10mo ago
The relations are exported from the schema so I would assume they're also imported. When I make a db call, I simply reference teh D1 instance on event.locals.D1 that is previously setup.
Want results from more Discord servers?
Add your server