New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

browserdb

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browserdb

this zero-dependency package will provide a persistent, in-browser database

  • 2017.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

db-lite

this zero-dependency package will provide a persistent, in-browser database

travis-ci.org build-status istanbul-coverage

NPM

package-listing

cdn download

live demo

github.com test-server

documentation

api-doc

api-doc

todo
  • add remote http-api
  • none
change since 420c05c1
  • npm publish 2017.2.2
  • add ttl-cache
  • do not auto-uglify js-assets in production
  • replace env var npm_package_name with npm_package_nameAlias in istanbul code-coverage
  • use async-api's in example.js
  • none
this package requires
  • darwin or linux os

build status travis-ci.org build-status

build commit status

git-branch :masterbetaalpha
test-server-1 :github.com test-servergithub.com test-servergithub.com test-server
test-server-2 :heroku.com test-serverheroku.com test-serverheroku.com test-server
test-report :test-reporttest-reporttest-report
coverage :istanbul-coverageistanbul-coverageistanbul-coverage
build-artifacts :build-artifactsbuild-artifactsbuild-artifacts
master branch
  • stable branch
  • HEAD should be tagged, npm-published package
beta branch
  • semi-stable branch
  • HEAD should be latest, npm-published package
alpha branch
  • unstable branch
  • HEAD is arbitrary
  • commit history may be rewritten

quickstart web example

screen-capture

to run this example, follow the instruction in the script below
/*
example.js

this script will demo the browser-version of db-lite

instruction
    1. save this script as example.js
    2. run the shell command:
        $ npm install db-lite && PORT=8081 node example.js
    3. play with the browser-demo on http://127.0.0.1:8081
*/



