how should i store lists of lists in MYSQL - javascript

I want to store this list of lists but I don't know how to store it in MySQL
list[x][y] the items in this list contains {li:[{x:x,y:y}] , pos:{x:y}}
list[x][y].li[z].x
list[x][y].li[z].y
list[x][y].pos.x
list[x][y].pos.y
for better undersing, please have a look at this
edited:
is this right? so this means i will only have 2 tables?

You should use a separate table with sub-lists that have a column parent_id, and then a third table with actual list items of low level lists.
The query for this will look like this:
SELECT li.x, li.y, sl.id
FROM li_items li
JOIN sub_lists sl on li.list_id = sl.id
JOIN lists l on sl.parent_id = l.id;
The process of converting the result rows depends on if you use some ORM or plain mysql client.
You could also store it as a JSON, as deleted answer has suggested, but than you wan't be able to query specific items without selecting and parsing all the lists. You could also use MySQL's JSON column, but In your case having separate tables seems to be better

Related

Is there any clean way to append items to a one-to-many relationship?

Very innocent question
If we have an array of items in a table cell, is there any way to append data to it without having the need to fetch the data of the whole row to get its content, in order to finally push the updated data to the cell?
I have a column named content, which is an array of file identifiers, and I want to append some new data to it.
Is there any clean way to do this action? Or we need to perform a select() before an update()?
It is generally better to create a separate table to store 1 to many relationships than to store them in an array, but with that being said, you could achieve appending items to array by using rpc() and calling your Postgres function.
You could create a function like the following to append array values:
create or replace function append_array(new_element text, id uuid)
returns void
as
$func$
update array_table
set array_element = array_append(array_element, $1)
where id = $2
$func$
language sql;
You can read more about this here

What's the best way to structure data in a file, when most keys have the same value?

I'm working on a browser extension and have a set of items, that has to be mapped to some categories.
item-1: category-1
item-2: category-1
item-3: category-2
item-4: category-2
...
The mapping of the items to the categories is saved in a text file (json) but I'll only need a subset of about 60 items from all the item/category pairs when the extension runs. As keys I have a list of items and I need to search for their category in the file. There are ~200 items and 5 categories in the file.
Saving all the items as keys and the categories as their values creates a lot of redundancy. Therefore I had the idea to use the categories as keys and the items as values, like this:
category-1:
item-1,
item-2
category-2,
item-3,
item-4
...
But like this i have to iterate over all categories to search for the value I'm looking for, which is not an ideal solution.
How can I structure my data in a way, that the redundancy is minimal and the processing of the data is as fast as possible? Or is there a better way to read the data from my second example, than iterating over all the categories?
Other formats for storing the data are ok, as long as it can be processed in JavaScript.
There are ~200 items and 5 categories.
At that scale you won't have to think about this at all, unless you'll be running your app on a toaster.
I'd suggest just keeping items in a flat list in your data store (akin to what a RDBMS would do with database rows).
If you need all items in a single category, use .filter(item => item.category === something) or similar; if you really feel like it, you can pre-cache that mapping when you load your data (or when it changes), with something like
const byCategory = {};
items.forEach(item =>
(byCategory[item.category] =
byCategory[item.category] || []).push(item)
);

SharePoint REST API - Is it possible to expand lookup column in list A to list B and get data with attachments of B at once?

I have a list A. In this list, I have a lookup column authorId. In author list, I have some data columns and one attachment. I need to get ifformations about both - I need data and attachment too.
Is it possible in one query?
I believe you will need to do a select and expand for both columns in your REST call similar to this answer.
To borrow their answer:
/_api/web/lists(guid'<guid>')/items?$select=AttachmentFiles,authorId/FieldToExpand&$expand=AttachmentFiles,authorId

How to save HTML list number in JSON

