Skip to content

Drizzle option generates code that did not work for me. #452

@VictorioBerra

Description

@VictorioBerra

New to SK, Drizzle, and my TS is weak too.

So I generated a new SK app with npx sv create whatever and it generated this code in src\lib\server\db\index.ts:

import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { env } from '$env/dynamic/private';
if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
const client = postgres(env.DATABASE_URL);
export const db = drizzle(client);

I was unable to use db at all without errors IE: await db.query.user.findMany(); would throw TypeError: Cannot read properties of undefined (reading 'findMany').

I needed to use the following code... where I pass schema to drizzle.

import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { env } from '$env/dynamic/private';
import * as schema from './schema';

if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
const client = postgres(env.DATABASE_URL);
export const db = drizzle(client, {
    schema
});

Hope this helps someone...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpkg:addsv add

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions