• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript的IndexedDB帮助类笔记

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近在做BI项目,需要用到IndexedDB来做Key-Value类型的表 来来缓存GIS本地渲染需要海量数据

做法和传统用Redis做缓存思路一样:Web本地DB有则区DB没有则请求WebAPI

(尼玛蛋,为什么 LocalStore只支持10M!~)

class DataPak {
    public CodeID: number = 1;
    public Data: any;
    public Message: string = '';
    public Description: string = '成功';

    constructor(Data: any = null, CodeID: number = 1, Description: string = '成功', Msg: string = '') {
        this.CodeID = CodeID;
        this.Data = Data;
        this.Message = Msg;
        this.Description = Description;
    };
};



class DB_Instance {
    private DB_Cache: string = 'DB_Cache';
    private DB_VERSION: number = 1;
    private STORE_GISCache: string = 'Store_GISCache';
    private db_ins: any;
    private DB: IDBFactory;
    private request: IDBOpenDBRequest;

    //初始化函数
    constructor() {
        let _this = this;
        this.DB = window.indexedDB;
        this.request = this.DB.open(this.DB_Cache, this.DB_VERSION);

        this.request.onsuccess = function (ev: Event) {
            console.log('Successful request for IndexedDB.');

            _this.db_ins = _this.request.result;
            var transaction = _this.db_ins.transaction(_this.STORE_GISCache, 'readwrite');

            transaction.oncomplete = function () {
                console.log('Transaction completed successfully.');
            }

            transaction.onerror = function (error) {
                console.log('An error occurred during the transaction: ' + error);
            }
        };

        this.request.onupgradeneeded = function (this: IDBRequest, ev: Event) {

            _this.db_ins = this.result;
            //创建 GIS 表
            let Store_GISCache = _this.db_ins.createObjectStore(
                _this.STORE_GISCache,
                {
                    keyPath: 'KeyID',
                    autoIncrement: 'true' //自动自增加
                }
            );

            //索引 A
            Store_GISCache.createIndex(
                'by_keyid',
                'KeyID',
                {
                    'unique': true,
                }
            );

            //索引 B
            Store_GISCache.createIndex(
                'by_token',
                'Token',
                {
                    'unique': true,
                }
            );

            console.log('The database has been created/updated.');
        };

        this.request.onerror = function (ev: Event) {
            console.log('错误');
        }

        console.log('初始化成功');
    };


    //暴露  GISCache表实例。
    getStore_GISCache(): IDBObjectStore {
        let transaction = this.db_ins.transaction(this.STORE_GISCache, 'readwrite');
        let store: IDBObjectStore = transaction.objectStore(this.STORE_GISCache);
        return store;
    }

    //Key-Value Query
    query_GISCache(token: string, func_onsuccess: any, func_onerror: any = null) {
        let transaction = this.db_ins.transaction(this.STORE_GISCache, 'readwrite');
        let store = transaction.objectStore(this.STORE_GISCache);

        //let idIndex = store.index('by_keyid');
        let TokenIndex = store.index('by_token');

        let temp_request = TokenIndex.get(token);

        transaction.onsuccess = function () {
            let ret_pak = new DataPak(temp_request.result);
            func_onsuccess(ret_pak);
        };

        transaction.oncomplete = function () {
            let ret_pak = new DataPak(temp_request.result);
            func_onsuccess(ret_pak);
        };

        transaction.onerror = function (ev) {
            if (func_onerror != null) {
                let ret_pak = new DataPak(null, 0, '错误');
                func_onerror(ret_pak);
            }
        };
    };

    //Query List;
    queryList_GISCache(WhereFunc: any, func_onsuccess: any, func_onerror: any = null) {
        let transaction = this.db_ins.transaction(this.STORE_GISCache, 'readwrite');
        let store = transaction.objectStore(this.STORE_GISCache);

        let ret_pak = new DataPak([], 1, '成功');

        store.openCursor().onsuccess = function (event) {
            var cursor = event.target.result;

            if (cursor) {
                let Current_Value = cursor.value;
                let bEnable = WhereFunc(Current_Value);
                if (bEnable) {
                    ret_pak.Data.push(Current_Value);
                }

                else {
                    ret_pak.Data.push(Current_Value);
                }
                cursor.continue();
            }
            else {
                func_onsuccess(ret_pak);
            }
        };

        store.openCursor().onerror = function (ev) {
            if (func_onerror != null) {
                ret_pak.CodeID = 0;
                ret_pak.Description = '错误';
                func_onerror(ret_pak);
            }
        };
    };

    //Insert
    insert_GISCache(val, func_onsuccess: any = null, func_onerror: any = null) {
        let transaction = this.db_ins.transaction(this.STORE_GISCache, 'readwrite');

        transaction.onsuccess = function () {
            if (func_onsuccess != null) {
                let ret_pak = new DataPak();
                func_onsuccess(ret_pak);
            }
        };

        transaction.oncomplete = function () {
            //console.log(this);
            if (func_onsuccess != null) {
                let ret_pak = new DataPak();
                func_onsuccess(ret_pak);
            }
        };

        transaction.onerror = function (ev) {
            if (func_onerror != null) {
                let ret_pak = new DataPak(null, 0, '错误');
                func_onerror(ret_pak);
            }
        };

        let store = transaction.objectStore(this.STORE_GISCache);
        store.put(val);
    };

    //删除
    delete_GISCache(token: string, func_onsuccess: any = null, func_onerror: any = null) {

    };

    //删除
    deleteList_GISCache() {

    }

};

var db_Instance = new DB_Instance();

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript的HTML5游戏自动化的基于TypeScript的HTML5游戏开发发布时间:2022-07-22
下一篇:
typescript定义函数(参数)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap