npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

database-js-multiple

v1.0.0

Published

Database-js driver that pulls tables from multiple backends

Downloads

3

Readme

database-js-multiple

Database-js driver that pulls tables from multiple backends

About

Database-js-multiple creates a virtual database from multiple data sources. You can write SQL queries that perform joins of data from a MySQL table and an SQLite table. You can construct queries that join data from Firebase, PostgreSQL, and Excel.

Limitations

  • The virtual database implementation is in memory and implemented in Javascript. This is not going to work well on large datasets. Use the SQL definitions of the views to reduce the datasets.
  • The views are read-only. However, you have access to the underlying connections where writes can be performed.

Installation

# npm install -s database-js-multiple
# npm install -s database-js-... [other drivers]

Usage

Database-js-multiple is a driver for database-js, but because of the ability to interact with multiple databases the usage is slightly different.

This example performs a join of two tables from different databases. This can work with any database driver supported by database-js.

const MultipleDatabase = require("database-js-multiple");

(async function() {
    var multdb = new MultipleDatabase();
    multdb.add("states", "sqlite:///test.sqlite", "SELECT * FROM states");
    multdb.add("abbr", "localstorage:///tests", "SELECT * FROM abbr");

    var conn = multdb.connection;
    var statement = conn.prepareStatement("SELECT abbr.Abbr, states.Ranking, states.Population FROM states JOIN abbr ON states.State = abbr.State");

    let rows = await statement.query();

    console.log(rows);

    await conn.close();
})();

Each view can be given an unlimited number of parameters:

multdb.add("states", "sqlite:///test.sqlite", "SELECT * FROM states WHERE Ranking < ?", 10);

Later on those parameters can be changed:

multdb.view("states").setParameters(5);

If necessary, the view SQL can be changed:

multdb.view("states").sql = "SELECT * FROM states WHERE State LIKE ?";

The connection object for each view can also be accessed:

var sqliteConnection = multdb.view("states").connection;
...

Share Connections

It is best practice to share the connection for views on the same database. If you want to use multiple tables from one database and multiple from a second database, reuse the connection object for each one.

You can create the connection objects seperately:

var connPG = new Connection('postgres://user:password@pghost/db1');
var connMS = new Connection('mysql://user:password@mshost/db2');
var multdb = new MultipleDatabase();

multdb.add('pgTable1', connPG, 'SELECT ...');
multdb.add('pgTable2', connPG, 'SELECT ...');
multdb.add('msTable1', connMS, 'SELECT ...');
multdb.add('msTable2', connMS, 'SELECT ...');

Or you can let the database-js-multiple object handle it for you:

var multdb = new MultipleDatabase();
multdb.add('pgTable1', 'postgres://user:password@pghost/db1', 'SELECT ...');
multdb.add('pgTable2', multdb.view('pgTable1').connection, 'SELECT ...');
multdb.add('msTable1', 'mysql://user:password@mshost/db2', 'SELECT ...');
multdb.add('msTable2', multdb.view('msTable1').connection, 'SELECT ...');