Giter Site home page Giter Site logo

fakeindexeddb's Introduction

Fake IndexedDB Build Status

This is a pure JS in-memory implementation of the IndexedDB API.

It passes the W3C IndexedDB test suite (a feat that all browsers except Chrome fail) plus a couple hundred more tests just to be sure. It also works well enough to run fairly complex IndexedDB-based software.

Installation

For use with CommonJS (Node.js/Browserify), install through npm:

npm install fake-indexeddb

Otherwise, download the bundled version and include it in your page like:

<script type="text/javascript" src="fakeIndexedDB.js"></script>

If you're using AMD, you'll have to shim it.

Use

Functionally, it works exactly like IndexedDB except data is not persisted to disk.

Example usage:

var fakeIndexedDB = require('fake-indexeddb');
var FDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange');

var request = fakeIndexedDB.open('test', 3);
request.onupgradeneeded = function () {
    var db = request.result;
    var store = db.createObjectStore("books", {keyPath: "isbn"});
    store.createIndex("by_title", "title", {unique: true});

    store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
    store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
    store.put({title: "Bedrock Nights", author: "Barney", isbn: 345678});
}
request.onsuccess = function (event) {
    var db = event.target.result;

    var tx = db.transaction("books");

    tx.objectStore("books").index("by_title").get("Quarry Memories").addEventListener('success', function (event) {
        console.log('From index:', event.target.result);
    });
    tx.objectStore("books").openCursor(FDBKeyRange.lowerBound(200000)).onsuccess = function (event) {
        var cursor = event.target.result;
        if (cursor) {
            console.log('From cursor:', cursor.value);
            cursor.continue();
        }
    };
    tx.oncomplete = function () {
        console.log('All done!');
    };
};

Variable names of all the objects are like the normal IndexedDB ones except with F replacing I, e.g. FDBIndex instead of IDBIndex.

If you're using the bundled version (not installed through npm), then all of the variables are created and attached to window (or self in a Web Worker), like window.fakeIndexedDB, window.FDBKeyRange, etc.

Potential applications:

  1. Use as a mock database in unit tests.

  2. Use the same API in Node.js and in the browser.

  3. Support IndexedDB in old or crappy browsers.

  4. Somehow use it within a caching layer on top of IndexedDB in the browser, since IndexedDB can be kind of slow.

  5. Abstract the core database functions out, so what is left is a shell that allows the IndexedDB API to easily sit on top of many different backends.

  6. Serve as a playground for experimenting with IndexedDB.

License

Apache 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.