5

I recently started working with taffydb. Assuming I have this as my data

db= TAFFY([
{OrderNo:'prod1',range: 3,description:'one two'},
{OrderNo:'prod2',range: 2,description:'one two three'},
{OrderNo:'prod3',range: 2,description:'one three two'},
{OrderNo:'prod4',range: 6,description:'one two four three'},
{OrderNo:'prod5',range: 5,description:'three'},...

if I wanted to write a query to find all records with "one two" and "three" I'd do something like

db({description:{likenocase:"one two"}},{description:{likenocase:"three"}}).get()

this would return products 2 and 4. Unfortunately I can't figure out how to do this with a dynamic query that will have an unknown number of variables to search for. I'm doing this to let the user search for their own supplied words.

Anyone got any ideas?

Darksbane
  • 205
  • 1
  • 3
  • 11

3 Answers3

0

As a precursor, this will not be the best answer to your problem. But it will work. :)

So the user will have the option of searching a database with an "unknown number of variables". Let's add a maximum amount of variables--perhaps 10?

Now we catch all the user's search variables in an array:

// Create a dynamic array
var userSearchVars = [];

// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
   userSearchVars.push( $(this).val());
}

// Note: by default an empty input will return the empty string: ""

Using your code snippet, just query the database with the array:

db(
    {description:{likenocase:userSearchVars[0]}},
    {description:{likenocase:userSearchVars[1]}},
    {description:{likenocase:userSearchVars[2]}},
    {description:{likenocase:userSearchVars[3]}},
    {description:{likenocase:userSearchVars[4]}},
    {description:{likenocase:userSearchVars[5]}},
    {description:{likenocase:userSearchVars[6]}},
    {description:{likenocase:userSearchVars[7]}},
    {description:{likenocase:userSearchVars[8]}},
    {description:{likenocase:userSearchVars[9]}}
).get()
Jacob Bridges
  • 725
  • 2
  • 8
  • 16
  • This however does not explain how to generate a db query dynamically, the question is how to generate dynamic like queries, when the number of like inputs vary – Clain Dsilva Oct 28 '16 at 13:14
0

Adapting @Jacob-IT's answer so that its dynamic. Used Taffy for the first time tonight and just discovered you can pass an array of objects as the query argument.

// Create a dynamic array
var userSearchVars = [];

// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
// This is my edit - push the whole query on to the array.
   userSearchVars.push({description:{likenocase: $(this).val() }});
}
// Then pass the whole query array in...
db( userSearchVars ).get() 

Tested the above - and it worked for me.

Rob
  • 1,576
  • 3
  • 22
  • 52
0

You can do it like this

let items = [];
items.push({description:{likenocase:"one two"}});
items.push({description:{likenocase:"three"}});

db(...items).get()
K Chikuse
  • 11
  • 1
  • Welcome to Stack Overflow _ A 'good answer' also includes a sentence or two in plain text that explains why you think your solution will work. Please visit SO Help Center and specifically this guideline for more details >>> stackoverflow.com/help/how-to-answer – inputforcolor Jan 12 '22 at 21:41