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

Add a tutorial on setting up Drizzle with Neon #289

Merged
merged 5 commits into from
May 24, 2024

Conversation

ananis25
Copy link
Contributor

This PR adds a tutorial on using Drizzle with Neon Postgres database. There is an existing tutorial on a nextjs project with drizzle + Neon, but this one details the basic setup steps (migration/queries).

(I write technical content for Neon)

Copy link
Collaborator

@realmikesolo realmikesolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ananis25! Thank you for your efforts, we appreciate it! There are some edits that need to be implemented before merging this PR.

All tutorials with databases should be consistent and have similar structure/tables/queries. For instance you can follow tutorials for Turso & Supabase.

src/content/documentation/docs/get-started-postgresql.mdx Outdated Show resolved Hide resolved
src/content/documentation/docs/get-started-postgresql.mdx Outdated Show resolved Hide resolved
});
```

The code defines two tables: `authors`, which will contain the list of all the authors, and `books`, which will contain the list of books written by the authors. Each book is associated with an author using the `authorId` field.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this info so it would be like in other tutorials

tsx ./src/migrate.ts
```

Learn more about the [migration process](https://orm.drizzle.team/docs/migrations).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add information about drizzle-kit push command. You can find the example in other tutorials

import { db } from './db';
import { authors, books } from './schema';

export async function getBooksByAuthor(authorId: number) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to set returning type to show the type of query result

}
```

#### Select data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also add more examples like in other tutorials


### Basic file structure

This is the basic file structure of the project. In the `src/db` directory, we have database-related files including connection in `db.ts`, schema definitions in `schema.ts`, and a migration script in `migrate.ts` file which is responsible for applying migrations that stored in the `migrations` directory.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove part about migrate.ts cause we use drizzle-kit migrate command for applying migrations

- You should have installed the `dotenv` package for managing environment variables, and `tsx` to run typescript files.
<Npm>
dotenv
tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is used only for applying migrations then you can remove it as we use drizzle-kit migrate command

@ananis25
Copy link
Contributor Author

ananis25 commented May 19, 2024

Hey @realmikesolo - thanks for looking into the PR!

Before, I make the change from migrate.ts script to the drizzle kit migrate command, I wanted to check - is it possible to specify the driver as neon explicitly in the Drizzle config? From reading the release notes, neon is only a fall-through if the default pg script isn't available and can't be specified as one of the values for driver field.

We wanted Neon users to use the serverless driver for all their operations, hence the separate migration script.

@realmikesolo
Copy link
Collaborator

realmikesolo commented May 20, 2024

Hey @ananis25! As for now you can't explicity set neon as a driver. You should have installed only neon driver package and Drizzle kit will use it if there are no other drivers (e.g. pg or vercel/postgres)

UPD: I have asked core team member and we will think how to do it

@realmikesolo
Copy link
Collaborator

Small update: It is not necessary for users to know how the Drizzle kit connects to the database. The Drizzle kit looks for installed drivers (pg, postgres, vercel/postgres and neon) to execute queries locally via TCP.

If neon is the only driver installed, the Drizzle kit will use this package exclusively.

@ananis25
Copy link
Contributor Author

@realmikesolo - thank you for clarifying. I've made some edits, let me know if it looks okay.

@realmikesolo
Copy link
Collaborator

Hello @ananis25. I have used this tutorial and everything worked out. Great job! We are ready to merge, but there is one thing. Could you remove tsx installation? As I see, we don't use it anymore

@ananis25
Copy link
Contributor Author

Sure, removed it. I was using it to test the code locally.

@realmikesolo realmikesolo merged commit 3e874ed into drizzle-team:main May 24, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants