snorre søvold
snorre søvold

Reputation: 57

How to do a many to many field in Pocketbase

I have a database with users and products.

My products have a relation field relating to the users so i can get the usernames and such via

const product = await pb.collection('products').getOne(id, { expand: "user" })

How would I do the same for users, and getting all products that a user has? Should i add a relation field to my users?

Upvotes: 1

Views: 2031

Answers (1)

snorre søvold
snorre søvold

Reputation: 57

Got an answer from one of the devs.

If you have many users then you should:

const products = await pb.collection("products").getFullList(200, { filter: `user = "${userId}"` })

and if you only have 1:

const user = await pb.collection("user").getOne("USER_ID", { expand: "products(user)") })

Upvotes: 0

Related Questions