Anand
Anand
Explore posts from servers
DTDrizzle Team
Created by Anand on 11/3/2023 in #help
Postgresjs with Neon giving connnection refusal error
No description
8 replies
DTDrizzle Team
Created by Anand on 8/22/2023 in #help
Connection to SQL destroyed (after idle). Do I need to manually handle open + close for each query?
i deployed my SQL server and nodejs server on railway. The server is up and running, for some reason my SQL connection disconnected? I was wondering if I am missing something fundamental or what?
database.ts
import dotenv from "dotenv";
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import * as schema from "../schema.js";

dotenv.config();

const connection = await mysql.createConnection({
host: process.env.MYSQLHOST,
port: Number(process.env.MYSQLPORT),
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASSWORD,
database: process.env.MYSQLDATABASE,
});

const db = drizzle(connection, { schema, mode: "default" });

export { db };
database.ts
import dotenv from "dotenv";
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import * as schema from "../schema.js";

dotenv.config();

const connection = await mysql.createConnection({
host: process.env.MYSQLHOST,
port: Number(process.env.MYSQLPORT),
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASSWORD,
database: process.env.MYSQLDATABASE,
});

const db = drizzle(connection, { schema, mode: "default" });

export { db };
I am using this db in different controllers to make queries. Am i messing up somewhere?
example query
const userTodos = await db.query.users.findFirst({
where: eq(users.id, +userId),
with: {
todos: {
where: (todoItem) => eq(todoItem.type, type),
},
},
});
example query
const userTodos = await db.query.users.findFirst({
where: eq(users.id, +userId),
with: {
todos: {
where: (todoItem) => eq(todoItem.type, type),
},
},
});
Should I handle the open and close state manually in every query or am I missing something?
15 replies