Javascript CSV GTF使用dexie.js填充
我不确定Dexie的最佳实践是什么,从csv文本文件创建db,我昨天才开始使用它 我所做的就是创建我的商店Javascript CSV GTF使用dexie.js填充,javascript,csv,indexeddb,dexie,Javascript,Csv,Indexeddb,Dexie,我不确定Dexie的最佳实践是什么,从csv文本文件创建db,我昨天才开始使用它 我所做的就是创建我的商店 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': "++
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”中。如上所述,我创建了我的新店。不过,我遇到了这个障碍,我必须将我的表硬编码为一种方法。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");
});
})
为了使我的代码保持“干爽”,至少有一种方法可以在db中获取所有创建的表方法,我可以分别添加这些方法。如果是这样的话,这是否仍然允许我处理为每个表创建的关键路径?在提出此问题时,这是dexie版本: “德西”:“^1.4.2”