@superhero/db

Db interaction, designed to separate queries from the source code

Usage no npm install needed!

<script type="module">
  import superheroDb from 'https://cdn.skypack.dev/@superhero/db';
</script>

README

DB

Licence: MIT


npm version

A simple DB interface implementation that reads queries from a file, segregating your code from your queries.

Install

npm install @superhero/db

...or just set the dependency in your package.json file:

{
  "dependencies":
  {
    "@superhero/db": "*"
  }
}

Example | MySql

const
connections     = 5,
host            = 'mysql.example.com',
user            = 'root',
password        = 'b4real',
filePath        = '/sql',
fileSuffix      = '.sql',
mysql           = require('mysql'),
Db              = require('@superhero/db'),
AdapterFactory  = require('@superhero/db/adapter/mysql/factory'),
adapterFactory  = new AdapterFactory(),
adapter         = adapterFactory.create(mysql, { connections, host, user, password }),
db              = new Db(adapter, filePath, fileSuffix),
result          = await db.query('file', ['context'])

The example above will create a pool with 5 idle connections and query the database with the content from the /sql/file.sql file composed with the specified context.

Example | Postgres

const
host            = 'postgres.example.com',
user            = 'root',
database        = 'stuff',
password        = 'b4real',
filePath        = '/sql',
fileSuffix      = '.sql',
pg              = require('pg'),
Db              = require('@superhero/db'),
AdapterFactory  = require('@superhero/db/adapter/postgres/factory'),
adapterFactory  = new AdapterFactory(),
adapter         = adapterFactory.create(pg, { host, user, password, database }),
db              = new Db(adapter, filePath, fileSuffix),
result          = await db.query('file', ['context'])

The example above will create a pool and query the database with the content from the /sql/file.sql file composed with the specified context.