Skip to content

Commit

Permalink
Removed db64 object returns for set and setEntries methods
Browse files Browse the repository at this point in the history
Refactored
Added missing onerror handlers
Updated _readme and create_distribution to include brotli size
Bumped verison
  • Loading branch information
julienetie committed Jan 16, 2024
1 parent fbffd00 commit 8cf0dea
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A more practical alternative to [localStorage](https://developer.mozilla.org/en-
- Set and get single or multiple entries
- Delete single, multiple or all entries
- No versioning
- 2.38KB minified
- 2.38KB minified | 783 bytes _(brotli)_

E.g.
```javascript
Expand Down
7 changes: 6 additions & 1 deletion create-distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs/promises'
import path, { dirname } from 'path'
import { minify } from 'terser'
import { execSync } from 'child_process'
import brotliSize from 'brotli-size'

const currentFilePath = (new URL(import.meta.url)).pathname
const __dirname = dirname(currentFilePath)
Expand Down Expand Up @@ -83,8 +84,12 @@ const createCJS = async () => {
const stats = await fs.stat(paths.esMinDist)

// Readme
const brotliSizeBytes = brotliSize.sync(minifiedESData.code)
const fileSizeInKB = stats.size / 1024
const readmeWithSize = readmeSrc.replace(/{{ size }}/g, `${fileSizeInKB.toFixed(2)}KB`)

const readmeWithSize = readmeSrc
.replace(/{{ size }}/g, `${fileSizeInKB.toFixed(2)}KB`)
.replace(/{{ brotliSize }}/g, `${brotliSizeBytes} bytes`)
await fs.writeFile(paths.readmeRoot, readmeWithSize, 'utf8')
console.info(`Created ${paths.readmeRoot}`)
} catch (err) {
Expand Down
19 changes: 5 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ <h1>DB64</h1>
await db64.create('Games', ['Super Nintendo', 'Gameboy'])
const createReturn = await db64.create('Sports', ['Football', 'Tennis', 'Basketball']) // An example.

console.log('createReturn returns db64:', shallowEqual(createReturn, db64))



// Assign a variable with the selected database and store to modify
Expand All @@ -44,22 +42,16 @@ <h1>DB64</h1>
// .set key 5
const setReturn = await snes.set(5, 'Donkey Kong Country')

console.info('.set returns db64:', shallowEqual(setReturn, db64))



// .setEntries via Array for keys 0,1,2,3,4
const setEntriesArrayReturn = await snes.setEntries(['Mario Wrold', 'Zelda', 'Super Metroid', 'F-Zero', 'Street Fighter II'])

console.info('.setEntriesArrayReturn returns db64:', shallowEqual(setEntriesArrayReturn, db64))



// .setEntries via Object for keys racing, rpg, fighting, adventure
const setEntriesObjectReturn = await snes.setEntries({ adventure: 'Mario Wrold', rpg: 'Zelda', racing: 'F-Zero', fighting: 'Street Fighter II' })

console.info('.setEntriesObjectReturn returns db64:', shallowEqual(setEntriesObjectReturn, db64))



// .get key 4
Expand Down Expand Up @@ -100,9 +92,7 @@ <h1>DB64</h1>
// Create variable to modify Gameboy store
const gb = db64.use('Games', 'Gameboy')

console.log('USE GAMEBOY')




// .clear all entries for 'Gameboy'
await gb.setEntries({ strategy: 'Tetris', rpg: 'Zelda: Link\'s Awakening', adventure: 'Wario Land II' })
Expand All @@ -114,15 +104,16 @@ <h1>DB64</h1>

// Delete db
const db64Obj = await db64.delete('Games')
console.log('db64Obj', db64Obj)

console.info('db64.delete returns db64:', shallowEqual(db64Obj, db64))
} catch (e) {
console.log('TRY', e.name)
console.log('Error:', e)
switch (e.name) {
// Manage missing store
case 'NotFoundError':
// The operation failed because the requested database object could not be found.
case 'Db64MissingStore':
console.log('remake')
console.info('NotFoundError/ Db64MissingStore')
// Delete the store's database
await db64.delete('Games')
// Re-create the databse with all it's stores
Expand Down
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "db64",
"version": "0.8.1",
"version": "0.8.2",
"description": "A Practical IndexedDB API",
"main": "db64.js",
"type": "module",
Expand Down Expand Up @@ -29,6 +29,7 @@
},
"homepage": "https://github.com/julienetie/db64#readme",
"devDependencies": {
"brotli-size": "^4.0.0",
"eslint": "^8.56.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
Expand Down
2 changes: 1 addition & 1 deletion src/_readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A more practical alternative to [localStorage](https://developer.mozilla.org/en-
- Set and get single or multiple entries
- Delete single, multiple or all entries
- No versioning
- {{ size }} minified
- {{ size }} minified | {{ brotliSize }} _(brotli)_

E.g.
```javascript
Expand Down
16 changes: 9 additions & 7 deletions src/db64.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ const openDatabase = (name = 'default', storeNames) => new Promise((resolve, rej
resolve(target.result)
}

DBOpenRequest.onerror = ({ target }) => {
reject(target.result)
}
DBOpenRequest.onerror = ({ target }) => reject(target.result)
})


Expand All @@ -61,7 +59,6 @@ const setData = async (database, storeName, key, dataValue, entries) => new Prom
} else {
resolve(obStore.put(dataValue, key))
}
resolve(db64)
} catch (e) {
reject(e)
}
Expand All @@ -76,7 +73,7 @@ Gets an entry by a given key/value pair or a dataset of entries.
- entries array | object Entries to get
- Return object A promise fulfilled with the queried data
*/
const getData = async (database, storeName, key, entries) => new Promise((resolve) => {
const getData = async (database, storeName, key, entries) => new Promise((resolve, reject) => {
const objectStore = (database.transaction([storeName])).objectStore(storeName)

if (entries) {
Expand All @@ -93,9 +90,12 @@ const getData = async (database, storeName, key, entries) => new Promise((resolv
resolve(results)
}
}

cursorRequest.onerror = e => reject(e)
} else {
const dataRequest = objectStore.get(key)
dataRequest.onsuccess = () => resolve(dataRequest.result)
dataRequest.onerror = e => reject(e)
}
})

Expand All @@ -121,6 +121,8 @@ const deleteData = async (database, storeName, key) => new Promise((resolve, rej
cursor.continue()
}
}
cursorRequest.onerror = e => reject(e)

resolve(db64)
} catch (e) {
reject(e)
Expand Down Expand Up @@ -155,7 +157,7 @@ const deleteDB = name => {

DBDeleteRequest.onsuccess = () => resolve(db64)

DBDeleteRequest.onerror = ({ target }) => reject(new Error(`Error deleting database: ${target.error}`))
DBDeleteRequest.onerror = e => reject(e)

DBDeleteRequest.onblocked = () => {
for (const database of connections) {
Expand Down Expand Up @@ -198,7 +200,7 @@ const db64 = {
getEntries: async (keys) => openDatabase(name, storeName)
.then(database => getData(database, storeName, keys, 'entries')),
delete: async (keys) => openDatabase(name, storeName)
.then(database => deleteData(database, storeName, keys)),
.then(database => deleteData(database, storeName, keys))
}
},
clear: async (name, storeName) => openDatabase(name, storeName)
Expand Down

0 comments on commit 8cf0dea

Please sign in to comment.