Skip to content

Commit

Permalink
chore: change procedure setup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbrusegard committed Sep 1, 2024
1 parent 53d74aa commit cb3c8df
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
9 changes: 1 addition & 8 deletions src/server/api/routers/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { createTRPCRouter, publicProcedure } from '@/server/api/trpc';
import { posts } from '@/server/db/schema';

const testRouter = createTRPCRouter({
getLatest: publicProcedure.query(async ({ ctx }) => {
const post = await ctx.db.query.posts.findFirst({
orderBy: (posts, { desc }) => [desc(posts.createdAt)],
});

return post ?? null;
}),
newCofee: publicProcedure.query(async ({ ctx }) => {}),
});

export { testRouter };
4 changes: 3 additions & 1 deletion src/server/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Lucia } from 'lucia';

const adapter = new DrizzlePostgreSQLAdapter(db, sessions, users);

export const auth = new Lucia(adapter, {
const auth = new Lucia(adapter, {
sessionCookie: {
// this sets cookies with super long expiration
// since Next.js doesn't allow Lucia to extend cookie expiration when rendering pages
Expand Down Expand Up @@ -41,3 +41,5 @@ type DatabaseUserAttributes = {
username: string;
name: string;
};

export { auth };
4 changes: 3 additions & 1 deletion src/server/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { auth } from '@/server/auth';
import { cookies } from 'next/headers';
import { cache } from 'react';

export const getUser = cache(async () => {
const getUser = cache(async () => {
const sessionId = cookies().get(auth.sessionCookieName)?.value ?? null;
if (!sessionId) return null;
const { user, session } = await auth.validateSession(sessionId);
Expand All @@ -28,3 +28,5 @@ export const getUser = cache(async () => {
}
return user;
});

export { getUser };
4 changes: 3 additions & 1 deletion src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ const connection =
);
if (env.NODE_ENV !== 'production') globalForDb.connection = connection;

export const db = drizzle(connection, { schema });
const db = drizzle(connection, { schema });

export { db };
4 changes: 3 additions & 1 deletion src/server/db/schema/office.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { pgTable, serial, timestamp } from 'drizzle-orm/pg-core';

export const coffee = pgTable('coffee', {
const coffee = pgTable('coffee', {
id: serial('id').primaryKey(),
createdAt: timestamp('created_at').defaultNow().notNull(),
});

export { coffee };
24 changes: 17 additions & 7 deletions src/server/db/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import {
varchar,
} from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
const users = pgTable('users', {
id: serial('id').primaryKey(),
username: varchar('username', { length: 8 }).unique().notNull(),
passwordHash: text('password_hash').notNull(),
name: varchar('name', { length: 256 }).notNull(),
});

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

export const sessions = pgTable('session', {
const sessions = pgTable('session', {
id: text('id').primaryKey(),
userId: integer('user_id')
.references(() => users.id)
Expand All @@ -31,16 +31,16 @@ export const sessions = pgTable('session', {
}).notNull(),
});

export const skills = pgTable('skills', {
const skills = pgTable('skills', {
id: serial('id').primaryKey(),
identifier: varchar('identifier', { length: 256 }).unique().notNull(),
});

export const skillsRelations = relations(skills, ({ many }) => ({
const skillsRelations = relations(skills, ({ many }) => ({
usersHasSkills: many(usersHasSkills),
}));

export const usersHasSkills = pgTable(
const usersHasSkills = pgTable(
'users_has_skills',
{
userId: integer('user_id')
Expand All @@ -55,7 +55,7 @@ export const usersHasSkills = pgTable(
}),
);

export const usersHasSkillsRelations = relations(usersHasSkills, ({ one }) => ({
const usersHasSkillsRelations = relations(usersHasSkills, ({ one }) => ({
group: one(skills, {
fields: [usersHasSkills.skillId],
references: [skills.id],
Expand All @@ -65,3 +65,13 @@ export const usersHasSkillsRelations = relations(usersHasSkills, ({ one }) => ({
references: [users.id],
}),
}));

export {
users,
usersRelations,
sessions,
skills,
skillsRelations,
usersHasSkills,
usersHasSkillsRelations,
};

0 comments on commit cb3c8df

Please sign in to comment.