Skip to content

Commit 945dfaf

Browse files
authored
fix: drizzle add-on outdated and broken (#251)
* fix: upgrade drizzle dependencies / fix .env load / fix mysql option * fix: remove unnecessary dotenv config in db connection * fix: simplify drizzle connection file
1 parent b92f001 commit 945dfaf

File tree

4 files changed

+19
-38
lines changed

4 files changed

+19
-38
lines changed

frameworks/react-cra/add-ons/drizzle/assets/drizzle.config.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { config } from "dotenv";
22
import { defineConfig } from 'drizzle-kit';
33

4-
config();
4+
config({ path: ['.env.local', '.env'] });
55

66
export default defineConfig({
77
out: "./drizzle",
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
import { config } from 'dotenv'
2-
<% if (addOnOption.drizzle.database === 'postgresql') { %>
3-
import { drizzle } from 'drizzle-orm/node-postgres';
4-
import { Pool } from 'pg';
1+
import { drizzle } from <% if (addOnOption.drizzle.database === 'postgresql') { %>
2+
'drizzle-orm/node-postgres';
53
<% } else if (addOnOption.drizzle.database === 'mysql') {%>
6-
import { drizzle } from 'drizzle-orm/mysql2';
7-
import mysql from 'mysql2/promise';
4+
'drizzle-orm/mysql2';
85
<% } else if (addOnOption.drizzle.database === 'sqlite') {%>
9-
import { drizzle } from 'drizzle-orm/better-sqlite3';
10-
import Database from 'better-sqlite3';
6+
'drizzle-orm/better-sqlite3';
117
<% } %>
128
import * as schema from './schema.ts'
139

14-
config()
15-
16-
<% if (addOnOption.drizzle.database === 'sqlite') { %>
17-
const sqlite = new Database(process.env.DATABASE_URL!);
18-
export const db = drizzle(sqlite, { schema });
19-
<% } else if (addOnOption.drizzle.database === 'postgresql') { %>
20-
const pool = new Pool({
21-
connectionString: process.env.DATABASE_URL!,
22-
});
23-
export const db = drizzle(pool, { schema });
24-
<% } else if (addOnOption.drizzle.database === 'mysql') { %>
25-
const connection = await mysql.createConnection(process.env.DATABASE_URL!);
26-
export const db = drizzle(connection, { schema });
27-
<% } %>
10+
export const db = drizzle(process.env.DATABASE_URL!, { schema<% if (addOnOption.drizzle.database === 'mysql') {%>, mode: 'default'<% } %>});

frameworks/react-cra/add-ons/drizzle/assets/src/db/schema.ts.ejs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { pgTable, serial, text, timestamp } from
33
'drizzle-orm/pg-core';
44
55
export const todos = pgTable('todos', {
6-
id: serial('id').primaryKey(),
7-
title: text('title').notNull(),
6+
id: serial().primaryKey(),
7+
title: text().notNull(),
88
createdAt: timestamp('created_at').defaultNow(),
99
});
1010
<% } else if (addOnOption.drizzle.database === 'mysql') {
@@ -13,8 +13,8 @@ import { mysqlTable, int, text, timestamp } from
1313
'drizzle-orm/mysql-core';
1414
1515
export const todos = mysqlTable('todos', {
16-
id: int('id').primaryKey().autoincrement(),
17-
title: text('title').notNull(),
16+
id: int().primaryKey().autoincrement(),
17+
title: text().notNull(),
1818
createdAt: timestamp('created_at', { mode: 'date' }).defaultNow(),
1919
});
2020
<% } else if (addOnOption.drizzle.database === 'sqlite') {
@@ -24,9 +24,9 @@ import { sqliteTable, integer, text } from
2424
import { sql } from 'drizzle-orm';
2525
2626
export const todos = sqliteTable('todos', {
27-
id: integer('id', { mode: 'number' }).primaryKey({
27+
id: integer({ mode: 'number' }).primaryKey({
2828
autoIncrement: true }),
29-
title: text('title').notNull(),
29+
title: text().notNull(),
3030
createdAt: integer('created_at', { mode: 'timestamp' }).default(sql`(unixepoch())`),
3131
});
3232
<% } %>

frameworks/react-cra/add-ons/drizzle/package.json.ejs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
{
22
"dependencies": {
3-
"drizzle-orm": "^0.39.0",
4-
"drizzle-kit": "^0.30.0"<% if (addOnOption.drizzle.database === 'postgresql') { %>,
5-
"pg": "^8.11.0"<% } %><% if (addOnOption.drizzle.database === 'mysql') { %>,
6-
"mysql2": "^3.6.0"<% } %><% if (addOnOption.drizzle.database === 'sqlite') { %>,
7-
"better-sqlite3": "^9.4.0"<% } %>
3+
"drizzle-orm": "^0.45.0",
4+
"drizzle-kit": "^0.31.8"<% if (addOnOption.drizzle.database === 'postgresql') { %>,
5+
"pg": "^8.16.3"<% } %><% if (addOnOption.drizzle.database === 'mysql') { %>,
6+
"mysql2": "^3.15.3"<% } %><% if (addOnOption.drizzle.database === 'sqlite') { %>,
7+
"better-sqlite3": "^12.5.0"<% } %>
88
},
99
"devDependencies": {
1010
"dotenv": "^16.0.0",
11-
"tsx": "^4.0.0",
12-
<% if (addOnOption.drizzle.database === 'postgresql') { %>
13-
"@types/pg": "^8.10.0"<% } %><% if (addOnOption.drizzle.database === 'mysql') { %>
14-
"@types/mysql2": "^3.6.0"<% } %><% if (addOnOption.drizzle.database === 'sqlite') { %>
11+
"tsx": "^4.0.0"<% if (addOnOption.drizzle.database === 'postgresql') { %>,
12+
"@types/pg": "^8.15.6"<% } %><% if (addOnOption.drizzle.database === 'sqlite') { %>,
1513
"@types/better-sqlite3": "^7.6.0"<% } %>
1614
},
1715
"scripts": {

0 commit comments

Comments
 (0)