{"id":23925252,"url":"https://github.com/programming-communities/drizzle-and-neon","last_synced_at":"2026-02-04T13:39:25.091Z","repository":{"id":262866123,"uuid":"888598068","full_name":"Programming-Communities/Drizzle-and-Neon","owner":"Programming-Communities","description":"Drizzle-and-Neon use in Next js 15","archived":false,"fork":false,"pushed_at":"2024-11-14T17:31:20.000Z","size":2737,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T09:34:08.055Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Programming-Communities.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-14T17:11:54.000Z","updated_at":"2024-12-23T03:50:41.000Z","dependencies_parsed_at":"2024-11-14T18:42:12.515Z","dependency_job_id":null,"html_url":"https://github.com/Programming-Communities/Drizzle-and-Neon","commit_stats":null,"previous_names":["programming-communities/drizzle-and-neon"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Programming-Communities%2FDrizzle-and-Neon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Programming-Communities%2FDrizzle-and-Neon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Programming-Communities%2FDrizzle-and-Neon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Programming-Communities%2FDrizzle-and-Neon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Programming-Communities","download_url":"https://codeload.github.com/Programming-Communities/Drizzle-and-Neon/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249730570,"owners_count":21317327,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-05T20:12:43.809Z","updated_at":"2026-02-04T13:39:25.045Z","avatar_url":"https://github.com/Programming-Communities.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![alt text](public/1.png)\nhttps://orm.drizzle.team/\n---\n\n## Project Overview and Setup\n\nHere’s what we’ll do:\n1. Set up a Neon database connection.\n2. Define the database structure (schema).\n3. Use Drizzle ORM to interact with the database.\n4. Seed and query the database with basic CRUD operations.\n\n### Project Structure\n\nYour project should look like this:\n\n```\n📦 \u003cproject root\u003e\n ├ 📂 drizzle         # Contains migration files and snapshots\n ├ 📂 src\n │   ├ 📂 db\n │   │  └ 📜 schema.js  # Database schema file\n │   └ 📜 index.js      # File for main database interactions\n ├ 📜 .env.local       # Contains sensitive environment variables\n ├ 📜 drizzle.config.js # Configuration file for Drizzle Kit\n ├ 📜 package.json\n```\n\n---\n\n### Step 1: Install Required Packages\n\nRun these commands in your project root:\n![alt text](public/2.png)\n```bash\nnpm i drizzle-orm @neondatabase/serverless dotenv\nnpm i -D drizzle-kit\n```\n\n**Explanation of Packages**:\n- **`drizzle-orm`**: Helps us interact with the database.\n- **`@neondatabase/serverless`**: Connects Drizzle to Neon.\n- **`dotenv`**: Manages environment variables securely.\n\n---\n\n### Step 2: Set Up Environment Variables\n\nCreate a `.env.local` file in your project root. Add your Neon database connection URL:\n\n```plaintext\nDATABASE_URL=your_neon_database_url\n```\n\n\u003e ⚠️ **Replace `your_neon_database_url`** with your actual Neon database connection link.\n\n---\n\n### Step 3: Connect Drizzle ORM to Neon\n\nIn `src/db`, create a file called `db.js` to handle the connection to Drizzle and Neon:\n\n```javascript\n// src/db/db.js\nimport { drizzle } from 'drizzle-orm/neon-http';\nimport dotenv from 'dotenv';\n\ndotenv.config(); // Loads environment variables\n\n// Initialize Drizzle connection\nconst db = drizzle(process.env.DATABASE_URL);\n\nexport default db;\n```\n\n---\n\n### Step 4: Define the Database Schema\n\nDefine tables and columns for Drizzle to use with Neon.\n\n1. In `src/db`, create a file called `schema.js`.\n2. Add the following code to create a simple `users` table:\n\n```javascript\n// src/db/schema.js\nimport { integer, pgTable, varchar } from 'drizzle-orm/pg-core';\n\n// Define \"users\" table\nexport const usersTable = pgTable('users', {\n  id: integer().primaryKey().generatedAlwaysAsIdentity(),\n  name: varchar({ length: 255 }).notNull(),\n  age: integer().notNull(),\n  email: varchar({ length: 255 }).notNull().unique(),\n});\n```\n\n---\n\n### Step 5: Configure Drizzle Kit\n\nDrizzle Kit helps manage migrations and other database tasks.\n\n1. In your project root, create a file named `drizzle.config.js`.\n2. Add the following content:\n\n```javascript\n// drizzle.config.js\nimport 'dotenv/config';\nimport { defineConfig } from 'drizzle-kit';\n\nexport default defineConfig({\n  out: './drizzle',\n  schema: './src/db/schema.js',\n  dialect: 'postgresql',\n  dbCredentials: {\n    url: process.env.DATABASE_URL,\n  },\n});\n```\n\nThis configuration points Drizzle to our schema file and sets up the Neon connection.\n\n---\n\n### Step 6: Applying Changes to the Database\n\nTo create tables and apply changes to the Neon database, use Drizzle Kit's `push` command:\n\n```bash\nnpx drizzle-kit push\n```\n\n\u003e **Tip**: This directly updates the database with the schema you defined.\n\nAlternatively, you can:\n1. **Generate migration files**: `npx drizzle-kit generate`\n2. **Apply migrations manually**: `npx drizzle-kit migrate`\n\n---\n\n### Step 7: Seed and Query the Database\n\nNow, let’s write basic code to interact with the database.\n\n1. In `src`, create an `index.js` file.\n2. Add the following code to create, read, update, and delete users in the `users` table:\n\n```javascript\n// src/index.js\nimport 'dotenv/config';\nimport { drizzle } from 'drizzle-orm/neon-http';\nimport { usersTable } from './db/schema';\n\nconst db = drizzle(process.env.DATABASE_URL);\n\nasync function main() {\n  // Create a new user\n  const newUser = { name: 'John', age: 30, email: 'john@example.com' };\n  await db.insert(usersTable).values(newUser);\n  console.log('New user created!');\n\n  // Read all users\n  const users = await db.select().from(usersTable);\n  console.log('Getting all users:', users);\n\n  // Update a user's age\n  await db.update(usersTable).set({ age: 31 }).where(usersTable.email.eq(newUser.email));\n  console.log('User updated!');\n\n  // Delete a user\n  await db.delete(usersTable).where(usersTable.email.eq(newUser.email));\n  console.log('User deleted!');\n}\n\nmain();\n```\n\nThis code performs basic CRUD operations:\n- **Create** a new user.\n- **Read** all users.\n- **Update** an existing user.\n- **Delete** the user by email.\n\n---\n\n### Step 8: Run the `index.js` File\n\nTo run this JavaScript file:\n\n```bash\nnode src/index.js\n```\n\n---\n\n### Tips for TypeScript Projects\n\nIf you ever switch to TypeScript, replace `.js` files with `.ts`, and add `tsx` to run TypeScript files with commands like `npx tsx src/index.ts`. \n\nLet me know if you need further clarification on any of these steps!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogramming-communities%2Fdrizzle-and-neon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogramming-communities%2Fdrizzle-and-neon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogramming-communities%2Fdrizzle-and-neon/lists"}