Skip to content

Commit

Permalink
fix: Tipagem e ajustes no package/db
Browse files Browse the repository at this point in the history
  • Loading branch information
ErickCReis committed May 12, 2024
1 parent c878ff3 commit 26facc0
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@

import { useMemo, useState } from "react";
import Link from "next/link";
import { CalendarIcon, ChevronLeft, ChevronRight, FileUp } from "lucide-react";
import { ChevronLeft, ChevronRight, FileUp } from "lucide-react";

import type { RouterOutputs } from "@acme/api";
import { cn } from "@acme/ui";
import { Button } from "@acme/ui/button";
import { Calendar } from "@acme/ui/calendar";
import { Dialog, DialogContent, DialogTrigger } from "@acme/ui/dialog";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
useForm,
} from "@acme/ui/form";
import { Form, FormControl, FormField, FormItem, useForm } from "@acme/ui/form";
import { Input } from "@acme/ui/input";
import { Popover, PopoverContent, PopoverTrigger } from "@acme/ui/popover";
import {
Expand Down Expand Up @@ -204,8 +195,12 @@ const AddTime: React.FC<{ teamId: string; date: Dayjs }> = ({
onChange={(selectedTime) => {
const currentTime = field.value;
currentTime.setHours(
parseInt(selectedTime.target.value.split(":")[0]),
parseInt(selectedTime.target.value.split(":")[1]),
parseInt(
selectedTime.target.value.split(":")[0] ?? "0",
),
parseInt(
selectedTime.target.value.split(":")[1] ?? "0",
),
0,
);
field.onChange(currentTime);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import superjson from "superjson";
import { ZodError } from "zod";

import type { Session } from "@acme/auth";
import { db } from "@acme/db";
import { db } from "@acme/db/client";

/**
* 1. CONTEXT
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@auth/drizzle-adapter": "^1.1.0",
"@t3-oss/env-nextjs": "^0.10.1",
"next": "^14.2.3",
"next-auth": "5.0.0-beta.17",
"next-auth": "5.0.0-beta.18",
"react": "18.3.1",
"react-dom": "18.3.1",
"zod": "^3.23.8"
Expand Down
3 changes: 2 additions & 1 deletion packages/auth/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { DefaultSession, NextAuthConfig } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import Discord from "next-auth/providers/discord";

import { db, schema } from "@acme/db";
import { schema } from "@acme/db";
import { db } from "@acme/db/client";

declare module "next-auth" {
interface Session {
Expand Down
10 changes: 10 additions & 0 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from "drizzle-kit";

import { connectionStr } from "./src/client";

export default {
schema: "./src/schema",
dialect: "postgresql",
dbCredentials: { url: connectionStr.href },
tablesFilter: ["ponto_*"],
} satisfies Config;
15 changes: 15 additions & 0 deletions packages/db/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable no-restricted-properties */

import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export const env = createEnv({
server: {
DB_HOST: z.string(),
DB_NAME: z.string(),
DB_USERNAME: z.string(),
DB_PASSWORD: z.string(),
},
runtimeEnv: process.env,
emptyStringAsUndefined: true,
});
3 changes: 2 additions & 1 deletion packages/db/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import baseConfig from "@acme/eslint-config/base";
import baseConfig, { restrictEnvAccess } from "@acme/eslint-config/base";

/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: ["dist/**"],
},
...baseConfig,
...restrictEnvAccess,
];
8 changes: 6 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
".": {
"types": "./dist/index.d.ts",
"default": "./src/index.ts"
},
"./client": {
"types": "./dist/client.d.ts",
"default": "./src/client.ts"
}
},
"license": "MIT",
Expand All @@ -16,8 +20,8 @@
"clean": "rm -rf .turbo node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"push": "pnpm with-env drizzle-kit push:pg --config src/config.ts",
"studio": "pnpm with-env drizzle-kit studio --config src/config.ts",
"push": "pnpm with-env drizzle-kit push",
"studio": "pnpm with-env drizzle-kit studio",
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
"with-env": "dotenv -e ../../.env --"
},
Expand Down
15 changes: 15 additions & 0 deletions packages/db/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";

import { schema } from ".";
import { env } from "../env";

export const connectionStr = new URL(
`postgresql://${env.DB_HOST}/${env.DB_NAME}`,
);
connectionStr.username = env.DB_USERNAME;
connectionStr.password = env.DB_PASSWORD;
connectionStr.searchParams.set("sslmode", "require");

const neonClient = neon(connectionStr.href);
export const db = drizzle(neonClient, { schema });
29 changes: 0 additions & 29 deletions packages/db/src/config.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";

import { connectionStr } from "./config";
import * as auth from "./schema/auth";
import * as team from "./schema/team";

export { alias } from "drizzle-orm/pg-core";
export * from "drizzle-orm/sql";

export const schema = { ...auth, ...team };

const neonClient = neon(connectionStr.href);
export const db = drizzle(neonClient, { schema });
69 changes: 50 additions & 19 deletions packages/db/src/schema/auth.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import { integer, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import {
integer,
primaryKey,
text,
timestamp,
uuid,
varchar,
} from "drizzle-orm/pg-core";

import { pgSqlTable } from "./_table";

export const users = pgSqlTable("user", {
id: text("id").notNull().primaryKey(),
name: text("name"),
email: text("email").notNull(),
emailVerified: timestamp("emailVerified", { mode: "date" }),
image: text("image"),
id: uuid("id").notNull().primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("emailVerified", {
mode: "date",
withTimezone: true,
}),
image: varchar("image", { length: 255 }),
});

export const usersRelations = relations(users, ({ many }) => ({
accounts: many(accounts),
}));

export const accounts = pgSqlTable(
"account",
{
userId: text("userId")
userId: uuid("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
type: text("type").notNull(),
provider: text("provider").notNull(),
providerAccountId: text("providerAccountId").notNull(),
refresh_token: text("refresh_token"),
type: varchar("type", { length: 255 })
.$type<"email" | "oauth" | "oidc" | "webauthn">()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
refresh_token: varchar("refresh_token", { length: 255 }),
access_token: text("access_token"),
expires_at: integer("expires_at"),
token_type: text("token_type"),
scope: text("scope"),
token_type: varchar("token_type", { length: 255 }),
scope: varchar("scope", { length: 255 }),
id_token: text("id_token"),
session_state: text("session_state"),
session_state: varchar("session_state", { length: 255 }),
},
(account) => ({
compoundKey: primaryKey({
Expand All @@ -34,20 +51,34 @@ export const accounts = pgSqlTable(
}),
);

export const accountsRelations = relations(accounts, ({ one }) => ({
user: one(users, { fields: [accounts.userId], references: [users.id] }),
}));

export const sessions = pgSqlTable("session", {
sessionToken: text("sessionToken").notNull().primaryKey(),
userId: text("userId")
sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
userId: uuid("userId")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expires: timestamp("expires", { mode: "date" }).notNull(),
expires: timestamp("expires", {
mode: "date",
withTimezone: true,
}).notNull(),
});

export const sessionsRelations = relations(sessions, ({ one }) => ({
user: one(users, { fields: [sessions.userId], references: [users.id] }),
}));

export const verificationTokens = pgSqlTable(
"verificationToken",
{
identifier: text("identifier").notNull(),
token: text("token").notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", {
mode: "date",
withTimezone: true,
}).notNull(),
},
(vt) => ({
compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
Expand Down
41 changes: 6 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tooling/eslint/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const restrictEnvAccess = tseslint.config({
export default tseslint.config(
{
// Globally ignored files
ignores: ["**/*.config.js"],
ignores: ["**/*.config.*"],
},
{
files: ["**/*.js", "**/*.ts", "**/*.tsx"],
Expand Down
Loading

0 comments on commit 26facc0

Please sign in to comment.