如何解决如何为其他用户创建模型 为特定用户添加/删除关注者查找用户的关注者和关注列表
我一直在做一个项目,最近我开始关注用户关注其他用户并看到我正在使用的内容(prisma、graphql 和 nexus),所以今天我创建了一个如下所示的模型
model Following {
id Int @id @default(autoincrement())
followId Int
User User? @relation(fields: [userId],references: [id])
userId Int?
}
我认为这会奏效,但我意识到它只会让我成为用户,而不是我关注的用户,所以我的问题是如何解决这个问题,我是创建另一个模型还是只是在当前模型上重新调整一些东西
解决方法
如果我没记错的话,对于每个用户,您需要以下内容:
- 能够向
function animate() { this.animationFrameRef = requestAnimationFrame(this.animate.bind(this)); this.renderer.render(this.scene,this.camera); } function init() { let width = this.elementRef.nativeElement.clientWidth; let height = this.elementRef.nativeElement.clientHeight; this.scene = new Scene(); this.scene.background = new Color(0xeeeeee); this.camera = new PerspectiveCamera(50,width / height,.1,2000); this.camera.rotation.y = 45 / 180 * Math.PI; this.camera.position.x = 800; this.camera.position.y = 100; this.camera.position.z = 1000; this.renderer = new WebGLRenderer({ antialias: true }); this.renderer.setPixelRatio(window.devicePixelRatio); this.renderer.setSize(width,height); document.body.appendChild(this.renderer.domElement); this.controls = new OrbitControls(this.camera,this.renderer.domElement); this.controls.minDistance = 1; this.controls.maxDistance = 800; this.controls.enableDamping = true; this.controls.dampingFactor = 0.05; this.controls.update(); this.hlight = new AmbientLight(null); this.scene.add(this.hlight); this.directionalLight = new DirectionalLight(null); this.directionalLight.position.set(1,1,0); this.directionalLight.castShadow = true; this.scene.add(this.directionalLight); let loader = new GLTFLoader(); this.isLoading = true; loader.load(this.model,(gltf) => { var box = new Box3().setFromObject(gltf.scene); var center = new Vector3(); box.getCenter(center); gltf.scene.position.sub(center); // center the model this.scene.add(gltf.scene); },(xhr) => { console.log((xhr.loaded / xhr.total * 100) + '% loaded'); },(event) => {}); window.addEventListener('resize',this.onWindowResize.bind(this)); this.animate(); }
的关注者添加/删除User A
- 查找关注某个用户的
User B
条记录。 - 查找特定用户关注的
User
条记录。
您不一定需要明确定义一个单独的模型。相反,您可以为此使用 User
self-relation。这是您的 many-to-many
模型在 Prisma 架构中的样子
User
为特定用户添加/删除关注者
如果用户记录已经存在,您可以像这样添加另一个用户作为关注者:
model User {
id String @id @default(uuid()) // or whatever your unique id field is.
following User[] @relation("UserFollows",references: [id])
followers User[] @relation("UserFollows",references: [id])
// ...other fields in user table
}
您还可以通过将 const updatedUser = await prisma.user.update({
where: {
id: followedUserId,},data: {
followers: {
connect: { // change to disconect for removing a follower.
id: followerUserId,});
调用更改为 connect
调用来从其他用户的关注者中删除用户。
查找用户的关注者和关注列表。
您可以执行此操作来获取 disconnect
记录以及他们的关注者和关注者列表。
user
您可以在 Prisma Docs 的此 concept guide 中了解有关自我关系的更多信息。
,看看这个:
https://www.prisma.io/dataguide/datamodeling/making-connections#introduction 和
似乎您正在尝试创建一对多关系:
https://www.prisma.io/docs/concepts/components/prisma-schema/relations/one-to-many-relations
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。