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

@live2ride/db

v1.1.2

Published

simple mssql operations

Downloads

1

Readme

Description

Simple way to interact with MS SQL Server(MSSQL)

Config

const dbConfig = {
  database: "master",
  user: "demo user",
  password: "demo password",
  server: 192.168.0.1,
};
const db = new DB(dbConfig);
Alternatively you can set variables in your env file
.env {
  DB_DATABASE=my-database-name
  DB_USER=demo-user
  DB_PASSWORD=demo-password
  DB_SERVER=server-name
}
const db = new DB();

Other config options

tranHeader: transaction header. string added before each query. responseHeaders: array of headers added to response when using db.send errors:

  • print: prints errors in console with statement prepared for testing. true in development
example:
const dbConfig = {
  tranHeader: "set nocount on;",
  errors:{
    print: true,
    includeInResponse: true,
  },
  responseHeaders: [
      ["Access-Control-Allow-Origin", "*"],
      ["Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE"],
  ];
};

Usage

exec: executes query with parameters and returns results;

await db.exec(
  query: any sql statement
  parameters: (json object) ,
  first_row_only: default false
    * true returns json object
    * false returns array of objects
)

Examples

Create table

let qry = `
    create table dbo.test (
        id int identity, 
        num int, 
        text nvarchar(100), 
        obj nvarchar(300)
    )
`;
await db.exec(qry);

Insert data into table

let qry = `
    insert into dbo.test (text) 
    select @_text
`;
let params = {
  text: "keys are converted into paramters (@_ + key)",
};

await db.exec(qry, params);

let qry = `
    insert into dbo.test (num, text, obj) 
    select @_num, @_text, @_obj 
`;
let params = {
  num: 123,
  text: "add '@_' for each key you want to use in your query ",
  obj: {
    message: "im an object",
  },
};
await db.exec(qry, params);

Select from table

results are always an array of records in json format.
let qry = "select * from dbo.test where id = @_id";
let params = { id: 1 };

await db.exec(qry, params);
console.log(res);

results: [
  {
    id: 1,
    num: undefined,
    text: "add '@_' for each key you want to use in your query ",
    obj: undefined,
  },
  {
    id: 2,
    num: 123,
    text: "add '@_' for each key you want to use in your query ",
    obj: { message: "im an object" },
  },
];

Select first row
const first_row_only = true
let res = await db.exec("select * from dbo.test", null, first_row_only);
console.log(res);

result:
 {
    id: 1,
    num: undefined,
    text: "add '@_' for each key you want to use in your query ",
    obj: undefined
  },

Using with express?

const express = require("express");
const asyncHandler = require("your/asyncHandler");
const router = express.Router();


    router.get("/pow",
      asyncHandler(async (req, res) => {
          let qry = `select name from dbo.table where id = @_id`
          let params = { id = 100 }

          db.send(req, res, qry, params);
        })
  );

db.send sends the data back to client

Troubleshooting

Print errors

const config = {
  printErrors: true,
};
const db = new DB(config);
let qry = `select top 2 hello, world from dbo.testTable`;
let params = {
  par: "parameter value",
};
db.exec(qry, params);
result:
****************** MSSQL ERROR start ******************
--------  (db:dev): Invalid object name 'dbo.testTable'.  --------
declare
@_par NVarChar(37) = 'parameter value'

select top 2 hello, world from dbo.testTable
****************** MSSQL ERROR end ******************

db.printParams

const params = {
    num: 123,
    text: "add '@_' for each key you want to use in your query ",
    obj: {
      message: "im an object",
    },
  };

  db.printParams(params);

  prints to console:
  declare
     @_num int = 123
    , @_text NVarChar(74) = 'add '@_' for each key you want to use in your query '
    , @_obj NVarChar(max) = '{"message":"im an object"}'