drizzle-team / drizzle-kit-mirror

Docs and issues repository for drizzle-kit
291 stars 17 forks source link

Issue when generating sql using zod for environment variable validation #21

Open STNeto1 opened 1 year ago

STNeto1 commented 1 year ago

When using npx drizzle-kit generate:mysql with other source for env variables, an error is thrown.

npx drizzle-kit generate:mysql       
drizzle-kit: v0.17.0
drizzle-orm: v0.22.0-88c1ad3

/home/stneto/apps/edge-shortener/src/env.mjs:2
import { z } from "zod";
^^^^^^

SyntaxError: Cannot use import statement outside a module

The .env validation comes from here

The issue on code can be found here

karmux commented 1 year ago

I also get SyntaxError: Cannot use import statement outside a module from the first import of helper package when I run drizzle-kit generate:pg. I'm using esm in package.

AndriiSherman commented 1 year ago

@karmux @STNeto1 do you have anything else in file with schemas except of tables/enums? Ideally is to have schema.ts(Or any name you want to use for this file) containing only database schema definitions

STNeto1 commented 1 year ago

The current files, @AndriiSherman

// DB
import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";
import { env } from "~/env.mjs";

const connection = connect({
  host: env.DATABASE_HOST,
  username: env.DATABASE_USERNAME,
  password: env.DATABASE_PASSWORD,
});

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

// =============

// SCHEMA
import {
  serial,
  text,
  timestamp,
  varchar,
} from "drizzle-orm/mysql-core/columns";
import { mysqlTable } from "drizzle-orm/mysql-core/table";

export const shortLink = mysqlTable("short_links", {
  id: serial("id").primaryKey(),
  url: text("url").notNull(),
  slug: varchar("slug", {
    length: 20,
  }).notNull(),
  created_at: timestamp("created_at").notNull().defaultNow(),
});

I can't see anything else being imported that would cause the issue

karmux commented 1 year ago

I'm importing own esm package to schemas that contains some convenience helper functions.

Minimum reproducible example:

  1. Download and extract example utils package drizzle-utils.tar.gz

  2. Run npm install and npm run build inside drizzle-utils

  3. Add it as a local dependency of an app that has migrations and run npm install

    "drizzle-utils": "../drizzle-utils",
  4. Add these two lines to the header of schemas.ts

    import { createTimestampColumns } from 'drizzle-utils';
    console.log(createTimestampColumns());

This triggers SyntaxError: Cannot use import statement outside a module error when trying to generate migration.

mikebuilds commented 1 year ago

Did you get this working?

karmo-sisutech commented 1 year ago

No. @AndriiSherman in Discord said that he reproduced it and started working on it.

deadcoder0904 commented 7 months ago

would like support for t3-env or some other way to read environment variables?

see https://github.com/t3-oss/t3-env/issues/180 for more.

in my next.js app, everything works but i wish drizzle-kit has some built-in support for it because those are the only commands that fail to read .env file or check validations. i wish it somehow works magically.