/* istanbul instrument in package db */
/*jslint
    bitwise: true,
    browser: true,
    maxerr: 8,
    maxlen: 96,
    node: true,
    nomen: true,
    regexp: true,
    stupid: true
*/
(function () {
    'use strict';
    var local;



    // run shared js-env code - pre-init
    (function () {
        // init local
        local = {};
        // init modeJs
        local.modeJs = (function () {
            try {
                return typeof navigator.userAgent === 'string' &&
                    typeof document.querySelector('body') === 'object' &&
                    typeof XMLHttpRequest.prototype.open === 'function' &&
                    'browser';
            } catch (errorCaughtBrowser) {
                return module.exports &&
                    typeof process.versions.node === 'string' &&
                    typeof require('http').createServer === 'function' &&
                    'node';
            }
        }());
        // init global
        local.global = local.modeJs === 'browser'
            ? window
            : global;
        // init utility2_rollup
        local = local.global.utility2_rollup || (local.modeJs === 'browser'
            ? local.global.utility2_db
            : require('db-lite'));
        // export local
        local.global.local = local;
        // load db
        local.db.dbLoad(function () {
            console.log('db loaded from ' + local.storageDir);
        });
        // coverage-hack
        [null, local.global.utility2].forEach(function (element) {
            if (!element) {
                return;
            }
            element._testRunBefore = function () {
                local.onReadyBefore.counter += 1;
                local.db.dbDrop(local.onReadyBefore);
            };
        });
    }());
    switch (local.modeJs) {



    // post-init
    /* istanbul ignore next */
    // run browser js-env code - post-init
    case 'browser':
        local.testRunBrowser = function (event) {
            var reader, tmp;
            if (!event || (event &&
                    event.currentTarget &&
                    event.currentTarget.className &&
                    event.currentTarget.className.includes &&
                    event.currentTarget.className.includes('onreset'))) {
                // reset output
                Array.from(
                    document.querySelectorAll('body > .resettable')
                ).forEach(function (element) {
                    switch (element.tagName) {
                    case 'INPUT':
                    case 'TEXTAREA':
                        element.value = '';
                        break;
                    default:
                        element.textContent = '';
                    }
                });
            }
            switch (event && event.currentTarget && event.currentTarget.id) {
            case 'testRunButton1':
                // show tests
                if (document.querySelector('#testReportDiv1').style.display === 'none') {
                    document.querySelector('#testReportDiv1').style.display = 'block';
                    document.querySelector('#testRunButton1').textContent =
                        'hide internal test';
                    local.modeTest = true;
                    local.testRunDefault(local);
                // hide tests
                } else {
                    document.querySelector('#testReportDiv1').style.display = 'none';
                    document.querySelector('#testRunButton1').textContent = 'run internal test';
                }
                break;
            // custom-case
            case 'dbExportButton1':
                tmp = window.URL.createObjectURL(new window.Blob([local.db.dbExport()]));
                document.querySelector('#dbExportA1').href = tmp;
                document.querySelector('#dbExportA1').click();
                setTimeout(function () {
                    window.URL.revokeObjectURL(tmp);
                }, 30000);
                break;
            case 'dbImportButton1':
                document.querySelector('#dbImportInput1').click();
                break;
            case 'dbImportInput1':
                console.log('importing db-lite database ...');
                reader = new window.FileReader();
                tmp = document.querySelector('#dbImportInput1').files[0];
                if (!tmp) {
                    return;
                }
                reader.addEventListener('load', function () {
                    local.db.dbImport(reader.result);
                    console.log('... imported db-lite database');
                });
                reader.readAsText(tmp);
                break;
            case 'dbResetButton1':
                console.log('resetting db-lite database ...');
                local.db.dbDrop(function () {
                    console.log('... resetted db-lite database');
                });
                break;
            }
            if (document.querySelector('#inputTextareaEval1') && (!event || (event &&
                    event.currentTarget &&
                    event.currentTarget.className &&
                    event.currentTarget.className.includes &&
                    event.currentTarget.className.includes('oneval')))) {
                // try to eval input-code
                try {
                    /*jslint evil: true*/
                    eval(document.querySelector('#inputTextareaEval1').value);
                } catch (errorCaught) {
                    console.error(errorCaught.stack);
                }
            }
        };
        // log stderr and stdout to #outputTextareaStdout1
        ['error', 'log'].forEach(function (key) {
            console[key + '_original'] = console[key];
            console[key] = function () {
                var element;
                console[key + '_original'].apply(console, arguments);
                element = document.querySelector('#outputTextareaStdout1');
                if (!element) {
                    return;
                }
                // append text to #outputTextareaStdout1
                element.value += Array.from(arguments).map(function (arg) {
                    return typeof arg === 'string'
                        ? arg
                        : JSON.stringify(arg, null, 4);
                }).join(' ') + '\n';
                // scroll textarea to bottom
                element.scrollTop = element.scrollHeight;
            };
        });
        // init event-handling
        ['change', 'click', 'keyup'].forEach(function (event) {
            Array.from(document.querySelectorAll('.on' + event)).forEach(function (element) {
                element.addEventListener(event, local.testRunBrowser);
            });
        });
        // run tests
        local.testRunBrowser();
        break;



    /* istanbul ignore next */
    // run node js-env code - post-init
    case 'node':
        // export local
        module.exports = local;
        // require modules
        local.fs = require('fs');
        local.http = require('http');
        local.url = require('url');
        // init assets
        local.assetsDict = local.assetsDict || {};
        /* jslint-ignore-begin */
        local.assetsDict['/assets.index.template.html'] = '\
<!doctype html>\n\
<html lang="en">\n\
<head>\n\
<meta charset="UTF-8">\n\
<meta name="viewport" content="width=device-width, initial-scale=1">\n\
<title>{{env.npm_package_nameAlias}} v{{env.npm_package_version}}</title>\n\
<style>\n\
/*csslint\n\
    box-sizing: false,\n\
    universal-selector: false\n\
*/\n\
* {\n\
    box-sizing: border-box;\n\
}\n\
body {\n\
    background: #dde;\n\
    font-family: Arial, Helvetica, sans-serif;\n\
    margin: 2rem;\n\
}\n\
body > * {\n\
    margin-bottom: 1rem;\n\
}\n\
</style>\n\
<style>\n\
/*csslint\n\
    adjoining-classes: false,\n\
    box-model: false,\n\
    box-sizing: false,\n\
    universal-selector: false\n\
*/\n\
body > button {\n\
    width: 15rem;\n\
}\n\
textarea {\n\
    font-family: monospace;\n\
    height: 10rem;\n\
    width: 100%;\n\
}\n\
textarea[readonly] {\n\
    background: #ddd;\n\
}\n\
.zeroPixel {\n\
    border: 0;\n\
    height: 0;\n\
    margin: 0;\n\
    padding: 0;\n\
    width: 0;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<!-- utility2-comment\n\
    <div id="ajaxProgressDiv1" style="background: #d00; height: 2px; left: 0; margin: 0; padding: 0; position: fixed; top: 0; transition: background 0.5s, width 1.5s; width: 25%;"></div>\n\
utility2-comment -->\n\
    <h1>\n\
<!-- utility2-comment\n\
        <a\n\
            {{#if env.npm_package_homepage}}\n\
            href="{{env.npm_package_homepage}}"\n\
            {{/if env.npm_package_homepage}}\n\
            target="_blank"\n\
        >\n\
utility2-comment -->\n\
            {{env.npm_package_nameAlias}} v{{env.npm_package_version}}\n\
<!-- utility2-comment\n\
        </a>\n\
utility2-comment -->\n\
    </h1>\n\
    <h3>{{env.npm_package_description}}</h3>\n\
<!-- utility2-comment\n\
    <h4><a download href="assets.app.js">download standalone app</a></h4>\n\
    <button class="onclick onreset" id="testRunButton1">run internal test</button><br>\n\
    <div id="testReportDiv1" style="display: none;"></div>\n\
utility2-comment -->\n\
\n\
\n\
\n\
    <button class="onclick onreset" id="dbResetButton1">reset database</button><br>\n\
    <button class="onclick" id="dbExportButton1">export database -&gt; file</button><br>\n\
    <a download="db.persistence.json" href="" id="dbExportA1"></a>\n\
    <button class="onclick" id="dbImportButton1">import database &lt;- file</button><br>\n\
    <input class="onchange onreset zeroPixel" type="file" id="dbImportInput1">\n\
    <label>edit or paste script below to\n\
        <a\n\
            href="https://kaizhu256.github.io/node-db-lite/build..beta..travis-ci.org/api-doc.html"\n\
            target="_blank"\n\
        >eval</a>\n\
    </label>\n\
<textarea id="inputTextareaEval1">\n\
/*jslint browser: true, node: true*/\n\
"use strict";\n\
var dbTable1, modeNext, onNext;\n\
modeNext = 0;\n\
onNext = function (error, data) {\n\
    modeNext = error\n\
        ? Infinity\n\
        : modeNext + 1;\n\
    switch (modeNext) {\n\
    case 1:\n\
        dbTable1 = window.dbTable1 = window.utility2_db.dbTableCreateOne({\n\
            name: "dbTable1"\n\
        }, onNext);\n\
        break;\n\
    case 2:\n\
        dbTable1.idIndexCreate({ name: "field1" }, onNext);\n\
        break;\n\
    case 3:\n\
        dbTable1.crudSetOneById({ field1: "aa", field2: 1, field3: "foo" }, onNext);\n\
        break;\n\
    case 4:\n\
        dbTable1.crudSetOneById({ field1: "bb", field2: 2, field3: "bar" }, onNext);\n\
        break;\n\
    case 5:\n\
        dbTable1.crudSetOneById({ field1: "cc", field2: 3, field3: "baz" }, onNext);\n\
        break;\n\
    case 6:\n\
        dbTable1.crudRemoveOneById({ field1: "aa" }, onNext);\n\
        break;\n\
    case 7:\n\
        dbTable1.crudUpdateOneById({ field1: "bb", field2: -1 }, onNext);\n\
        break;\n\
    case 8:\n\
        dbTable1.crudSetOneById({ field2: Math.random() }, onNext);\n\
        break;\n\
    case 9:\n\
        dbTable1.crudGetManyByQuery({\n\
            limit: Infinity,\n\
            query: { field2: { $gte: -Infinity, $lte: Infinity } },\n\
            skip: 0,\n\
            sort: [{ fieldName: "_timeUpdated", idDescending: true }]\n\
        }, onNext);\n\
        break;\n\
    case 10:\n\
        console.log(data);\n\
        dbTable1.crudCountAll(onNext);\n\
        break;\n\
    case 11:\n\
        console.log("number of rows: " + data);\n\
        break;\n\
    default:\n\
        console.error(error.stack);\n\
    }\n\
};\n\
onNext();\n\
</textarea>\n\
    <button class="onclick oneval onreset" id="dbEvalButton1">eval script</button><br>\n\
    <label>stderr and stdout</label>\n\
    <textarea class="resettable" id="outputTextareaStdout1" readonly></textarea>\n\
<!-- utility2-comment\n\
    {{#if isRollup}}\n\
    <script src="assets.app.js"></script>\n\
    {{#unless isRollup}}\n\
utility2-comment -->\n\
    <script src="assets.utility2.rollup.js"></script>\n\
    <script src="jsonp.utility2._stateInit?callback=window.utility2._stateInit"></script>\n\
    <script src="assets.db.rollup.js"></script>\n\
    <script src="assets.example.js"></script>\n\
    <script src="assets.test.js"></script>\n\
<!-- utility2-comment\n\
    {{/if isRollup}}\n\
utility2-comment -->\n\
</body>\n\
</html>\n\
';
        /* jslint-ignore-end */
        if (local.templateRender) {
            local.assetsDict['/'] = local.templateRender(
                local.assetsDict['/assets.index.template.html'],
                {
                    env: local.objectSetDefault(local.env, {
                        npm_package_description: 'example module',
                        npm_package_nameAlias: 'example',
                        npm_package_version: '0.0.1'
                    })
                }
            );
        } else {
            local.assetsDict['/'] = local.assetsDict['/assets.index.template.html']
                .replace((/\{\{env\.(\w+?)\}\}/g), function (match0, match1) {
                    // jslint-hack
                    String(match0);
                    switch (match1) {
                    case 'npm_package_description':
                        return 'example module';
                    case 'npm_package_nameAlias':
                        return 'example';
                    case 'npm_package_version':
                        return '0.0.1';
                    }
                });
        }
        // run the cli
        if (local.global.utility2_rollup || module !== require.main) {
            break;
        }
        local.assetsDict['/assets.example.js'] = local.assetsDict['/assets.example.js'] ||
            local.fs.readFileSync(__filename, 'utf8');
        local.assetsDict['/assets.db.rollup.js'] =
            local.assetsDict['/assets.db.rollup.js'] || local.fs.readFileSync(
                local.db.__dirname + '/lib.db.js',
                'utf8'
            ).replace((/^#!/), '//');
        local.assetsDict['/favicon.ico'] = local.assetsDict['/favicon.ico'] || '';
        // if $npm_config_timeout_exit exists,
        // then exit this process after $npm_config_timeout_exit ms
        if (Number(process.env.npm_config_timeout_exit)) {
            setTimeout(process.exit, Number(process.env.npm_config_timeout_exit));
        }
        // start server
        if (local.global.utility2_serverHttp1) {
            break;
        }
        process.env.PORT = process.env.PORT || '8081';
        console.log('server starting on port ' + process.env.PORT);
        local.http.createServer(function (request, response) {
            request.urlParsed = local.url.parse(request.url);
            if (local.assetsDict[request.urlParsed.pathname] !== undefined) {
                response.end(local.assetsDict[request.urlParsed.pathname]);
                return;
            }
            response.statusCode = 404;
            response.end();
        }).listen(process.env.PORT);
        break;
    }
}());
output from electron

screen-capture

output from shell

screen-capture

package.json

{
    "author": "kai zhu <kaizhu256@gmail.com>",
    "description": "this zero-dependency package will provide a persistent, in-browser database",
    "devDependencies": {
        "electron-lite": "kaizhu256/node-electron-lite#alpha",
        "utility2": "kaizhu256/node-utility2#alpha"
    },
    "engines": {
        "node": ">=4.0"
    },
    "homepage": "https://github.com/kaizhu256/node-db-lite",
    "keywords": [
        "browser",
        "db",
        "indexeddb",
        "localstorage",
        "mongo",
        "mongodb",
        "nedb",
        "no-sql",
        "nosql",
        "persist",
        "persistence",
        "persistent",
        "ttl",
        "ttl-cache",
        "web-sql",
        "websql"
    ],
    "license": "MIT",
    "main": "lib.db.js",
    "name": "db-lite",
    "nameAlias": "db",
    "nameOriginal": "db-lite",
    "os": [
        "darwin",
        "linux"
    ],
    "repository": {
        "type": "git",
        "url": "https://github.com/kaizhu256/node-db-lite.git"
    },
    "scripts": {
        "build-ci": "utility2 shRun shReadmeBuild",
        "env": "env",
        "heroku-postbuild": "npm install 'kaizhu256/node-utility2#alpha' && utility2 shRun shDeployHeroku",
        "postinstall": "if [ -f lib.db-lite.npm-scripts.sh ]; then ./lib.db-lite.npm-scripts.sh postinstall; fi",
        "publish-alias": "VERSION=$(npm info $npm_package_name version); for ALIAS in browserdb cachedb microdb nanodb netdb swaggerdb swggdb udb; do utility2 shRun shNpmPublish $ALIAS $VERSION; utility2 shRun shNpmTestPublished $ALIAS || exit $?; done",
        "start": "export PORT=${PORT:-8080} && export npm_config_mode_auto_restart=1 && utility2 shRun shIstanbulCover test.js",
        "test": "export PORT=$(utility2 shServerPortRandom) && utility2 test test.js"
    },
    "version": "2017.2.2"
}

changelog of last 50 commits

screen-capture

internal build-script

  • build.sh
# build.sh

# this shell script will run the build for this package

shBuild() {(set -e
# this function will run the main build
    # init env
    . node_modules/.bin/utility2 && shInit
    # cleanup github-gh-pages dir
    # export BUILD_GITHUB_UPLOAD_PRE_SH="rm -fr build"
    # init github-gh-pages commit-limit
    export COMMIT_LIMIT=20
    case "$CI_BRANCH" in
    alpha)
        shBuildCiDefault
        ;;
    beta)
        shBuildCiDefault
        ;;
    master)
        shBuildCiDefault
        ;;
    esac
)}

shBuildCiTestPost() {(set -e
# this function will run the post-test build
    # if running legacy-node, then return
    [ "$(node --version)" \< "v7.0" ] && return || true
    export NODE_ENV=production
    # deploy app to gh-pages
    (export MODE_BUILD=deployGithub && shDeployGithub) || return $?
    # deploy app to heroku
    (export MODE_BUILD=deployHeroku && shDeployHeroku) || return $?
)}

shBuildCiTestPre() {(set -e
# this function will run the pre-test build
    # test example.js
    (export MODE_BUILD=testExampleJs && shRunScreenCapture shReadmeTestExampleJs) || return $?
    # test published-package
    (export MODE_BUILD=npmTestPublished && shRunScreenCapture shNpmTestPublished) || return $?
)}

shBuild

Keywords

FAQs

Package last updated on 24 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc