-
Notifications
You must be signed in to change notification settings - Fork 0
Create Data Access Layer abstraction #1
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
Open
Karnak19
wants to merge
2
commits into
main
Choose a base branch
from
claude/create-data-access-layer-JyKlw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # Data Access Layer | ||
|
|
||
| Simple pattern for organizing database queries. Keep your Elysia endpoints clean by putting all database logic in dedicated files. | ||
|
|
||
| ## Structure | ||
|
|
||
| ``` | ||
| src/shared/data-access/ | ||
| ├── posts.ts # Post-related queries | ||
| ├── comments.ts # Comment-related queries | ||
| └── index.ts # Export all functions | ||
| ``` | ||
|
|
||
| ## Pattern | ||
|
|
||
| Each file contains simple functions that interact with the database: | ||
|
|
||
| ```typescript | ||
| // src/shared/data-access/posts.ts | ||
| import "server-only"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { db } from "../db/client"; | ||
| import { post } from "../db/schema"; | ||
|
|
||
| export async function getPostById(id: string) { | ||
| const [result] = await db | ||
| .select() | ||
| .from(post) | ||
| .where(eq(post.id, id)) | ||
| .limit(1); | ||
| return result ?? null; | ||
| } | ||
|
|
||
| export async function getAllPosts() { | ||
| return db.select().from(post); | ||
| } | ||
|
|
||
| export async function createPost(data: { title: string; content: string }) { | ||
| const [newPost] = await db | ||
| .insert(post) | ||
| .values({ | ||
| id: crypto.randomUUID(), | ||
| ...data, | ||
| }) | ||
| .returning(); | ||
| return newPost; | ||
| } | ||
| ``` | ||
|
|
||
| ## Usage in Elysia Endpoints | ||
|
|
||
| ```typescript | ||
| // app/api/[[...slugs]]/route.ts | ||
| import { Elysia } from "elysia"; | ||
| import { getPostById, getAllPosts, createPost } from "@/shared/data-access"; | ||
|
|
||
| export const app = new Elysia({ prefix: "/api" }) | ||
| .get("/posts", async () => { | ||
| const posts = await getAllPosts(); | ||
| return posts; | ||
| }) | ||
| .get("/posts/:id", async ({ params }) => { | ||
| const post = await getPostById(params.id); | ||
| if (!post) { | ||
| throw new Error("Post not found"); | ||
| } | ||
| return post; | ||
| }) | ||
| .post("/posts", async ({ body }) => { | ||
| const post = await createPost(body); | ||
| return post; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Why This Pattern? | ||
|
|
||
| 1. **Separation of concerns**: Database logic separate from API logic | ||
| 2. **Reusable**: Use the same functions in Server Components, API routes, server actions | ||
| 3. **Easy to test**: Mock these functions in tests | ||
| 4. **Easy to migrate**: If you switch ORMs, just update these files | ||
| 5. **Type-safe**: Full TypeScript support | ||
|
|
||
| ## Example: Adding a New Entity | ||
|
|
||
| 1. **Create the schema**: | ||
|
|
||
| ```typescript | ||
| // src/shared/db/schema/posts.ts | ||
| import { pgTable, text, timestamp } from "drizzle-orm/pg-core"; | ||
|
|
||
| export const post = pgTable("post", { | ||
| id: text("id").primaryKey(), | ||
| title: text("title").notNull(), | ||
| content: text("content").notNull(), | ||
| createdAt: timestamp("created_at").defaultNow().notNull(), | ||
| updatedAt: timestamp("updated_at") | ||
| .defaultNow() | ||
| .$onUpdate(() => new Date()) | ||
| .notNull(), | ||
| }); | ||
| ``` | ||
|
|
||
| 2. **Create data access functions**: | ||
|
|
||
| ```typescript | ||
| // src/shared/data-access/posts.ts | ||
| import "server-only"; | ||
| import { db } from "../db/client"; | ||
| import { post } from "../db/schema"; | ||
| import { eq } from "drizzle-orm"; | ||
|
|
||
| export async function getPostById(id: string) { | ||
| const [result] = await db.select().from(post).where(eq(post.id, id)).limit(1); | ||
| return result ?? null; | ||
| } | ||
|
|
||
| export async function getAllPosts() { | ||
| return db.select().from(post); | ||
| } | ||
|
|
||
| export async function createPost(data: { title: string; content: string }) { | ||
| const [newPost] = await db | ||
| .insert(post) | ||
| .values({ | ||
| id: crypto.randomUUID(), | ||
| ...data, | ||
| }) | ||
| .returning(); | ||
| return newPost; | ||
| } | ||
|
|
||
| export async function updatePost( | ||
| id: string, | ||
| data: { title?: string; content?: string }, | ||
| ) { | ||
| const [updated] = await db | ||
| .update(post) | ||
| .set(data) | ||
| .where(eq(post.id, id)) | ||
| .returning(); | ||
| return updated ?? null; | ||
| } | ||
|
|
||
| export async function deletePost(id: string) { | ||
| await db.delete(post).where(eq(post.id, id)); | ||
| } | ||
| ``` | ||
|
|
||
| 3. **Export from index**: | ||
|
|
||
| ```typescript | ||
| // src/shared/data-access/index.ts | ||
| export * from "./posts"; | ||
| ``` | ||
|
|
||
| 4. **Use in your API**: | ||
|
|
||
| ```typescript | ||
| import { getAllPosts, createPost } from "@/shared/data-access"; | ||
|
|
||
| const app = new Elysia() | ||
| .get("/posts", () => getAllPosts()) | ||
| .post("/posts", ({ body }) => createPost(body)); | ||
| ``` | ||
|
|
||
| That's it! Simple, clean, and easy to understand. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Data Access Layer | ||
| * | ||
| * Export all data access functions from here. | ||
| * When you create a new data access file (e.g., posts.ts, comments.ts), | ||
| * add the export here: | ||
| * | ||
| * export * from "./posts"; | ||
| * export * from "./comments"; | ||
| */ | ||
|
|
||
| // Example: | ||
| // export * from "./posts"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add language specification to the fenced code block.
The directory structure block should specify a language for the fenced code block to comply with markdown standards.
🔎 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
7-7: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents