Skip to content

Commit

Permalink
switch from prisma to drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed May 8, 2024
1 parent 7de245d commit 4f2cc6c
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 160 deletions.
14 changes: 10 additions & 4 deletions src/server/backend/bancho.py/drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ export const emailTokens = mysqlTable('email_tokens', {
})

export const relationships = mysqlTable('relationships', {
fromUser: int('user1').notNull(),
toUser: int('user2').notNull(),
fromUserId: int('user1').notNull(),
toUserId: int('user2').notNull(),
type: mysqlEnum('type', ['friend', 'block']).notNull(),
},
(table) => {
return {
relationshipsFromUserToUserPk: primaryKey({ columns: [table.fromUser, table.toUser], name: 'relationships_user1_user2_pk' }),
relationshipsFromUserToUserPk: primaryKey({ columns: [table.fromUserId, table.toUserId], name: 'relationships_user1_user2_pk' }),
}
})

Expand Down Expand Up @@ -442,8 +442,14 @@ export const scoresRelations = relations(scores, ({ one }) => ({
export const clansRelations = relations(clans, ({ one }) => ({
owner: one(users, { fields: [clans.ownerId], references: [users.id] }),
}))
export const usersRelations = relations(users, ({ one }) => ({
export const relationshipsRelations = relations(relationships, ({ one }) => ({
fromUser: one(users, { fields: [relationships.fromUserId], references: [users.id], relationName: 'fromUser' }),
toUser: one(users, { fields: [relationships.toUserId], references: [users.id], relationName: 'toUser' }),
}))
export const usersRelations = relations(users, ({ one, many }) => ({
clan: one(clans, { fields: [users.clanId], references: [clans.id] }),
relations: many(relationships, { relationName: 'toUser' }),
gorRelations: many(relationships, { relationName: 'fromUser' }),
}))

export const usersAchievementsRelations = relations(userAchievements, ({ one }) => ({
Expand Down
8 changes: 1 addition & 7 deletions src/server/backend/bancho.py/server/clan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,7 @@ export class ClanProvider extends Base<Id> {
return {
user: toUserCompact(item.user, this.config),
score: toRankingSystemScore({
score: {
...item.score,
beatmap: {
...item.beatmap,
source: item.source,
},
},
...item,
rankingSystem: Rank.PPv2,
mode,
rank: index + 1,
Expand Down
4 changes: 2 additions & 2 deletions src/server/backend/bancho.py/server/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class MapProvider implements Base<Id, Id> {
source: Source
}

return toBeatmapWithBeatmapset(beatmap)
return toBeatmapWithBeatmapset(beatmap, beatmap.source)
}

async getBeatmapset(query: { id: Id }) {
Expand Down Expand Up @@ -129,7 +129,7 @@ export class MapProvider implements Base<Id, Id> {
})

const result = (await sql)
.map(toBeatmapWithBeatmapset)
.map(i => toBeatmapWithBeatmapset(i, i.source))
.filter(
(item): item is typeof item & { status: Exclude<RankingStatus, AbnormalStatus> } =>
item.status !== RankingStatus.NotFound && item.status !== RankingStatus.Deleted
Expand Down
6 changes: 3 additions & 3 deletions src/server/backend/bancho.py/server/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export class ScoreProvider implements Base<bigint, Id> {
}

#transformScore(dbScore: AbleToTransformToScores & { user: Pick<typeof schema['users']['$inferSelect'], DatabaseUserCompactFields> }) {
const [mode, ruleset] = fromBanchoPyMode(dbScore.mode)
const [mode, ruleset] = fromBanchoPyMode(dbScore.score.mode)
return Object.assign(
toScore({
score: dbScore,
...dbScore,
mode,
ruleset,
}),
Expand All @@ -77,7 +77,7 @@ export class ScoreProvider implements Base<bigint, Id> {
},
}) ?? raise(Error, 'score not found')

return this.#transformScore(result)
return this.#transformScore({ score: result, beatmap: result.beatmap, source: result.beatmap.source, user: result.user })
}

async findOne(opt: Base.SearchQuery<Id>) {
Expand Down
84 changes: 47 additions & 37 deletions src/server/backend/bancho.py/server/user-relations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { dedupeUserRelationship, fromBanchoPyRelationType, idToString, stringToId, toBanchoPyRelationType, toUserCompact } from '../transforms'
import { aliasedTable, and, eq, notExists } from 'drizzle-orm'
import { type DatabaseUserCompactFields, dedupeUserRelationship, fromBanchoPyRelationType, idToString, stringToId, toBanchoPyRelationType, toUserCompact } from '../transforms'

// import { idToString, stringToId } from '../transforms'
import type { Id } from '..'
import * as schema from '../drizzle/schema'
import { config as _config } from '../env'
import { useDrizzle } from './source/drizzle'
import { prismaClient } from './source/prisma'
import type { UserRelationProvider as Base } from '$base/server'
import { Relationship } from '~/def'
import type { UserCompact } from '~/def/user'
import { Relationship } from '~/def'
import type { UserRelationProvider as Base } from '$base/server'

const config = _config()

Expand All @@ -15,44 +18,46 @@ export class UserRelationProvider implements Base<Id> {
static readonly idToString = idToString

prisma = prismaClient
drizzle = useDrizzle(schema)
config = config

async getOne(fromUser: { id: Id }, toUser: { id: Id }) {
const relationships = await this.prisma.relationship.findFirst({
where: {
fromUserId: fromUser.id,
toUserId: toUser.id,
},
select: {
const relationship = await this.drizzle.query.relationships.findFirst({
where: and(
eq(schema.relationships.fromUserId, fromUser.id),
eq(schema.relationships.toUserId, toUser.id)
),
columns: {
type: true,
},
})
if (!relationships) {

if (!relationship) {
return undefined
}
switch (relationships.type) {

switch (relationship.type) {
case 'friend': return Relationship.Friend
case 'block': return Relationship.Blocked
default: assertNotReachable(relationships.type)
default: assertNotReachable(relationship.type)
}
}

async get({ user }: { user: { id: Id } }) {
const pRelationResult = this.prisma.relationship.findMany({
where: {
fromUserId: user.id,
},
select: {
const pRelationResult = this.drizzle.query.relationships.findMany({
where: eq(schema.relationships.fromUserId, user.id),
columns: {
type: true,
toUser: true,
toUserId: true,
},
})
const pGotRelationResult = this.prisma.relationship.findMany({
where: {
toUserId: user.id,
with: {
toUser: true,
},
select: {
})

const pGotRelationResult = this.drizzle.query.relationships.findMany({
where: eq(schema.relationships.toUserId, user.id),
columns: {
type: true,
fromUserId: true,
},
Expand Down Expand Up @@ -84,20 +89,25 @@ export class UserRelationProvider implements Base<Id> {
}

async notMutual(user: { id: Id }) {
return this.prisma.user.findMany({
where: {
relations: {
some: {
toUserId: user.id,
},
},
gotRelations: {
none: {
fromUserId: user.id,
},
},
},
}).then(res => res.map(r => toUserCompact(r, this.config)))
// rel toUserId = u.id and exists (fromUser = u.id, toUser = toUserId)
const _u = aliasedTable(schema.users, 'pickUsers')
const _rel = aliasedTable(schema.relationships, 'r')
const _got = aliasedTable(schema.relationships, 'got')

const res = await this.drizzle
.select(pick(_u, ['country', 'id', 'name', 'priv', 'safeName'] satisfies DatabaseUserCompactFields[]))
.from(_u)
.innerJoin(_rel, eq(_rel.fromUserId, _u.id))
.where(
and(
eq(_rel.type, 'friend'),
eq(_rel.toUserId, user.id),

notExists(this.drizzle.select().from(_got).where(and(eq(_got.fromUserId, user.id), eq(_got.toUserId, _rel.fromUserId))))
)
)

return res.map(r => toUserCompact(r, this.config))
}

async removeOne({
Expand Down
Loading

0 comments on commit 4f2cc6c

Please sign in to comment.