Skip to content

Instantly share code, notes, and snippets.

@sechel
Created February 20, 2018 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sechel/415c34b9bf80f3adeba52856d2377393 to your computer and use it in GitHub Desktop.
Save sechel/415c34b9bf80f3adeba52856d2377393 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.1/qunit.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/dexie/3.0.0-alpha.1/dexie.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.1/qunit.js"></script>
<title>IndexedDB, WebCrypto, and Primary Keys</title>
</head>
<body>
<div id="qunit"></div>
<script>
let dbName;
let key;
let store;
let tx;
const wcs = window.crypto.subtle || window.crypto.webkitSubtle;
QUnit.module('IndexedDB, CryptoKeys, and Primary Keys', {
beforeEach: async () => {
dbName = 'TestDB-' + Math.random();
key = await wcs.generateKey({name: 'AES-CBC', length: 128}, false, ['encrypt']);
}
});
QUnit.test('Dexie', async assert => {
db = new Dexie(dbName);
db.version(1).stores({test: 'id++'});
const testDataWithoutKey = {};
const testDataWithKey = { key };
await db.table('test').put(testDataWithoutKey);
try {
await db.table('test').put(testDataWithKey);
assert.notOk('webkit has fixed auto incrementing primary keys');
} catch (error) {
assert.equal(error.message, 'Unable to inject record key into record value');
}
await db.delete();
});
QUnit.test('Native', async assert => {
let req = indexedDB.open(dbName, 1);
req.onupgradeneeded = function() {
const db = this.result;
const testStore = db.createObjectStore('test', { autoIncrement: true, keyPath: 'id' });
};
await new Promise(r => req.onsuccess = r);
const db = req.result;
const testData = { key };
tx = db.transaction(['test'], 'readwrite');
store = tx.objectStore('test');
req = store.add(testData);
try {
await new Promise(
(r, f) => { tx.oncomplete = r; tx.onerror = f }
);
assert.notOk('webkit has fixed auto incrementing primary keys');
} catch (error) {
assert.equal(req.error.message, 'Unable to inject record key into record value');
}
indexedDB.deleteDatabase(dbName);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment