Skip to content

Commit da63d72

Browse files
committed
Extra test and bug fix
1 parent de74c00 commit da63d72

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22

3-
## v2.1.4
3+
## v2.1.5
44

55
- Fix bug when import JSON is missing keys that are present as object store names (#25).
66

7+
## v2.1.4
8+
9+
*Bad release - do not use.*
10+
711
## v2.1.3
812

913
- Fix bug when import JSON contains keys that are not present as object store names (#21).

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ function importFromJsonString(idbDatabase, jsonString, cb) {
8181
objectStoreNames.forEach((storeName) => {
8282
let count = 0;
8383

84-
// setting aux to an empty array if the key doesn't exists in the import object.
85-
const dataToImport = importObject[storeName] || [];
86-
const aux = Array.from(dataToImport);
84+
const aux = Array.from(importObject[storeName] || []);
8785

8886
if (importObject[storeName] && aux.length > 0) {
8987
aux.forEach((toAdd) => {
@@ -104,10 +102,12 @@ function importFromJsonString(idbDatabase, jsonString, cb) {
104102
};
105103
});
106104
} else {
107-
delete importObject[storeName];
108-
if (Object.keys(importObject).length === 0) {
109-
// added all object stores
110-
cb(null);
105+
if (importObject[storeName]) {
106+
delete importObject[storeName];
107+
if (Object.keys(importObject).length === 0) {
108+
// added all object stores
109+
cb(null);
110+
}
111111
}
112112
}
113113
});

test/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ describe('IDBExportImport', function() {
121121
assert.ifError(e);
122122
});
123123
});
124+
it('Should ignore stores that are not present when importing', function(done) {
125+
const db = new Dexie('MyDB', {indexedDB: fakeIndexedDB});
126+
db.version(1).stores({
127+
things: 'id++, thing_name, thing_description',
128+
});
129+
db.open().catch(function(e) {
130+
console.error('Could not connect. ' + e);
131+
});
132+
133+
const thingsToAdd = [{thing_name: 'First thing', thing_description: 'This is the first thing'},
134+
{thing_name: 'Second thing', thing_description: 'This is the second thing'}];
135+
db.things.bulkAdd(thingsToAdd).then(function() {
136+
const idbDB = db.backendDB(); // get native IDBDatabase object from Dexie wrapper
137+
IDBExportImport.importFromJsonString(idbDB, '{"other":[' +
138+
'{"thing_name":"First thing","thing_description":"This is the first thing","id":1},' +
139+
'{"thing_name":"Second thing","thing_description":"This is the second thing","id":2}]}', function(err) {
140+
assert.ifError(err);
141+
console.log('Imported data successfully');
142+
143+
IDBExportImport.clearDatabase(idbDB, () => {
144+
IDBExportImport.importFromJsonString(idbDB, '{"other": []}', function(err) {
145+
assert.ifError(err);
146+
console.log('Imported data successfully');
147+
done();
148+
});
149+
});
150+
});
151+
}).catch(Dexie.BulkError, function(e) {
152+
assert.ifError(e);
153+
});
154+
});
124155
it('Should import and export the database with empty keys', function(done) {
125156
const db = new Dexie('myDB', {indexedDB: fakeIndexedDB});
126157
db.version(1).stores({

0 commit comments

Comments
 (0)