Skip to content

Commit

Permalink
🦫 feat(db): add drizzle config
Browse files Browse the repository at this point in the history
  • Loading branch information
blefnk committed Aug 16, 2023
1 parent 6247d5c commit 4dbcf8d
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ next-env.d.ts
robots.txt
sitemap.xml
sitemap-*.xml

/src/data/db/dm/
16 changes: 16 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Config } from "drizzle-kit";

import "dotenv/config";

if (!process.env.NEXT_SECRET_URL_PSCALE) {
throw new Error("NEXT_SECRET_URL_PSCALE is missing");
}

export default {
schema: "./src/data/db/schema.ts",
out: "./src/data/db/dm",
driver: "mysql2",
dbCredentials: {
connectionString: process.env.NEXT_SECRET_URL_PSCALE
}
} satisfies Config;
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
"scripts": {
"build": "next build",
"canary": "pnpm add react@canary react-dom@canary next@canary",
"db:introspect": "drizzle-kit introspect:mysql",
"db:push": "drizzle-kit push:mysql",
"db:studio": "drizzle-kit studio",
"db:introspect": "drizzle-kit introspect:mysql --config drizzle.config.ts",
"db:push": "drizzle-kit push:mysql --config drizzle.config.ts",
"db:studio": "drizzle-kit studio --config drizzle.config.ts",
"dev": "next dev",
"edge": "pnpm up --latest && pnpm canary",
"email:dev": "email dev --dir src/islands/emails -p 3001",
"format": "prettier --write \"**/*.{ts,tsx,mdx}\" --cache",
"format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache",
"postinstall": "drizzle-kit generate:mysql",
"postinstall": "drizzle-kit generate:mysql --config drizzle.config.ts",
"lint": "next lint",
"lint:fix": "next lint --fix",
"prepare": "husky install",
"shadcn": "pnpm dlx shadcn-ui@latest add",
"start": "next start",
"stripe:listen": "stripe listen --forward-to localhost:3000/api/webhooks/stripe --latest",
Expand All @@ -41,6 +40,7 @@
},
"dependencies": {
"@hookform/resolvers": "^3.2.0",
"@planetscale/database": "^1.10.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-dropdown-menu": "^2.0.5",
Expand All @@ -59,6 +59,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"cmdk": "^0.2.0",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.28.2",
"ky": "^0.33.3",
"lucia": "^2.3.0",
Expand Down
21 changes: 19 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/data/db/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { connect } from "@planetscale/database";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { drizzle } from "drizzle-orm/planetscale-serverless";

// create the connection
const connection = connect({
host: process.env["DATABASE_HOST"],
username: process.env["DATABASE_USERNAME"],
password: process.env["DATABASE_PASSWORD"]
});

export const db = drizzle(connection);

// syncs the migrations folder to PlanetScale
process.env.NODE_ENV === "development" &&
migrate(db as any, { migrationsFolder: "./migrations" })
.then((res) => res)
.catch((err) => console.log("Migration error in db.ts:", err));
96 changes: 96 additions & 0 deletions src/data/db/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { InferModel } from "drizzle-orm";
import {
boolean,
decimal,
int,
json,
mysqlTable,
serial,
text,
uniqueIndex,
varchar
} from "drizzle-orm/mysql-core";

export const stores = mysqlTable(
"stores",
{
id: serial("id").primaryKey(),
name: varchar("store_name", { length: 40 }),
industry: text("industry"),
description: text("description"),
slug: varchar("slug", { length: 50 })
},
(table) => {
return {
storeNameIndex: uniqueIndex("store_name_index").on(table.name),
storeSlugIndex: uniqueIndex("store_slug_index").on(table.slug)
};
}
);
export type Store = InferModel<typeof stores>;

export const products = mysqlTable("products", {
id: serial("id").primaryKey(),
name: text("name"),
price: decimal("price", { precision: 10, scale: 2 }).default("0"),
description: text("description"),
inventory: decimal("inventory").default("0"),
images: json("images"),
storeId: int("store_id")
});
export type Product = InferModel<typeof products>;

export const carts = mysqlTable("carts", {
id: serial("id").primaryKey(),
items: json("items"),
paymentIntentId: text("payment_intent_id"),
clientSecret: text("client_secret"),
isClosed: boolean("is_closed").default(false)
});
export type Cart = InferModel<typeof carts>;

export const payments = mysqlTable("payments", {
id: serial("id").primaryKey(),
storeId: int("store_id"),
stripeAccountId: text("stripe_account_id"),
stripeAccountCreatedAt: int("stripe_account_created_at"),
stripeAccountExpiresAt: int("stripe_account_expires_at"),
details_submitted: boolean("details_submitted").default(false)
});
export type Payment = InferModel<typeof payments>;

export const orders = mysqlTable(
"orders",
{
id: serial("id").primaryKey(),
prettyOrderId: int("pretty_order_id"),
storeId: int("store_id"),
items: json("items"),
total: decimal("total", { precision: 10, scale: 2 }).default("0"),
stripePaymentIntentId: varchar("stripe_payment_intent_id", { length: 256 }), // text field is valid for uniqueIndex in MySQL
stripePaymentIntentStatus: text("stripe_payment_intent_status"),
name: text("name"),
email: text("email"),
createdAt: int("created_at"),
addressId: int("address")
},
(table) => {
return {
stripePaymentIntentIdIndex: uniqueIndex(
"stripe_payment_intent_id_index"
).on(table.stripePaymentIntentId)
};
}
);
export type Order = InferModel<typeof orders>;

export const addresses = mysqlTable("addresses", {
id: serial("id").primaryKey(),
line1: text("line1"),
line2: text("line2"),
city: text("city"),
state: text("state"),
postal_code: text("postal_code"),
country: text("country")
});
export type Address = InferModel<typeof addresses>;

0 comments on commit 4dbcf8d

Please sign in to comment.