drizzle-team / drizzle-team/drizzle-orm

[BUG]: Wrapping a pg client in drizzle affects query response of other clients

Open
#1,991 4 comments 1 reaction 0 assignees View on GitHub
driver/pg improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.30.1

### What version of `drizzle-kit` are you using?

0.20.14

### Describe the Bug

In the following snippet, the 2 query responses are different, even though the client and the query are the same.

```ts
import { drizzle } from 'drizzle-orm/node-postgres';
import { Client } from 'pg';

(async () => {
const vanillaClient = new Client({
user: myConfig.username,
host: myConfig.proxy,
database: myConfig.dbname,
password: myConfig.password,
port: myConfig.port,
});
await vanillaClient.connect();

// Set up
await vanillaClient.query('BEGIN');
await vanillaClient.query(
"CREATE TABLE my_table (date_created timestamp DEFAULT NOW(), my_interval interval DEFAULT '1 year')"
);
await vanillaClient.query('INSERT INTO my_table DEFAULT VALUES');

// Query 1
const query1Response = await vanillaClient.query('SELECT * FROM my_table');
console.log('query1 typeof date_created:', typeof query1Response.rows[0].date_created); // query1 typeof date_created: object
console.log('query1 my_interval:', query1Response.rows[0].my_interval); // query1 my_interval: PostgresInterval { years: 1 }

// Create a separate client and wrap it in drizzle
drizzle(
new Client({
user: myConfig.username,
host: myConfig.proxy,
database: myConfig.dbname,
password: myConfig.password,
port: myConfig.port,
})
);

// Query 2, identical to query 1 and still using the vanilla client instance, but different response now
const query2Response = await vanillaClient.query('SELECT * FROM my_table');
console.log('query2 typeof date_created:', typeof query2Response.rows[0].date_created); // query2 typeof date_created: string
console.log('query2 my_interval:', query2Response.rows[0].my_interval); // query2 my_interval: 1 year

// Tear down
await vanillaClient.query('DROP TABLE my_table');
await vanillaClient.query('COMMIT');
})();
```

It looks like this is because the [NodePgDriver.initMappers method](https://github.com/drizzle-team/drizzle-orm/blob/bfc757f2adb843192e1b5508fcb4abd9737edd09/drizzle-orm/src/node-postgres/driver.ts#L40) sets the type parsers globally, instead of setting them just for the client. I verified that this can be fixed by using `this.client.setTypeParser` instead of using the imported `types`, although I'm not sure what all the implications of that are.

Drizzle looks crazily cool but this bug is a hard blocker for our team.

### Expected behavior

Wrapping one client in drizzle should not affect the behavior of other clients.

### Environment & setup

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.