Skip to content

Commit

Permalink
chore: replace better-sqlite3 with libsql (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Apr 22, 2024
1 parent ed5f545 commit 3257d0a
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-buttons-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

feat: replace better-sqlite3 with libsql
3 changes: 1 addition & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"devDependencies": {
"@auth/drizzle-adapter": "^0.7.0",
"@auth/prisma-adapter": "^1.4.0",
"@libsql/client": "^0.6.0",
"@planetscale/database": "^1.16.0",
"@prisma/adapter-planetscale": "^5.10.2",
"@prisma/client": "^5.10.2",
Expand All @@ -73,11 +74,9 @@
"@trpc/next": "next",
"@trpc/react-query": "next",
"@trpc/server": "next",
"@types/better-sqlite3": "^7.6.9",
"@types/fs-extra": "^11.0.4",
"@types/gradient-string": "^1.1.5",
"@types/node": "^20.11.20",
"better-sqlite3": "^9.4.3",
"drizzle-kit": "^0.20.14",
"drizzle-orm": "^0.29.4",
"mysql2": "^3.9.1",
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const runCli = async (): Promise<CliResults> => {
return p.select({
message: "What database provider would you like to use?",
options: [
{ value: "sqlite", label: "SQLite" },
{ value: "sqlite", label: "SQLite (LibSQL)" },
{ value: "mysql", label: "MySQL" },
{ value: "postgres", label: "PostgreSQL" },
{ value: "planetscale", label: "PlanetScale" },
Expand Down
3 changes: 1 addition & 2 deletions cli/src/installers/dependencyVersionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export const dependencyVersionMap = {
"@planetscale/database": "^1.16.0",
postgres: "^3.4.3",
pg: "^8.11.3",
"@types/better-sqlite3": "^7.6.9",
"better-sqlite3": "^9.4.3",
"@libsql/client": "^0.6.0",

// TailwindCSS
tailwindcss: "^3.4.1",
Expand Down
3 changes: 1 addition & 2 deletions cli/src/installers/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const drizzleInstaller: Installer = ({
"eslint-plugin-drizzle",
];
if (databaseProvider === "planetscale") devPackages.push("mysql2");
if (databaseProvider === "sqlite") devPackages.push("@types/better-sqlite3");
if (databaseProvider === "postgres") devPackages.push("pg");

addPackageDependency({
Expand All @@ -35,7 +34,7 @@ export const drizzleInstaller: Installer = ({
planetscale: "@planetscale/database",
mysql: "mysql2",
postgres: "postgres",
sqlite: "better-sqlite3",
sqlite: "@libsql/client",
} as const
)[databaseProvider],
],
Expand Down
13 changes: 2 additions & 11 deletions cli/src/installers/envVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ export const envVariablesInstaller: Installer = ({
"template/extras/src/env",
envFile
);
const envFileText = fs.readFileSync(envSchemaSrc, "utf-8");
const envSchemaDest = path.join(projectDir, "src/env.js");
fs.writeFileSync(
envSchemaDest,
databaseProvider === "sqlite"
? envFileText.replace("\n .url()", "")
: envFileText,
"utf-8"
);
fs.copyFileSync(envSchemaSrc, envSchemaDest);
}

const envDest = path.join(projectDir, ".env");
Expand Down Expand Up @@ -99,9 +92,7 @@ DATABASE_URL='mysql:https://YOUR_MYSQL_URL_HERE?sslaccept=strict'`;
} else if (databaseProvider === "postgres") {
content += `DATABASE_URL="postgresql:https://postgres:password@localhost:5432/${projectName}"`;
} else if (databaseProvider === "sqlite") {
content += usingPrisma
? 'DATABASE_URL="file:./db.sqlite"'
: 'DATABASE_URL="db.sqlite"';
content += 'DATABASE_URL="file:./db.sqlite"';
}
content += "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion cli/template/extras/config/drizzle-config-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { env } from "~/env";

export default {
schema: "./src/server/db/schema.ts",
driver: "better-sqlite",
driver: "libsql",
dbCredentials: {
url: env.DATABASE_URL,
},
Expand Down
14 changes: 7 additions & 7 deletions cli/template/extras/src/server/db/index-drizzle/with-sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { createClient, type Client } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";

import { env } from "~/env";
import * as schema from "./schema";
Expand All @@ -9,11 +9,11 @@ import * as schema from "./schema";
* update.
*/
const globalForDb = globalThis as unknown as {
conn: Database.Database | undefined;
client: Client | undefined;
};

export const conn =
globalForDb.conn ?? new Database(env.DATABASE_URL, { fileMustExist: false });
if (env.NODE_ENV !== "production") globalForDb.conn = conn;
export const client =
globalForDb.client ?? createClient({ url: env.DATABASE_URL });
if (env.NODE_ENV !== "production") globalForDb.client = client;

export const db = drizzle(conn, { schema });
export const db = drizzle(client, { schema });

0 comments on commit 3257d0a

Please sign in to comment.