I have a HTML page where I can create lists from a javascript. It saves the data in JSON. To the list there is also a javascript, which makes it possible to drag and sort the list items if you want to do so. All the javascripts are independent of each other.
My problem is that I don't know how I should index these list items in the order they have been placed, and afterwards send it to the database. Of course all this should happen on the client before sending it to database.
Remember this question is ONLY about storing the right index number in each list item; in the right order. If I would change the order of a list item, the index number should change too, so it will be saved in the right order.
You can iterate over the collection just before saving them and assign the current values.
I did this with a menu I built once. In jQuery I did something like this:
$("#mylist li").each(function(inx){
$(this).attr('index',inx);
});
Then, when you save them, include the index attribute so you know the final order.
EDIT: per comment by #Boaz

Most Efficient way of Filtering an Html Table?

I have an ajax function which call a servlet to get list of products from various webservices, the number of products can go up to 100,000. I need to show this list in a html table.
I am trying to provide users an interface to filter this list based on several criteria. Currently I am using a simple jQuery plugin to achieve this, but I found it to hog memory and time.
The Javascript that I use basically uses regex to search and filter rows matching the filtering criteria.
I was thinking of an alternate solution wherein I filter the JSON array returned by my servlet and bind the html table to it. Is there a way to achieve this, if there is, then is it more efficient than the regex approach.
Going through up to 100,000 items and checking if they meet your criteria is going to take a while, especially if the criteria might be complex (must be CONDO with 2 OR 3 bedrooms NOT in zip code 12345 and FIREPLACE but not JACUZZI).
Perhaps your servlet could cache the data for the 100,000 items and it could do the filtering, based on criteria posted by the user's browser. It could return, say, "items 1-50 of 12,456 selected from 100,000" and let the user page forward to the next 50 or so, and even select how many items to get back (25, 50, all).
If they select "all" before narrowing down the number very far, then a halfway observant user will expect it to take a while to load.
In other words, don't even TRY to manage the 100,000 items in the browser, let the server do it.
User enters filter and hits
search.
Ajax call to database, database has indexes on appropriate
columns and the database does the filtering.
Database returns result
Show result in table. (Probably want it to be paged to
only show 100-1000 rows at a time
because 100,000 rows in a table can
really slow down your browser.
Edit: Since you don't have a database, the best you're going to be able to do is run the regex over the JSON dataset and add results that match to the table. You'll want to save the JSON dataset in a variable in case they change the search. (I'm assuming that right now you're adding everything to the table and then using the jquery table plugin to filter it)
I'm assuming that by filtering you mean only displaying a subset of the data; and not sorting.
As you are populating the data into the table add classes to each row for everything in that row you want to filter by. e.g.:
<tr class="filter1 filter2 filter3">....
<tr class="filter1 filter3">....
<tr class="filter2">....
<tr class="filter3">....
Then when you want to apply a filter you can do something like:
$('TR:not(.filter1)').hide();
I agree with Berry that 100000 rows in the browser is bit of a stretch, but if there's anything that comes close to handling something of that magnitude then it's jOrder. http://github.com/danstocker/jorder
Create a jOrder table based on your JSON, and add the most necessary indexes. I mean the ones that you must at all cost filter by.
E.g. you have a "Name" field with people's names.
var table = jOrder(json)
.index('name', ['Name'], { sorted: true, ordered: true });
Then, for instance, this is how you select the records where the Name field starts with "John":
var filtered = table.where([{ Name: 'John' }], { mode: jOrder.startof, renumber: true });
Later, if you need paging in your table, just feed the table builder a filtered.slice(...).
If you're getting back xml, you could just use jQuery selection
$('.class', context) where context is your xml response.
From this selection, you could just write the xml to the page and use CSS to style it. That's where I'd start at first, at least. I'm doing something similar in one of my applications, but my dataset is smaller.
I don't know what you mean by "bind"? You can parse JSON and then use for loop (or $.each()) to populate ether straight HTML or by using grid plugin's insert/add

Categories

Resources