Now the INDEXEDDB has a few mature libraries, such as the west of these several, any one is very good. The advantage of other people's things is to get started quickly, look at the document is good, if the document is not very good, it is a little awkward.
Dexie.js:a minimalistic Wrapper for IndexedDB
Zangodb:mongodb-like Interface for HTML5 IndexedDB
Localforage:offline storage, improved. Wraps IndexedDB, Websql, or localstorage using a simple but powerful AP
IDB.FILESYSTEM.JS:HTML5 filesystem API Polyfill using IndexedDB
File System
FILER:A Wrapper library for the HTML5 Filesystem API what is reuses UNIX commands (CP, MV, LS) for its API.
The theme of this article is to develop a simple file system based on INDEXEDDB, of course, is simple, can have a thought, think so simple.
I do not say much, the source code in the Idbprovider.js, the core is _topromise,
Because indexed operation, basically is to create a transaction first, obtain ObjectStore, then carry on the enhancement and deletion check.
Said here is a simple version, the control of the directory, the control of the file information, delete are not specifically implemented, for what, and so you come ah.
_topromise (method, ... args) { try { return new Promise ((Resolve, reject) + = {//Get transaction let trans = this. Transaction //GET request let req = Trans.objectstore (This._storename) [Method] (.... args) //Request succeeded Req.onsuccess = event = Resolve (Event.target.result) //request failed Req.onerror = event = Reject (Req.error) c12/>//transaction Failure Trans.onerror = event = Reject (Trans.error) }) } catch (Err) { Promise.reject (err ) } }
Let _w_ = window_indexeddb_ = _w_.indexeddb | | _W_.MOZINDEXEDDB | | _W_.WEBKITINDEXEDDB | | _w_.msindexeddb_w_. IDbTransaction = _w_. IDbTransaction | | _w_.webkitidbtransaction | | _w_.msidbtransaction_w_. Idbkeyrange = _w_. Idbkeyrange | | _w_.webkitidbkeyrange | | _w_.msidbkeyrangeclass Idbprovider {Constructor () {//DB this._db = NULL//Instance This._inst ance = NULL//store Name this._storename = idbprovider._storename} Get Transaction () {return This._db.transaction ([This._storename], Idbtransaction.read_write | | ' ReadWrite ')} _topromise (method, ... args) {try {return new Promise (Resolve, reject) + = { Get transaction Let trans = this.transaction//GET request let Req = Trans.obj Ectstore (This._storename) [Method] (... args)//Request Success Req.onsuccess = Event = Resolve (Event . Target.result)//request failed Req.onerRor = event = Reject (Req.error)/transaction Failure Trans.onerror = event = Reject (Trans.error) })} catch (Err) {Promise.reject (ERR)}} static getinstance (Dbversion = 1.0) { if (this._instance) {promise.resolve (this._instance)} return new Promise (Resolve, Reje CT) = = {Let request = _indexeddb_.open (Idbprovider._dbname, dbversion) Request.onerror = event = > {return reject (null)} request.onsuccess = Event = = {Let db = Request.result//old version, new version is onupgradeneeded if (db.setversion && db.version!== dbve rsion) {var setversion = db.setversion (dbversion); setversion.onsuccess = function () {db.createobjectstore (this._storename) t His._instance = new Idbprovider () this._instance._db = Request.result return Resolve (This._instance)} } else {this._instance = new Idbprovider () this._instance._db = Request.result Return resolve (This._instance)}} request.onupgradeneeded = even T = = {Event.target.result.createObjectStore (this._storename)}})}/** * Received Fetch file * @param {*string} path */getFile (path) {return this._topromise (' get ', Path)}/** * Write Enter file * @param {*string} path Path * @param {*string| BLOB} Content Contents * @param {*string} type * @param {*string} append temporarily useless */async writetofile (Path, content , type = NULL, append = False) {Let data = content//Not blob, to blob if (content instanceof Arraybuff ER) {data = new Blob ([New Uint8array (content)], {type})} else if (typeof content = = = ' String ') {data = new Blob ([content], {type: ' Text/plain '})} else {dat A = new Blob ([content])} await this._topromise (' Put ', data, path) return This.getfile (PATH)/ * Return new Promise ((resolve, reject) = {Let data = content//not blob, blob if (content instanceof ArrayBuffer) {data = new Blob ([New Uint8array (content)], {type})} El Se if (typeof content = = = ' String ') {data = new Blob ([content])}//Deposit Let trans = This.transaction Trans.objectstore (this._storename). Put (data, path) Trans.objectstore (This._storename). Get (path). onsuccess = Event = {Resolve (Event.target.result)} Trans.onerror = Event = = {reject (trans.error)}}) */} readentries (Path = ") {if (!path) { Return This.readallentries ()} return this._topromise (' Getallkeys ', Idbkeyrange.lowerbound (path)). Then (r = R.filter (p = +//starts with the current path && (truncates the current empty string, or truncates to/begins with/) return P.indexof (path) = = = 0 && (p.substring (path.length) = = = "| | p.substring (path.length). IndexOf ('/') = = = 0)})} Readal Lentries () {return this._topromise (' Getallkeys ')} ensuredirectory (directory = ') {return PROMISE.R Esolve (Directory)} clear () {return this._topromise (' Clear '). Then (R = True)}/** * processing path, than such as special characters, such as/start and so on * @param {*string} path */_handlepath (path) {return path}}idbprovider._dbname = ' _ Fs_db_ ' idbprovider._storename = ' _fs_store '//test statement//Read subdirectories and files for a directory: Idbprovider.getinstance (). Then (fs=> Fs.readentries ()). Then (F=>console.log (f))//write file Idbprovider.getinstance (). Then (Fs=>fs.writetofile (' music/ Txt.txt ', ' Love your Death '). Then (F=>console.log (f))//Get File: Idbprovider.getinstance (). Then (Fs=>fs.getfile (' Music/txt.txt ')). Then (F=>console.log (f))//Create directory recursively: Idbprovider.getinstance (). Then (Fs=>fs.ensuredirectory (' Music/vbox ')). Then (R=>console.log (R))//Recursive acquisition: Idbprovider.getinstance (). Then (Fs=>fs.readallentries ()). Then (F=>console.log (f))//Delete all: Idbprovider.getinstance (). Then (Fs=>fs.clear ()). Then (F=>console.log (f)). catch (Err=>console.log (err))
Reference:
Indexed Database API
Indexed DB API
Using IndexedDB
Basic Concepts-web APIs | MDN
Storing images and files in IndexedDB
IndexedDB Development Guide
Implement a simple file system based on INDEXEDDB