Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun support with --bun #199

Open
mikerockett opened this issue Sep 25, 2023 · 9 comments
Open

Bun support with --bun #199

mikerockett opened this issue Sep 25, 2023 · 9 comments

Comments

@mikerockett
Copy link

I am trying to execute drizzle-kit commands using bun (instead of passing it off to node by way of the default shebang).

For example:

bun run --bun drizzle-kit generate:pg

or via package.json:

{
  "scripts": {
    "db:generate": "bun run --bun drizzle-kit generate:pg"
    // $ bun run db:generate
  }
}

When I do this, the command executes, but it cannot see my schema files. If I generated previously with node (ie, via the default shebang), and then with bun, it would generate a migration that drops all tables.

Output of the above command is always as if no schema files were present.

Drizzle Kit Config

import type { Config } from 'drizzle-kit'

export default {
  driver: 'pg',
  dbCredentials: { connectionString: process.env.DATABASE_URL! },
  schema: './lib/db/schema/*.ts',
  out: './lib/db/migrations',
  introspect: { casing: 'preserve' },
  schemaFilter: ['public'],
  strict: true,
  verbose: true,
} satisfies Config

Could it be because I am globbing the schema? Or, am I missing something?

Versions:

$ bun pm ls | grep drizzle
[0.03ms] ".env"
├── [email protected]
├── [email protected]
@mikerockett mikerockett changed the title Bun support with --bun Bun support with --bun Sep 25, 2023
@hazelnutcloud
Copy link

hazelnutcloud commented Oct 16, 2023

can confirm facing this issue too, with plain './schema.ts' as input

EDIT: Found a workaround to this by using module.export instead of esm exports

@eknowles
Copy link

spent too many hours on this before i search for this issue.
I wasn't able to get the module exports workaround working, @hazelnutcloud are you able to clarify if this was via the drizzle config or cli?

@SnowMarble
Copy link

I found another way to fix it. It's kind of working, but we can use export in schema.ts

First, go to node_modules/drizzle-kit/bin.js and find src/serializer/<your-driver>Imports.ts(pg for pgImports.ts and mysql for mysqlImports.ts).

And, change forEach statement in prepareFromExports or prepareFromMySqlImports(function that contains forEach) to for ... in statement.
For example, we can fix pg like this:

// bin.js:12048
prepareFromExports = (exports) => {
  const tables = [];
  const enums = [];
  const schemas = [];
  for(let e in exports) {
    const t = exports[e];
    if ((0, import_pg_core.isPgEnum)(t)) {
      enums.push(t);
      continue;
    }
    if ((0, import_drizzle_orm4.is)(t, import_pg_core.PgTable)) {
      tables.push(t);
    }
    if ((0, import_drizzle_orm4.is)(t, import_pg_core.PgSchema)) {
      schemas.push(t);
    }
  }
  return { tables, enums, schemas };
};

Lastly, follow this comment to apply the change.

The problem is that Bun's Object.values or Object.keys for the Module object (a return value of require) give us nothing unlike Nodejs.

const schema = require('./db/schema.ts')
const exports = Object.values(schema) // []

and somehow for ... in can iterate the object even in Bun.

@malvinpratama
Copy link

I found another way to fix it. It's kind of working, but we can use export in schema.ts

First, go to node_modules/drizzle-kit/bin.js and find src/serializer/<your-driver>Imports.ts(pg for pgImports.ts and mysql for mysqlImports.ts).

And, change forEach statement in prepareFromExports or prepareFromMySqlImports(function that contains forEach) to for ... in statement. For example, we can fix pg like this:

// bin.js:12048
prepareFromExports = (exports) => {
  const tables = [];
  const enums = [];
  const schemas = [];
  for(let e in exports) {
    const t = exports[e];
    if ((0, import_pg_core.isPgEnum)(t)) {
      enums.push(t);
      continue;
    }
    if ((0, import_drizzle_orm4.is)(t, import_pg_core.PgTable)) {
      tables.push(t);
    }
    if ((0, import_drizzle_orm4.is)(t, import_pg_core.PgSchema)) {
      schemas.push(t);
    }
  }
  return { tables, enums, schemas };
};

Lastly, follow this comment to apply the change.

The problem is that Bun's Object.values or Object.keys for the Module object (a return value of require) give us nothing unlike Nodejs.

const schema = require('./db/schema.ts')
const exports = Object.values(schema) // []

and somehow for ... in can iterate the object even in Bun.

it's works.
can you create PR for this ?
I think this major issue for drizzle-kit if we want to support bun.

@SnowMarble
Copy link

@malvinpratama Unfortunately, drizzle kit is not an open source that we can't make a contrubution right away. But we can mention drizzle team to let them know about it.

@itsyoboieltr
Copy link

Looking forward to this fix! I only need to install node on my docker image so I can run drizzle-kit, everything else is already running with bun.

@itsyoboieltr
Copy link

@Angelelz Is there any way we could merge the fix proposed by @SnowMarble?
cc: @AndriiSherman

@gnosticdev
Copy link

For those having the same issue with sqlite, you can patch by changing require to await import in the prepareFromSqliteImports function.

// node_modules/drizzle-kit/bin.cjs: 12840
 prepareFromSqliteImports = async (imports) => {
      const tables = [];
      const enums = [];
      const { unregister } = await safeRegister();
      for (let i = 0; i < imports.length; i++) {
        const it = imports[i];
        // const i0 = require(`${it}`); <-- does not work with --bun flag
        const i0 = await import(`${it}`); // <-- this works with --bun
        const i0values = Object.values(i0);
        i0values.forEach((t) => {
          if ((0, import_drizzle_orm6.is)(t, import_sqlite_core.SQLiteTable)) {
            tables.push(t);
          }
        });
      }

Also, from instructions above:

  1. add yarn.lock and patch-package:
 bun i --yarn && bunx patch-package drizzle-kit
  1. make the change above
  2. add postinstall: bunx patch-package to package.json scripts
  3. delete yarn.lock

Thanks @SnowMarble for the fix!

@GoncaloJoseMoura
Copy link

this problem still persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants