Question

In my SQL server, I have 64k+ records in a single table. I want to store those records in my client-side IndexedDB database.

I thought to create XML files of those data, read those files and save the contents in my IndexedDB. I am getting stuck in reading XML file and saving it to IndexedDB.

How can I save an XML file into an IndexedDB database?

Was it helpful?

Solution 2

I wouldn't use XML for that. Why don't you write a query that gives you the data in a JSON format? This will be a lot easier to save this into IDB.

Also storing such a large number of records in the IDB isn't really preferable. Think good about the reason you want to do this. Maybe there are other ways to achive the same functionality without having to transfer all this data.

OTHER TIPS

Manikandan K, thanks for joining Stackoverflow to discuss IDB.

Something to keep in mind is that IndexedDB is an "indexed key value store" that works a little bit like a document-oriented NoSQL database such as MongoDB, in case you're familiar. What this means is that it doesn't match up 1-to-1 with concepts from relational databases such as columns and tables.

What IDB stores are "objects" so it's best to transform your SQL database into a format that is "object-like." JavaScript object notation, or "JSON" is that representation.

If you transform your database into XML, you'll still have to transform it into an object such that IDB can store it. Thus, it's good advice from Kristof to transform your database directly into JSON.

To help you wrap your head around this, think of a table as an object. That table would have rows and columns, which you'd map to JavaScript object attributes and values. Most "primitive" JavaScript values such as strings, numbers and so forth can be stored.

For example, if your SQL table looked like this:

+------+--------+--------+
|      | ColA   | ColB   |
+------+--------+--------+
| Row1 | CellA1 | CellB1 |
| Row2 | CellA2 | CellB2 |
| Row3 | CellA3 | CellB3 |
+------+--------+--------+

Your JavaScript object may look like this:

var myObjectToStore = {
    'Row1': { 'ColA': 'CellA1', 'ColB': 'CellB1' },
    'Row2': { 'ColA': 'CellA2', 'ColB': 'CellB2' },
    'Row3': { 'ColA': 'CellA3', 'ColB': 'CellB3' }
};

The JSON representation of that object is very similar:

{
  "Row1": {
    "ColA": "CellA1",
    "ColB": "CellB1"
  },
  "Row2": {
    "ColA": "CellA2",
    "ColB": "CellB2"
  },
  "Row3": {
    "ColA": "CellA3",
    "ColB": "CellB3"
  }
}

You can use a browser-native method like JSON.parse to turn it into an object that could be stored directly into IDB without additional transformations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top