Sorry for the non-descriptive title, I don't know how to explain what I'm trying to achieve succinctly.
In most SQL use-cases, we have records in the database that have properties, and we are trying to retrieve records which match some conditions. For example, we may have for instance a table of products, where each product has a release date, a price and a category. We can then easily query for products released after a certain date, with a price below a certain value and belonging or not belonging to a specific category.
What I am trying to achieve is essentially the reverse of this. I have a table of services with conditions, and each user has various properties. (think 'date of birth', 'has drivers license', etc...) I need to display a list of all services that a given user is eligible for.
Another one of my requirements is that this needs to be possible for anonymous users, so the property values for the user won't be stored in a table anywhere, they will simply be provided as part of the user's request to my server.
I'm struggling to think of what the best way to achieve this would be. At the moment I see two main possibilities:
1. Build out a relational model for the conditions, and build long SQL queries based on user input
In order for a user to be eligible for a service, they must fulfill at least one Condition Group. In order for a user to fulfill a Condition Group, they must fulfill all the Conditions within that group.
Pros:
Works with a well defined ERD and proper SQL queries
Cons:
Have to generate very complicated SQL queries with multiple joins, reduced flexibility for writing conditions
2. Store conditions as a string. Build a condition parser and go through all services testing their condition against the user input.
Pros
Flexible and easier-to-write conditions, easier to develop
Cons
Queries have to take place in application-layer instead of database-layer, possibly bad performance
I’m wondering if anyone has dealt with a similar problem in the past, and how they solved it. Maybe there’s an option I’m not considering? I’m open to use a NoSQL solution if it’s a better fit for the problem, but my preference is to do this against a SQL database if practical.
SELECT someFields FROM wherever WHERE DOB < 1998 AND hasDriversLicense = true
SELECT someFields FROM wherever WHERE DOB < 1998 AND hasDriversLicense = true
and executing it against the server to achieve what you want?