Select only UUID column with "with()" method


#1

Hi Team!

I want display a list of comments with only UUID column.

It is possible ?

const post = await Post.query()
	.where('uuid', params.postUuid)
	.where('user_id', user.id)
	.with('comments')
	.first()

console.log(post)

As:

{
	"title": "Hello World!"
	"content": "Lorem ipsum dolor sit amet",
	"comments": [
		"uuid_1",
		"uuid_2",
		"uuid_3"
	]
}

I tried this:

const post = await Post.query()
	.where('uuid', params.postUuid)
	.where('user_id', user.id)
	.with('comments', (builder) => {
    	builder.select('uuid')
  	})
	.first()

console.log(post)

Only, the problem is that there is a JSON key that is added (pivot) and I do not want it.


#2

In order to form the nested tree. Adonis will need the primary key and it is not possible without that.

So in short, you will always get an object of array, that you can map over to pick the UUID’s