Budi
Budi
Explore posts from servers
DTDrizzle Team
Created by Budi on 3/31/2024 in #help
What's the best way to query my db?
I'm running Supabase Postgres locally with Drizzle. In production, I'd use the connection pooler which would ensure I don't have too many concurrent connections. I can't locally however and Supabase often gives this error: PostgresError: remaining connection slots are reserved for non-replication superuser connections. I'm initiating the db as such:
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import * as schema from "../schemas/drizzleSchema"

export default function db() {
const connectionString = process.env.DATABASE_URL

if (!connectionString) {
throw new Error("LOCAL_DATABASE_URL is undefined")
}

const client = postgres(connectionString)

return drizzle(client, { schema })
}
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import * as schema from "../schemas/drizzleSchema"

export default function db() {
const connectionString = process.env.DATABASE_URL

if (!connectionString) {
throw new Error("LOCAL_DATABASE_URL is undefined")
}

const client = postgres(connectionString)

return drizzle(client, { schema })
}
And then in the necessary functions I import db and call it, like so: const database = db(). I have many helper functions which each create a db connection for the query, for example:
export async function getPostById(postId: number) {
try {
const database = db()
return await database.query.posts.findFirst({ where: eq(posts.id, postId) })
} catch (err) {
error(400, "Couldn't get post")
}
}
export async function getPostById(postId: number) {
try {
const database = db()
return await database.query.posts.findFirst({ where: eq(posts.id, postId) })
} catch (err) {
error(400, "Couldn't get post")
}
}
Is this the right way of doing things? Or should I pass the database to the helper functions, instead of initiating a connection there? I want to ensure I'm not creating unnecessary connections to the DB. Are there any best practices I should be aware of? Thanks!
8 replies
DTDrizzle Team
Created by Budi on 3/30/2024 in #help
Studio not enough info to infer relation
I'm getting this error:
throw new Error(
^

Error: There is not enough information to infer relation "__public__.posts.tags"
throw new Error(
^

Error: There is not enough information to infer relation "__public__.posts.tags"
Here's my schema: https://github.com/budivoogt/bv_markdown_blog/blob/a15ec5062337460647693dd070e504e9eca54e14/src/lib/schemas/drizzleSchema.ts I believe I followed the docs carefully and ensured every table has its relations defined. The app works fine but studio doesn't launch. Any suggestions?
19 replies
DTDrizzle Team
Created by Budi on 3/15/2024 in #help
Can't access innerJoin result in page data using Svelte
In src/routes/+layout.server.ts I have this load function, which returns postTags via a Drizzle query to my Supabase Postgres DB. It's successfully returning a result which I'm able to log to my console. However, I am unable to access the property via export let data or $page.data on child pages. Strangely, the other properties returned from my load functions are made accessible fine. I don't have any load functions that override the returned postTags in this server load function. Why could this be? I can't figure it out and since the other properties are returning fine, I doubt it's a Svelte issue. I'm not getting any typescript errors either. So I suspect it may be something related to the ORM. Thanks! Relevant code: https://github.com/brucey0x/bv_markdown_blog/blob/ebe102f5a4713443d5b2e8e1c1a77cad6b88403c/src/routes/%2Blayout.server.ts https://github.com/brucey0x/bv_markdown_blog/blob/ebe102f5a4713443d5b2e8e1c1a77cad6b88403c/src/app.d.ts
4 replies
DTDrizzle Team
Created by Budi on 12/29/2023 in #help
Drizzle Studio not working with bun
I had a basic project running with pnpm, then switched to bun and removed the dotenv package and its imports in /drizzle.config.ts. Now when I run bun drizzle-kit studio I get this error: /drizzle/drizzle.config.json file does not exist Previously it didn't look in the /drizzle folder for the config file, and automatically detected that my config was in root and ended with a .ts extension. If I run bun drizzle-kit studio --config ../drizzle.config.ts it picks up the config file, but I get this error:
Invalid input "url" is a required option for driver "turso". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference
Invalid input "authToken" is a required option for driver "turso". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference
error: "drizzle-kit" exited with code 1
Invalid input "url" is a required option for driver "turso". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference
Invalid input "authToken" is a required option for driver "turso". You can read more about drizzle.config: https://orm.drizzle.team/kit-docs/config-reference
error: "drizzle-kit" exited with code 1
Here's my drizzle.config.ts:
import type { Config } from "drizzle-kit"

export default {
schema: "./drizzle/schema.ts",
out: "./drizzle/migrations",
driver: "turso",
dbCredentials: {
url: process.env.LOCAL_DATABASE_URL as string,
authToken: process.env.LOCAL_DATABASE_AUTH_TOKEN as string
},
// Print all statements
verbose: true,
// Always ask for confirmation, even they aren't breaking
strict: true,
// Execute every migration statement individually
breakpoints: true
} satisfies Config
import type { Config } from "drizzle-kit"

export default {
schema: "./drizzle/schema.ts",
out: "./drizzle/migrations",
driver: "turso",
dbCredentials: {
url: process.env.LOCAL_DATABASE_URL as string,
authToken: process.env.LOCAL_DATABASE_AUTH_TOKEN as string
},
// Print all statements
verbose: true,
// Always ask for confirmation, even they aren't breaking
strict: true,
// Execute every migration statement individually
breakpoints: true
} satisfies Config
The one change I've made to my config file is that I removed the dotenv package that I previously used (when still using pnpm/node). It seems that drizzle studio isn't picking up the env variables via bun. Is this intentional behaviour? How should I resolve this? Thanks!
2 replies