Typescript error when importing the planetscale-drizzle db instance.

my db is hosted on planetscale and im trying to do the setup as explained in the drizzle docs but when I import db instance i get the following error. My code
import * as schema from './migrations/schema';
import { InsertUser, usersTable } from './schema/user';

const { drizzle } = require('drizzle-orm/planetscale-serverless');
const { connect } = require('@planetscale/database');

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

export const drizzleDb = drizzle(connection, { schema });

export async function createUser(insertUser: InsertUser) {
const user = await drizzleDb.insert(usersTable).values(insertUser).execute();

return user;
}
import * as schema from './migrations/schema';
import { InsertUser, usersTable } from './schema/user';

const { drizzle } = require('drizzle-orm/planetscale-serverless');
const { connect } = require('@planetscale/database');

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

export const drizzleDb = drizzle(connection, { schema });

export async function createUser(insertUser: InsertUser) {
const user = await drizzleDb.insert(usersTable).values(insertUser).execute();

return user;
}
Error
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mohamedziyad/Desktop/Janit/backend/node_modules/.pnpm/@planetscale+database@1.7.0/node_modules/@planetscale/database/dist/index.js from /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts not supported.
Instead change the require of index.js in /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mohamedziyad/Desktop/Janit/backend/node_modules/.pnpm/@planetscale+database@1.7.0/node_modules/@planetscale/database/dist/index.js from /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts not supported.
Instead change the require of index.js in /Users/mohamedziyad/Desktop/Janit/backend/src/db/index.ts to a dynamic import() which is available in all CommonJS modules.
tsConfig
{
"compilerOptions": {
"target": "ES2020",
"rootDir": "./",
"baseUrl": "./",
"module": "commonjs",
"outDir": "./build",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
"lib": ["es2020"],
"types": ["node", "dotenv"],
//
"noEmit": true,
"resolveJsonModule": true,
"isolatedModules": true,
"incremental": true
}
}
{
"compilerOptions": {
"target": "ES2020",
"rootDir": "./",
"baseUrl": "./",
"module": "commonjs",
"outDir": "./build",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
"lib": ["es2020"],
"types": ["node", "dotenv"],
//
"noEmit": true,
"resolveJsonModule": true,
"isolatedModules": true,
"incremental": true
}
}
ben
benā€¢315d ago
Reviving this thread because I'm also experiencing this issue and haven't found a way to fix it Any luck?
Zee_008
Zee_008ā€¢313d ago
No, I stopped working on this! It was a hustle
shaw93
shaw93ā€¢304d ago
Dave Gray
YouTube
Build a Random Quote Machine with Nextjs, PlanetScale MySQL, & Driz...
Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmap Build a random quote machine with Nextjs, PlanetScale MySQL, & Drizzle ORM. In this tutorial, we'll set up a MySQL database on PlanetScale, connect to and query the database with Drizzle ORM, and build a Next.js frontend and API routes. šŸš© Subscribe āžœ https://bit.ly/3n...
iukea
iukeaā€¢292d ago
Also it is asking for a dynamic important (kinda strange but also looks like you are windows) - recommend learning WSL on windows
Want results from more Discord servers?
Add your server
More Posts
Mapped column name and relation with the original nameHi, I am taking over an old project and have found an issue, I'm not sure if I should file it as a bHow to properly do foreign keys in schema with PlanetScale?I'm aware that foreign key *constrains* aren't supported, but foreign keys themselves are. push: ``Typescript path alias not working properlyI'm new to Drizzle, but I'm not able to use the path alias on schema files. Using absolute paths onExpand query from ID using .select()Hi! I am trying to get the first name of a comment author, but only store the ID so the user can chaPGEnum -> Typescript EnumHey there. Is there any convenient way people have found to conveniently turn a pgEnum into a typescIs Drizzle Kit/Drizzle Studio possible with RDS Data API?I'm able to create the RDSDataClient for querying my DB using Drizzle ORM, but I don't see a way to How to apply migrations on postgres in a serverless enviroment?Hello, I want to apply postgres migrations to my vercel DB. I see that the push command does not woError when trying to generate a migration schemaanyone any ideas to what the problem is? It was working fine yesterday and now it throws an error whModelling self relationsI have a table `categories` with a parent fields: ``` { id: text("id").notNull().primaryKey(), .Issue running migrations to DBI am having an issue running migrations to Neon. I checked that everything is exporting const variabIt is possible to have prepared statements inside transactions?Is there a way to insert prepared queries inside a transaction ?type config findMany or findFirstHi, I've one question i don't find the way to type correctly findMany() Like: export async function