Skip to content

Commit

Permalink
Migrate front-end to Turso's SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianUribe6 committed Mar 11, 2024
1 parent ec7f1d3 commit 3fed779
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 81 deletions.
10 changes: 0 additions & 10 deletions .env.example

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ yarn-error.log*
# Misc
.DS_Store
*.pem

# SQLite
local.db
5 changes: 1 addition & 4 deletions apps/front-end/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
NEXT_PUBLIC_COLLAB_SERVER_URL=ws://localhost:1234

# Local development database
# This should match the same one configured for the collaboration server
DATABASE_URL=mysql://root:pass@localhost/jotter

TURSO_DATABASE_URL=file:../../local.db

NEXTAUTH_URL=http://localhost:3000
4 changes: 2 additions & 2 deletions apps/front-end/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./src/schema.ts",
driver: "mysql2",
driver: "libsql",
out: "./drizzle",
dbCredentials: {
uri: process.env.DATABASE_URL!,
url: process.env.TURSO_DATABASE_URL!,
},
});
9 changes: 4 additions & 5 deletions apps/front-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"version": "0.1.0",
"private": true,
"scripts": {
"predev": "docker compose --env-file ../../.env up -d",
"dev": "next dev",
"dev:collab": "turbo run dev --filter=front-end --filter=collab-server",
"build": "next build",
"start": "next start",
"lint": "next lint",
"test:e2e": "playwright test",
"db:generate": "drizzle-kit generate:mysql",
"db:push": "drizzle-kit push:mysql"
"db:generate": "drizzle-kit generate:sqlite",
"db:push": "drizzle-kit push:sqlite"
},
"dependencies": {
"@auth/drizzle-adapter": "^0.7.0",
Expand All @@ -24,6 +23,7 @@
"@lexical/selection": "^0.13.0",
"@lexical/utils": "^0.13.0",
"@lexical/yjs": "^0.13.0",
"@libsql/client": "^0.5.3",
"@radix-ui/react-accordion": "^1.1.1",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-context-menu": "^2.1.3",
Expand All @@ -40,13 +40,12 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"cmdk": "^0.2.0",
"drizzle-orm": "^0.29.3",
"drizzle-orm": "^0.29.5",
"eslint": "^8.51.0",
"eslint-config-next": "^14.1.1",
"jose": "^5.2.2",
"jotai": "^2.4.3",
"lexical": "^0.13.0",
"mysql2": "^3.9.1",
"nanoid": "^5.0.2",
"next": "^14.1.1",
"next-auth": "^4.24.5",
Expand Down
12 changes: 11 additions & 1 deletion apps/front-end/src/config/env-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ const envSchema = z.object({
DISCORD_SECRET: z.string().min(1),
DISCORD_CLIENT_ID: z.string().min(1),
NEXTAUTH_SECRET: z.string().min(1),
DATABASE_URL: z.string().min(1),
TURSO_DATABASE_URL: z.string().min(1),
TURSO_AUTH_TOKEN: z
.string()
.min(1)
.optional()
.refine((token) => {
if (process.env.NODE_ENV === "production") {
return Boolean(token);
}
return true;
}, "A token is required to access production database."),
NEXTAUTH_URL: z.string().min(1),
});

Expand Down
11 changes: 7 additions & 4 deletions apps/front-end/src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import mysql from "mysql2/promise";
import env from "@/config/env-server";
import { drizzle } from "drizzle-orm/mysql2";
import * as schema from "@/schema";
import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";

const connection = await mysql.createConnection(env.DATABASE_URL);
const connection = await createClient({
url: env.TURSO_DATABASE_URL,
authToken: env.TURSO_AUTH_TOKEN,
});

const db = drizzle(connection, { mode: "planetscale", schema });
const db = drizzle(connection, { schema });

export default db;
97 changes: 42 additions & 55 deletions apps/front-end/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
import type { AdapterAccount } from "@auth/core/adapters";
import { relations } from "drizzle-orm";
import {
char,
customType,
int,
mysqlTable,
blob,
integer,
primaryKey,
timestamp,
varchar,
} from "drizzle-orm/mysql-core";
sqliteTable,
text,
} from "drizzle-orm/sqlite-core";

const blob = customType<{ data: Buffer }>({
dataType() {
return "blob";
},
});

export const users = mysqlTable("user", {
id: varchar("id", { length: 255 }).notNull().primaryKey(),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }),
emailVerified: timestamp("emailVerified", {
mode: "date",
fsp: 3,
}).defaultNow(),
image: varchar("image", { length: 255 }),
export const users = sqliteTable("user", {
id: text("id").notNull().primaryKey(),
name: text("name"),
email: text("email"),
emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
image: text("image"),
});

export const accounts = mysqlTable(
export const accounts = sqliteTable(
"account",
{
userId: varchar("userId", { length: 255 }).notNull(),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
refresh_token: varchar("refresh_token", { length: 255 }),
access_token: varchar("access_token", { length: 255 }),
expires_at: int("expires_at"),
token_type: varchar("token_type", { length: 255 }),
scope: varchar("scope", { length: 255 }),
id_token: varchar("id_token", { length: 2048 }),
session_state: varchar("session_state", { length: 255 }),
userId: text("userId").notNull(),
type: text("type").$type<AdapterAccount["type"]>().notNull(),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: integer("expires_at"),
token_type: text("token_type"),
scope: text("scope"),
id_token: text("id_token"),
session_state: text("session_state"),
},
(account) => ({
compoundKey: primaryKey({
Expand All @@ -51,18 +38,18 @@ export const accounts = mysqlTable(
}),
);

export const sessions = mysqlTable("session", {
sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
userId: varchar("userId", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
export const sessions = sqliteTable("session", {
sessionToken: text("sessionToken").notNull().primaryKey(),
userId: text("userId").notNull(),
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
});

export const verificationTokens = mysqlTable(
export const verificationTokens = sqliteTable(
"verificationToken",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
identifier: text("identifier").notNull(),
token: text("token").notNull(),
expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
},
(vt) => ({
compoundKey: primaryKey({
Expand All @@ -71,22 +58,22 @@ export const verificationTokens = mysqlTable(
}),
);

export const documents = mysqlTable("document", {
name: char("name", { length: 21 }).primaryKey(),
createdOn: timestamp("createdOn", { mode: "date" }).defaultNow(),
modifiedOn: timestamp("modifiedOn", { mode: "date" }).defaultNow(),
export const documents = sqliteTable("document", {
name: text("name").primaryKey(),
createdOn: integer("createdOn", { mode: "timestamp_ms" }),
modifiedOn: integer("modifiedOn", { mode: "timestamp_ms" }),
data: blob("data"),
notebookId: char("id", { length: 21 }).notNull(),
notebookId: text("id").notNull(),
});

export const notebooks = mysqlTable("notebook", {
id: char("id", { length: 21 }).notNull().primaryKey(),
authorId: varchar("authorId", { length: 255 }).notNull(),
export const notebooks = sqliteTable("notebook", {
id: text("id").notNull().primaryKey(),
authorId: text("authorId").notNull(),
});

export const notebookDocuments = mysqlTable("notebook_document", {
notebookId: char("notebookId", { length: 21 }).notNull().primaryKey(),
documentName: char("documentName", { length: 21 }).notNull(),
export const notebookDocuments = sqliteTable("notebook_document", {
notebookId: text("notebookId").notNull().primaryKey(),
documentName: text("documentName").notNull(),
});

/******************************************************************************
Expand Down

0 comments on commit 3fed779

Please sign in to comment.