Skip to content

Commit

Permalink
chore: use common package to share socket interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Aug 17, 2024
1 parent 06b2077 commit ad0751d
Show file tree
Hide file tree
Showing 35 changed files with 151 additions and 146 deletions.
7 changes: 7 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "common",
"version": "1.0.0",
"dependencies": {
"drizzle-orm": "^0.31.2"
}
}
1 change: 1 addition & 0 deletions common/socket/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './interface'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Group } from '@/modules/groups/groups.schema'
import { Member } from '@/modules/members/members.schema'
import { Message } from '@/modules/messages/messages.schema'
import { Server, Socket } from 'socket.io'
import { Group } from '../tables/groups.table'
import { Member } from '../tables/members.table'
import { Message } from '../tables/messages.table'

export type ChatMode = 'group' | 'direct'

Expand Down
11 changes: 11 additions & 0 deletions common/tables/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { bigserial, timestamp } from 'drizzle-orm/pg-core'

export const baseSchema = {
id: bigserial('id', { mode: 'number' }).primaryKey(),
createdAt: timestamp('created_at', { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true })
.defaultNow()
.$onUpdate(() => new Date()),
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { baseSchema } from '@/database/constants'
import { usersTable } from '@/modules/users/users.schema'
import { bigint, pgTable, varchar } from 'drizzle-orm/pg-core'
import { baseSchema } from './base'
import { usersTable } from './users.table'

export const groupsTable = pgTable('groups', {
...baseSchema,
Expand Down
5 changes: 5 additions & 0 deletions common/tables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './groups.table'
export * from './members.table'
export * from './message-recipients.table'
export * from './messages.table'
export * from './users.table'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { baseSchema } from '@/database/constants'
import { bigint, index, pgEnum, pgTable, unique } from 'drizzle-orm/pg-core'
import { groupsTable } from '../groups/groups.schema'
import { usersTable } from '../users/users.schema'
import { baseSchema } from './base'
import { groupsTable } from './groups.table'
import { usersTable } from './users.table'

// order of roles shows auth precedence
export const memberRoleEnum = pgEnum('member_role', [
Expand Down
23 changes: 23 additions & 0 deletions common/tables/message-recipients.table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bigint, pgTable, unique } from 'drizzle-orm/pg-core'
import { baseSchema } from './base'
import { messagesTable } from './messages.table'
import { usersTable } from './users.table'

export const messageRecipientsTable = pgTable(
'message_recipients',
{
...baseSchema,
messageId: bigint('message_id', { mode: 'number' })
.references(() => messagesTable.id, { onDelete: 'cascade' })
.notNull(),
recipientId: bigint('recipient_id', { mode: 'number' })
.references(() => usersTable.id)
.notNull(),
},
table => ({
uniqueMessageRecipientIndex: unique().on(
table.messageId,
table.recipientId,
),
}),
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { baseSchema } from '@/database/constants'
import {
AnyPgColumn,
bigint,
boolean,
index,
pgTable,
text,
unique,
} from 'drizzle-orm/pg-core'
import { groupsTable } from '../groups/groups.schema'
import { usersTable } from '../users/users.schema'
import { baseSchema } from './base'
import { groupsTable } from './groups.table'
import { usersTable } from './users.table'

export const messagesTable = pgTable(
'messages',
Expand Down Expand Up @@ -37,24 +36,5 @@ export const messagesTable = pgTable(
}),
)

export const messageRecipientsTable = pgTable(
'message_recipients',
{
...baseSchema,
messageId: bigint('message_id', { mode: 'number' })
.references(() => messagesTable.id, { onDelete: 'cascade' })
.notNull(),
recipientId: bigint('recipient_id', { mode: 'number' })
.references(() => usersTable.id)
.notNull(),
},
table => ({
uniqueMessageRecipientIndex: unique().on(
table.messageId,
table.recipientId,
),
}),
)

export type Message = typeof messagesTable.$inferSelect
export type NewMessage = typeof messagesTable.$inferInsert
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { baseSchema } from '@/database/constants'
import { sql } from 'drizzle-orm'
import { pgTable, timestamp, varchar } from 'drizzle-orm/pg-core'
import { baseSchema } from './base'

export const usersTable = pgTable('users', {
...baseSchema,
Expand Down
26 changes: 20 additions & 6 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion pnpm-workspace.prod.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- 'web'
- 'server'
- 'server'
- 'common'
1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
packages:
- 'web'
- 'server'
- 'common'
- 'e2e'
2 changes: 1 addition & 1 deletion server/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
schema: './src/modules/**/*.schema.ts',
schema: 'common/tables/index.ts',
out: './drizzle',
dialect: 'postgresql',
dbCredentials: {
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"socket.io": "^4.7.5",
"swagger-autogen": "^2.23.7",
"swagger-ui-express": "^5.0.1",
"uuid": "^10.0.0"
"uuid": "^10.0.0",
"common": "workspace:*"
},
"devDependencies": {
"@eslint/js": "^9.6.0",
Expand Down
13 changes: 0 additions & 13 deletions server/src/database/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
import { bigserial, timestamp } from 'drizzle-orm/pg-core'

export const baseSchema = {
id: bigserial('id', { mode: 'number' }).primaryKey(),
createdAt: timestamp('created_at', { withTimezone: true })
.notNull()
.defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true })
.defaultNow()
.$onUpdate(() => new Date()),
}

export const defaultLimit = 15
3 changes: 2 additions & 1 deletion server/src/database/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
} from 'drizzle-orm'
import { PgColumn, PgSelect } from 'drizzle-orm/pg-core'
import get from 'lodash/get'
import { defaultLimit } from './constants'

export const defaultLimit = 15

export const getPaginationParams = (
query: Record<string, unknown>,
Expand Down
12 changes: 6 additions & 6 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import './utils/loadModules'

import { createAdapter } from '@socket.io/redis-streams-adapter'
import 'colors'
import {
ClientToServerEvents,
InterServerEvents,
ServerToClientEvents,
SocketData,
} from 'common/socket'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import { migrate } from 'drizzle-orm/node-postgres/migrator'
Expand All @@ -19,12 +25,6 @@ import { getRedisClient } from './redis'
import rootRouter from './routes'
import { registerSocketEvents } from './socket/events'
import { socketAuthMiddleware } from './socket/middlewares'
import {
ClientToServerEvents,
InterServerEvents,
ServerToClientEvents,
SocketData,
} from './socket/socket.interface'
import swaggerDocument from './swagger-output.json'

const createApp = async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemberRole } from 'common/tables'
import { ErrorRequestHandler, RequestHandler } from 'express'
import { config } from './config'
import { MemberRole } from './modules/members/members.schema'
import { checkPermission } from './modules/members/members.service'
import { badRequest, notAuthenticated, notAuthorized } from './utils/api'
import { verifyAccessToken } from './utils/jwt'
Expand Down
16 changes: 8 additions & 8 deletions server/src/modules/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import {
} from '@/database/helpers'
import { deleteGroupRoles, deleteMemberRole } from '@/redis/handlers'
import { roomKeys } from '@/socket/helpers'
import { TypedIOServer } from '@/socket/socket.interface'
import { badRequest, notAuthorized, notFound } from '@/utils/api'
import { TypedIOServer } from 'common/socket'
import {
groupsTable,
membersTable,
messageRecipientsTable,
messagesTable,
usersTable,
} from 'common/tables'
import {
and,
count,
Expand All @@ -28,14 +35,7 @@ import {
} from 'drizzle-orm'
import { union } from 'drizzle-orm/pg-core'
import { RequestHandler } from 'express'
import { membersTable } from '../members/members.schema'
import { addMembers } from '../members/members.service'
import {
messageRecipientsTable,
messagesTable,
} from '../messages/messages.schema'
import { usersTable } from '../users/users.schema'
import { groupsTable } from './groups.schema'
import { handleMemberDelete } from './groups.service'

// CREATE
Expand Down
2 changes: 1 addition & 1 deletion server/src/modules/groups/groups.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { roomKeys } from '@/socket/helpers'
import { TypedIOServer } from '@/socket/socket.interface'
import { TypedIOServer } from 'common/socket'
import { Request } from 'express'

export const handleMemberDelete = (
Expand Down
5 changes: 2 additions & 3 deletions server/src/modules/members/members.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { db } from '@/database'
import { getPaginationParams, withPagination } from '@/database/helpers'
import { checkOnlineUsers, setGroupMemberRoleTxn } from '@/redis/handlers'
import { roomKeys } from '@/socket/helpers'
import { TypedIOServer } from '@/socket/socket.interface'
import { badRequest, notFound } from '@/utils/api'
import { TypedIOServer } from 'common/socket'
import { MemberRole, membersTable, usersTable } from 'common/tables'
import { and, asc, eq, getTableColumns, gt, like } from 'drizzle-orm'
import { RequestHandler } from 'express'
import { usersTable } from '../users/users.schema'
import { MemberRole, membersTable } from './members.schema'

export const joinRooms: RequestHandler = async (req, res, next) => {
try {
Expand Down
10 changes: 5 additions & 5 deletions server/src/modules/members/members.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { db } from '@/database'
import { getMemberRole, setMemberRolesForAGroup } from '@/redis/handlers'
import { roomKeys } from '@/socket/helpers'
import { TypedIOServer } from '@/socket/socket.interface'
import { and, eq } from 'drizzle-orm'
import { NodePgDatabase } from 'drizzle-orm/node-postgres'
import { Group } from '../groups/groups.schema'
import { TypedIOServer } from 'common/socket'
import {
Group,
MemberRole,
NewMember,
memberRoles,
membersTable,
} from './members.schema'
} from 'common/tables'
import { and, eq } from 'drizzle-orm'
import { NodePgDatabase } from 'drizzle-orm/node-postgres'

export const checkPermission = async (
groupId: number,
Expand Down
Loading

0 comments on commit ad0751d

Please sign in to comment.