Top Level await issue with latest [email protected], mysql2, planetscale

We are getting this error when following the release notes recommended setup for pscale/mysql:
[ERROR] Top-level await is not available in the configured target environment ("node14")
remix-app:dev:
remix-app:dev: ../../packages/db/index.ts:18:19:
remix-app:dev: 18 │ const connection = await mysql.createConnection({
[ERROR] Top-level await is not available in the configured target environment ("node14")
remix-app:dev:
remix-app:dev: ../../packages/db/index.ts:18:19:
remix-app:dev: 18 │ const connection = await mysql.createConnection({
5 Replies
focused_morning
focused_morningOP2y ago
We are using Remix.run and Node v18.6.0, here is our exact implementation:
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';

const connection = await mysql.createConnection({
uri: process.env.PLANETSCALE_DATABASE_URL,
});

const db = drizzle(connection, { schema, mode: 'planetscale' });
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';

const connection = await mysql.createConnection({
uri: process.env.PLANETSCALE_DATABASE_URL,
});

const db = drizzle(connection, { schema, mode: 'planetscale' });
mr_pablo
mr_pablo2y ago
Top-level await is not available in the configured target environment ("node14")
You're not running on Node 18
focused_morning
focused_morningOP2y ago
we are, but we figured out the issue and a solution. I'm gathering this information now for others in the future. This is what we did to solve this: Vercel uses undici for fetch on the server, which doesn't seem to be fully compatible with node 18. So, in serverless functions, we get this error when using drizzle: Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: First parameter has member 'readable' that is not a ReadableStream." To fix this, in the @planetscale/database connect config we can set the fetch property to use fetch-ponyfill.
import { connect } from "@planetscale/database";
import {
PlanetScaleDatabase,
drizzle,
} from "drizzle-orm/planetscale-serverless";
import * as schema from "./schema";
import fetchPonyfill from "fetch-ponyfill";

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

const db: PlanetScaleDatabase<typeof schema> = drizzle(connection, { schema });

export { db, schema };
import { connect } from "@planetscale/database";
import {
PlanetScaleDatabase,
drizzle,
} from "drizzle-orm/planetscale-serverless";
import * as schema from "./schema";
import fetchPonyfill from "fetch-ponyfill";

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

const db: PlanetScaleDatabase<typeof schema> = drizzle(connection, { schema });

export { db, schema };
titongo
titongo14mo ago
You have no clue how much I love you for this. It was driving me crazy!
focused_morning
focused_morningOP14mo ago
Drove us crazy too.
Want results from more Discord servers?
Add your server