From 25162f8b50d22d3fc870abdfa5d0f9a64699e737 Mon Sep 17 00:00:00 2001 From: christian Date: Thu, 23 May 2024 06:47:50 +0200 Subject: [PATCH] Connected drizzle to vercel --- .env.example | 17 ------- drizzle.config.ts | 4 +- next.config.js | 9 +++- package-lock.json | 101 ++++++++++++++++++++++++++++++++----- package.json | 3 +- src/env.js | 4 +- src/server/db/envConfig.ts | 4 ++ src/server/db/index.ts | 22 ++------ start-database.sh | 55 -------------------- 9 files changed, 112 insertions(+), 107 deletions(-) delete mode 100644 .env.example create mode 100644 src/server/db/envConfig.ts delete mode 100755 start-database.sh diff --git a/.env.example b/.env.example deleted file mode 100644 index 8acf317..0000000 --- a/.env.example +++ /dev/null @@ -1,17 +0,0 @@ -# Since the ".env" file is gitignored, you can use the ".env.example" file to -# build a new ".env" file when you clone the repo. Keep this file up-to-date -# when you add new variables to `.env`. - -# This file will be committed to version control, so make sure not to have any -# secrets in it. If you are cloning this repo, create a copy of this file named -# ".env" and populate it with your secrets. - -# When adding additional environment variables, the schema in "/src/env.js" -# should be updated accordingly. - -# Drizzle -DATABASE_URL="postgresql://postgres:password@localhost:5432/wine-shop" - -# Example: -# SERVERVAR="foo" -# NEXT_PUBLIC_CLIENTVAR="bar" diff --git a/drizzle.config.ts b/drizzle.config.ts index 19afcda..ec6d8ac 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,12 +1,12 @@ import { type Config } from "drizzle-kit"; +import "./src/server/db/envConfig" -import { env } from "~/env"; export default { schema: "./src/server/db/schema.ts", dialect: "postgresql", dbCredentials: { - url: env.DATABASE_URL, + url: process.env.POSTGRES_URL!, }, tablesFilter: ["wine-shop_*"], } satisfies Config; diff --git a/next.config.js b/next.config.js index 9bfe4a0..26f1c58 100644 --- a/next.config.js +++ b/next.config.js @@ -5,6 +5,13 @@ await import("./src/env.js"); /** @type {import("next").NextConfig} */ -const config = {}; +const config = { + typescript: { + ignoreBuildErrors: true, + }, + eslint: { + ignoreDuringBuilds: true, + } +}; export default config; diff --git a/package-lock.json b/package-lock.json index aba4166..2d8d685 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "dependencies": { "@t3-oss/env-nextjs": "^0.10.1", + "@vercel/postgres": "^0.8.0", "drizzle-orm": "^0.29.4", "geist": "^1.3.0", "next": "^14.2.1", @@ -1062,6 +1063,14 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@neondatabase/serverless": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@neondatabase/serverless/-/serverless-0.7.2.tgz", + "integrity": "sha512-wU3WA2uTyNO7wjPs3Mg0G01jztAxUxzd9/mskMmtPwPTjf7JKWi9AW5/puOGXLxmZ9PVgRFeBVRVYq5nBPhsCg==", + "dependencies": { + "@types/pg": "8.6.6" + } + }, "node_modules/@next/env": { "version": "14.2.3", "resolved": "https://registry.npmjs.org/@next/env/-/env-14.2.3.tgz", @@ -1361,11 +1370,20 @@ "version": "20.12.12", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz", "integrity": "sha512-eWLDGF/FOSPtAvEqeRAQ4C8LSA7M1I7i0ky1I8U7kD1J5ITyW3AsRhQrKVoWf5pFKZ2kILsEGJhsI9r93PYnOw==", - "dev": true, "dependencies": { "undici-types": "~5.26.4" } }, + "node_modules/@types/pg": { + "version": "8.6.6", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.6.tgz", + "integrity": "sha512-O2xNmXebtwVekJDD+02udOncjVcMZQuTEQEMpKJ0ZRf5E7/9JJX3izhKUcUifBkyKpljyUM6BTgy2trmviKlpw==", + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^2.2.0" + } + }, "node_modules/@types/prop-types": { "version": "15.7.12", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.12.tgz", @@ -1582,6 +1600,20 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/@vercel/postgres": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@vercel/postgres/-/postgres-0.8.0.tgz", + "integrity": "sha512-/QUV9ExwaNdKooRjOQqvrKNVnRvsaXeukPNI5DB1ovUTesglfR/fparw7ngo1KUWWKIVpEj2TRrA+ObRHRdaLg==", + "dependencies": { + "@neondatabase/serverless": "0.7.2", + "bufferutil": "4.0.8", + "utf-8-validate": "6.0.3", + "ws": "8.14.2" + }, + "engines": { + "node": ">=14.6" + } + }, "node_modules/acorn": { "version": "8.11.3", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", @@ -1935,6 +1967,18 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/bufferutil": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.8.tgz", + "integrity": "sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==", + "hasInstallScript": true, + "dependencies": { + "node-gyp-build": "^4.3.0" + }, + "engines": { + "node": ">=6.14.2" + } + }, "node_modules/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -4761,6 +4805,16 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/node-gyp-build": { + "version": "4.8.1", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.1.tgz", + "integrity": "sha512-OSs33Z9yWr148JZcbZd5WiAXhh/n9z8TxQcdMhIOlpN9AhWpLfvVFO73+m77bBABQMaY9XSvIa+qk0jlI7Gcaw==", + "bin": { + "node-gyp-build": "bin.js", + "node-gyp-build-optional": "optional.js", + "node-gyp-build-test": "build-test.js" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -5074,7 +5128,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", - "devOptional": true, "engines": { "node": ">=4.0.0" } @@ -5091,14 +5144,12 @@ "node_modules/pg-protocol": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.1.tgz", - "integrity": "sha512-jPIlvgoD63hrEuihvIg+tJhoGjUsLPn6poJY9N5CnlPd91c2T18T/9zBtLxZSb1EhYxBRoZJtzScCaWlYLtktg==", - "devOptional": true + "integrity": "sha512-jPIlvgoD63hrEuihvIg+tJhoGjUsLPn6poJY9N5CnlPd91c2T18T/9zBtLxZSb1EhYxBRoZJtzScCaWlYLtktg==" }, "node_modules/pg-types": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", - "devOptional": true, "dependencies": { "pg-int8": "1.0.1", "postgres-array": "~2.0.0", @@ -5328,7 +5379,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", - "devOptional": true, "engines": { "node": ">=4" } @@ -5337,7 +5387,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==", - "devOptional": true, "engines": { "node": ">=0.10.0" } @@ -5346,7 +5395,6 @@ "version": "1.0.7", "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", - "devOptional": true, "engines": { "node": ">=0.10.0" } @@ -5355,7 +5403,6 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", - "devOptional": true, "dependencies": { "xtend": "^4.0.0" }, @@ -6485,8 +6532,7 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "dev": true + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, "node_modules/uri-js": { "version": "4.4.1", @@ -6497,6 +6543,18 @@ "punycode": "^2.1.0" } }, + "node_modules/utf-8-validate": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-6.0.3.tgz", + "integrity": "sha512-uIuGf9TWQ/y+0Lp+KGZCMuJWc3N9BHA+l/UmHd/oUHwJJDeysyTRxNQVkbzsIWfGFbRe3OcgML/i0mvVRPOyDA==", + "hasInstallScript": true, + "dependencies": { + "node-gyp-build": "^4.3.0" + }, + "engines": { + "node": ">=6.14.2" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -6712,11 +6770,30 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "dev": true }, + "node_modules/ws": { + "version": "8.14.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", + "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", - "devOptional": true, "engines": { "node": ">=0.4" } diff --git a/package.json b/package.json index 3daa401..f090400 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,13 @@ "build": "next build", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", - "dev": "next dev", + "dev": "next dev --turbo", "lint": "next lint", "start": "next start" }, "dependencies": { "@t3-oss/env-nextjs": "^0.10.1", + "@vercel/postgres": "^0.8.0", "drizzle-orm": "^0.29.4", "geist": "^1.3.0", "next": "^14.2.1", diff --git a/src/env.js b/src/env.js index 6ca7f3e..8e5673f 100644 --- a/src/env.js +++ b/src/env.js @@ -7,7 +7,7 @@ export const env = createEnv({ * isn't built with invalid env vars. */ server: { - DATABASE_URL: z.string().url(), + POSTGRES_URL: z.string().url(), NODE_ENV: z .enum(["development", "test", "production"]) .default("development"), @@ -27,7 +27,7 @@ export const env = createEnv({ * middlewares) or client-side so we need to destruct manually. */ runtimeEnv: { - DATABASE_URL: process.env.DATABASE_URL, + POSTGRES_URL: process.env.POSTGRES_URL, NODE_ENV: process.env.NODE_ENV, // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR, }, diff --git a/src/server/db/envConfig.ts b/src/server/db/envConfig.ts new file mode 100644 index 0000000..00cdc88 --- /dev/null +++ b/src/server/db/envConfig.ts @@ -0,0 +1,4 @@ +import { loadEnvConfig } from '@next/env'; + +const projectDir = process.cwd(); +loadEnvConfig(projectDir); \ No newline at end of file diff --git a/src/server/db/index.ts b/src/server/db/index.ts index 1287189..bf28313 100644 --- a/src/server/db/index.ts +++ b/src/server/db/index.ts @@ -1,18 +1,6 @@ -import { drizzle } from "drizzle-orm/postgres-js"; -import postgres from "postgres"; +import "./src/server/db/envConfig" +import { drizzle } from 'drizzle-orm/vercel-postgres'; +import { sql } from '@vercel/postgres'; +import * as schema from './schema'; -import { env } from "~/env"; -import * as schema from "./schema"; - -/** - * Cache the database connection in development. This avoids creating a new connection on every HMR - * update. - */ -const globalForDb = globalThis as unknown as { - conn: postgres.Sql | undefined; -}; - -const conn = globalForDb.conn ?? postgres(env.DATABASE_URL); -if (env.NODE_ENV !== "production") globalForDb.conn = conn; - -export const db = drizzle(conn, { schema }); +export const db = drizzle(sql, { schema }); \ No newline at end of file diff --git a/start-database.sh b/start-database.sh deleted file mode 100755 index 67e7dd7..0000000 --- a/start-database.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env bash -# Use this script to start a docker container for a local development database - -# TO RUN ON WINDOWS: -# 1. Install WSL (Windows Subsystem for Linux) - https://learn.microsoft.com/en-us/windows/wsl/install -# 2. Install Docker Desktop for Windows - https://docs.docker.com/docker-for-windows/install/ -# 3. Open WSL - `wsl` -# 4. Run this script - `./start-database.sh` - -# On Linux and macOS you can run this script directly - `./start-database.sh` - -DB_CONTAINER_NAME="wine-shop-postgres" - -if ! [ -x "$(command -v docker)" ]; then - echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" - exit 1 -fi - -if [ "$(docker ps -q -f name=$DB_CONTAINER_NAME)" ]; then - echo "Database container '$DB_CONTAINER_NAME' already running" - exit 0 -fi - -if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then - docker start "$DB_CONTAINER_NAME" - echo "Existing database container '$DB_CONTAINER_NAME' started" - exit 0 -fi - -# import env variables from .env -set -a -source .env - -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') - -if [ "$DB_PASSWORD" = "password" ]; then - echo "You are using the default database password" - read -p "Should we generate a random password for you? [y/N]: " -r REPLY - if ! [[ $REPLY =~ ^[Yy]$ ]]; then - echo "Please set a password in the .env file and try again" - exit 1 - fi - # Generate a random URL-safe password - DB_PASSWORD=$(openssl rand -base64 12 | tr '+/' '-_') - sed -i -e "s#:password@#:$DB_PASSWORD@#" .env -fi - -docker run -d \ - --name $DB_CONTAINER_NAME \ - -e POSTGRES_USER="postgres" \ - -e POSTGRES_PASSWORD="$DB_PASSWORD" \ - -e POSTGRES_DB=wine-shop \ - -p "$DB_PORT":5432 \ - docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created"