-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* separate controller class unrelated to the app * group routes into plugins * inject database and controller into state * Add prefix to all endpoints * refactor: core design final pass * Add vscode tabsize to settings * Remove extra comments and refactor --------- Co-authored-by: yamyam263 <yamyam263@gmail.com>
- Loading branch information
Showing
10 changed files
with
73 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
{ | ||
"editor.tabSize": 2 | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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,10 @@ | ||
import { Elysia } from "elysia"; | ||
import { UsersService } from "@/users/users.service"; | ||
import { UsersRepository } from "@/users/users.repository"; | ||
import { db } from "@/database.providers"; | ||
|
||
export const setupUsers = () => { | ||
const usersRepository = new UsersRepository(db); | ||
const usersService = new UsersService(usersRepository); | ||
return new Elysia().state(() => ({ usersService })); | ||
}; |
This file contains 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,7 @@ | ||
import { Elysia } from "elysia"; | ||
import { setupUsers } from "@/users/users.module"; | ||
|
||
export const usersPlugin = new Elysia({ prefix: "/users" }) | ||
.use(setupUsers) | ||
.post("/", ({ store }) => store.usersService.findAll()) | ||
.post("/login", ({ store }) => store.usersService.findAll()); |
This file contains 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,14 @@ | ||
// users.repository.ts | ||
// in charge of database interactions | ||
|
||
import { users } from "./users.schema"; | ||
import { PostgresJsDatabase } from "drizzle-orm/postgres-js"; | ||
|
||
export class UsersRepository { | ||
// the type here is | ||
constructor(private readonly db: PostgresJsDatabase) {} | ||
|
||
async findAll() { | ||
return this.db.select().from(users); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,22 +1,26 @@ | ||
import { sql } from "drizzle-orm"; | ||
import { pgTable, text, date, serial} from "drizzle-orm/pg-core"; | ||
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox'; | ||
import { Type } from '@sinclair/typebox'; | ||
import { pgTable, text, date, serial } from "drizzle-orm/pg-core"; | ||
import { createInsertSchema, createSelectSchema } from "drizzle-typebox"; | ||
import { Type } from "@sinclair/typebox"; | ||
|
||
export const users = pgTable('users', { | ||
id: serial('id').primaryKey(), | ||
email: text('email').notNull(), | ||
bio: text('bio').notNull(), | ||
image: text('image').notNull(), | ||
password: text('password').notNull(), | ||
username: text('username').notNull(), | ||
created_at: date('created_at').default(sql`CURRENT_DATE`), | ||
updated_at: date('updated_at').default(sql`CURRENT_DATE`), | ||
export const users = pgTable("users", { | ||
id: serial("id").primaryKey(), | ||
email: text("email").notNull(), | ||
bio: text("bio").notNull(), | ||
image: text("image").notNull(), | ||
password: text("password").notNull(), | ||
username: text("username").notNull(), | ||
created_at: date("created_at").default(sql`CURRENT_DATE`), | ||
updated_at: date("updated_at").default(sql`CURRENT_DATE`), | ||
}); | ||
|
||
// Schema for inserting a user - can be used to validate API requests | ||
const insertUserSchemaRaw = createInsertSchema(users); | ||
export const insertUserSchema = Type.Omit(insertUserSchemaRaw, ['id', 'created_at', 'updated_at']); | ||
export const insertUserSchema = Type.Omit(insertUserSchemaRaw, [ | ||
"id", | ||
"created_at", | ||
"updated_at", | ||
]); | ||
|
||
// Schema for selecting a user - can be used to validate API responses | ||
export const selectUserSchema = createSelectSchema(users); |
This file contains 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 |
---|---|---|
@@ -1,19 +1,12 @@ | ||
import {users} from "./users.schema"; | ||
import {PostgresJsDatabase} from "drizzle-orm/postgres-js"; | ||
// users.service.ts | ||
// in charge of business logic - generate slug, fetch data from other services, cache something, etc. | ||
|
||
// note that we should specifically NOT import the db and use it here | ||
// so we can mock it in tests and switch it out as needed | ||
// also, this would hurt the single responsibility principle. | ||
import { UsersRepository } from "@/users/users.repository"; | ||
|
||
export class UsersService { | ||
constructor(private readonly repository: UsersRepository) {} | ||
|
||
// the type here is | ||
constructor(private readonly db: PostgresJsDatabase) {} | ||
|
||
async findAll() { | ||
return this.db.select().from(users); | ||
} | ||
async findAll() { | ||
return this.repository.findAll(); | ||
} | ||
} | ||
|
||
// export a factory for consistency with other providers (like the controller) | ||
export default (db: PostgresJsDatabase) => new UsersService(db); |