Indexed DB wrapper with promises
Demo
Installation
npm install @n1md7/indexeddb-promise --save
# or
yarn add @n1md7/indexeddb-promise
or
<script src="https://bundle.run/@n1md7/indexeddb-promise@2.0.0"></script>
<script src="https://unpkg.com/@n1md7/indexeddb-promise@2.0.0/src/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@n1md7/indexeddb-promise@2.0.0/dist/indexed-db.min.js"></script>
Available methods
- select
- insert
- selectAll
- selectByPk
- updateByPk
- deleteByPk
.selectAll(): Promise
Gets all the data from db and returns promise with response data
.selectByPk(pKey: string): Promise
Has one parameter pkey
as primaryKey and returns promise with data
.select({...}): Promise
Has one parameter props
which can be
props = {
limit: 10,
where: (dataArray) => {
return dataArray;
},
orderByDESC: true,
sortBy: 'comments',
};
@where property can filter out data like
where: (data) => data.filter((item) => item.username === 'admin');
or it can be an object, which gets data with AND(&&) comparison
where: {
username: 'admin',
password:'admin123'
}
.updateByPk(pKey, {...}): Promise
Has two parameters pkey
and keyValue
pair of updated data
.updateByPk(123, { username: 'admin' })
.deleteByPk(pKey): Promise
Has one parameter pKey
which record to delete based on primary key
note primary key is type sensitive. If it is saved as integer then should pass as integer and vice versa
Usage example
<html>
<head>
<title>IndexedDB app</title>
<script src="./dist/indexed-db.min.js"></script>
</head>
<body>
<script>
</script>
</body>
</html>
Once you add indexed-db.min.js in your document then you will be able to access
IndexedDBModel
variable globally which contains Model
. They can be extracted as following
const { Model } = IndexedDBModel;
const Model = IndexedDBModel.Model;
Create example config
class DB extends IndexedDBModel.Model {
get config() {
return {
version: 1,
databaseName: 'myNewDatabase',
tableName: 'myNewTable',
primaryKey: {
name: 'id',
autoIncrement: false,
unique: true,
},
initData: [],
structure: {
username: { unique: false, autoIncrement: false },
password: { unique: false, autoIncrement: false },
createdAt: { unique: false, autoIncrement: false },
updatedAt: { unique: false, autoIncrement: false },
},
};
}
}
Create connector
const db = new DB();
Full example
<html>
<head>
<title>IndexedDB app</title>
<script src="./dist/indexed-db.min.js"></script>
</head>
<body>
<script>
class DB extends IndexedDBModel.Model {
get config() {
return {
version: 1,
databaseName: 'myNewDatabase',
tableName: 'myNewTable',
primaryKey: {
name: 'id',
autoIncrement: false,
unique: true,
},
initData: [],
structure: {
username: { unique: false, autoIncrement: false },
password: { unique: false, autoIncrement: false },
createdAt: { unique: false, autoIncrement: false },
updatedAt: { unique: false, autoIncrement: false },
},
};
}
}
const db = new DB();
db.insert({
id: Math.random() * 10,
username: 'admin',
password: 'nimda',
createdAt: new Date(),
updatedAt: new Date(),
})
.then(function () {
console.info('Yay, you have saved the data.');
})
.catch(function (error) {
console.error(error);
});
db.selectAll().then(function (results) {
console.log(...results);
});
</script>
</body>
</html>
const IndexedDBModel = require('@n1md7/indexeddb-promise');
const { Model } = IndexedDBModel;
import { Model } from '@n1md7/indexeddb-promise';