Alex
Alex

Reputation: 2959

Where can I store a large amount of "business" data/object in my JS project?

I work with AngularJs. I have a factory providing services about buildings.

I have a lot of buildings (around 50-60) with a lot properties and sub-properties related to them (around 15-20, more or less complex). Business requirements force me to store all the data about buildings in JS format.

So I created an object like that:

var Buildings = {
        myFirstBuilding: {
            id: 1,
            name: 'Building 1',
            quantity: 0,
            costs: {
                myFirstCost: {
                    name : 'Cost 1',
                    value: 10,
                    isAvailable: 0
                },
                mySecondCost: {
                    name :  'Cost 2',
                    value: 20,
                    isAvailable: 0
                }
            },
            workersAvailable: 0,
            type: 'Type 1',
            /*
              other properties...
            */,
        mySecondBuilding: {
            id: 1,
            name: 'Building 2',
            quantity: 3,
            /*
              and so on...
            */

I can update the structure of this object if needed. The only requirements is to be able to store it in user's browser and to "JSON it" when it's needed.

Where should I store this object in my project? Can I reference it from an external file in my factory? Should I include it directly in my factory?

Do you see any inconsistencies in this object as it's actually made in my example above?

Upvotes: 1

Views: 130

Answers (1)

Ori Drori
Ori Drori

Reputation: 191976

You've got 3 options as far as I see it:

  1. Use an angular value or constant do store the data, if you just need to render it, and don't require database search/filter capabilities, and it's small enough.
  2. If it's large, but not huge, and you don't need database search/filter, store it in local storage and read it using an angular service.
  3. If it's very large and/or you need db search/filter use indexedDB on the browser. As some implementations of indexedDB are pretty broken, you should use a wrapper library such as ydn-db.

Upvotes: 4

Related Questions