Skip to content

Commit 9f1a893

Browse files
authored
Merge pull request #285 from ixartz/switch-postgres
feat: switch to Postgres in Drizzle ORM
2 parents 9d4a6cf + 07d1f17 commit 9f1a893

File tree

23 files changed

+6303
-3528
lines changed

23 files changed

+6303
-3528
lines changed

.env

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
# FIXME: Configure environment variables for your project
22

3-
# For security reason, don't push secret key in your git repo.
4-
# Append .local to the environment files to prevent your secret key from being commited to Git.
5-
6-
# Hosting
7-
# Replace by your domain name, only for production
8-
# NEXT_PUBLIC_APP_URL=https://example.com
9-
10-
# Database
11-
# Using an incorrect DATABASE_URL value, Next.js build will timeout and you will get the following error: "because it took more than 60 seconds"
12-
# DATABASE_URL=libsql://[RANDOM-CHARS]-[DB-NAME]-[ORG-NAME].turso.io
13-
DATABASE_URL=file:next-js-boilerplate.db
3+
# If you need to build a SaaS application with Stripe subscription payment with checkout page, customer portal, webhook, etc.
4+
# You can check out the Next.js Boilerplate SaaS: https://nextjs-boilerplate.com/pro-saas-starter-kit
145

156
# Clerk authentication
16-
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_b3Blbi1zdGlua2J1Zy04LmNsZXJrLmFjY291bnRzLmRldiQ
7+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_cmVsYXhlZC10dXJrZXktNjcuY2xlcmsuYWNjb3VudHMuZGV2JA
178

189
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
1910

@@ -22,7 +13,5 @@ NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
2213
######## After creating the file, you can add the following variables.
2314
CLERK_SECRET_KEY=your_clerk_secret_key
2415

25-
DATABASE_AUTH_TOKEN=your_database_auth_token
26-
2716
# LOGTAIL_SOURCE_TOKEN=
2817
######## [END] SENSITIVE DATA

.env.production

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# FIXME: Configure environment variables for production
2+
3+
# If you need to build a SaaS application with Stripe subscription payment with checkout page, customer portal, webhook, etc.
4+
# You can check out the Next.js Boilerplate SaaS: https://nextjs-boilerplate.com/pro-saas-starter-kit
5+
6+
# Hosting
7+
# Replace by your domain name
8+
NEXT_PUBLIC_APP_URL=https://example.com
9+
10+
######## [BEGIN] SENSITIVE DATA ######## For security reason, don't update the following variables (secret key) directly in this file.
11+
######## Please create a new file named `.env.production.local`, all environment files ending with `.local` won't be tracked by Git.
12+
######## After creating the file, you can add the following variables.
13+
# Database
14+
# Using an incorrect DATABASE_URL value, Next.js build will timeout and you will get the following error: "because it took more than 60 seconds"
15+
# DATABASE_URL=postgresql://postgres@localhost:5432/postgres
16+
######## [END] SENSITIVE DATA

.github/FUNDING.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
github: ixartz
22
custom:
3-
["https://donate.stripe.com/7sI5m5146ehfddm7tj", "https://nextlessjs.com"]
3+
[
4+
"https://nextjs-boilerplate.com/pro-saas-starter-kit",
5+
"https://nextlessjs.com",
6+
]

README.md

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<a href="https://creativedesignsguru.com/demo/Nextjs-Boilerplate/"><img height="300" src="public/assets/images/nextjs-starter-banner.png?raw=true" alt="Next js starter banner"></a>
55
</p>
66

7-
🚀 Boilerplate and Starter for Next.js with App Router support, Tailwind CSS, and TypeScript ⚡️ Prioritizing developer experience first: Next.js, TypeScript, ESLint, Prettier, Husky, Lint-Staged, Jest (replaced by Vitest), Testing Library, Commitlint, VSCode, PostCSS, Tailwind CSS, Authentication with [Clerk](https://clerk.com?utm_source=github&utm_medium=sponsorship&utm_campaign=nextjs-boilerplate), Database with DrizzleORM (SQLite, PostgreSQL, and MySQL) and [Turso](https://turso.tech/?utm_source=nextjsstarterbp), Error Monitoring with [Sentry](https://sentry.io/for/nextjs/?utm_source=github&utm_medium=paid-community&utm_campaign=general-fy25q1-nextjs&utm_content=github-banner-nextjsboilerplate-logo), Logging with Pino.js and Log Management, Monitoring as Code, Storybook, Multi-language (i18n), and more. Ready for Next.js 15.
7+
🚀 Boilerplate and Starter for Next.js with App Router support, Tailwind CSS, and TypeScript ⚡️ Prioritizing developer experience first: Next.js, TypeScript, ESLint, Prettier, Husky, Lint-Staged, Jest (replaced by Vitest), Testing Library, Commitlint, VSCode, PostCSS, Tailwind CSS, Authentication with [Clerk](https://clerk.com?utm_source=github&utm_medium=sponsorship&utm_campaign=nextjs-boilerplate), Database with DrizzleORM (PostgreSQL, SQLite, and MySQL), Error Monitoring with [Sentry](https://sentry.io/for/nextjs/?utm_source=github&utm_medium=paid-community&utm_campaign=general-fy25q1-nextjs&utm_content=github-banner-nextjsboilerplate-logo), Logging with Pino.js and Log Management, Monitoring as Code, Storybook, Multi-language (i18n), and more. Ready for Next.js 15.
88

99
Clone this project and use it to create your own [Next.js](https://nextjs.org) project. You can check a [Next js templates demo](https://creativedesignsguru.com/demo/Nextjs-Boilerplate/).
1010

@@ -21,15 +21,6 @@ Clone this project and use it to create your own [Next.js](https://nextjs.org) p
2121
</picture>
2222
</a>
2323
</td>
24-
<td align="center" width="33%">
25-
<a href="https://turso.tech/?utm_source=nextjsstarterbp">
26-
<picture>
27-
<source media="(prefers-color-scheme: dark)" srcset="public/assets/images/turso-light.png?raw=true">
28-
<source media="(prefers-color-scheme: light)" srcset="public/assets/images/turso-dark.png?raw=true">
29-
<img alt="Turso - SQLite for Production" src="public/assets/images/turso-dark.png?raw=true">
30-
</picture>
31-
</a>
32-
</td>
3324
<td align="center" width="33%">
3425
<a href="https://l.crowdin.com/next-js">
3526
<picture>
@@ -39,8 +30,6 @@ Clone this project and use it to create your own [Next.js](https://nextjs.org) p
3930
</picture>
4031
</a>
4132
</td>
42-
</tr>
43-
<tr height="187px">
4433
<td align="center" width="33%">
4534
<a href="https://sentry.io/for/nextjs/?utm_source=github&utm_medium=paid-community&utm_campaign=general-fy25q1-nextjs&utm_content=github-banner-nextjsboilerplate-logo">
4635
<picture>
@@ -57,6 +46,8 @@ Clone this project and use it to create your own [Next.js](https://nextjs.org) p
5746
</picture>
5847
</a>
5948
</td>
49+
</tr>
50+
<tr height="187px">
6051
<td align="center" width="33%">
6152
<a href="https://posthog.com/?utm_source=github&utm_medium=sponsorship&utm_campaign=next-js-boilerplate">
6253
<picture>
@@ -75,8 +66,6 @@ Clone this project and use it to create your own [Next.js](https://nextjs.org) p
7566
</picture>
7667
</a>
7768
</td>
78-
</tr>
79-
<tr height="187px">
8069
<td align="center" width="33%">
8170
<a href="https://www.checklyhq.com/?utm_source=github&utm_medium=sponsorship&utm_campaign=next-js-boilerplate">
8271
<picture>
@@ -86,6 +75,8 @@ Clone this project and use it to create your own [Next.js](https://nextjs.org) p
8675
</picture>
8776
</a>
8877
</td>
78+
</tr>
79+
<tr height="187px">
8980
<td align="center" style=width="33%">
9081
<a href="https://nextlessjs.com">
9182
<img src="public/assets/images/nextlessjs.png?raw=true" alt="React SaaS Boilerplate Next.js" />
@@ -111,8 +102,7 @@ Developer experience first, extremely flexible code structure and only keep what
111102
- ✅ Strict Mode for TypeScript and React 18
112103
- 🔒 Authentication with [Clerk](https://clerk.com?utm_source=github&utm_medium=sponsorship&utm_campaign=nextjs-boilerplate): Sign up, Sign in, Sign out, Forgot password, Reset password, and more.
113104
- 👤 Passwordless Authentication with Magic Links, Multi-Factor Auth (MFA), Social Auth (Google, Facebook, Twitter, GitHub, Apple, and more), Passwordless login with Passkeys, User Impersonation
114-
- 📦 Type-safe ORM with DrizzleORM, compatible with SQLite, PostgreSQL, and MySQL
115-
- 💽 Global Database with [Turso](https://turso.tech/?utm_source=nextjsstarterbp)
105+
- 📦 Type-safe ORM with DrizzleORM, compatible with PostgreSQL, SQLite, and MySQL
116106
- 🌐 Multi-language (i18n) with [next-intl](https://next-intl-docs.vercel.app/) and [Crowdin](https://l.crowdin.com/next-js)
117107
- ♻️ Type-safe environment variables with T3 Env
118108
- ⌨️ Form handling with React Hook Form
@@ -151,6 +141,7 @@ Built-in feature from Next.js:
151141
### Philosophy
152142

153143
- Nothing is hidden from you, so you have the freedom to make the necessary adjustments to fit your needs and preferences.
144+
- Dependencies are updated every month
154145
- Easy to customize
155146
- Minimal code
156147
- SEO-friendly
@@ -170,6 +161,8 @@ cd my-project-name
170161
npm install
171162
```
172163

164+
For your information, all dependencies are updated every month.
165+
173166
Then, you can run the project locally in development mode with live reload by executing:
174167

175168
```shell
@@ -191,38 +184,7 @@ Now, you have a fully working authentication system with Next.js: Sign up, Sign
191184

192185
### Set up remote database
193186

194-
The project uses DrizzleORM, a type-safe ORM compatible with SQLite, PostgreSQL, and MySQL databases. By default, the project is set up to work seamlessly with libSQL, and for production purposes, it's integrated with [Turso](https://turso.tech/?utm_source=nextjsstarterbp). The Next.js Boilerplate also enables a smooth transition to an alternative database provider if your project requires it.
195-
196-
First, you need to create a Turso account at [Turso.tech](https://turso.tech/?utm_source=nextjsstarterbp) and install the Turso CLI:
197-
198-
```shell
199-
brew install tursodatabase/tap/turso
200-
turso auth signup # Sign up to Turso
201-
```
202-
203-
Then, create a new database:
204-
205-
```shell
206-
turso db create nextjs-boilerplate
207-
```
208-
209-
Now, you need to update the `DATABASE_URL` in `.env` file with the database URL provided by Turso:
210-
211-
```shell
212-
turso db show nextjs-boilerplate --url
213-
214-
# .env
215-
# DATABASE_URL=libsql://[RANDOM-CHARS]-[DB-NAME]-[ORG-NAME].turso.io
216-
```
217-
218-
Finally, you also need to create a new environment variable `DATABASE_AUTH_TOKEN` in `.env.local` (not tracked by Git) with the auth token provided by Turso:
219-
220-
```shell
221-
turso db tokens create nextjs-boilerplate
222-
223-
# .env.local
224-
# DATABASE_AUTH_TOKEN=[your-auth-token]
225-
```
187+
The project uses DrizzleORM, a type-safe ORM compatible with PostgreSQL, SQLite, and MySQL databases. By default, the project is set up to work seamlessly with PostgreSQL and you can easily choose any PostgreSQL database provider.
226188

227189
### Translation (i18n) setup
228190

@@ -303,18 +265,16 @@ npm run test:e2e
303265

304266
### Enable Edge runtime (optional)
305267

306-
The App Router folder is compatible with the Edge runtime. You can enable it by uncommenting the following lines `src/app/layouts.tsx`:
268+
The App Router folder is compatible with the Edge runtime. You can enable it by adding the following lines `src/app/layouts.tsx`:
307269

308270
```tsx
309-
// export const runtime = 'edge';
271+
export const runtime = 'edge';
310272
```
311273

312274
For your information, the database migration is not compatible with the Edge runtime. So, you need to disable the automatic migration in `src/libs/DB.ts`:
313275

314276
```tsx
315-
if (process.env.NODE_ENV === 'development') {
316-
await migrate(db, { migrationsFolder: './migrations' });
317-
}
277+
await migrate(db, { migrationsFolder: './migrations' });
318278
```
319279

320280
After disabling it, you are required to run the migration manually with:
@@ -327,7 +287,7 @@ You also require to run the command each time you want to update the database sc
327287

328288
### Deploy to production
329289

330-
During the build process, the database migration is automatically executed. So, you don't need to run the migration manually. But, in your environment variable, `DATABASE_URL` and `DATABASE_AUTH_TOKEN` need to be defined.
290+
During the build process, the database migration is automatically executed. So, you don't need to run the migration manually. But, in your environment variable, `DATABASE_URL` need to be defined.
331291

332292
Then, you can generate a production build with:
333293

@@ -426,15 +386,6 @@ See [LICENSE](LICENSE) for more information.
426386
</picture>
427387
</a>
428388
</td>
429-
<td align="center" width="33%">
430-
<a href="https://turso.tech/?utm_source=nextjsstarterbp">
431-
<picture>
432-
<source media="(prefers-color-scheme: dark)" srcset="public/assets/images/turso-light.png?raw=true">
433-
<source media="(prefers-color-scheme: light)" srcset="public/assets/images/turso-dark.png?raw=true">
434-
<img alt="Turso - SQLite for Production" src="public/assets/images/turso-dark.png?raw=true">
435-
</picture>
436-
</a>
437-
</td>
438389
<td align="center" width="33%">
439390
<a href="https://l.crowdin.com/next-js">
440391
<picture>
@@ -444,8 +395,6 @@ See [LICENSE](LICENSE) for more information.
444395
</picture>
445396
</a>
446397
</td>
447-
</tr>
448-
<tr height="187px">
449398
<td align="center" width="33%">
450399
<a href="https://sentry.io/for/nextjs/?utm_source=github&utm_medium=paid-community&utm_campaign=general-fy25q1-nextjs&utm_content=github-banner-nextjsboilerplate-logo">
451400
<picture>
@@ -462,6 +411,8 @@ See [LICENSE](LICENSE) for more information.
462411
</picture>
463412
</a>
464413
</td>
414+
</tr>
415+
<tr height="187px">
465416
<td align="center" width="33%">
466417
<a href="https://posthog.com/?utm_source=github&utm_medium=sponsorship&utm_campaign=next-js-boilerplate">
467418
<picture>
@@ -480,8 +431,6 @@ See [LICENSE](LICENSE) for more information.
480431
</picture>
481432
</a>
482433
</td>
483-
</tr>
484-
<tr height="187px">
485434
<td align="center" width="33%">
486435
<a href="https://www.checklyhq.com/?utm_source=github&utm_medium=sponsorship&utm_campaign=next-js-boilerplate">
487436
<picture>
@@ -491,6 +440,8 @@ See [LICENSE](LICENSE) for more information.
491440
</picture>
492441
</a>
493442
</td>
443+
</tr>
444+
<tr height="187px">
494445
<td align="center" style=width="33%">
495446
<a href="https://nextlessjs.com">
496447
<img src="public/assets/images/nextlessjs.png?raw=true" alt="React SaaS Boilerplate Next.js" />

drizzle.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import type { Config } from 'drizzle-kit';
44
export default {
55
out: './migrations',
66
schema: './src/models/Schema.ts',
7-
dialect: 'sqlite',
8-
driver: 'turso',
7+
dialect: 'postgresql',
98
dbCredentials: {
109
url: process.env.DATABASE_URL ?? '',
11-
authToken: process.env.DATABASE_AUTH_TOKEN ?? '',
1210
},
1311
verbose: true,
1412
strict: true,

migrations/0000_init_db.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
CREATE TABLE `guestbook` (
2-
`id` integer PRIMARY KEY NOT NULL,
3-
`username` text NOT NULL,
4-
`body` text NOT NULL,
5-
`created_at` integer DEFAULT (strftime('%s', 'now')),
6-
`updated_at` integer DEFAULT (strftime('%s', 'now'))
1+
CREATE TABLE IF NOT EXISTS "guestbook" (
2+
"id" serial NOT NULL,
3+
"username" text NOT NULL,
4+
"body" text NOT NULL,
5+
"updated_at" timestamp DEFAULT now() NOT NULL,
6+
"created_at" timestamp DEFAULT now() NOT NULL
77
);

migrations/meta/0000_snapshot.json

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
{
2-
"version": "6",
3-
"dialect": "sqlite",
4-
"id": "d3078f57-71ae-450f-ba1c-af29b40f3a97",
2+
"id": "476c2d87-fe0f-47d2-9386-42280f661547",
53
"prevId": "00000000-0000-0000-0000-000000000000",
4+
"version": "7",
5+
"dialect": "postgresql",
66
"tables": {
7-
"guestbook": {
7+
"public.guestbook": {
88
"name": "guestbook",
9+
"schema": "",
910
"columns": {
1011
"id": {
1112
"name": "id",
12-
"type": "integer",
13-
"primaryKey": true,
14-
"notNull": true,
15-
"autoincrement": false
13+
"type": "serial",
14+
"primaryKey": false,
15+
"notNull": true
1616
},
1717
"username": {
1818
"name": "username",
1919
"type": "text",
2020
"primaryKey": false,
21-
"notNull": true,
22-
"autoincrement": false
21+
"notNull": true
2322
},
2423
"body": {
2524
"name": "body",
2625
"type": "text",
2726
"primaryKey": false,
27+
"notNull": true
28+
},
29+
"updated_at": {
30+
"name": "updated_at",
31+
"type": "timestamp",
32+
"primaryKey": false,
2833
"notNull": true,
29-
"autoincrement": false
34+
"default": "now()"
3035
},
3136
"created_at": {
3237
"name": "created_at",
33-
"type": "integer",
34-
"primaryKey": false,
35-
"notNull": false,
36-
"autoincrement": false,
37-
"default": "(strftime('%s', 'now'))"
38-
},
39-
"updated_at": {
40-
"name": "updated_at",
41-
"type": "integer",
38+
"type": "timestamp",
4239
"primaryKey": false,
43-
"notNull": false,
44-
"autoincrement": false,
45-
"default": "(strftime('%s', 'now'))"
40+
"notNull": true,
41+
"default": "now()"
4642
}
4743
},
4844
"indexes": {},
@@ -52,9 +48,10 @@
5248
}
5349
},
5450
"enums": {},
51+
"schemas": {},
5552
"_meta": {
53+
"columns": {},
5654
"schemas": {},
57-
"tables": {},
58-
"columns": {}
55+
"tables": {}
5956
}
6057
}

migrations/meta/_journal.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"version": "6",
3-
"dialect": "sqlite",
2+
"version": "7",
3+
"dialect": "postgresql",
44
"entries": [
55
{
66
"idx": 0,
7-
"version": "6",
8-
"when": 1716976326006,
7+
"version": "7",
8+
"when": 1719279419936,
99
"tag": "0000_init_db",
1010
"breakpoints": true
1111
}

0 commit comments

Comments
 (0)