I have a 3 tables:
- services – info about services (id, title)
- city – info about city (id, city_name)
- service_city – map multiple cities to services (service_id, city_id)
and I need functionality to select only those services which contain certain city provided by user.
So far I have got is solution below:
const cityToSearch = {
id: 1,
city_name: 'Amsterdam'
}
let query = supabase
.from('service')
.select('title, city(id,city_name)'
)
if (search) query.ilike('title', `%${search}%`);
if (city) {
query.eq('city.id', cityToSearch.id);
query.not('city', 'is', null);
}
query.range(from, to);
const { data, error } = await query;
The problem is it returns me array of cities with only one city that I search for:
[
{
title: 'Service 1',
id: 'service_1',
city: [
{
id: 1,
city_name: 'Amsterdam'
}
],
},
{
title: 'Service 2',
id: 'service_2',
city: [
{
id: 1,
city_name: 'Amsterdam'
}
],
}
]
however, I need an array of cities that include my cityToSearch (ex.):
[
{
title: 'Service 1',
id: 'service_1',
city: [
{
id: 1,
city_name: 'Amsterdam'
},
{
id: 2,
city_name: 'Paris'
}
],
},
{
title: 'Service 2',
id: 'service_2',
city: [
{
id: 1,
city_name: 'Amsterdam'
},
{
id: 3,
city_name: 'Krakow'
}
],
}
]
Supabase documentation: https://supabase.com/docs/reference/javascript
Any ideas? : )