Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

db64

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

db64 - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.eslintrc.json

223

db64.js

@@ -1,2 +0,1 @@

let dbName = 'default'

@@ -6,146 +5,132 @@ const { isArray } = Array

const openDBErrMessage = err => console.error(`Error creating database: ${err}`)
const setDataError = e => console.error(`Error setting data`, e)
const getDataError = e => console.error(`Error retrieving data`, e)
const setDataError = e => console.error('Error setting data', e)
const getDataError = e => console.error('Error retrieving data', e)
const openDB = (storeName) => new Promise((resolve, reject) => {
let db
try {
db = window.indexedDB.open(dbName, 1)
} catch (e) {
reject(e)
}
let db
try {
db = window.indexedDB.open(dbName, 1)
} catch (e) {
reject(openDBErrMessage)
}
db.onupgradeneeded = e => (e.target.result).createObjectStore(storeName)
db.onsuccess = e => resolve(e.target.result)
db.onerror = e => reject(e.target.result)
db.onupgradeneeded = e => (e.target.result).createObjectStore(storeName)
db.onsuccess = e => resolve(e.target.result)
db.onerror = e => reject(e.target.result)
return db
return db
})
const setData = async (db, storeName, key, dataValue, entries) => {
return new Promise(async (resolve, reject) => {
try {
const transaction = db.transaction([storeName], "readwrite")
const obStore = transaction.objectStore(storeName)
try {
const transaction = db.transaction([storeName], 'readwrite')
const obStore = transaction.objectStore(storeName)
if (entries) {
const dataEntries = isArray(dataValue)
? () => dataValue.map((fruitName, index) => obStore.put(fruitName, index))
: () => Object.entries(dataValue).map(([key, value]) => obStore.put(value, key))
await Promise.all(dataEntries())
} else {
await obStore.put(dataValue, key)
}
resolve()
} catch (e) {
setDataError(e)
reject(e)
}
})
if (entries) {
const dataEntries = isArray(dataValue)
? () => dataValue.map((fruitName, index) => obStore.put(fruitName, index))
: () => Object.entries(dataValue).map(([key, value]) => obStore.put(value, key))
await Promise.all(dataEntries())
} else {
await obStore.put(dataValue, key)
}
} catch (e) {
setDataError(e)
}
return await db64
}
const getData = async (db, storeName, key, entries) => {
return new Promise(async (resolve, reject) => {
try {
const transaction = db.transaction([storeName])
const objectStore = transaction.objectStore(storeName)
let dataRequest
if (entries) {
const results = {}
const cursorRequest = objectStore.openCursor()
cursorRequest.onsuccess = e => {
const cursor = e.target.result
return new Promise((resolve) => {
const transaction = db.transaction([storeName])
const objectStore = transaction.objectStore(storeName)
let dataRequest
if (entries) {
const results = {}
const cursorRequest = objectStore.openCursor()
cursorRequest.onsuccess = e => {
const cursor = e.target.result
if (cursor) {
if (key.includes(cursor.key)) {
const value = cursor.value
results[cursor.key] = value
}
cursor.continue()
} else {
resolve(results)
}
}
} else {
dataRequest = objectStore.get(key)
dataRequest.onsuccess = () => resolve(dataRequest.result)
}
} catch (e) {
getDataError(e)
reject(e)
if (cursor) {
if (key.includes(cursor.key)) {
const value = cursor.value
results[cursor.key] = value
}
cursor.continue()
} else {
resolve(results)
}
})
}
} else {
dataRequest = objectStore.get(key)
dataRequest.onsuccess = () => resolve(dataRequest.result)
}
})
}
const deleteData = async (db, storeName, key) => {
const keyArr = isArray(key) ? key : [key]
return new Promise(async (resolve, reject) => {
try {
const transaction = db.transaction([storeName], 'readwrite')
const objectStore = transaction.objectStore(storeName)
const cursorRequest = objectStore.openCursor()
const keyArr = isArray(key) ? key : [key]
try {
const transaction = db.transaction([storeName], 'readwrite')
const objectStore = transaction.objectStore(storeName)
const cursorRequest = objectStore.openCursor()
cursorRequest.onsuccess = e => {
const cursor = e.target.result
cursorRequest.onsuccess = e => {
const cursor = e.target.result
if (cursor) {
if (keyArr.includes(cursor.key)) cursor.delete()
cursor.continue()
}
resolve()
}
} catch (e) {
reject(e)
}
})
if (cursor) {
if (keyArr.includes(cursor.key)) cursor.delete()
cursor.continue()
}
}
} catch (e) {
console.error(e)
}
return await db64
}
const clearStore = (db, storeName) => {
return new Promise(async (resolve, reject) => {
const transaction = db.transaction([storeName], 'readwrite')
const objectStore = transaction.objectStore(storeName)
const objectStoreRequest = objectStore.clear()
return new Promise((resolve, reject) => {
const transaction = db.transaction([storeName], 'readwrite')
const objectStore = transaction.objectStore(storeName)
const objectStoreRequest = objectStore.clear()
objectStoreRequest.onsuccess = resolve
objectStoreRequest.onerror = e => reject(e.target.error)
})
objectStoreRequest.onsuccess = resolve
objectStoreRequest.onerror = e => reject(e.target.error)
})
}
const db64 = {
from: storeName => {
return {
set: async (key, value) => {
return openDB(storeName).then(db => setData(db, storeName, key, value))
.catch(openDBErrMessage)
},
setEntries: async (value) => {
return openDB(storeName).then(db => setData(db, storeName, null, value, 'entries'))
.catch(openDBErrMessage)
},
get: async key => {
return openDB(storeName).then(db => getData(db, storeName, key))
.catch(openDBErrMessage)
},
getEntries: async (keys) => {
return openDB(storeName).then(db => getData(db, storeName, keys, 'entries'))
.catch(openDBErrMessage)
},
delete: async (keys) => {
return openDB(storeName).then(db => deleteData(db, storeName, keys))
.catch(openDBErrMessage)
},
}
},
clear: async storeName => {
return openDB(storeName).then(db => clearStore(db, storeName))
.catch(openDBErrMessage)
},
use: name => (dbName = name, db64)
from: storeName => {
return {
set: async (key, value) => {
return openDB(storeName).then(db => setData(db, storeName, key, value))
.catch(setDataError)
},
setEntries: async (value) => {
return openDB(storeName).then(db => setData(db, storeName, null, value, 'entries'))
.catch(setDataError)
},
get: async key => {
return openDB(storeName).then(db => getData(db, storeName, key))
.catch(getDataError)
},
getEntries: async (keys) => {
return openDB(storeName).then(db => getData(db, storeName, keys, 'entries'))
.catch(getDataError)
},
delete: async (keys) => {
return openDB(storeName).then(db => deleteData(db, storeName, keys))
.catch(console.error)
}
}
},
clear: async storeName => {
return openDB(storeName).then(db => clearStore(db, storeName))
.catch(console.error)
},
use: name => (dbName = name, db64) // eslint-disable-line
}
export default db64
export default db64
{
"name": "db64",
"version": "0.1.0",
"version": "0.2.0",
"description": "A Practical IndexedDB API",

@@ -8,3 +8,5 @@ "main": "db64.js",

"scripts": {
"test": "./node_modules/.bin/http-server -a 127.0.0.1 -p 2222 -o"
"test": "./node_modules/.bin/http-server -a 127.0.0.1 -p 2222 -o",
"lint": "./node_modules/.bin/eslint db64.js",
"lint:fix": "./node_modules/.bin/eslint --fix db64.js"
},

@@ -27,4 +29,9 @@ "repository": {

"devDependencies": {
"eslint": "^8.56.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.5.0",
"eslint-plugin-promise": "^6.1.1",
"http-server": "^14.1.1"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc