Back to Drizzle Orm

Get Started with Drizzle and Neon

src/content/docs/get-started/neon-new.mdx

latest2.6 KB
Original Source

import Tab from '@mdx/Tab.astro'; import Tabs from '@mdx/Tabs.astro'; import Npm from "@mdx/Npm.astro"; import Callout from '@mdx/Callout.astro'; import Steps from '@mdx/Steps.astro'; import AnchorCards from '@mdx/AnchorCards.astro'; import Breadcrumbs from '@mdx/Breadcrumbs.astro'; import Prerequisites from "@mdx/Prerequisites.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import FileStructure from '@mdx/get-started/FileStructure.mdx'; import InstallPackages from '@mdx/get-started/InstallPackages.mdx'; import ConnectNeon from '@mdx/get-started/postgresql/ConnectNeon.mdx' import CreateTable from '@mdx/get-started/postgresql/CreateTable.mdx' import SetupConfig from '@mdx/get-started/SetupConfig.mdx'; import ApplyChanges from '@mdx/get-started/ApplyChanges.mdx'; import RunFile from '@mdx/get-started/RunFile.mdx'; import QueryDatabase from '@mdx/get-started/QueryDatabase.mdx'; import SetupEnv from '@mdx/get-started/SetupEnv.mdx';

<Breadcrumbs/>

Get Started with Drizzle and Neon

<Prerequisites> - **dotenv** - package for managing environment variables - [read here](https://www.npmjs.com/package/dotenv) - **tsx** - package for running TypeScript files - [read here](https://tsx.is/) - **Neon** - serverless Postgres platform - [read here](https://neon.tech/docs/introduction) </Prerequisites>

Drizzle has native support for Neon connections with the neon-http and neon-websockets drivers. These use the neon-serverless driver under the hood.

With the neon-http and neon-websockets drivers, you can access a Neon database from serverless environments over HTTP or WebSockets instead of TCP. Querying over HTTP is faster for single, non-interactive transactions.

If you need session or interactive transaction support, or a fully compatible drop-in replacement for the pg driver, you can use the WebSocket-based neon-serverless driver. You can connect to a Neon database directly using Postgres

<FileStructure/>

Step 1 - Install @neondatabase/serverless package

<InstallPackages lib='@neondatabase/serverless'/>

Step 2 - Setup connection variables

<SetupEnv env_variable='DATABASE_URL' />

Step 3 - Connect Drizzle ORM to the database

<ConnectNeon/>

Step 4 - Create a table

<CreateTable />

Step 5 - Setup Drizzle config file

<SetupConfig dialect='postgresql' env_variable='DATABASE_URL'/>

Step 6 - Applying changes to the database

<ApplyChanges />

Step 7 - Seed and Query the database

<QueryDatabase dialect='neon-http' env_variable='DATABASE_URL'/>

Step 8 - Run index.ts file

<RunFile/>