Skip to content

Commit

Permalink
feat: enhance database configuration and add user schema✨
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamvijaywargi committed Nov 3, 2024
1 parent f9edf77 commit 67c7ceb
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 20 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export default defineConfig({
},
verbose: true,
strict: true,
casing: "snake_case",
});
37 changes: 19 additions & 18 deletions package.json
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 .",
Expand All @@ -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"
]
}
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ routes.forEach((route) => {
});

app.get("/", (c) => {
return c.text("Hello Hono!");
return c.text("Finance Management API!");
});
7 changes: 7 additions & 0 deletions src/common/enums/index.ts
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",
}
10 changes: 9 additions & 1 deletion src/db/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ import postgres from "postgres";

import env from "@/env";

import * as schema from "./schema";

const queryClient = postgres(env.DATABASE_URL);
export const db = drizzle(queryClient);
export const db = drizzle(queryClient, {
casing: "snake_case",
schema,
logger: true,
});

export type TDb = typeof db;
1 change: 1 addition & 0 deletions src/db/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as userSchema } from "./user.schema";
54 changes: 54 additions & 0 deletions src/db/schema/user.schema.ts
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;

0 comments on commit 67c7ceb

Please sign in to comment.