From 33f29bf84dadd44b2785cc61c288384807e1e1cd Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Tue, 3 Nov 2020 17:03:22 +0200 Subject: [PATCH] refactor player table --- generated/graphql.tsx | 27 ++++++------ graphql/schema/xRankPlacement.ts | 13 +++--- nexus-typegen.ts | 24 +++++------ pages/player/[id].tsx | 10 ++--- .../README.md | 41 ++++++++----------- .../schema.prisma | 18 ++++---- .../steps.json | 39 ++++-------------- prisma/migrations/migrate.lock | 2 +- prisma/schema.prisma | 18 ++++---- prisma/seed.ts | 5 +-- .../queries/getPlayersXRankPlacements.graphql | 4 +- scenes/Top500/index.tsx | 2 +- scenes/Top500/queries/XRankPlacements.graphql | 2 +- schema.graphql | 27 ++++++------ 14 files changed, 96 insertions(+), 136 deletions(-) rename prisma/migrations/{20201028083612-init => 20201103144752-init}/README.md (70%) rename prisma/migrations/{20201028083612-init => 20201103144752-init}/schema.prisma (71%) rename prisma/migrations/{20201028083612-init => 20201103144752-init}/steps.json (93%) diff --git a/generated/graphql.tsx b/generated/graphql.tsx index 2d88b452d..f442667ff 100644 --- a/generated/graphql.tsx +++ b/generated/graphql.tsx @@ -55,7 +55,7 @@ export type QueryGetXRankPlacementsArgs = { export type QueryGetPlayersXRankPlacementsArgs = { - playerId: Scalars['String']; + switchAccountId: Scalars['String']; }; export type UpdateUserProfileInput = { @@ -83,7 +83,7 @@ export type MutationUpdateUserProfileArgs = { export type XRankPlacement = { __typename?: 'XRankPlacement'; id: Scalars['Int']; - playerId: Scalars['String']; + switchAccountId: Scalars['String']; playerName: Scalars['String']; ranking: Scalars['Int']; xPower: Scalars['Float']; @@ -96,9 +96,8 @@ export type XRankPlacement = { export type Player = { __typename?: 'Player'; - playerId: Scalars['String']; - /** Set of names player has had in Top 500 results. The most recent one is the first one of the list. */ - names: Array; + switchAccountId: Scalars['String']; + name: Scalars['String']; user?: Maybe; placements: Array; }; @@ -120,18 +119,18 @@ export enum RankedMode { export type XRankPlacementWhereUniqueInput = { id?: Maybe; - playerId_mode_month_year?: Maybe; + switchAccountId_mode_month_year?: Maybe; }; -export type PlayerIdModeMonthYearCompoundUniqueInput = { - playerId: Scalars['String']; +export type SwitchAccountIdModeMonthYearCompoundUniqueInput = { + switchAccountId: Scalars['String']; mode: RankedMode; month: Scalars['Int']; year: Scalars['Int']; }; export type GetPlayersXRankPlacementsQueryVariables = Exact<{ - playerId: Scalars['String']; + switchAccountId: Scalars['String']; }>; @@ -188,7 +187,7 @@ export type GetXRankPlacementsQuery = ( { __typename?: 'Query' } & { getXRankPlacements: Array<( { __typename?: 'XRankPlacement' } - & Pick + & Pick & { player: ( { __typename?: 'Player' } & { user?: Maybe<( @@ -201,8 +200,8 @@ export type GetXRankPlacementsQuery = ( export const GetPlayersXRankPlacementsDocument = gql` - query getPlayersXRankPlacements($playerId: String!) { - getPlayersXRankPlacements(playerId: $playerId) { + query getPlayersXRankPlacements($switchAccountId: String!) { + getPlayersXRankPlacements(switchAccountId: $switchAccountId) { id playerName ranking @@ -234,7 +233,7 @@ export const GetPlayersXRankPlacementsDocument = gql` * @example * const { data, loading, error } = useGetPlayersXRankPlacementsQuery({ * variables: { - * playerId: // value for 'playerId' + * switchAccountId: // value for 'switchAccountId' * }, * }); */ @@ -326,7 +325,7 @@ export type GetUserByIdentifierQueryResult = Apollo.QueryResult { return ctx.prisma.xRankPlacement.findMany({ - where: { playerId: args.playerId }, + where: { switchAccountId: args.switchAccountId }, orderBy: [{ month: "desc" }, { year: "desc" }], }); }, diff --git a/nexus-typegen.ts b/nexus-typegen.ts index 372af11ed..f1f95beff 100644 --- a/nexus-typegen.ts +++ b/nexus-typegen.ts @@ -20,10 +20,10 @@ declare global { } export interface NexusGenInputs { - PlayerIdModeMonthYearCompoundUniqueInput: { // input type + SwitchAccountIdModeMonthYearCompoundUniqueInput: { // input type mode: NexusGenEnums['RankedMode']; // RankedMode! month: number; // Int! - playerId: string; // String! + switchAccountId: string; // String! year: number; // Int! } UpdateUserProfileInput: { // input type @@ -39,7 +39,7 @@ export interface NexusGenInputs { } XRankPlacementWhereUniqueInput: { // input type id?: number | null; // Int - playerId_mode_month_year?: NexusGenInputs['PlayerIdModeMonthYearCompoundUniqueInput'] | null; // PlayerIdModeMonthYearCompoundUniqueInput + switchAccountId_mode_month_year?: NexusGenInputs['SwitchAccountIdModeMonthYearCompoundUniqueInput'] | null; // SwitchAccountIdModeMonthYearCompoundUniqueInput } } @@ -65,7 +65,7 @@ export interface NexusGenRootTypes { } export interface NexusGenAllTypes extends NexusGenRootTypes { - PlayerIdModeMonthYearCompoundUniqueInput: NexusGenInputs['PlayerIdModeMonthYearCompoundUniqueInput']; + SwitchAccountIdModeMonthYearCompoundUniqueInput: NexusGenInputs['SwitchAccountIdModeMonthYearCompoundUniqueInput']; UpdateUserProfileInput: NexusGenInputs['UpdateUserProfileInput']; XRankPlacementWhereUniqueInput: NexusGenInputs['XRankPlacementWhereUniqueInput']; RankedMode: NexusGenEnums['RankedMode']; @@ -81,9 +81,9 @@ export interface NexusGenFieldTypes { updateUserProfile: boolean; // Boolean! } Player: { // field return type - names: string[]; // [String!]! + name: string; // String! placements: NexusGenRootTypes['XRankPlacement'][]; // [XRankPlacement!]! - playerId: string; // String! + switchAccountId: string; // String! user: NexusGenRootTypes['User'] | null; // User } Profile: { // field return type @@ -115,9 +115,9 @@ export interface NexusGenFieldTypes { mode: NexusGenEnums['RankedMode']; // RankedMode! month: number; // Int! player: NexusGenRootTypes['Player']; // Player! - playerId: string; // String! playerName: string; // String! ranking: number; // Int! + switchAccountId: string; // String! weapon: string; // String! xPower: number; // Float! year: number; // Int! @@ -129,9 +129,9 @@ export interface NexusGenFieldTypeNames { updateUserProfile: 'Boolean' } Player: { // field return type name - names: 'String' + name: 'String' placements: 'XRankPlacement' - playerId: 'String' + switchAccountId: 'String' user: 'User' } Profile: { // field return type name @@ -163,9 +163,9 @@ export interface NexusGenFieldTypeNames { mode: 'RankedMode' month: 'Int' player: 'Player' - playerId: 'String' playerName: 'String' ranking: 'Int' + switchAccountId: 'String' weapon: 'String' xPower: 'Float' year: 'Int' @@ -188,7 +188,7 @@ export interface NexusGenArgTypes { } Query: { getPlayersXRankPlacements: { // args - playerId: string; // String! + switchAccountId: string; // String! } getUserByIdentifier: { // args identifier: string; // String! @@ -208,7 +208,7 @@ export interface NexusGenInheritedFields {} export type NexusGenObjectNames = "Mutation" | "Player" | "Profile" | "Query" | "User" | "XRankPlacement"; -export type NexusGenInputNames = "PlayerIdModeMonthYearCompoundUniqueInput" | "UpdateUserProfileInput" | "XRankPlacementWhereUniqueInput"; +export type NexusGenInputNames = "SwitchAccountIdModeMonthYearCompoundUniqueInput" | "UpdateUserProfileInput" | "XRankPlacementWhereUniqueInput"; export type NexusGenEnumNames = "RankedMode"; diff --git a/pages/player/[id].tsx b/pages/player/[id].tsx index e3a9306a6..4048904bc 100644 --- a/pages/player/[id].tsx +++ b/pages/player/[id].tsx @@ -15,7 +15,7 @@ const prisma = new PrismaClient(); export const getStaticPaths: GetStaticPaths = async () => { const players = await prisma.player.findMany({}); return { - paths: players.map((p) => ({ params: { id: p.playerId } })), + paths: players.map((p) => ({ params: { id: p.switchAccountId } })), fallback: true, }; }; @@ -27,7 +27,7 @@ export const getStaticProps: GetStaticProps = async ({ params }) => { query: GetPlayersXRankPlacementsDocument, variables: { // FIXME: why ! needed? - playerId: params!.id, + switchAccountId: params!.id, }, }); @@ -37,17 +37,17 @@ export const getStaticProps: GetStaticProps = async ({ params }) => { return { props: { initialApolloState: apolloClient.cache.extract(), - playerId: params!.id, + switchAccountId: params!.id, }, //notfound }; }; -const PlayerPage = ({ playerId }: { playerId: string }) => { +const PlayerPage = ({ switchAccountId }: { switchAccountId: string }) => { const router = useRouter(); const { data } = useGetPlayersXRankPlacementsQuery({ - variables: { playerId }, + variables: { switchAccountId }, skip: router.isFallback, }); diff --git a/prisma/migrations/20201028083612-init/README.md b/prisma/migrations/20201103144752-init/README.md similarity index 70% rename from prisma/migrations/20201028083612-init/README.md rename to prisma/migrations/20201103144752-init/README.md index 63468e6d1..aa98257af 100644 --- a/prisma/migrations/20201028083612-init/README.md +++ b/prisma/migrations/20201103144752-init/README.md @@ -1,6 +1,6 @@ -# Migration `20201028083612-init` +# Migration `20201103144752-init` -This migration has been generated by Kalle (Sendou) at 10/28/2020, 10:36:12 AM. +This migration has been generated by Kalle (Sendou) at 11/3/2020, 4:47:52 PM. You can check out the [state of the schema](./schema.prisma) after the migration. ## Database Steps @@ -30,9 +30,8 @@ CREATE TABLE "public"."Profile" ( CREATE TABLE "public"."XRankPlacement" ( "id" SERIAL, -"playerId" text NOT NULL , +"switchAccountId" text NOT NULL , "playerName" text NOT NULL , -"playerNameLower" text NOT NULL , "ranking" integer NOT NULL , "xPower" Decimal(65,30) NOT NULL , "weapon" text NOT NULL , @@ -43,9 +42,9 @@ PRIMARY KEY ("id") ) CREATE TABLE "public"."Player" ( -"playerId" text NOT NULL , +"switchAccountId" text NOT NULL , "userId" integer , -"names" text [] +"name" text NOT NULL ) CREATE UNIQUE INDEX "User.discordId_unique" ON "public"."User"("discordId") @@ -54,17 +53,15 @@ CREATE UNIQUE INDEX "Profile.customUrlPath_unique" ON "public"."Profile"("custom CREATE UNIQUE INDEX "Profile.userId_unique" ON "public"."Profile"("userId") -CREATE UNIQUE INDEX "XRankPlacement.playerId_mode_month_year_unique" ON "public"."XRankPlacement"("playerId", "mode", "month", "year") +CREATE UNIQUE INDEX "XRankPlacement.switchAccountId_mode_month_year_unique" ON "public"."XRankPlacement"("switchAccountId", "mode", "month", "year") -CREATE INDEX "XRankPlacement.playerNameLower_index" ON "public"."XRankPlacement"("playerNameLower") - -CREATE UNIQUE INDEX "Player.playerId_unique" ON "public"."Player"("playerId") +CREATE UNIQUE INDEX "Player.switchAccountId_unique" ON "public"."Player"("switchAccountId") CREATE UNIQUE INDEX "Player.userId_unique" ON "public"."Player"("userId") ALTER TABLE "public"."Profile" ADD FOREIGN KEY("userId")REFERENCES "public"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE -ALTER TABLE "public"."XRankPlacement" ADD FOREIGN KEY("playerId")REFERENCES "public"."Player"("playerId") ON DELETE CASCADE ON UPDATE CASCADE +ALTER TABLE "public"."XRankPlacement" ADD FOREIGN KEY("switchAccountId")REFERENCES "public"."Player"("switchAccountId") ON DELETE CASCADE ON UPDATE CASCADE ALTER TABLE "public"."Player" ADD FOREIGN KEY("userId")REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE ``` @@ -73,10 +70,10 @@ ALTER TABLE "public"."Player" ADD FOREIGN KEY("userId")REFERENCES "public"."User ```diff diff --git schema.prisma schema.prisma -migration ..20201028083612-init +migration ..20201103144752-init --- datamodel.dml +++ datamodel.dml -@@ -1,0 +1,66 @@ +@@ -1,0 +1,64 @@ +datasource db { + provider = "postgresql" + // FIXME: should use same .env system as Next.JS @@ -121,10 +118,9 @@ migration ..20201028083612-init + +model XRankPlacement { + id Int @id @default(autoincrement()) -+ playerId String -+ player Player @relation(fields: [playerId], references: [playerId]) ++ switchAccountId String ++ player Player @relation(fields: [switchAccountId], references: [switchAccountId]) + playerName String -+ playerNameLower String + ranking Int + xPower Float + weapon String @@ -132,16 +128,15 @@ migration ..20201028083612-init + month Int + year Int + -+ @@unique([playerId, mode, month, year]) -+ @@index([playerNameLower]) ++ @@unique([switchAccountId, mode, month, year]) +} + +model Player { -+ playerId String @unique -+ user User? @relation(fields: [userId], references: [id]) -+ userId Int? @unique -+ names String[] -+ placements XRankPlacement[] ++ switchAccountId String @unique ++ user User? @relation(fields: [userId], references: [id]) ++ userId Int? @unique ++ name String ++ placements XRankPlacement[] +} ``` diff --git a/prisma/migrations/20201028083612-init/schema.prisma b/prisma/migrations/20201103144752-init/schema.prisma similarity index 71% rename from prisma/migrations/20201028083612-init/schema.prisma rename to prisma/migrations/20201103144752-init/schema.prisma index 2d5891e0d..dcc3a9864 100644 --- a/prisma/migrations/20201028083612-init/schema.prisma +++ b/prisma/migrations/20201103144752-init/schema.prisma @@ -42,10 +42,9 @@ enum RankedMode { model XRankPlacement { id Int @id @default(autoincrement()) - playerId String - player Player @relation(fields: [playerId], references: [playerId]) + switchAccountId String + player Player @relation(fields: [switchAccountId], references: [switchAccountId]) playerName String - playerNameLower String ranking Int xPower Float weapon String @@ -53,14 +52,13 @@ model XRankPlacement { month Int year Int - @@unique([playerId, mode, month, year]) - @@index([playerNameLower]) + @@unique([switchAccountId, mode, month, year]) } model Player { - playerId String @unique - user User? @relation(fields: [userId], references: [id]) - userId Int? @unique - names String[] - placements XRankPlacement[] + switchAccountId String @unique + user User? @relation(fields: [userId], references: [id]) + userId Int? @unique + name String + placements XRankPlacement[] } diff --git a/prisma/migrations/20201028083612-init/steps.json b/prisma/migrations/20201103144752-init/steps.json similarity index 93% rename from prisma/migrations/20201028083612-init/steps.json rename to prisma/migrations/20201103144752-init/steps.json index 174cedb8e..aa9404864 100644 --- a/prisma/migrations/20201028083612-init/steps.json +++ b/prisma/migrations/20201103144752-init/steps.json @@ -325,7 +325,7 @@ { "tag": "CreateField", "model": "XRankPlacement", - "field": "playerId", + "field": "switchAccountId", "type": "String", "arity": "Required" }, @@ -359,7 +359,7 @@ "directive": "relation" }, "argument": "fields", - "value": "[playerId]" + "value": "[switchAccountId]" }, { "tag": "CreateArgument", @@ -373,7 +373,7 @@ "directive": "relation" }, "argument": "references", - "value": "[playerId]" + "value": "[switchAccountId]" }, { "tag": "CreateField", @@ -382,13 +382,6 @@ "type": "String", "arity": "Required" }, - { - "tag": "CreateField", - "model": "XRankPlacement", - "field": "playerNameLower", - "type": "String", - "arity": "Required" - }, { "tag": "CreateField", "model": "XRankPlacement", @@ -440,29 +433,13 @@ "arguments": [ { "name": "", - "value": "[playerId, mode, month, year]" + "value": "[switchAccountId, mode, month, year]" } ] }, "directive": "unique" } }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Model", - "model": "XRankPlacement", - "arguments": [ - { - "name": "", - "value": "[playerNameLower]" - } - ] - }, - "directive": "index" - } - }, { "tag": "CreateModel", "model": "Player" @@ -470,7 +447,7 @@ { "tag": "CreateField", "model": "Player", - "field": "playerId", + "field": "switchAccountId", "type": "String", "arity": "Required" }, @@ -480,7 +457,7 @@ "path": { "tag": "Field", "model": "Player", - "field": "playerId" + "field": "switchAccountId" }, "directive": "unique" } @@ -552,9 +529,9 @@ { "tag": "CreateField", "model": "Player", - "field": "names", + "field": "name", "type": "String", - "arity": "List" + "arity": "Required" }, { "tag": "CreateField", diff --git a/prisma/migrations/migrate.lock b/prisma/migrations/migrate.lock index 4fc8da153..cd8df2af2 100644 --- a/prisma/migrations/migrate.lock +++ b/prisma/migrations/migrate.lock @@ -1,3 +1,3 @@ # Prisma Migrate lockfile v1 -20201028083612-init \ No newline at end of file +20201103144752-init \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cfa534bc9..b67d297b7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,10 +42,9 @@ enum RankedMode { model XRankPlacement { id Int @id @default(autoincrement()) - playerId String - player Player @relation(fields: [playerId], references: [playerId]) + switchAccountId String + player Player @relation(fields: [switchAccountId], references: [switchAccountId]) playerName String - playerNameLower String ranking Int xPower Float weapon String @@ -53,14 +52,13 @@ model XRankPlacement { month Int year Int - @@unique([playerId, mode, month, year]) - @@index([playerNameLower]) + @@unique([switchAccountId, mode, month, year]) } model Player { - playerId String @unique - user User? @relation(fields: [userId], references: [id]) - userId Int? @unique - names String[] - placements XRankPlacement[] + switchAccountId String @unique + user User? @relation(fields: [userId], references: [id]) + userId Int? @unique + name String + placements XRankPlacement[] } diff --git a/prisma/seed.ts b/prisma/seed.ts index 61736eef0..417056518 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -75,7 +75,6 @@ const main = async () => { return prisma.xRankPlacement.create({ data: { playerName, - playerNameLower: i % 2 === 0 ? `player${i}` : `選手${i}`, mode: getMode(i), month: 12, year: 2020, @@ -84,8 +83,8 @@ const main = async () => { weapon: "Splattershot Jr.", player: { create: { - playerId: "" + i, - names: [playerName], + switchAccountId: "" + i, + name: playerName, user: i === 0 ? { diff --git a/scenes/Player/queries/getPlayersXRankPlacements.graphql b/scenes/Player/queries/getPlayersXRankPlacements.graphql index 15fb7ebcd..b6b0a1313 100644 --- a/scenes/Player/queries/getPlayersXRankPlacements.graphql +++ b/scenes/Player/queries/getPlayersXRankPlacements.graphql @@ -1,5 +1,5 @@ -query getPlayersXRankPlacements($playerId: String!) { - getPlayersXRankPlacements(playerId: $playerId) { +query getPlayersXRankPlacements($switchAccountId: String!) { + getPlayersXRankPlacements(switchAccountId: $switchAccountId) { id playerName ranking diff --git a/scenes/Top500/index.tsx b/scenes/Top500/index.tsx index 3cf4c86a9..298ca7319 100644 --- a/scenes/Top500/index.tsx +++ b/scenes/Top500/index.tsx @@ -47,7 +47,7 @@ const XSearch: React.FC = ({ placements }) => { {placements.map((record) => { return ( - + {getRankingString(record.ranking)} diff --git a/scenes/Top500/queries/XRankPlacements.graphql b/scenes/Top500/queries/XRankPlacements.graphql index 209fe1305..3d8de425c 100644 --- a/scenes/Top500/queries/XRankPlacements.graphql +++ b/scenes/Top500/queries/XRankPlacements.graphql @@ -1,6 +1,6 @@ query getXRankPlacements($month: Int!, $year: Int!, $mode: RankedMode!) { getXRankPlacements(month: $month, year: $year, mode: $mode) { - playerId + switchAccountId playerName ranking xPower diff --git a/schema.graphql b/schema.graphql index bde605d67..6c35c2d40 100644 --- a/schema.graphql +++ b/schema.graphql @@ -7,22 +7,12 @@ type Mutation { } type Player { - """ - Set of names player has had in Top 500 results. The most recent one is the first one of the list. - """ - names: [String!]! + name: String! placements(after: XRankPlacementWhereUniqueInput, before: XRankPlacementWhereUniqueInput, first: Int, last: Int): [XRankPlacement!]! - playerId: String! + switchAccountId: String! user: User } -input PlayerIdModeMonthYearCompoundUniqueInput { - mode: RankedMode! - month: Int! - playerId: String! - year: Int! -} - type Profile { bio: String country: String @@ -36,7 +26,7 @@ type Profile { } type Query { - getPlayersXRankPlacements(playerId: String!): [XRankPlacement!]! + getPlayersXRankPlacements(switchAccountId: String!): [XRankPlacement!]! getUserByIdentifier(identifier: String!): User getXRankPlacements(mode: RankedMode!, month: Int!, year: Int!): [XRankPlacement!]! } @@ -48,6 +38,13 @@ enum RankedMode { TC } +input SwitchAccountIdModeMonthYearCompoundUniqueInput { + mode: RankedMode! + month: Int! + switchAccountId: String! + year: Int! +} + input UpdateUserProfileInput { bio: String country: String @@ -74,9 +71,9 @@ type XRankPlacement { mode: RankedMode! month: Int! player: Player! - playerId: String! playerName: String! ranking: Int! + switchAccountId: String! weapon: String! xPower: Float! year: Int! @@ -84,5 +81,5 @@ type XRankPlacement { input XRankPlacementWhereUniqueInput { id: Int - playerId_mode_month_year: PlayerIdModeMonthYearCompoundUniqueInput + switchAccountId_mode_month_year: SwitchAccountIdModeMonthYearCompoundUniqueInput }