Using Drizzle as a package in Turborepo doesn't work ?

Jjeanhdev4/22/2023
I have a monorepo (Turborepo) with a shared package called @mono/database in which I put my drizzle schema and my types.

I'm testing a query on an app of this monorepo (a simple Node.js folder with TS files), and I also run the exact same query (with the same exact drizzle client definition) from my package to test it out. The thing is: the query works perfectly when ran from my package but outputs an error when run from my app that uses the content of the package.

When I run the query from my package directly, the logger it outputs the beautiful SQL statement complete with all the tables and fields.

But when I run it from my app (the Node.js folder with TS files) I get the following weird statement;
Query: select  from $1 left join $2 on $3 = $4 order by $5 desc limit $6 -- params: [[object Object], [object Object], [object Object], [object Object], [object Object], 1]

Which returns me the error below
.../.pnpm/pg@8.10.0/node_modules/pg/lib/utils.js:81
  return JSON.stringify(val)
              ^
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'PgTable'
    |     property 'id' -> object with constructor 'PgSerial'
    --- property 'table' closes the circle
    at JSON.stringify (<anonymous>)

------------------------------------------------
I logged the client in both cases, and also the tables which outputted, for both, the exact object of 750 lines.

But when I copy / pasted the corresponding pgTable directly in the file of my app in which I was importing my tables from the package and it works with no errors.

Here is the package.json of my @mono/database package.
{
  "name": "@mono/database",
  "version": "0.1.0",
  "main": "./index.ts",
  ...
 }


Any idea ? So that means the bundling in the turborepo process might be creating the error ?
Jjeanhdev4/22/2023
Side-note: I'm running my file using ts-node
Bbloberenober4/22/2023
you probably have multiple versions of drizzle-orm used in the same project, so our instanceof checks don't work
Bbloberenober4/22/2023
we have plans to mitigate this issue by replacing the instanceof checks, but for now you can workaround by de-duping the drizzle-orm instances in your monorepo
Bbloberenober4/22/2023
for example, you can try installing drizzle-orm in the monorepo root
Jjeanhdev4/22/2023
I have been grinding my switch from Prisma to Drizzle for the past 2 days, and I just thought I was stuck all over again. I installed globally drizzle-orm pg @types/pg AND IT WORKS !!!
Jjeanhdev4/22/2023
Thanks @bloberenober you saved my day !!
Bbloberenober4/22/2023
By globally do you mean in the monorepo root, or using -g?
Jjeanhdev4/22/2023
Globally, in the turborepo workspace using the -w flag
MMorrocoy5/2/2023
Hey @jeanhdev how does your "@mono/database" file structure look like?

I keep getting
The requested module 'database/schema' does not provide an export named 'cities'
Mmmurto5/2/2023
I have the same setup - I have this in my @mono/database/index.ts and everything works flawlessly:

export * from "./schema";
export * from "drizzle-orm";
export { type PostgresJsDatabase } from "drizzle-orm/postgres-js";
Mmmurto5/2/2023
Then just don't install drizzle-orm anywhere else, but import what is needed from @mono/database.
MMorrocoy5/2/2023
I also figured it out with something similar but I had to install "drizzle-orm" using the -W in the monorepo root

const connection = drizzle(poolConnection, {logger: true});

export {
  connection,
  cities
};

export type { City };