【问题标题】:CSV GTFS populate with dexie.js用 dexie.js 填充 CSV GTFS
【发布时间】:2017-01-29 21:09:03
【问题描述】:

我不确定 Dexie 的最佳实践是从 csv 文本文件创建数据库,我昨天才开始使用它。

我所做的是创建我的商店

  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 不是很熟悉,但到目前为止看起来还不错。

现在,因为我有一个包含所有 txt 文件的 gtfs 文件夹,所以我遍历并从 txt 文件中获取数据,然后转换为 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);
             }
          });
        });

所以现在它正在工作(实际上,它会遍历每个 txt 文件,但只解析最后一个文件),我不能像原始示例代码那样将所有内容都保留在“someTable”中。所以如上所示,我创建了我的新商店。不过,我遇到了这个障碍,我必须将我的表硬编码为一种方法。即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");
          });
    })

为了保持我的代码“DRY”,至少有一种方法可以在 db 中获取所有创建的表方法,我可以分别添加到这些方法中。如果是这样,那是否仍然让我处理我为每个表创建的关键路径?

【问题讨论】:

    标签: javascript csv indexeddb dexie


    【解决方案1】:

    在提出这个问题时,这是 dexie 版本: "dexie": "^1.4.2"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-29
      • 2015-01-08
      • 2013-03-03
      • 1970-01-01
      • 2019-07-23
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多