Back to Drizzle Orm

Get Started with Drizzle and PlanetScale

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

latest2.8 KB
Original Source

import Npm from '@mdx/Npm.astro'; import Callout from '@mdx/Callout.astro'; import Prerequisites from "@mdx/Prerequisites.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import WhatsNextPostgres from "@mdx/WhatsNextPostgres.astro"; import Breadcrumbs from '@mdx/Breadcrumbs.astro'; import FileStructure from '@mdx/get-started/FileStructure.mdx'; import InstallPackages from '@mdx/get-started/InstallPackages.mdx'; import SetupEnv from '@mdx/get-started/SetupEnv.mdx'; import ConnectPlanetScale from '@mdx/get-started/mysql/ConnectPlanetScale.mdx'; import CreateTable from '@mdx/get-started/mysql/CreateTable.mdx'; import QueryPlanetScale from '@mdx/get-started/mysql/QueryPlanetScale.mdx'; import ApplyChanges from '@mdx/get-started/ApplyChanges.mdx'; import RunFile from '@mdx/get-started/RunFile.mdx'; import SetupConfig from '@mdx/get-started/SetupConfig.mdx';

<Breadcrumbs/>

Get Started with Drizzle and PlanetScale

<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) - **PlanetScale** - MySQL database platform - [read here](https://planetscale.com) - **database-js** - PlanetScale serverless driver - [read here](https://github.com/planetscale/database-js) </Prerequisites> <Callout title='PlanetScale also offers Postgres' type='info'> Looking for PostgreSQL? Check out our [PlanetScale Postgres guide](/docs/get-started/planetscale-postgres-new) </Callout> <Callout title='important' type='warning'> For this tutorial, we will use the `database-js` driver to make **HTTP** calls to the PlanetScale database. If you need to connect to PlanetScale through TCP, you can refer to our [MySQL Get Started](/docs/get-started/mysql-new) page </Callout> <FileStructure/>

Step 1 - Install @planetscale/database package

<InstallPackages lib='@planetscale/database'/>

Step 2 - Setup connection variables

Create a .env file in the root of your project and add your database connection variable:

plaintext
DATABASE_HOST=
DATABASE_USERNAME=
DATABASE_PASSWORD=
<Callout title='tips'> To get all the necessary environment variables to connect through the `database-js` driver, you can check the [PlanetScale docs](https://planetscale.com/docs/tutorials/planetscale-serverless-driver-node-example#use-the-sample-repository) </Callout>

Step 3 - Connect Drizzle ORM to the database

<ConnectPlanetScale/>

Step 4 - Create a table

<CreateTable/>

Step 5 - Setup Drizzle config file

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

Step 6 - Applying changes to the database

<ApplyChanges />

Step 7 - Seed and Query the database

<QueryPlanetScale />

Step 8 - Run index.ts file

<RunFile/>