Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'dexie' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
test("Issue #30 - Problem with existing db", (assert) => {
let done = assert.async();
if (!supports("compound+multiEntry")) {
ok(true, "SKIPPED - COMPOUND + MULTIENTRY UNSUPPORTED");
return done();
}
///<var type="Dexie">
var db; // Will be used as a migrated version of the db.
// Start by deleting the db if it exists:
Dexie.delete("raw-db").then(function () {
// Create a bare-bone indexedDB database with custom indexes of various kinds.
return new Dexie.Promise(function (resolve, reject) {
var indexedDB = Dexie.dependencies.indexedDB;
var rawdb, req;
function error(e) {
if (rawdb) rawdb.close();
reject(e.target.error);
}
req = indexedDB.open("raw-db", 2);
req.onupgradeneeded = function (ev) {
try {
console.log("onupgradeneeded called");
rawdb = req.result;</var>
it('should encrypt whitelist', async done => {
const db = new Dexie('whitelist');
encrypt(
db,
keyPair.publicKey,
{
friends: {
type: encrypt.WHITELIST,
fields: ['picture'],
},
},
clearAllTables,
new Uint8Array(24)
);
// Declare tables, IDs and indexes
db.version(1).stores({
friends: '++id, name, age',
Dexie.delete("raw-db").then(function () {
// Create a bare-bone indexedDB database with custom indexes of various kinds.
return new Dexie.Promise(function (resolve, reject) {
var indexedDB = Dexie.dependencies.indexedDB;
var rawdb, req;
function error(e) {
if (rawdb) rawdb.close();
reject(e.target.error);
}
req = indexedDB.open("raw-db", 2);
req.onupgradeneeded = function (ev) {
try {
console.log("onupgradeneeded called");
rawdb = req.result;
// Stores
var people = rawdb.createObjectStore("people", {keyPath: "_id", autoIncrement: false});
var messages = rawdb.createObjectStore("messages", {autoIncrement: true});
asyncTest("open database without specifying version or schema", Dexie.Observable ? 1 : 10, function () {
if (Dexie.Observable) {
ok(true, "Dexie.Observable currently not compatible with this mode");
return start();
}
var db = new Dexie("TestDB");
var db2 = null;
db.open().then(function () {
ok(false, "Should not be able to open a non-existing database when not specifying any version schema");
}).catch(function (err) {
ok(true, "Got error when trying to open non-existing DB: " + err);
// Create a non-empty database that we later on will open in other instance (see next then()-clause)...
db = new Dexie("TestDB");
db.version(1).stores({ friends: "++id,name", pets: "++,name,kind" });
return db.open();
}).then(function () {
ok(true, "Could create TestDB with specified version schema.");
db2 = new Dexie("TestDB"); // Opening another instans without specifying schema
return db2.open().then(function () {
equal(db2.tables.length, 2, "We got two tables in database");
ok(db2.tables.every(function (table) { return table.name == "friends" || table.name == "pets" }), "db2 contains the tables friends and pets");
equal(db2.table("friends").schema.primKey.name, "id", "Primary key of friends is 'id'");
asyncTest("open, add and query data using transaction", function () {
var db = new Dexie("TestDB");
db.version(1).stores({ employees: "++id,first,last" });
db.open().catch(function () {
ok(false, "Could not open database");
start();
});
db.transaction("rw", db.employees, function () {
// Add employee
db.employees.add({ first: "David", last: "Fahlander" });
// Query employee
db.employees.where("first").equals("David").toArray(function (a) {
equal(a.length, 1, "Could retrieve employee based on where() clause");
var first = a[0].first;
var last = a[0].last;
Dexie.ignoreTransaction(()=>{
ok(Dexie.currentTransaction == null, "No Transaction in this zone");
function promiseFlow () {
return NativePromise.resolve().then(()=>{
if(Dexie.currentTransaction !== null) ok(false, "PSD zone leaked");
return new NativePromise(resolve => NativePromise.resolve().then(resolve));
});
};
otherZonePromise = promiseFlow();
for (let i=0;i<100;++i) {
otherZonePromise = otherZonePromise.then(promiseFlow);
}
});
// In parallell with the above 2*100 async tasks are being executed and verified,
db.transaction('rw', db.items, ()=>{
let trans = Dexie.currentTransaction;
ok(trans !== null, "Should have a current transaction");
let otherZonePromise;
Dexie.ignoreTransaction(()=>{
ok(Dexie.currentTransaction == null, "No Transaction in this zone");
function promiseFlow () {
return NativePromise.resolve().then(()=>{
if(Dexie.currentTransaction !== null) ok(false, "PSD zone leaked");
return new NativePromise(resolve => NativePromise.resolve().then(resolve));
});
};
otherZonePromise = promiseFlow();
for (let i=0;i<100;++i) {
otherZonePromise = otherZonePromise.then(promiseFlow);
}
});
// In parallell with the above 2*100 async tasks are being executed and verified,
// This special treatment in the unit tests may not need to be here if we can work around Dexie issue #1.
deepEqual(idbNames,
expected,
"IDB object stores must match expected.");
}
}
function checkTransactionObjectStores(t, expected) {
// Add baseTables.
expected = expected.concat(baseTables).sort();
deepEqual(t.storeNames.slice().sort(),
expected,
"Transaction stores must match expected.");
}
Dexie.delete(DBNAME).then(() => {
// --------------------------------------------------------------------
// Test: Empty schema
db = new Dexie(DBNAME);
db.version(1).stores({});
return db.open().then(() => {
ok(true, "Could create empty database without any schema");
// Set so add-on tables don't invalidate checks.
baseNumberOfTables = db.tables.length;
baseTables = db.tables.map(t => t.name);
});
}).then(() => {
db.close();
// --------------------------------------------------------------------
// Test: Adding version.
db = new Dexie(DBNAME);
db.version(1).stores({});
});
// Also if the rejection was caused by a throw...
new Dexie.Promise(function() {
throw "second error (throw)";
});
// But when catched it should not trigger the global event:
new Dexie.Promise(function(resolve, reject) {
reject("third error (catched)");
}).catch(function(e) {
ok(true, "Catched error explicitely: " + e);
});
// If catching an explicit error type that was not thrown, it should be triggered
new Dexie.Promise(function(resolve, reject) {
reject("Simple error 1");
}).catch(TypeError, function(e) {
ok(false, "Error should not have been TypeError");
});// Real error slip away... should be handled by global handler
new Dexie.Promise(function(resolve, reject) {
reject(new TypeError("Type Error 1"));
}).catch(TypeError, function(e) {
ok(true, "Catched the TypeError");
// Now we have handled it. Not bubble to global handler!
});
Dexie.Promise.resolve(Promise.reject(new Error("Converting a rejected standard promise to Dexie.Promise but don't catch it")))
.finally(()=>{
// With finally, it should yet trigger the global event:
return new Dexie.Promise(function(resolve, reject) {
return db.transaction('rw', db.users, function () {
return new Dexie.Promise(function (resolve, reject) {
// Notify log when transaction completes too early
Dexie.currentTransaction.complete(function () {
ok(true, "Transaction committing too early...");
// Resolve the promise after transaction commit.
// Flow will continue in the same Transaction scope but with an
// inactive transaction
resolve();
});
}).then(function () {
// Now when transaction has already committed, try to add a user with the current transaction:
return db.users.add({ username: "arne" });
}).then(function () {
ok(false, "Should not be able to get a here transaction has become inactive");
});