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

calustra-conn

v0.3.0

Published

A database connector

Downloads

14

Readme

Calustra logo

NPM Version NPM Downloads

Intro

calustra-conn is a database connector.

Currently, supported databases are:

We may add support for other databases (MySql, MsSql, ...) in the future... or may not.

Install

npm install calustra [--save-dev]

Getting Started

calustra-conn exposes just the method getConnection which returns a Connection object.

import {getConnection} from 'calustra-conn'

// Init connection
const config= {
  dialect:  'postgres',
  host:     'localhost',
  port:     5432,
  database: 'calustra',
  user:     'postgres',
  password: 'postgres'
}
const options= {
  log: 'debug'
}
const conn = getConnection(config, options)

// create a table

const q_drop = `DROP TABLE IF EXISTS screw_stock`
await conn.execute(q_drop)

const q_create = `
  CREATE TABLE screw_stock (
    id           serial,
    screw_type   TEXT NOT NULL,
    stock        INT
  )`
await conn.execute(q_create)

// fill table
const data = [
  ['Wood Screws', 1034],
  ['Machine Screws', 3545],
  ['Thread Cutting Machine Screws', 466],
  ['Sheet Metal Screws', 6436],
  ['Self Drilling Screws', 265],
  ['Hex Bolts', 3653],
  ['Carriage Bolts', 63],
  ['Tag Bolts', 3573]
]

const q_insert = 'INSERT INTO screw_stock (screw_type, stock) VALUES ($1, $2)
for (const d of data) {
  await conn.execute(q_insert, d)
}

// select many records
const q_select = 'SELECT * FROM screw_stock'
const screws = await conn.select(q_select)


// select one record
const q_select_one = 'SELECT * FROM screw_stock WHERE screw_type = $1'
const hex_bolts = await conn.selectOne(q_select_one, ['Hex Bolts'])

// clean some records
const q_del = 'DELETE FROM screw_stock WHERE stock >= $1'
const del_records = await conn.executeAndCount(q_del, [1000])

Cached connections

Notice that calustra-conn keeps a simple cache of connections once they are initialized. You can get them using getConnection(selector):

import {getConnection} from 'calustra-conn'
const conn = getConnection(`calustra`)

where selector is just a string matching some part of the config you passed the first time to init the connection.

You can uncache a connection:

conn.uncache()

Closing it also uncaches it, but closing connections must be done carefully:

conn.close()

You can disable caching of a connection by specifying the option nocache: true:

const conn = getConnection(config, {nocache: true})
// connection will not be available trough getConnection(selector)

API

getConnection(configOrSelector, options)

Initializes and returns a connection object.

Connections are cached. The first time you init the connection, you have to pass a config object. But for further usages of the connection, you can take the cached connection just by passing a selector.

config

Contains the connection parameters (which depend on the database).

For PostgreSQL:

  config= {
    dialect:  'postgres',
    host:     'localhost',
    port:     5432,
    database: 'calustra',
    user:     'postgres',
    password: 'postgres',
    // Maximum/Minimum number of connection in pool
    max: 5,
    min: 0,
    // The maximum time, in milliseconds, that a connection can be idle before being released. 
    // Use with combination of evict for proper working, for more details read 
    // https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870
    idleTimeoutMillis: 10000,
    allowExitOnIdle: true
  }

For SQLite:

  config={
    dialect:  'sqlite',
    filename: ':memory:',

    verbose:   true,
    
    // https://github.com/mapbox/node-sqlite3/wiki/Caching
    cached:    true,
    
    /*user:     'sqlite',
    password: 'sqlite'*/

    trace: undefined,
    profile: undefined,
    busyTimeout: undefined
  }

selector

It will be matched against the config object you had initialized the connection with.

Given this, the most easy thing to do is to just specify the database as the selector if using PostgreSQL, or the filename if using SQLite. But that's up to you!

options

  • log: can be a string with the log level (silly, debug, info, warn, error) or a class exposing methods named as those log levels.
  • nocache: if true, connections are not cached. Default is false.

Some examples:

  options= {    
    log: 'debug',
    nocache: true
  }
class CustomLogger {
  _log(l, s) {
    console.log(`[${l}] ${s}`)
  }
  
  silly(s) { this.log('silly', s) }
  debug(s) { this.log('debug', s) }
  info(s)  { this.log('info', s) }
  warn(s)  { this.log('warn', s) }
  error(s) { this.log('error', s) }
}

const options= {    
    log: CustomLogger
}

Connection object

async connection.select(query, values, options)

  • query: string with SQL query. It may contain wildcards ($1, $2...) or (?, ?...).
  • values: array of values if query contains wildcards
  • options:
    • transaction
    • log: if false, logging is disabled for this particular call

Returns an array of objects with the result of the query.

async connection.selectOne(query, values, options)

  • query: string with SQL query. It may contain wildcards ($1, $2...) or (?, ?...).
  • values: array of values if query contains wildcards
  • options:
    • transaction
    • log: if false, logging is disabled for this particular call
    • omitWarning: by default, if query returns more than one record, a logging warning is shown. If omitWarning is true, this warning is ignored.

Returns an object with the result of the query.

async connection.execute(query, values, options)

  • query: string with SQL query. It may contain wildcards ($1, $2...) or (?, ?...).
  • values: array of values if query contains wildcards
  • options:
    • transaction
    • log: if false, logging is disabled for this particular call

Returns an array of objects with the result of the query.

async connection.executeAndCount(query, values, options)

  • query: string with SQL query. It may contain wildcards ($1, $2...) or (?, ?...).
  • values: array of values if query contains wildcards
  • options:
    • transaction
    • log: if false, logging is disabled for this particular call

Returns an integer with the number of rows affected by the query.

async connection.getTableNames(schema= 'public')

Returns an array with the table names present in the specified database schema:

Notice the results of this method will be in-memory cached: so query runs just once per connection.

async connection.getTableDetails(tableName, schema= 'public')

Returns an object with the details of a database table definition, like:

{
  'field_name': {
      type     : <type identifier>,
      key      : <bool>,
      nullable : <bool>,
      default  : <default value>   
  },...
}

Notice the results of this method will be in-memory cached: so query runs just once per connection and table.

Notes

Closing connections

If you close a connection:

connection.close()

notice that the database's pool will be removed, being no longer available. Even if recreating the Connection object you will get errors.

So, use it with care!