Search code examples
javascriptcsvindexeddbdexie

CSV GTFS populate with dexie.js


I'm not sure the best practice for Dexie, in creating a db from csv text files, I just started using it yesterday.

What I've done is created my stores

  var db = new Dexie('gtfs');
db.version(1).stores({
    'calendar'      : "++id,service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date",
    'calendar_dates': "++id,service_id,date,exception_type",
    'stop_times'    : "++id,trip_id,arrival_time,departure_time,stop_id,stop_sequence,pickup_type,drop_off_type",
    'stops'         : "++id,stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding",
    'trips'         : "++id,route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,shape_id,wheelchair_accessible,bikes_allowed"
});

indexedDB list

This is how it looks so far, not very familiar with indexedDB but it's looking good so far.

Now since I have a gtfs folder with all my txt files, I iterate through and get the data from txt files, and convert to JSON.

 db.on('ready', function () {

      return new Dexie.Promise(function (resolve, reject) {
        // var url = gtfs
        gtfs.forEach(function (name, index) {
          var url = 'gtfs/' + name + '.txt';
          $.ajax(url, {
            type: 'get',
            dataType: 'text',
            error: function (xhr, textStatus) {
              // Rejecting promise to make db.open() fail.
              reject(textStatus);
            },
            success: function (data) {
              // Resolving Promise will launch then() below.
              var result = csvJSON(data);
              var res = JSON.parse(result);
              console.log("Got ajax response. We'll now add the objects.");
              // By returning the db.transaction() promise, framework will keep
              // waiting for this transaction to commit before resuming other
              // db-operations.
              resolve(data);
             }
          });
        });

So now that's working (actually, it goes through every txt file, but only resolves on the last one), I can't keep everything in 'someTable' as the original sample code was doing. So as seen above I created my new stores. I hit this obstacle though, where I have to hardcode my table as a method. ie db.calendar.

 }).then(function (data) {
            var result = csvJSON(data);
            var res = JSON.parse(result);
            console.log("Got ajax response. We'll now add the objects.");
            // By returning the db.transaction() promise, framework will keep
            // waiting for this transaction to commit before resuming other
            // db-operations.
             return db.transaction('rw', db.calendar, function () {
                res.forEach(function (item) {
                  console.log("Adding object: " + JSON.stringify(item));
                  db.calendar.add(item);
                })
              })
          }).then(function () {
            console.log("Transaction committed");
          });
    })

For sake of keeping my code 'DRY' is there at least a way to get all created table methods in db that I can add to respectively. If so, does that still let me handle the key paths I've created for each table?


Solution

  • At the time of this question this was the dexie version: "dexie": "^1.4.2"

    https://github.com/dfahlander/Dexie.js/issues/320#issuecomment-248760222