indexDB
Lightweight, extensible, zero dependencies JavaScript indexDB.
API
支持增删改查、条数。
getCount
获取总条数
db.getCount().then(res => {console.log(res)})
remove
删除条数
const count = 10; // 删除最早的10条
db.remove(count).then(res => {console.log(res)})
getRangeData
const lower = 1694885864655;
const upper = new Date().getTime();
const where = {
level: "INFO"
}
// 查找时间段内 level 为 INFO 的数据。
db.getRangeData("time", lower, upper, where).then(res => {console.log(res)})
addList
// 新增数据(见demo)
db.addList([]).then(res => {console.log(res)})
update
// 更新某条数据
db.update({ id: 1, level: "ERROR", info: "1111", time: 1111 }).then(res => {console.log(res)})
test
// 测试 API
db.test().then(res => {console.log(res)})
esm 使用方式
<script type="module">
import DB from "./dist/index.esm.js"
const db = new DB({
dbname: "LOG_DB",
tablename: "log",
dbversion: 1,
storeParams: {
objectStoreParams: {
autoIncrement: true,
keyPath: "id"
},
indexs: [
{
indexName: "id",
key: "id",
unique: true
},
{
indexName: "level",
key: "level",
unique: false
},
{
indexName: "msg",
key: "msg",
unique: false
},
{
indexName: "time",
key: "time",
unique: false
},
]
}
});
const lower = 1694885864655;
const upper = new Date().getTime();
setInterval(() => {
const levelList = ["DEBUG", "INFO", "WARING", "ERROR", "FATAL"];
const getIndex = () => {
return Math.floor(Math.random() * (levelList.length))
}
const textList = [
`The twinkling stars scatter
In the fresh grass and became
Countless bright fireflies`,
`The vast sea asks the
Yellow bleak desert whether it is
Thirsty all year round.`,
`The west envies the
East ‘cause the red sun rises there
Without exception.`,
`Clock jumped down from the
Wall he said he was too tired
To serve me again.`,
`A group of words are
Weeping ‘cause they lost the way
To enter my poems.`,
]
const index = Math.floor(Math.random() * (levelList.length));
const _info = levelList[getIndex()];
db.getCount().then((count) => {
console.log("all count", count)
if (count > 800000) {
db.remove(10000).then((res) => {
console.log("remove", res)
})
}
});
// db.getRangeData("time", lower, upper,).then(res => {
// console.log(res.length)
// // const str = JSON.stringify(res)
// // const encoder = new TextEncoder();
// // const encodedData = encoder.encode(str);
// // const byteSize = encodedData.length;
// // console.log("Byte Size:", byteSize / 1024 / 1024);
// // downloadTxtFile(str);
// });
const list = new Array(1000).fill({
level: _info,
msg: _info + "->" + new Date().toLocaleTimeString() + textList[getIndex()] + "\/n" + textList[getIndex()],
time: new Date().getTime(),
extra: null
});
db.addList(list).then(res => {
console.log("addList", res);
});
// db.update({ id: 1, level: "ERROR", info: "1111", time: 1111 }).then(res => {
// console.log("update", res);
// })
// db.test("getAllKeys").then(res => {
// // console.log("test", res);
// })
}, 1000);
function downloadTxtFile(text) {
const element = document.createElement("a");
const file = new Blob([text], { type: "text/plain" });
element.href = URL.createObjectURL(file);
element.download = "file.txt";
document.body.appendChild(element); // 为了兼容 Firefox
element.click();
document.body.removeChild(element); // 为了兼容 Firefox
}
</script>
umd 使用方式
<script src="./dist/index.umd.js">
</script>
<script>
const db = new DB({
dbname: "LOG_DB",
tablename: "log",
dbversion: 1,
storeParams: {
objectStoreParams: {
autoIncrement: true,
keyPath: "id"
},
indexs: [
{
indexName: "id",
key: "id",
unique: true
},
{
indexName: "level",
key: "level",
unique: false
},
{
indexName: "msg",
key: "msg",
unique: false
},
{
indexName: "time",
key: "time",
unique: false
},
]
}
});
const lower = 1694885864655;
const upper = new Date().getTime();
setInterval(() => {
const levelList = ["DEBUG", "INFO", "WARING", "ERROR", "FATAL"];
const getIndex = () => {
return Math.floor(Math.random() * (levelList.length))
}
const textList = [
`The twinkling stars scatter
In the fresh grass and became
Countless bright fireflies`,
`The vast sea asks the
Yellow bleak desert whether it is
Thirsty all year round.`,
`The west envies the
East ‘cause the red sun rises there
Without exception.`,
`Clock jumped down from the
Wall he said he was too tired
To serve me again.`,
`A group of words are
Weeping ‘cause they lost the way
To enter my poems.`,
]
const index = Math.floor(Math.random() * (levelList.length));
const _info = levelList[getIndex()];
db.getCount().then((count) => {
console.log("all count", count)
if (count > 800000) {
db.remove(10000).then((res) => {
console.log("remove", res)
})
}
});
// db.getRangeData("time", lower, upper,).then(res => {
// console.log(res.length)
// // const str = JSON.stringify(res)
// // const encoder = new TextEncoder();
// // const encodedData = encoder.encode(str);
// // const byteSize = encodedData.length;
// // console.log("Byte Size:", byteSize / 1024 / 1024);
// // downloadTxtFile(str);
// });
const list = new Array(1000).fill({
level: _info,
msg: _info + "->" + new Date().toLocaleTimeString() + textList[getIndex()] + "\/n" + textList[getIndex()],
time: new Date().getTime(),
extra: null
});
db.addList(list).then(res => {
console.log("addList", res);
});
// db.update({ id: 1, level: "ERROR", info: "1111", time: 1111 }).then(res => {
// console.log("update", res);
// })
// db.test("getAllKeys").then(res => {
// // console.log("test", res);
// })
}, 1000);
function downloadTxtFile(text) {
const element = document.createElement("a");
const file = new Blob([text], { type: "text/plain" });
element.href = URL.createObjectURL(file);
element.download = "file.txt";
document.body.appendChild(element); // 为了兼容 Firefox
element.click();
document.body.removeChild(element); // 为了兼容 Firefox
}
</script>