graphql prisma 查询获取有歌曲曲目的艺术家
graphql prisma query get artists who has song track
我有三个 table,艺术家,专辑和曲目。我想查询至少拥有一首歌曲曲目的艺术家
这是我的 graphql prisma 模式。我可以知道如何写查询吗?
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-musl"]
}
generator typegraphql {
provider = "typegraphql-prisma"
emitTranspiledCode = true
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Artist {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
bio String? @db.VarChar(1024)
profile_picture String @db.VarChar(512)
albums Album[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("artists")
}
model Album {
id String @id @default(cuid())
title String @db.VarChar(255)
album_cover String @db.VarChar(512)
description String? @db.VarChar(5120)
released Int @default(1900)
artists Artist[]
genres Genre[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("albums")
}
model Track {
id String @id @default(cuid())
title String @db.VarChar(255)
description String? @db.VarChar(5120)
lyric String? @db.LongText
mp3_url String @db.VarChar(1024)
youtube_url String @db.VarChar(1024)
band String? @db.VarChar(255)
duration Int @default(0)
artists Artist[]
album Album @relation(fields: [album_id], references: [id])
album_id String
songWriters SongWriter[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("tracks")
}
这是解析器
import { Resolver, ArgsType, Field, Args, Query, Ctx, ObjectType,
Int } from "type-graphql";
import { Artist } from "@generated/type-graphql";
import { Context } from "../interfaces";
@ArgsType()
export class ArtistArgs {
@Field((type) => Int, { nullable: false })
page!: number;
@Field((type) => Int, { nullable: false })
take!: number;
}
@ObjectType()
export class ArtistResponse {
@Field(type => [Artist])
artists: Artist[] =[];
@Field((type) => Int, { nullable: false })
total!: number;
}
@Resolver()
class ArtistResolver {
//@Authorized("ADMIN")
@Query((returns) => ArtistResponse, { nullable: true })
async getArtists(@Ctx() { req, res, prisma }: Context, @Args()
{ page, take }: ArtistArgs): Promise<ArtistResponse | null> {
const artists = (await prisma.artist.findMany({
take: take,
skip: (page - 1) * take,
orderBy: [
{
name: "asc"
}
]
}));
const count = (await prisma.artist.count({
}));
return {artists: artists, total: count};
}
}
export default ArtistResolver;
您可以使用 prisma OR
and gt
来过滤至少拥有一首歌曲曲目的艺术家。
或接受一系列条件,这些条件必须 return 为真,而 gt 表示值必须大于 x。
这就像我在
中的回答
要仅获取至少有一个 Track
的 Artist
,您应该使用一些并指定一个条件,该条件对于任何存在的相关记录始终 return 为真。
如果您希望查询包含相关的 Track
,您必须在包含 属性.
中指定它
await prisma.artist.findMany({
where: {
tracks: {
some: {
id: { not: "" } // It always should be true.
},
},
},
// if you want to include related tracks in returned object:
include: {
tracks: true,
},
});
我有三个 table,艺术家,专辑和曲目。我想查询至少拥有一首歌曲曲目的艺术家
这是我的 graphql prisma 模式。我可以知道如何写查询吗?
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-musl"]
}
generator typegraphql {
provider = "typegraphql-prisma"
emitTranspiledCode = true
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Artist {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
bio String? @db.VarChar(1024)
profile_picture String @db.VarChar(512)
albums Album[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("artists")
}
model Album {
id String @id @default(cuid())
title String @db.VarChar(255)
album_cover String @db.VarChar(512)
description String? @db.VarChar(5120)
released Int @default(1900)
artists Artist[]
genres Genre[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("albums")
}
model Track {
id String @id @default(cuid())
title String @db.VarChar(255)
description String? @db.VarChar(5120)
lyric String? @db.LongText
mp3_url String @db.VarChar(1024)
youtube_url String @db.VarChar(1024)
band String? @db.VarChar(255)
duration Int @default(0)
artists Artist[]
album Album @relation(fields: [album_id], references: [id])
album_id String
songWriters SongWriter[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("tracks")
}
这是解析器
import { Resolver, ArgsType, Field, Args, Query, Ctx, ObjectType,
Int } from "type-graphql";
import { Artist } from "@generated/type-graphql";
import { Context } from "../interfaces";
@ArgsType()
export class ArtistArgs {
@Field((type) => Int, { nullable: false })
page!: number;
@Field((type) => Int, { nullable: false })
take!: number;
}
@ObjectType()
export class ArtistResponse {
@Field(type => [Artist])
artists: Artist[] =[];
@Field((type) => Int, { nullable: false })
total!: number;
}
@Resolver()
class ArtistResolver {
//@Authorized("ADMIN")
@Query((returns) => ArtistResponse, { nullable: true })
async getArtists(@Ctx() { req, res, prisma }: Context, @Args()
{ page, take }: ArtistArgs): Promise<ArtistResponse | null> {
const artists = (await prisma.artist.findMany({
take: take,
skip: (page - 1) * take,
orderBy: [
{
name: "asc"
}
]
}));
const count = (await prisma.artist.count({
}));
return {artists: artists, total: count};
}
}
export default ArtistResolver;
您可以使用 prisma OR
and gt
来过滤至少拥有一首歌曲曲目的艺术家。
或接受一系列条件,这些条件必须 return 为真,而 gt 表示值必须大于 x。
这就像我在
要仅获取至少有一个 Track
的 Artist
,您应该使用一些并指定一个条件,该条件对于任何存在的相关记录始终 return 为真。
如果您希望查询包含相关的 Track
,您必须在包含 属性.
await prisma.artist.findMany({
where: {
tracks: {
some: {
id: { not: "" } // It always should be true.
},
},
},
// if you want to include related tracks in returned object:
include: {
tracks: true,
},
});