UNPKG

indexed-db-utilities

Version:

Helper methods and utilities for creating managing values dynamically for the browser's IndexDB API.

344 lines (330 loc) 22.1 kB
<!doctype html> <html class="default no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>indexed-db-utilities</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="assets/css/main.css"> </head> <body> <header> <div class="tsd-page-toolbar"> <div class="container"> <div class="table-wrap"> <div class="table-cell" id="tsd-search" data-index="assets/js/search.js" data-base="."> <div class="field"> <label for="tsd-search-field" class="tsd-widget search no-caption">Search</label> <input id="tsd-search-field" type="text" /> </div> <ul class="results"> <li class="state loading">Preparing search index...</li> <li class="state failure">The search index is not available</li> </ul> <a href="index.html" class="title">indexed-db-utilities</a> </div> <div class="table-cell" id="tsd-widgets"> <div id="tsd-filter"> <a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a> <div class="tsd-filter-group"> <div class="tsd-select" id="tsd-filter-visibility"> <span class="tsd-select-label">All</span> <ul class="tsd-select-list"> <li data-value="public">Public</li> <li data-value="protected">Public/Protected</li> <li data-value="private" class="selected">All</li> </ul> </div> <input type="checkbox" id="tsd-filter-inherited" checked /> <label class="tsd-widget" for="tsd-filter-inherited">Inherited</label> <input type="checkbox" id="tsd-filter-externals" checked /> <label class="tsd-widget" for="tsd-filter-externals">Externals</label> <input type="checkbox" id="tsd-filter-only-exported" /> <label class="tsd-widget" for="tsd-filter-only-exported">Only exported</label> </div> </div> <a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a> </div> </div> </div> </div> <div class="tsd-page-title"> <div class="container"> <ul class="tsd-breadcrumb"> <li> <a href="globals.html">Globals</a> </li> </ul> <h1> indexed-db-utilities</h1> </div> </div> </header> <div class="container container-main"> <div class="row"> <div class="col-8 col-content"> <div class="tsd-panel tsd-typography"> <h1 id="indexdb-utilities">IndexDB-Utilities</h1> <blockquote> <p>A lightweight utility helper that opens or creates an <code>IndexedDB</code> with <code>objectStores</code> that returns a promise that resolves utility methods.</p> </blockquote> <h2 id="install">Install</h2> <h3 id="npm">NPM</h3> <pre><code class="lang-shell">npm i --save indexed-db-utilities </code></pre> <h3 id="yarn">Yarn</h3> <pre><code class="lang-shell">yarn add indexed-db-utilities </code></pre> <h2 id="how-to-use">How to use</h2> <p>Opening an instance requires a config object. <b>Note:</b> Once the dbName is declared with an instance the keyPath is then tied to it and cannot change without changing the version number.</p> <pre><code class="lang-typescript"><span class="hljs-keyword">const</span> config: { version: <span class="hljs-built_in">Number</span> dbName: <span class="hljs-built_in">String</span> storeNames: <span class="hljs-built_in">String</span>[] keyPath?: <span class="hljs-built_in">String</span> } </code></pre> <p>Call <code>openIDBUtilities(config)</code> to return a promise holding an <code>IDBUtility</code> instance which has utility methods that return a promise. These promises throw a <code>Request.result</code> object on error.</p> <pre><code class="lang-typescript"> <span class="hljs-keyword">const</span> IDBUtility: { add: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span>, value: {}</span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;<span class="hljs-built_in">string</span> | Request.result&gt;; put: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span>, value: {}</span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;<span class="hljs-built_in">string</span> | Request.result&gt;; update: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span>, keyValue: <span class="hljs-built_in">string</span>, value: {}</span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;<span class="hljs-built_in">string</span> | Request.result&gt;; <span class="hljs-keyword">get</span>: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span>, keyValue: <span class="hljs-built_in">string</span></span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;{[keyPath]: <span class="hljs-built_in">string</span> , ...{}} | Request.result&gt;; getAll: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span></span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;{}&gt;; remove: <span class="hljs-function">(<span class="hljs-params">storeName: <span class="hljs-built_in">string</span>, keyValue: <span class="hljs-built_in">string</span></span>) =&gt;</span> <span class="hljs-built_in">Promise</span>&lt;<span class="hljs-built_in">void</span> | Request.result&gt;; } </code></pre> <h2 id="typescript">TypeScript</h2> <pre><code class="lang-typescript"> <span class="hljs-keyword">import</span> { openIDBUtilities } <span class="hljs-keyword">from</span> <span class="hljs-string">'indexed-db-utilities/dist/utilities/index-db.utility'</span>; <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">/** * * Call openIDBUtilities(config) with config parameters to open an IndexedDB and return a promise that holds * an Object with helper methods, add(), put(), update(), get() and remove(). * if one doesn't exist then it will be made. Note: Once ObjectStores are defined they * IDBs version number and to add more you must provide a new version number. * * The Config Object = { * version: Number * dbName: String * storeNames: String[] * keyPath?: String * } * **/</span> <span class="hljs-keyword">const</span> stores = <span class="hljs-keyword">await</span> openIDBUtilities({ version: <span class="hljs-number">1</span>, <span class="hljs-comment">// Numbers should be a positive Number and not contain any decimal digits.</span> dbName: <span class="hljs-string">'DemoIDB-1'</span>, storeNames: [<span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'demoStore1'</span>], <span class="hljs-comment">// Provide and array of the storeNames in which you wish to store values in.</span> keyPath: <span class="hljs-string">'myKey'</span> <span class="hljs-comment">// keyPath is optional, if provided this will be how you get selective values from the stores and must be a property in your object value you wish to store. If omitted the stores will be indexed 0 to (n-1);</span> }); <span class="hljs-comment">/** * add(storeName: string, value: any) =&gt; Promise&lt;string | {}&gt;; * returns a string with the saved value's key or Request.Result Object. * Note: You cannot add new values to objects with keys that already exsist, use put() or update(). */</span> <span class="hljs-keyword">const</span> addResponse = <span class="hljs-keyword">await</span> stores.add( <span class="hljs-string">'demoStore0'</span>, { myKey: <span class="hljs-string">'foo'</span>, value: [{ bat: <span class="hljs-string">'squeak'</span>}, {bear: <span class="hljs-string">'grrr'</span>}] }) .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Add Error'</span>, err)); <span class="hljs-comment">/** * get(storeName: string, key: string) =&gt; Promise&lt;any&gt;; * returns the value stored that matches the value stored in [keyPath]: String * or throws an Request.Result Object */</span> <span class="hljs-keyword">let</span> getResponse = <span class="hljs-keyword">await</span> stores.get(<span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'foo'</span>); <span class="hljs-comment">/** * put(storeName: string, value: any) =&gt; Promise&lt;string | {}&gt;; * returns a string with the saved value's key or throws Request.Result Object. * Note: This will overwrite your value saved related your key. */</span> <span class="hljs-keyword">const</span> putResponse = <span class="hljs-keyword">await</span> stores.put( <span class="hljs-string">'demoStore0'</span>, { myKey: <span class="hljs-string">'foo'</span>, value: [{ cat: <span class="hljs-string">'meow'</span>}] }); <span class="hljs-comment">/** * update(storeName: string, key: string, value: any) =&gt; Promise&lt;string | {}&gt;; * returns a string with the updated value's key or throws an Request.Result Object. * Note: This will merge with your stored value, if it is an array it will concatenate * the new values. */</span> <span class="hljs-keyword">const</span> updateResponse = <span class="hljs-keyword">await</span> stores.update( <span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'foo'</span>, { myKey: <span class="hljs-string">'foo'</span>, value: [{ bat: <span class="hljs-string">'squeak'</span>}, {bear: <span class="hljs-string">'grrr'</span>, dog: [<span class="hljs-string">'woof'</span>, <span class="hljs-string">'bark'</span>]}, {cat: <span class="hljs-string">'purr'</span>}, [<span class="hljs-string">'happy hacking!'</span>]] }); <span class="hljs-comment">/** * getAll (storeName: string) =&gt; Promise&lt;{}&gt; * Returns all values in the store. */</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) { <span class="hljs-keyword">await</span> stores.put(<span class="hljs-string">'demoStore0'</span>, { myKey: i.toString(), value: i }) } <span class="hljs-keyword">const</span> getAllResponse = <span class="hljs-keyword">await</span> stores.getAll(<span class="hljs-string">'demoStore0'</span>); <span class="hljs-comment">/** * remove(storeName: string, key: string) =&gt; Promise&lt;any&gt;; * returns void or throws an Request.Result Object */</span> <span class="hljs-keyword">const</span> removeResponse = <span class="hljs-keyword">await</span> stores.remove(<span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'foo'</span>) .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'remove error'</span>, err)); <span class="hljs-comment">// removeResponse undefined</span> } </code></pre> <h2 id="es6">ES6</h2> <p>example</p> <pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { openIDBUtilities } <span class="hljs-keyword">from</span> <span class="hljs-string">'indexed-db-utilities/dist/utilities/index-db.utility'</span>; <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">offlineCache</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">const</span> cache = <span class="hljs-keyword">await</span> openIDBUtilities({ <span class="hljs-attr">version</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">dbName</span>: <span class="hljs-string">'DemoIDB-1'</span>, <span class="hljs-attr">storeNames</span>: [<span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'demoStore1'</span>], <span class="hljs-attr">keyPath</span>: <span class="hljs-string">'myKey'</span> }); cache.add(<span class="hljs-string">'demoStore1'</span>, {<span class="hljs-attr">myKey</span>: <span class="hljs-string">'foo'</span>, <span class="hljs-attr">bat</span>: <span class="hljs-string">'squeak'</span>}); <span class="hljs-comment">// addResponse === 'foo'</span> } </code></pre> <h2 id="browser">Browser</h2> <p>Place a <code>&lt;script src=&quot;/node_modules/indexed-db-utilities/dist/indexed-db-utilities.js&quot;&gt;&lt;/script&gt;</code> inside of your document. Note: This attaches <code>openIDBUtilities()</code> to your window, use ES6&#39;s <code>import</code> or TypeScript to avoid the global.</p> <p>example</p> <pre><code class="lang-html"> <span class="hljs-meta">&lt;!DOCTYPE html&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Demo<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/node_modules/indexed-db-utilities/dist/indexed-db-utilities.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript"> <span class="hljs-keyword">const</span> stores = openIDBUtilities({ <span class="hljs-attr">version</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">dbName</span>: <span class="hljs-string">'DemoIDB-1'</span>, <span class="hljs-attr">storeNames</span>: [<span class="hljs-string">'demoStore0'</span>, <span class="hljs-string">'demoStore1'</span>], <span class="hljs-attr">keyPath</span>: <span class="hljs-string">'myKey'</span> }); stores.then(<span class="hljs-function"><span class="hljs-params">db</span> =&gt;</span> { <span class="hljs-comment">// call helper methods on the resolved object</span> <span class="hljs-keyword">const</span> addResponse = db.add(<span class="hljs-string">'demoStore1'</span>, {<span class="hljs-attr">myKey</span>: <span class="hljs-string">'foo'</span>, <span class="hljs-attr">bat</span>: <span class="hljs-string">'squeak'</span>}); <span class="hljs-comment">// addResponse === 'foo'</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'addResponse'</span>, addResponse); }); </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span> </code></pre> </div> </div> <div class="col-4 col-menu menu-sticky-wrap menu-highlight"> <nav class="tsd-navigation primary"> <ul> <li class="globals "> <a href="globals.html"><em>Globals</em></a> </li> <li class="label tsd-is-external"> <span>Internals</span> </li> <li class=" tsd-kind-external-module"> <a href="modules/_indexed_db_utilities_.html">"indexed-<wbr>db-<wbr>utilities"</a> </li> <li class="label tsd-is-external"> <span>Externals</span> </li> <li class=" tsd-kind-external-module tsd-is-external"> <a href="modules/_models_config_model_.html">"models/config.model"</a> </li> <li class=" tsd-kind-external-module tsd-is-external"> <a href="modules/_models_idb_utility_model_.html">"models/idb-<wbr>utility.model"</a> </li> <li class=" tsd-kind-external-module tsd-is-external"> <a href="modules/_utilities_index_db_utility_.html">"utilities/index-<wbr>db.utility"</a> </li> </ul> </nav> <nav class="tsd-navigation secondary menu-sticky"> <ul class="before-current"> </ul> </nav> </div> </div> </div> <footer class="with-border-bottom"> <div class="container"> <h2>Legend</h2> <div class="tsd-legend-group"> <ul class="tsd-legend"> <li class="tsd-kind-module"><span class="tsd-kind-icon">Module</span></li> <li class="tsd-kind-object-literal"><span class="tsd-kind-icon">Object literal</span></li> <li class="tsd-kind-variable"><span class="tsd-kind-icon">Variable</span></li> <li class="tsd-kind-function"><span class="tsd-kind-icon">Function</span></li> <li class="tsd-kind-function tsd-has-type-parameter"><span class="tsd-kind-icon">Function with type parameter</span></li> <li class="tsd-kind-index-signature"><span class="tsd-kind-icon">Index signature</span></li> <li class="tsd-kind-type-alias"><span class="tsd-kind-icon">Type alias</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-enum"><span class="tsd-kind-icon">Enumeration</span></li> <li class="tsd-kind-enum-member"><span class="tsd-kind-icon">Enumeration member</span></li> <li class="tsd-kind-property tsd-parent-kind-enum"><span class="tsd-kind-icon">Property</span></li> <li class="tsd-kind-method tsd-parent-kind-enum"><span class="tsd-kind-icon">Method</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-interface"><span class="tsd-kind-icon">Interface</span></li> <li class="tsd-kind-interface tsd-has-type-parameter"><span class="tsd-kind-icon">Interface with type parameter</span></li> <li class="tsd-kind-constructor tsd-parent-kind-interface"><span class="tsd-kind-icon">Constructor</span></li> <li class="tsd-kind-property tsd-parent-kind-interface"><span class="tsd-kind-icon">Property</span></li> <li class="tsd-kind-method tsd-parent-kind-interface"><span class="tsd-kind-icon">Method</span></li> <li class="tsd-kind-index-signature tsd-parent-kind-interface"><span class="tsd-kind-icon">Index signature</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-class"><span class="tsd-kind-icon">Class</span></li> <li class="tsd-kind-class tsd-has-type-parameter"><span class="tsd-kind-icon">Class with type parameter</span></li> <li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li> <li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li> <li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li> <li class="tsd-kind-accessor tsd-parent-kind-class"><span class="tsd-kind-icon">Accessor</span></li> <li class="tsd-kind-index-signature tsd-parent-kind-class"><span class="tsd-kind-icon">Index signature</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-constructor tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited constructor</span></li> <li class="tsd-kind-property tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited property</span></li> <li class="tsd-kind-method tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited method</span></li> <li class="tsd-kind-accessor tsd-parent-kind-class tsd-is-inherited"><span class="tsd-kind-icon">Inherited accessor</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-property tsd-parent-kind-class tsd-is-protected"><span class="tsd-kind-icon">Protected property</span></li> <li class="tsd-kind-method tsd-parent-kind-class tsd-is-protected"><span class="tsd-kind-icon">Protected method</span></li> <li class="tsd-kind-accessor tsd-parent-kind-class tsd-is-protected"><span class="tsd-kind-icon">Protected accessor</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-property tsd-parent-kind-class tsd-is-private"><span class="tsd-kind-icon">Private property</span></li> <li class="tsd-kind-method tsd-parent-kind-class tsd-is-private"><span class="tsd-kind-icon">Private method</span></li> <li class="tsd-kind-accessor tsd-parent-kind-class tsd-is-private"><span class="tsd-kind-icon">Private accessor</span></li> </ul> <ul class="tsd-legend"> <li class="tsd-kind-property tsd-parent-kind-class tsd-is-static"><span class="tsd-kind-icon">Static property</span></li> <li class="tsd-kind-call-signature tsd-parent-kind-class tsd-is-static"><span class="tsd-kind-icon">Static method</span></li> </ul> </div> </div> </footer> <div class="container tsd-generator"> <p>Generated using <a href="http://typedoc.org/" target="_blank">TypeDoc</a></p> </div> <div class="overlay"></div> <script src="assets/js/main.js"></script> <script>if (location.protocol == 'file:') document.write('<script src="assets/js/search.js"><' + '/script>');</script> </body> </html>