-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,5 @@ next-env.d.ts | |
robots.txt | ||
sitemap.xml | ||
sitemap-*.xml | ||
|
||
/src/data/db/dm/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |