indexedDb数据库基本操作
随便写写
把回调函数改写为Promise
异步操作,新增加了分页功能。
import { indexedDbConfig as config } from "../config";
export class IndexedDb {
databasename: string;
v: number;
private _openDBRequest!: IDBOpenDBRequest;
private _instance!: IDBDatabase;
private _upgradeneeded?: (e: Event) => void;
constructor(databasename, v = 1, upgradeneeded?: (e: Event) => void) {
this.databasename = databasename;
this.v = v;
this._upgradeneeded = upgradeneeded;
}
async instance(): Promise<IDBDatabase> {
if (!this._instance) {
this._openDBRequest = window.indexedDB.open(this.databasename, this.v);
return new Promise((resolve, reject) => {
this._openDBRequest.onsuccess = (e: any) => {
resolve(e.target.result);
};
this._openDBRequest.onerror = (e: any) => {
reject(e.target.error);
};
this._openDBRequest.onupgradeneeded = (e) => {
this._upgradeneeded && this._upgradeneeded(e);
};
});
}
return Promise.resolve(this._instance);
}
}
export class IndexedDbObjectStore {
private _indexedDb: IndexedDb = indexdb;
private _name: string;
private _instance!: IDBObjectStore;
private _mode: "readonly" | "readwrite" | "versionchange";
constructor(storename: string, mode: "readonly" | "readwrite" | "versionchange" = "readonly") {
this._name = storename;
this._mode = mode;
}
async instance() {
if (!this._instance) {
const indexdb = await this._indexedDb.instance();
this._instance = indexdb.transaction(this._name, this._mode).objectStore(this._name);
}
return Promise.resolve(this._instance);
}
async add(value: any, key?: string | number | Date): Promise<IDBValidKey> {
return this._callback((await this.instance()).add(value, key));
}
async put(value: any, key?: string | number | Date): Promise<IDBValidKey> {
return this._callback((await this.instance()).put(value, key));
}
async delete(key: string | number | Date) {
return this._callback((await this.instance()).delete(key));
}
async deleteIndex(name: string) {
(await this.instance()).deleteIndex(name);
}
async clear() {
return this._callback((await this.instance()).clear());
}
async get(query: string | number | Date) {
return this._callback((await this.instance()).get(query));
}
async getAll(query?: string | number | Date) {
return this._callback((await this.instance()).getAll(query));
}
async getAllKeys(query?: string | number | Date) {
return this._callback((await this.instance()).getAllKeys(query));
}
async getKey(query: string | number | Date) {
return this._callback((await this.instance()).getKey(query));
}
async index(name: string) {
return (await this.instance()).index(name);
}
async count(key?: string | number | Date) {
(await this.instance()).openCursor;
return this._callback((await this.instance()).count(key));
}
/**
* 分页
*/
pagination(page = 1, rows = 20) {
return async (data: Array<any>, range?: string | number | Date) => {
let [state, count] = ["start", 0, new Array(), ""];
const result = (await this.instance()).openCursor(range);
const start = () => {
if (result.result) {
const count = (page - 1) * rows;
count ? result.result.advance(count) : next();
}
state = "next";
};
const next = () => {
if (result.result) {
count < rows ? data.push(result.result?.value) : (state = "done");
count < rows && result.result.continue();
}
count++;
};
const done = () => page++;
const obj = Object.assign({ start, next, done }, {});
result.onsuccess = (e) => obj[state]();
};
}
private _callback<T>(result: IDBRequest<T>): Promise<T> {
return new Promise((resolve, reject) => {
result.onsuccess = function() {
resolve(this.result);
};
result.onerror = function() {
reject(this.error);
};
});
}
}
export const indexdb = new IndexedDb(config.name, config.v, (e: any) => {
const result = <IDBDatabase>e.target.result;
result.createObjectStore("sitemaps", { autoIncrement: true, keyPath: "id" });
});
// const store = new IndexedDbObjectStore("sitemaps", "readwrite");
// store.add({ name: 1, sex: "男" });
// store.add({ name: 2, sex: "男" });
// store.add({ name: 3, sex: "妇" });
// store.add({ name: 4, sex: "磊" });
// const data = new Array();
// store.pagination()(data);
// store.pagination()(data);
// store.pagination()(data);
// console.log(data);