Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to scalar #77

Merged
merged 4 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
519 changes: 513 additions & 6 deletions backend/bun.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions backend/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default defineConfig({
schemaFilter: ["auth", "data"],
dialect: "postgresql",
dbCredentials: {
url: process.env.AUTH_POSTGRES_URL!,
url: process.env.AUTH_POSTGRES_URL!
}
})
});
3 changes: 1 addition & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@
"@types/express": "^5.0.0",
"@types/luxon": "^3.4.2",
"@types/pg": "^8.11.11",
"@types/swagger-ui-express": "^4.1.8",
"drizzle-kit": "^0.30.5",
"prettier": "^3.5.2"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@scalar/express-api-reference": "^0.4.186",
"better-auth": "^1.1.19",
"cors": "^2.8.5",
"drizzle-orm": "^0.40.0",
"express": "^4.21.2",
"luxon": "^3.5.0",
"pg": "^8.13.3",
"redis": "^4.7.0",
"swagger-ui-express": "^5.0.1",
"tsoa": "^6.6.0"
}
}
33 changes: 8 additions & 25 deletions backend/src/controllers/user/station/UserStationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ export class UserStationController extends Controller {
const [result] = await db
.select()
.from(favoriteStations)
.where(
and(
eq(favoriteStations.userId, session?.user?.id!),
eq(favoriteStations.evaNumber, Number(evaNumber))
)
)
.where(and(eq(favoriteStations.userId, session?.user?.id!), eq(favoriteStations.evaNumber, Number(evaNumber))))
.limit(1);
return { evaNumber: Number(evaNumber), favored: !!result };
}
Expand All @@ -58,32 +53,20 @@ export class UserStationController extends Controller {
const [existing] = await db
.select()
.from(favoriteStations)
.where(
and(
eq(favoriteStations.userId, session?.user?.id!),
eq(favoriteStations.evaNumber, Number(evaNumber))
)
)
.where(and(eq(favoriteStations.userId, session?.user?.id!), eq(favoriteStations.evaNumber, Number(evaNumber))))
.limit(1);

if (existing) {
await db
.delete(favoriteStations)
.where(
and(
eq(favoriteStations.userId, session?.user?.id!),
eq(favoriteStations.evaNumber, Number(evaNumber))
)
);
.where(and(eq(favoriteStations.userId, session?.user?.id!), eq(favoriteStations.evaNumber, Number(evaNumber))));
return { evaNumber: Number(evaNumber), favored: false };
}

await db
.insert(favoriteStations)
.values({
userId: session?.user?.id!,
evaNumber: Number(evaNumber)
});
await db.insert(favoriteStations).values({
userId: session?.user?.id!,
evaNumber: Number(evaNumber)
});
return { evaNumber: Number(evaNumber), favored: true };
}
}
}
14 changes: 9 additions & 5 deletions backend/src/db/auth.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const user = authSchema.table("user", {
image: text("image"),
createdAt: timestamp("createdAt", { precision: 3 }).notNull(),
updatedAt: timestamp("updatedAt", { precision: 3 }).notNull(),
username: text("username").notNull().unique(),
username: text("username").notNull().unique()
});

export const session = authSchema.table("session", {
Expand All @@ -20,14 +20,18 @@ export const session = authSchema.table("session", {
updatedAt: timestamp("updatedAt", { precision: 3 }).notNull(),
ipAddress: text("ipAddress"),
userAgent: text("userAgent"),
userId: text("userId").notNull().references(() => user.id),
userId: text("userId")
.notNull()
.references(() => user.id)
});

export const account = authSchema.table("account", {
id: text("id").notNull().primaryKey(),
accountId: text("accountId").notNull(),
providerId: text("providerId").notNull(),
userId: text("userId").notNull().references(() => user.id),
userId: text("userId")
.notNull()
.references(() => user.id),
accessToken: text("accessToken"),
refreshToken: text("refreshToken"),
idToken: text("idToken"),
Expand All @@ -36,7 +40,7 @@ export const account = authSchema.table("account", {
scope: text("scope"),
password: text("password"),
createdAt: timestamp("createdAt", { precision: 3 }).notNull(),
updatedAt: timestamp("updatedAt", { precision: 3 }).notNull(),
updatedAt: timestamp("updatedAt", { precision: 3 }).notNull()
});

export const verification = authSchema.table("verification", {
Expand All @@ -45,5 +49,5 @@ export const verification = authSchema.table("verification", {
value: text("value").notNull(),
expiresAt: timestamp("expiresAt", { precision: 3 }).notNull(),
createdAt: timestamp("createdAt", { precision: 3 }),
updatedAt: timestamp("updatedAt", { precision: 3 }),
updatedAt: timestamp("updatedAt", { precision: 3 })
});
20 changes: 13 additions & 7 deletions backend/src/db/data.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { integer, pgSchema, serial, text, unique } from "drizzle-orm/pg-core";
import { user } from "./auth.schema.ts";

export const dataSchema = pgSchema("data");
export const favoriteStations = dataSchema.table("favorite_stations", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
userId: text("user_id").notNull().references(() => user.id, { onDelete: "cascade" }),
evaNumber: integer("evaNumber").notNull(),
}, (table) => ({
uniqueUserEva: unique().on(table.userId, table.evaNumber)
}));
export const favoriteStations = dataSchema.table(
"favorite_stations",
{
id: integer().primaryKey().generatedAlwaysAsIdentity(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
evaNumber: integer("evaNumber").notNull()
},
(table) => ({
uniqueUserEva: unique().on(table.userId, table.evaNumber)
})
);
2 changes: 1 addition & 1 deletion backend/src/lib/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Pool } from "pg";

export const auth = betterAuth({
database: new Pool({
connectionString: process.env.AUTH_POSTGRES_URL! + "?options=-csearch_path=auth",
connectionString: process.env.AUTH_POSTGRES_URL! + "?options=-csearch_path=auth"
}),
socialProviders: {
github: {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/lib/auth/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export function errorHandler(err: unknown, req: express.Request, res: express.Re
}

next();
}
}
2 changes: 1 addition & 1 deletion backend/src/lib/auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export const expressAuthentication = async (req: express.Request, securityName:

return Promise.resolve();
}
};
};
2 changes: 1 addition & 1 deletion backend/src/lib/db/data-db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { drizzle } from "drizzle-orm/node-postgres";

export const db = drizzle(process.env.AUTH_POSTGRES_URL! + "?options=-csearch_path=data");
export const db = drizzle(process.env.AUTH_POSTGRES_URL! + "?options=-csearch_path=data");
2 changes: 1 addition & 1 deletion backend/src/lib/errors/HttpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export class HttpError extends Error {

Object.setPrototypeOf(this, HttpError.prototype);
}
}
}
15 changes: 11 additions & 4 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import cors from "cors";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "../build/swagger.json";
import { RegisterRoutes } from "../build/routes.ts";
import express from "express";
import * as path from "node:path";
import { toNodeHandler } from "better-auth/node";
import { auth } from "./lib/auth/auth.ts";
import { errorHandler } from "./lib/auth/errorHandler.ts";
import { apiReference } from "@scalar/express-api-reference";

const app = express();

Expand All @@ -15,8 +14,16 @@ app.all("/auth/*", toNodeHandler(auth));
app.use(express.json());
app.use(cors({ origin: "*" }));

app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.get("/api-spec", (req, res) => res.sendFile(path.join(__dirname, "build", "swagger.json")));
app.get("/openapi.json", (req, res) => res.sendFile(path.join(__dirname, "..", "build", "openapi.json")));
app.use(
"/api-docs",
apiReference({
theme: "purple",
spec: {
url: "/openapi.json"
}
})
);
app.get("/api", (req, res) => res.redirect("/api-docs"));

RegisterRoutes(app);
Expand Down
1 change: 1 addition & 0 deletions backend/tsoa.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"controllerPathGlobs": ["src/controllers/**/*Controller.ts"],
"spec": {
"outputDirectory": "build",
"specFileBaseName": "openapi",
"specVersion": 3,
"version": "1.0.0",
"basePath": "/api/v1/",
Expand Down
10 changes: 10 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/src/components/auth/UserInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

{#if $session.data}
<div class="flex items-center gap-x-4">
<span class="hidden md:block text-xl font-medium">{$session.data.user.username}</span>
<span class="hidden text-xl font-medium md:block">{$session.data.user.username}</span>
<img src={$session.data.user.image} height={30} width={30} class="md:h-12 md:w-12" alt="User avatar" />
</div>
{:else}
<button
class="hover:text-accent text-xl font-bold transition-colors duration-500 cursor-pointer"
class="hover:text-accent cursor-pointer text-xl font-bold transition-colors duration-500"
onclick={async () => await goto("/login")}>SIGN IN</button
>
{/if}
5 changes: 3 additions & 2 deletions frontend/src/components/ui/icons/GitHub.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<title>GitHub</title>
<path
fill="currentColor"
d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
</svg>
d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"
/>
</svg>
2 changes: 1 addition & 1 deletion nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ server {
ssl_prefer_server_ciphers on;

# Backend base URL
location ~ ^/(api|auth) {
location ~ ^/(api|auth|openapi.json) {
proxy_pass http://navigator-backend:8000; # container_name of the backend
proxy_http_version 1.1;

Expand Down