- Node.js and npm installed
- PostgreSQL database (Neon)
- ImageKit account
npm install drizzle-orm @neondatabase/serverless
npm install -D drizzle-kitCreate lib/db/schema.ts:
import { pgTable, text, timestamp, boolean, integer } from "drizzle-orm/pg-core";
import { createId } from "@paralleldrive/cuid2";
export const files = pgTable("files", {
id: text("id").primaryKey().$defaultFn(() => createId()),
name: text("name").notNull(),
path: text("path").notNull(),
size: integer("size").notNull(),
type: text("type").notNull(),
fileUrl: text("file_url"),
thumbnailUrl: text("thumbnail_url"),
userId: text("user_id").notNull(),
parentId: text("parent_id"),
isFolder: boolean("is_folder").default(false),
isStarred: boolean("is_starred").default(false),
isTrash: boolean("is_trash").default(false),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});Create lib/db/index.ts:
import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";
import * as schema from "./schema";
const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql, { schema });Create drizzle.config.ts in the root directory:
import type { Config } from "drizzle-kit";
export default {
schema: "./lib/db/schema.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
} satisfies Config;Add to your .env file:
DATABASE_URL="your-postgres-connection-string"Add to your package.json:
{
"scripts": {
"db:generate": "drizzle-kit generate:pg",
"db:push": "drizzle-kit push:pg",
"db:studio": "drizzle-kit studio"
}
}Run these commands:
npm run db:generate # Generate migrations
npm run db:push # Push schema to databaseYou can now use the Drizzle ORM in your application to interact with the database:
import { db } from "@/lib/db";
import { files } from "@/lib/db/schema";
// Example: Insert a new file
const newFile = await db.insert(files).values({
name: "example.txt",
path: "/files/example.txt",
size: 1024,
type: "text/plain",
userId: "user123"
}).returning();That's all you need for a basic Drizzle setup! The schema includes a files table with all necessary fields for file management.