Advertisement
yahyaaa

db

Aug 1st, 2020
1,751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const dbPromised = idb.open("db-footbal", 1, function(upgradeDb) {
  2.     if(!upgradeDb.objectStoreNames.contains('competitions')){
  3.         upgradeDb.createObjectStore("competitions",{keyPath : "id"});
  4.     }
  5. });
  6.  
  7.  
  8.  
  9. const dbGetAllCompetitions = () =>{
  10.     return new Promise((resolve, reject)=>{
  11.         dbPromised.then(db =>{
  12.             const transaction = db.transaction("competitions", `readonly`);
  13.             return transaction.objectStore("competitions").getAll();
  14.         }).then(data =>{
  15.             if(data!==undefined){
  16.                 resolve(data)
  17.             }else{
  18.                 reject(new Error("Favorite Not Found"))
  19.             }
  20.         })
  21.     })
  22. }
  23.  
  24. const checkFavoriteCompetitions = id =>{
  25.     return new Promise((resolve, reject)=>{
  26.         dbPromised.then(db =>{
  27.             const transaction = db.transaction("competitions", `readonly`);
  28.             return transaction.objectStore("competitions").get(id);
  29.         }).then(data =>{
  30.             if(data!==undefined){
  31.                 resolve(true)
  32.             }else{
  33.                 resolve(false)
  34.             }
  35.         })
  36.     })
  37. }
  38.  
  39. const dbInsertCompetitions = competition =>{
  40.     return new Promise((resolve, reject)=>{
  41.         dbPromised.then(db =>{
  42.             const transaction = db.transaction("competitions", `readwrite`);
  43.             transaction.objectStore("competitions")
  44.             .add(competition);
  45.             return transaction;
  46.         }).then(transaction =>{
  47.             if(transaction.complete){
  48.                 resolve(true)
  49.             }else{
  50.                 reject(new Error(transaction.onerror))
  51.             }
  52.         })
  53.     })
  54. }
  55. const dbDeleteCompetitions = id =>{
  56.     return new Promise((resolve, reject)=>{
  57.         dbPromised.then(db =>{
  58.             const transaction = db.transaction("competitions", `readwrite`);
  59.             transaction.objectStore("competitions").delete(id);
  60.             return transaction;
  61.         }).then(transaction =>{
  62.             if(transaction.complete){
  63.                 resolve(true)
  64.             }else{
  65.                 reject(new Error(transaction.onerror))
  66.             }
  67.         })
  68.     })
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement