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

will-sql

v1.0.26

Published

SQL by place holder parameter naming and value setting for execute statement on databases

Downloads

25

Readme

will-sql

SQL by place holder parameter naming and value setting for execute statement on databases

Installation

npm install will-sql

Examples

Configuration

This module require configuration(config) setting by config/default.json under project and will-util

npm install config
{
    "MYSQL" : { "alias": "mysql", "dialect": "mysql", "url": "mysql://user:password@localhost:3306/testdb?charset=utf8&connectionLimit=10", "user": "user", "password": "password" },
    "ODBC" : { "alias": "odbc", "dialect": "mysql", "url": "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=localhost;DATABASE=testdb;HOST=localhost;PORT=3306;UID=user;PWD=password;", "user": "user", "password": "password" },
    "MSSQL": { "alias": "mssql", "dialect": "mssql", "url": "Server=localhost,1433;Database=testdb;User Id=user;Password=password;Encrypt=false;Trusted_Connection=Yes;", "user": "user", "password": "password" },
    "ORACLE": { "alias": "oracle", "dialect": "oracle", "url": "localhost:1521/ORCLCDB.localdomain", "user": "user", "password": "password" },
    "POSTGRES": { "alias": "postgres", "dialect": "postgres", "url": "postgresql://user:password@localhost:5432/testdb", "user": "user", "password": "password" },
    "INFORMIX": { "alias": "odbc", "dialect": "informix", "url": "DRIVER={IBM INFORMIX ODBC DRIVER (64-bit)};SERVER=online_localhost;DATABASE=refdb;HOST=localhost;SERVICE=9088;UID=user;PWD=password;CLIENT_LOCALE=th_th.thai620;DB_LOCALE=th_th.thai620;", "user": "user", "password":"password" },
    "SQLITE" : { "alias": "sqlite", "dialect": "sqlite", "url": ":memory:", "user": "", "password": "" },
    "MYSQL2" : { "alias": "mysql2", "dialect": "mysql", "url": "", "user": "user", "password": "password", "host": "localhost", "port": 3306, "database": "testdb", "options": { "charset": "utf8", "connectionLimit": 10 } },
}
npm install will-util

Queries

Since mysql, mssql, odbc, oracle, postgres node module using difference place holder for parameter naming and value setting, like mysql and odbc using ? sign, mssql using @ sign and oracledb using : sign, and postgres using $ sign for naming parameters

KnSQL

KnSQL wrap up query statement using only place holder ? sign as parameter naming and value setting

java script
const connector = require("will-sql");

async function testdb() {
    let knsql = new connector.KnSQL();
    knsql.append("select * from testdbx where share = ?share ");
    knsql.set("share","BBL");
    const db = connector.getDBConnector("MYSQL");
    let rs = await knsql.executeQuery(db);
    console.log("rs",rs);
    db.close();
}
type script
import { KnSQL, KnDBConnections } from "will-sql";

async function testQuery() {
    let knsql = new KnSQL();
    knsql.append("select * from testdbx where share = ?share ");
    knsql.set("share","BBL");
    const db = KnDBConnections.getDBConnector("MYSQL");
    let rs = await knsql.executeQuery(db);
    console.log("rs",rs);
    db.close();
}

Transaction

import { KnSQL, KnDBConnections } from "will-sql";

async function testTransaction() {
    let knsql = new KnSQL();
    knsql.append("update testdbx set percent = ?percent where mktid = ?mktid ");
    knsql.set("percent",60);
    knsql.set("mktid","TST");
    const db = KnDBConnections.getDBConnector("MYSQL");
    try {
        await db.beginWork();
        let rs = await knsql.executeUpdate(db);
        console.log("update",rs);
        await db.commitWork();
    } catch(ex) {
        await db.rollbackWork();
    }
    db.close();
}

Database Connector

In order to get database connection KnDBConnections.getDBConnector or getDBConnector method can specified by configuration section or configuration setting to establish

configuration section

    const db = KnDBConnections.getDBConnector("MYSQL");

For example "MYSQL" point to section in config/default.json

configuration setting

    const db = KnDBConnections.getDBConnector({
        schema: "MYSQL", 
        alias: "mysql", 
        dialect: "mysql", 
        url: "mysql://user:password@localhost:3306/testdb?charset=utf8&connectionLimit=10", 
        user: "user", 
        password: "password"
    });

Multiple pool supported by section or schema setting so it can defined in difference way

Database Adapter

Database adapter now support for mysql, mssql, odbc, oracle and postgres alias setting. When using database connector instance it can send raw query statement depending on database module

mysql

npm install mysql
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("MYSQL");
    let rs = await db.executeQuery("select * from testdbx where percent > ? ",{ 
        percent: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs",rs);
    db.close();
}

mysql2

npm install mysql2
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("MYSQL2");
    let rs = await db.executeQuery("select * from testdbx where percent > ? ",{ 
        percent: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs",rs);
    db.close();
}

odbc

npm install odbc
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("ODBC");
    let rs = await db.executeQuery("select * from testdbx where percent > ? ",{ 
        percent: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs2",rs);
    db.close();
}

mssql

npm install mssql
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("MSSQL");
    console.log("db",db);
    let rs = await db.executeQuery("select * from testdbx where percentage > @percentage ",{ 
        percentage: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs",rs);
    db.close();
}

oracledb

npm install oracledb
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("ORACLE");
    let rs = await db.executeQuery("select * from testdbx where percentage > :percentage ",{ 
        percentage: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs",rs);
    db.close();
}

postgres

npm install pg
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("POSTGRES");
    let rs = await db.executeQuery("select * from testdbx where percentage > $1 ",{ 
        percentage: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs",rs);
    db.close();
}

sqlite

npm install sqlite3
import { KnDBConnections } from "will-sql";

async function testdb() {
    const db = KnDBConnections.getDBConnector("SQLITE");

    await db.executeUpdate("create table testdbx(share text, mktid text, yield numeric, percent numeric)");
    await db.executeUpdate("insert into testdbx(share,mktid,yield,percent) values('BBL','TEST',100.50,25.50)");
    await db.executeUpdate("insert into testdbx(share,mktid,yield,percent) values('SCB','TEST',200.50,55.50)");

    let rs = await db.executeQuery("select * from testdbx");
    console.log("rs",rs);

    rs = await db.executeQuery("select * from testdbx where percent > ? ",{ 
        percent: {value: 50, type: "DECIMAL"} 
    });
    console.log("rs2",rs);
    db.close();
}

Connection Pool

Database adapter has connector via connection pool then after used, all connection pools must be closed (or else it do not exit to commamd prompt when running as stand alone application)

import { KnSQL, KnDBConnections } from "will-sql";

async function testQuery() {
    let knsql = new KnSQL();
    knsql.append("select * from testdbx where share = ?share ");
    knsql.set("share","BBL");
    const db = KnDBConnections.getDBConnector("MYSQL");
    let rs = await knsql.executeQuery(db);
    console.log("rs",rs);
    db.close(); //release connection to pool
    db.end(); //close connection pool
}