pato
pato
Explore posts from servers
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
I was gonna swear I've seen native drizzle code for that but I cant remember where
9 replies
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
if u read the function's body, it's preventing a new row from being created if a certain condition
9 replies
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
this is basically adding a constraint tho
9 replies
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
bump
9 replies
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
bump
9 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
this is what I'm working with
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
import 'dotenv/config';
import { drizzle, type NodePgDatabase } from 'drizzle-orm/node-postgres';
import { remember } from '@epic-web/remember';
import pg from 'pg';

import logger from '@quacker/logger';

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

let pool: pg.Pool;
let db: NodePgDatabase<typeof schema>;

/**
* Attempts to connect to the database.
*
* **This is a process-breaking method.** If it fails, it will cause the application to crash.
*/
async function connect() {
if (!db) {
try {
logger.info('Connecting to database...');

pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
await pool.connect();

addListeners(pool);

// Prevent HMR from constantly creating new connections
// by "remembering" the connection (only useful in dev mode).
db = remember('db', () => drizzle(pool, { schema }));

logger.info('Connected successfully to database.');
} catch (err) {
logger.fatal('Failed to connect to database.', err);
process.exit(1);
}
}
}

function addListeners(pool: pg.Pool) {
pool.on('error', (err) => {
logger.error('An idle client has experienced an error', err.stack);
});
}

export { db };

export default {
connect,
};
import 'dotenv/config';
import { drizzle, type NodePgDatabase } from 'drizzle-orm/node-postgres';
import { remember } from '@epic-web/remember';
import pg from 'pg';

import logger from '@quacker/logger';

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

let pool: pg.Pool;
let db: NodePgDatabase<typeof schema>;

/**
* Attempts to connect to the database.
*
* **This is a process-breaking method.** If it fails, it will cause the application to crash.
*/
async function connect() {
if (!db) {
try {
logger.info('Connecting to database...');

pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
await pool.connect();

addListeners(pool);

// Prevent HMR from constantly creating new connections
// by "remembering" the connection (only useful in dev mode).
db = remember('db', () => drizzle(pool, { schema }));

logger.info('Connected successfully to database.');
} catch (err) {
logger.fatal('Failed to connect to database.', err);
process.exit(1);
}
}
}

function addListeners(pool: pg.Pool) {
pool.on('error', (err) => {
logger.error('An idle client has experienced an error', err.stack);
});
}

export { db };

export default {
connect,
};
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
i'm confused
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
so what's the point of having to extract a client
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
because you can also do pool.query
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
I thought pools existed so they'd handle multiple connections for you when needed
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
oh I see, then if I need to create my own clients from the pool, what's the point of creating a pool?
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
function addListeners(pool: pg.Pool) {
pool.on('error', (err) => {
logger.error('An idle client has experienced an error', err.stack);
});
}
function addListeners(pool: pg.Pool) {
pool.on('error', (err) => {
logger.error('An idle client has experienced an error', err.stack);
});
}
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
I don't get why connections aren't ending gracefully though. it shouldn't be doing anything out of the ordinary. If the app restarts, why doesn't it properly close and open the connections?
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
by the way, I don't think I'm using HMR exactly, it's Turborepo's watch mode that triggers my app restarts and, therefore, causes them to reconnect
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
here is the function that connects to the db:
async function connect() {
if (!db) {
try {
logger.info('Connecting to database...');

pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
await pool.connect();

addListeners(pool);

// Prevent HMR from constantly creating new connections
// by "remembering" the connection (only useful in dev mode).
db = remember('db', () => drizzle(pool, { schema }));

logger.info('Connected successfully to database.');
} catch (err) {
logger.fatal('Failed to connect to database.', err);
process.exit(1);
}
}
}
async function connect() {
if (!db) {
try {
logger.info('Connecting to database...');

pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
await pool.connect();

addListeners(pool);

// Prevent HMR from constantly creating new connections
// by "remembering" the connection (only useful in dev mode).
db = remember('db', () => drizzle(pool, { schema }));

logger.info('Connected successfully to database.');
} catch (err) {
logger.fatal('Failed to connect to database.', err);
process.exit(1);
}
}
}
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
reviving this since I tried using that package, and it was working fine so far, but I'm getting the errors again
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
I just tried this and spent some time testing around, so far so good. I'll awake this post again if I did something wrong
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
thank you for all the replies, I only had time to look into this now
24 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
I’m not following
24 replies