Finding it hard to get started with vercel postgres

Hi guys, I'm totally new to Drizzle and I want to know if there is any blog/article or something where I can read step by step guide to get started with nextjs + vercel postgres with Drizzle ORM. I tried to read many articles including official Drizzle docs and watched some YT videos also but couldn't grasp where they're getting all the details like creating migrate and push scripts, creating schema.ts etc. which I couldn't find in Drizzle docs.
38 Replies
Naeemgg
NaeemggOP10mo ago
I have a big app running with Prisma that I'd like to migrate to drizzle, but first I'm trying to create some small projects to get familiar with drizzle I found prisma is very easy to setup and get started with but drizzle is kinda confusing at this moment
amg
amg10mo ago
I think the docs is a good starting point, can you specify what you did not found in the docs?
Naeemgg
NaeemggOP10mo ago
lets start with the beginning, I'll refer to prisma. In prisma we can specify our schema and just run prisma migrate dev or prisma db push or prisma db pull to confirm if we're in sync with remote db or not. How to do it with drizzle?
loup
loup10mo ago
Ive go some issue too with the official docs (TS error, and others stuff...)
amg
amg10mo ago
Medium
The Data-Access-Pattern first approach with Drizzle
Bringing database and backend work into a single TS file is just as beneficial as developing a full-stack application in one language.
loup
loup10mo ago
Add script no ?
"migrations:generate": "drizzle-kit generate:pg",
"migrations:push": "drizzle-kit push:pg",
"migrations:drop": "drizzle-kit drop --config=drizzle.config.ts",
"migrations:generate": "drizzle-kit generate:pg",
"migrations:push": "drizzle-kit push:pg",
"migrations:drop": "drizzle-kit drop --config=drizzle.config.ts",
Naeemgg
NaeemggOP10mo ago
Yep I know but where is it in the official docs I came across many things like these scripts too but couln't find in official docs At this moment I just want to connect with my vercel postgres
amg
amg10mo ago
did you check the drizzle kit docs?
amg
amg10mo ago
Drizzle ORM - Overview
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Naeemgg
NaeemggOP10mo ago
Nope before that is Drizzle ORM
Naeemgg
NaeemggOP10mo ago
No description
Naeemgg
NaeemggOP10mo ago
So I started from there And got confused so came here
amg
amg10mo ago
yes, but think about them as different tools
Naeemgg
NaeemggOP10mo ago
So lets start from the very beginning I created a vercel postgres db and added it to env variables
Naeemgg
NaeemggOP10mo ago
Drizzle ORM - PostgreSQL
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Naeemgg
NaeemggOP10mo ago
I installed these
pnpm add drizzle-orm @vercel/postgres
pnpm add -D drizzle-kit
pnpm add drizzle-orm @vercel/postgres
pnpm add -D drizzle-kit
made a index.ts file inside drizzle/
import { sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';

const db = drizzle(sql)
const result = await db.select().from(...);
import { sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';

const db = drizzle(sql)
const result = await db.select().from(...);
Now what is next? I want to create some tables in my db
amg
amg10mo ago
ok, you have to take into account 4 things, 1. you can create and manage your schema with drizzle [drizzle-orm] https://orm.drizzle.team/docs/sql-schema-declaration 2. you can generate migrations files that are the ones in charge of telling your database how to change to become what you have defined in yous schema (point 1) [drizzle-kit] 3. you apply your changes with [drizzle-kit] via terminal commands defined in the package.json 4. you can query with [drizzle-orm] your database in your application code
Drizzle ORM - Overview
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
amg
amg10mo ago
in package.json inside scripts
"migration:generate": "drizzle-kit generate:sqlite",
"migration:push": "drizzle-kit push:sqlite",
"migration:production:push": "drizzle-kit push:sqlite --config=./drizzle_production.config.ts",
"migration:generate": "drizzle-kit generate:sqlite",
"migration:push": "drizzle-kit push:sqlite",
"migration:production:push": "drizzle-kit push:sqlite --config=./drizzle_production.config.ts",
you run this commands with npm run migration:generate ...
amg
amg10mo ago
you have here 2 interesting post https://medium.com/@andriisherman
Medium
Andrii Sherman – Medium
Read writing from Andrii Sherman on Medium. Building Drizzle ORM. Every day, Andrii Sherman and thousands of other voices read, write, and share important stories on Medium.
Naeemgg
NaeemggOP10mo ago
Ok let me go through all of these Thanks @amg I'll let you know shortly if it worked or not
amg
amg10mo ago
you welcome! reach me if you need something
Naeemgg
NaeemggOP10mo ago
🙌🏻 Hey @amg
schema.ts
import { pgTable, serial, timestamp, varchar } from 'drizzle-orm/pg-core';


export const records = pgTable('records', {
id: serial('id').primaryKey(),
count: varchar('count', { length: 256 }).notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow()
},
);
schema.ts
import { pgTable, serial, timestamp, varchar } from 'drizzle-orm/pg-core';


export const records = pgTable('records', {
id: serial('id').primaryKey(),
count: varchar('count', { length: 256 }).notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow()
},
);
Is there anything wrong with this schema file?? I run drizzle-kit generate:pg --schema ./db/schema.ts --out=./db/migrations and got the .sql files under db/migrations Now I want to push it to neon db for that I have configured drizzle.config.ts like this:
import 'dotenv/config';
import type { Config } from 'drizzle-kit';

export default {
schema: './src/schema.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.NEON_DATABASE_URL as string
},
} satisfies Config;
import 'dotenv/config';
import type { Config } from 'drizzle-kit';

export default {
schema: './src/schema.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.NEON_DATABASE_URL as string
},
} satisfies Config;
Naeemgg
NaeemggOP10mo ago
But when I'm running drizzle-kit push it shows this error:
No description
Naeemgg
NaeemggOP10mo ago
just to be clear about deps I deleted and installed node modules but its still same
{
"name": "learn_drizzle",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"db:generate": "drizzle-kit generate:pg --schema ./db/schema.ts --out=./db/migrations",
"migration:push": "drizzle-kit push:pg",
"migration:production:push": "drizzle-kit push:pg --config=./drizzle_production.config.ts"
},
"dependencies": {
"@neondatabase/serverless": "^0.9.0",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.29.4",
"next": "14.1.2",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"drizzle-kit": "^0.20.14",
"eslint": "^8",
"eslint-config-next": "14.1.2",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
{
"name": "learn_drizzle",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"db:generate": "drizzle-kit generate:pg --schema ./db/schema.ts --out=./db/migrations",
"migration:push": "drizzle-kit push:pg",
"migration:production:push": "drizzle-kit push:pg --config=./drizzle_production.config.ts"
},
"dependencies": {
"@neondatabase/serverless": "^0.9.0",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.29.4",
"next": "14.1.2",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"drizzle-kit": "^0.20.14",
"eslint": "^8",
"eslint-config-next": "14.1.2",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
This is my package.json file if it helps
amg
amg10mo ago
can yo try pnpx drizzle-kit push:pg ?
Naeemgg
NaeemggOP10mo ago
yes, sure give me a minute
Naeemgg
NaeemggOP10mo ago
No description
Naeemgg
NaeemggOP10mo ago
unable to open studio also
No description
amg
amg10mo ago
Where are you executing it? Your machine?
Naeemgg
NaeemggOP10mo ago
nope its a codespace
Naeemgg
NaeemggOP10mo ago
GitHub
GitHub - Naeem-gg/learn_drizzle
Contribute to Naeem-gg/learn_drizzle development by creating an account on GitHub.
Nate Vukovich
Nate Vukovich10mo ago
is it possible to tell Drizzle which setup script to run? I want to have one script to work with local docker postgres (postgresjs) and another to run on Vercel (@vercel/postgress)?
amg
amg10mo ago
you can specify different config file per script for example
"migration:development:push": "drizzle-kit push:sqlite",
"migration:production:push": "drizzle-kit push:sqlite --config=./drizzle_production.config.ts",
"migration:production:embedded:push": "drizzle-kit push:sqlite --config=./drizzle_embedded_replica.config.ts",
"migration:development:push": "drizzle-kit push:sqlite",
"migration:production:push": "drizzle-kit push:sqlite --config=./drizzle_production.config.ts",
"migration:production:embedded:push": "drizzle-kit push:sqlite --config=./drizzle_embedded_replica.config.ts",
were you asking that?
Naeemgg
NaeemggOP10mo ago
Hi @amg, Thanks for your support in getting me started with drizzle I can seamlessly deal with drizzle now
Naeemgg
NaeemggOP10mo ago
only one problem is there which is I think with pnpm,I have created an issue here also for it https://github.com/drizzle-team/drizzle-orm/issues/2032
GitHub
[BUG]:drizzle-kit commands not working with pnpx · Issue #2032 · dr...
What version of drizzle-orm are you using? 0.30.2 What version of drizzle-kit are you using? 0.20.14 Describe the Bug I was debugging my code when no drizzle-kit command was working, all the comman...
Nate Vukovich
Nate Vukovich10mo ago
Hi. Thanks for the response. I guess that could be it... Will have a try and I'll respond with the info 😄
megio
megio10mo ago
hi, I found this blog post really really helpful to get started with Vercel Postgres and Drizzle. Maybe you could find some answers to your questions. https://www.thisdot.co/blog/configure-your-project-with-drizzle-for-local-and-deployed-databases
Naeemgg
NaeemggOP10mo ago
🙌🏻😊
Want results from more Discord servers?
Add your server