kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

Not working with Next.js 13 and Neon #407

Closed ziaukhan closed 1 year ago

ziaukhan commented 1 year ago

It is not working with Next.js 13 API and Neon, I have documented it here:

https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01_typed_sql

koskimas commented 1 year ago

Wot? Let's try this again. This time, use words.

I'm not going to:

Create a [Next.js 13 project with app routing](https://beta.nextjs.org/docs)
Signup and create a [Neon Project](https://neon.tech/)

How about you just describe the error in detail and we'll try to help you?

This error

error - node_modules/@neondatabase/serverless/index.js (43:3300) @ ws

does not come from kysely, but from @neondatabase.

igalklebanov commented 1 year ago

error - unhandledRejection: Error: All attempts to open a WebSocket to connect to the database failed. If using Node, please install the ws package (or simply use the pg package instead). at ws (webpack-internal:///(sc_server)/./node_modules/@neondatabase/serverless/index.js:3948:31)

Try npm i ws.

ziaukhan commented 1 year ago

I have already installed ws as you may see in the package.json file. I have installed it by giving the following command: npm i ws but it is still not working. I don't understand what you mean by peer dependency. https://github.com/panaverse/learn-nextjs/blob/main/step12_serverless_databases/relational/step01_typed_sql/package.json

ziaukhan commented 1 year ago

I have also documented the error here: https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01_typed_sql#note-i-am-getting-the-following-error

ziaukhan commented 1 year ago

I have got Next.js 13, Neon, and Kysely working with PostgresDialect: https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01b_typed_sql_working However still not able to run with Neon Dialect: https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01_typed_sql

developmentWithTariq commented 1 year ago

When I asked to chatgpt based with user conversion last solution is bellow:

I took a look at your code and it seems like the issue with using Neon dialect is caused by a mismatch between the version of kysely-neon and the version of @neondatabase/serverless that you are using.

Based on the error message you shared earlier, it seems that kysely-neon is expecting a specific version of @neondatabase/serverless, but the version you have installed does not meet that requirement. You can try downgrading the version of @neondatabase/serverless to match the one expected by kysely-neon. In your package.json file, change the following line:

"@neondatabase/serverless": "^0.12.0" to:

`"@neondatabase/serverless": "0.12.0" `This will ensure that you are using the exact version of @neondatabase/serverless that kysely-neon expects.

Additionally, it appears that the createTable function in your db.ts file is not properly using the NeonDialect. To use the NeonDialect, you need to specify it as an option when creating a new kysely client, like this:

import { kysely } from 'kysely';
import { NeonDialect } from 'kysely-neon';

const client = kysely({
  dialect: new NeonDialect(),
  connection: process.env.NEON_CONNECTION_STRING
});

export async function createTable() {
  await client.schema().createTable('playing_with_neon', table => {
    table.serial('id').primaryKey();
    table.text('name').notNullable();
    table.real('value').notNullable();
  });
}

With these changes, you should be able to use Neon dialect with your Next.js 13 app.

I hope this helps! Let me know if you have any further questions or concerns.

igalklebanov commented 1 year ago

When I asked to chatgpt based with user conversion last solution is bellow:

I took a look at your code and it seems like the issue with using Neon dialect is caused by a mismatch between the version of kysely-neon and the version of @neondatabase/serverless that you are using.

Based on the error message you shared earlier, it seems that kysely-neon is expecting a specific version of @neondatabase/serverless, but the version you have installed does not meet that requirement. You can try downgrading the version of @neondatabase/serverless to match the one expected by kysely-neon. In your package.json file, change the following line:

"@neondatabase/serverless": "^0.12.0" to:

`"@neondatabase/serverless": "0.12.0" `This will ensure that you are using the exact version of @neondatabase/serverless that kysely-neon expects.

Additionally, it appears that the createTable function in your db.ts file is not properly using the NeonDialect. To use the NeonDialect, you need to specify it as an option when creating a new kysely client, like this:

import { kysely } from 'kysely';
import { NeonDialect } from 'kysely-neon';

const client = kysely({
  dialect: new NeonDialect(),
  connection: process.env.NEON_CONNECTION_STRING
});

export async function createTable() {
  await client.schema().createTable('playing_with_neon', table => {
    table.serial('id').primaryKey();
    table.text('name').notNullable();
    table.real('value').notNullable();
  });
}

With these changes, you should be able to use Neon dialect with your Next.js 13 app.

I hope this helps! Let me know if you have any further questions or concerns.

We should add a rule against GPT spam.

This comment scares me to be frank. Its author clearly didn't review the contents and just pasted it. Imagine all the unreliable and wrong software being built as we speak.

igalklebanov commented 1 year ago

@ziaukhan

I have got Next.js 13, Neon, and Kysely working with PostgresDialect: https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01b_typed_sql_working However still not able to run with Neon Dialect: https://github.com/panaverse/learn-nextjs/tree/main/step12_serverless_databases/relational/step01_typed_sql

There are multiple issues at play.

  1. @neondatabase/serverless WebSocket related.
  2. Next.js issue with WebSockets and local development.

Both issues are not related to Kysely or the 3rd party dialect you're using.

koskimas commented 1 year ago

We should add a rule against GPT spam.

Yes. Kysely didn't exist in the training data. Everything ChatGPT can currently generate will be just plain wrong.