Need code that works? Grab a snippet. Made something cool? Share it back. Simple as that.
function addToIndexedDB(databaseName, storeName, item) { return new Promise((resolve, reject) => { const request = indexedDB.open(databaseName); request.onupgradeneeded = (event) => { const db = event.target.result; if (!db.objectStoreNames.contains(storeName)) { db.createObjectStore(storeName, { keyPath: "id" }); } }; request.onsuccess = (event) => { const db = event.target.result; const transaction = db.transaction(storeName, "readwrite"); const store = transaction.objectStore(storeName); const addRequest = store.add(item); addRequest.onsuccess = () => resolve(item); // return item object on success addRequest.onerror = () => reject(addRequest.error); }; request.onerror = () => reject(request.error); }); } addItemToIndexedDB("myDatabase", "users", { id: 1, name: "John", age: 25 }) .then(console.log) // { id: 1, name: "John", age: 25 } .catch(console.error);
Explore the powerful features that make SnipNest an awesome platform.
Easily fork, edit, and submit code snippets. Share your work with the community in just a few clicks.
function chunkArray(array, size) { return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size) ); } const numbers = [1, 2, 3, 4, 5, 6, 7]; chunkArray(numbers, 3); // [[1, 2, 3], [4, 5, 6], [7]]
Quickly filter through a vast collection of code snippets. Get the perfect code for your project every time.
Access and insert your saved snippets directly within your code editor. Boost your productivity without leaving your workspace.
function chunkArray(array, size) { return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size) ); } const numbers = [1, 2, 3, 4, 5, 6, 7]; chunkArray(numbers, 3); // [[1, 2, 3], [4, 5, 6], [7]]
Help improve the platform by contributing snippets and collaborating with other developers.