Skip to content

Commit

Permalink
updated queries and readme for better organization
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtheman committed Sep 18, 2024
1 parent d6eb076 commit 3f20cc3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 8 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Mental model for picking this or any stack:
- Testing framework? Didn't bother setting up tests for this. Bun comes with bun:test, probably use that.
- Typescript
- Caching implementation -- didn't get to this yet
- Haven't put much time into compression or minification of assets, just implemented the basics. Could use something like brotli or the recommended (CSSNano)[https://cssnano.github.io/cssnano/docs/introduction/]
- Advanced Hono features. Comes pre-built with (RPC)[https://hono.dev/docs/guides/rpc] and (JSX)[https://hono.dev/docs/guides/jsx]

## Going faster

Expand Down Expand Up @@ -267,11 +269,14 @@ bun run db:migrate

They should be automatically applied upon deployment.

### Organization
- /routes contains the routes for the application. As the application grows, each feature can be broken out into its own file.
- /views contains the html templates. These are rendered with Eta on the server. HTMX is used to update the DOM, check the hx-* attributes. In the future, it might be better to have sub-folders for each feature, /todo, /list, etc.
- queries.js contains the queries for the database. Queries is the main object here, and acts as your 'orm' to extend
- index.js is the entrypoint for the application.

Other stuff:
- index.js is the entrypoint for the application. It's a standard Express.js app. Edit business logic there.
- src/instrumentation.js is used to instrument the app with opentelemetry. You probably don't need to change this.
- src/queries.js is a convenience wrapper around the database for interacting with the database.
- views/* contains the html templates. These are rendered with EJS on the server. HTMX is used to update the DOM, check the hx-* attributes.
- public/* contains the static assets.

## Devops
Expand Down
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hono } from "hono";
import { Eta } from "eta";
import TodoQueries from "./queries";
import Queries from "./queries";
import { requiresAuth } from "./middleware/auth";
import { findDirectories, handleAsyncError, applyMiddleware } from "./utils";
import { logger } from "./instrumentation";
Expand All @@ -27,7 +27,7 @@ app.get("/", requiresAuth, async (c) => {

app.get("/todos", requiresAuth, async (c) => {
const [todos, getErr] = await handleAsyncError(() =>
TodoQueries.getAllTodos(c.get("user")?.sub)
Queries.todo.getAllTodos(c.get("user")?.sub)
);
if (getErr) {
return c.text("Error: " + getErr.message, 400);
Expand All @@ -39,7 +39,7 @@ app.get("/todos", requiresAuth, async (c) => {
app.post("/todos", requiresAuth, async (c) => {
const { listId, title, description, dueDate } = c.req.body;
const [result, createErr] = await handleAsyncError(() =>
TodoQueries.createTodo(
Queries.todo.createTodo(
listId,
title,
description,
Expand All @@ -51,7 +51,7 @@ app.post("/todos", requiresAuth, async (c) => {
return c.text("Error: " + createErr.message, 400);
}
const [newTodo, getErr] = await handleAsyncError(() =>
TodoQueries.getTodoById(result.id, c.get("user")?.sub)
Queries.todo.getTodoById(result.id, c.get("user")?.sub)
);
if (getErr) {
return c.text("Error: " + getErr.message, 400);
Expand All @@ -63,13 +63,13 @@ app.post("/todos", requiresAuth, async (c) => {
app.put("/todos/:id", requiresAuth, async (c) => {
const { id } = c.req.param();
const [todo, getErr] = await handleAsyncError(() =>
TodoQueries.getTodoById(id, c.get("user")?.sub)
Queries.todo.getTodoById(id, c.get("user")?.sub)
);
if (getErr) {
return c.text("Error: " + getErr.message, 400);
}
const [updatedTodo, updateErr] = await handleAsyncError(() =>
TodoQueries.updateTodo(
Queries.todo.updateTodo(
id,
todo.title,
todo.description,
Expand All @@ -82,7 +82,7 @@ app.put("/todos/:id", requiresAuth, async (c) => {
return c.text("Error: " + updateErr.message, 400);
}
const [refreshedTodo, refreshErr] = await handleAsyncError(() =>
TodoQueries.getTodoById(id, c.get("user")?.sub)
Queries.todo.getTodoById(id, c.get("user")?.sub)
);
if (refreshErr) {
return c.text("Error: " + refreshErr.message, 400);
Expand All @@ -94,7 +94,7 @@ app.put("/todos/:id", requiresAuth, async (c) => {
app.delete("/todos/:id", requiresAuth, async (c) => {
const { id } = c.req.param();
const [_, deleteErr] = await handleAsyncError(() =>
TodoQueries.deleteTodo(id, c.get("user")?.sub)
Queries.todo.deleteTodo(id, c.get("user")?.sub)
);
if (deleteErr) {
return c.text("Error: " + deleteErr.message, 400);
Expand Down
7 changes: 6 additions & 1 deletion src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ const TodoQueries = {
},
};

export default TodoQueries;
const Queries = {
todo: TodoQueries,
// add other queries here
};

export default Queries;

0 comments on commit 3f20cc3

Please sign in to comment.