Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function doTheTests() {
await db.users.add({ username: "monkey1" });
await db.users.add({ username: "monkey1" }).catch(function(ex) {
ok(true, "Should fail adding a second monkey1");
}); // Trigger creating.onerror
// Test bulkAdd as well:
ok(true, "Testing bulkAdd");
await db.users.bulkAdd([{ username: "monkey1" }, { username: "monkey2" }])
.then(()=>ok(false, "Should get error on one of the adds"))
.catch(Dexie.BulkError, e=>{
ok(true, "Got BulkError");
ok(e.failures.length === 1, "One error out of two: " + e);
});
await db.users.where("username").equals("monkey1").modify({
name: "Monkey 1"
});
await db.users.where("username").equals("monkey1").modify(user => {
user.username = "monkey2";// trigger updating.onerror
}).catch(function(ex) {
ok(true, "Should fail modifying primary key to an already existing primary key");
});
ok(true, "Will now be modifying primary key of monkey1...");
await db.users.where("username").equals("monkey1").modify(user => {
user.username = "monkey88";// trigger updating.onerror
}).then(res => {
ok(true, "Should succeed modifying primary key to non-existing primary key, resulting in deletion and creation: " + res);
yield db.transaction("rw", db.users, function() {
var newUsers = [
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["aper1@persbrant.net"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["aper2@persbrant.net"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["aper2@persbrant.net"] }, // Should fail
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["aper3@persbrant.net"] }
];
db.users.bulkAdd(newUsers).then(()=> {
ok(false, "Should not resolve when one operation failed");
}).catch(Dexie.BulkError, e=>{
ok(true, "Got BulkError: " + e.message);
equal(e.failures.length, 1, "One error due to a duplicate username: " + e.failures[0]);
});
// Now, since we catched the error, the transaction should continue living.
db.users.where("username").startsWith("aper").count(function(count) {
equal(count, 3, "Got three matches now when users are bulk-added");
});
});
// Now check that catching and rethrowing should indeed make it fail
var newUsersZ = [
{first: "Zke1", last: "Persbrant1", username: "zper1", email: ["zper1@persbrant.net"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["zper2@persbrant.net"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["zper2@persbrant.net"]}, // Should fail
{id: idOfLastUser, first: "Zke2", last: "Persbrant2", username: "zper2", email: ["zper2@persbrant.net"]}, // Should fail
{first: "Zke3", last: "Persbrant3", username: "zper3", email: ["zper3@persbrant.net"]}
];
yield db.transaction('rw', db.users, function*() {
try {
yield db.users.bulkPut(newUsersZ);
} catch (e) {
throw e;
}
}).catch(Dexie.BulkError, e => {
ok(true, "Got rethrown BulkError: " + e.stack);
});
equal(yield db.users.where('username').startsWith('zper').count(), 0, "0 users! Good - means that previous operation rethrown (via try..yield..catch--throw this time, and therefore not committed");
});
//if we have a value in this foreignKey:
if (jForeignKeyValue) {
if (updatedRecord = self.addPrimaryKeyDependency(jForeignKeyValue, tableName,iPrimaryKey,jForeignKey, service.name)) {
updatedRecords = updatedRecords || [];
updatedRecords.push(updatedRecord);
}
}
}
}
if(updatedRecords && updatedRecords.length) {
//We need to save:
self.primaryKeysTable.bulkPut(updatedRecords)
.then(function(lastKey) {
console.log("Updated offline primaryKeys dependencies" + lastKey); // Will be 100000.
}).catch(Dexie.BulkError, function (e) {
console.error (e);
});
}
});
}
export const saveOrUpdateAlbums = (dbApis, albums) => dbApis.transaction('rw', dbApis.albumsCollection, async () => {
try {
await dbApis.albumsCollection.bulkPut(albums);
console.log(`Done putting ${albums.length} albums to collection.`);
} catch (e) {
if (e instanceof Dexie.BulkError) {
console.error(`Some albums did not succeed. However, ${
albums.length - e.failures.length
} albums was added successfully.`);
} else {
throw e;
}
}
const [albumsCount, latestAlbum] = await Dexie.Promise.all(
[
dbApis.albumsCollection.count(),
dbApis.albumsCollection.orderBy('createdAt').reverse().first(),
],
);
return { albumsCount, latestCid: latestAlbum.cid };
});
onmessage = (message) => {
console.log(message);
const action = message.data[1];
if (action.type === 'bulkFiles') {
db.files.bulkAdd(action.data).then(lastkey => {
postMessage({ success: true, lastkey });
}).catch(reason => {
postMessage({ success: false, reason: reason.toString() });
console.log(reason);
}).catch(Dexie.BulkError, (e) => {
postMessage({ success: false, reason: e.toString() });
console.error(`${`Some raindrops did not succeed. However, ${
100000}` - e.failures.length} raindrops was added successfully`);
});
}
};
.bulkPut(imageDataIndexedDB)
.then(function() {
if (imageDataReduxStore != null) {
try {
store.dispatch(addImagesAction(imageDataReduxStore));
store.dispatch(toggleNewImagesEventAction());
} catch (e) {
console.error(
e,
'Could not save images to redux store, image data in indexeddb will be deleted'
);
indexeddb.delete();
}
}
})
.catch(Dexie.BulkError, function(e) {
console.error(
e +
'Could not save images to indexeddb, due to that image data will also not be saved to redux store'
);
});
}
return new Promise((resolve, reject) => {
store.bulkPut(slice).then(lastkey => {
this.progress(Math.min(data.length, chunkNr * chunkSize), data.length, `Saving ${data.length} '${store.name}' items to DB...`);
if (chunkNr * chunkSize >= data.length) {
return resolve(lastkey);
}
return task.saveChunked(data, store, chunkNr + 1, chunkSize).then(result => resolve(result)).catch(reason => reject(reason));
}).catch(reason => {
reject(reason);
}).catch(Dexie.BulkError, (e) => {
reject(e);
});
});
}