-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance database configuration and add user schema✨
- Loading branch information
1 parent
f9edf77
commit 67c7ceb
Showing
8 changed files
with
92 additions
and
20 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 |
---|---|---|
|
@@ -11,4 +11,5 @@ export default defineConfig({ | |
}, | ||
verbose: true, | ||
strict: true, | ||
casing: "snake_case", | ||
}); |
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,6 +1,17 @@ | ||
{ | ||
"name": "finance-management-api", | ||
"version": "1.0.0", | ||
"author": { | ||
"name": "Shivam Vijaywargi", | ||
"email": "[email protected]", | ||
"url": "https://github.com/shivamvijaywargi" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"Finance Management", | ||
"Bill Split", | ||
"Spend Tracker" | ||
], | ||
"scripts": { | ||
"dev": "bun run --watch src/index.ts", | ||
"lint": "eslint .", | ||
|
@@ -9,38 +20,28 @@ | |
}, | ||
"dependencies": { | ||
"@hono/zod-openapi": "^0.16.4", | ||
"@scalar/hono-api-reference": "^0.5.155", | ||
"drizzle-orm": "^0.35.1", | ||
"hono": "^4.6.5", | ||
"@scalar/hono-api-reference": "^0.5.158", | ||
"drizzle-orm": "^0.35.3", | ||
"drizzle-zod": "^0.5.1", | ||
"hono": "^4.6.8", | ||
"hono-pino": "^0.3.0", | ||
"pino": "^9.5.0", | ||
"postgres": "^3.4.4", | ||
"postgres": "^3.4.5", | ||
"zod": "^3.23.8" | ||
}, | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^3.7.3", | ||
"@antfu/eslint-config": "^3.8.0", | ||
"@types/bun": "latest", | ||
"drizzle-kit": "^0.26.2", | ||
"eslint": "^9.11.1", | ||
"eslint": "^9.14.0", | ||
"eslint-plugin-format": "^0.1.2", | ||
"pino-pretty": "^11.3.0" | ||
}, | ||
"keywords": [ | ||
"Finance Management", | ||
"Bill Split", | ||
"Spend Tracker" | ||
], | ||
"author": { | ||
"name": "Shivam Vijaywargi", | ||
"email": "[email protected]", | ||
"url": "https://github.com/shivamvijaywargi" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "Shivam Vijaywargi", | ||
"email": "[email protected]", | ||
"url": "https://github.com/shivamvijaywargi" | ||
} | ||
], | ||
"license": "MIT" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// NOTE: If updating values update in both | ||
export const AuthRoles = ["user", "admin"] as const; | ||
|
||
export enum AuthRole { | ||
USER = "user", | ||
ADMIN = "admin", | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as userSchema } from "./user.schema"; |
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,54 @@ | ||
import type { SQL } from "drizzle-orm"; | ||
import type { AnyPgColumn } from "drizzle-orm/pg-core"; | ||
import type { z } from "zod"; | ||
|
||
import { sql } from "drizzle-orm"; | ||
import { | ||
pgEnum, | ||
pgTable, | ||
text, | ||
timestamp, | ||
uniqueIndex, | ||
varchar, | ||
} from "drizzle-orm/pg-core"; | ||
import { createSelectSchema } from "drizzle-zod"; | ||
|
||
import { AuthRole, AuthRoles } from "@/common/enums"; | ||
|
||
export const authRolesEnum = pgEnum("status", AuthRoles); | ||
|
||
const userSchema = pgTable( | ||
"users", | ||
{ | ||
id: text() | ||
.$defaultFn(() => Bun.randomUUIDv7()) | ||
.primaryKey() | ||
.notNull(), | ||
email: varchar({ length: 255 }).notNull().unique(), | ||
fullName: varchar({ length: 255 }), | ||
password: varchar({ length: 255 }), | ||
role: authRolesEnum("role").default(AuthRole.USER), | ||
|
||
createdAt: timestamp({ mode: "string" }) | ||
.notNull() | ||
.defaultNow(), | ||
updatedAt: timestamp({ mode: "string" }) | ||
.notNull() | ||
.defaultNow(), | ||
}, | ||
table => ({ | ||
emailUniqueIndex: uniqueIndex().on(lower(table.email)), | ||
}), | ||
); | ||
|
||
// Schema for selecting a user - can be used to validate API responses | ||
export const selectUserSchema = createSelectSchema(userSchema); | ||
|
||
export type TSelectUserSchema = z.infer<typeof selectUserSchema>; | ||
|
||
// custom lower function | ||
export function lower(email: AnyPgColumn): SQL { | ||
return sql`lower(${email})`; | ||
} | ||
|
||
export default userSchema; |