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

nodesql-db

v0.0.15

Published

Node.js for Postgres, Mysql, Memsql

Downloads

17

Readme

INSTALL:

$ npm install nodesql-db

INITIALIZE: app.js ->

 var db = require('nodesql-db')

use a connection string:

# using coded connection string
$  node app.js

app.js ->

    var password =  ''; // or 'my_password_is_aight'
    var connStr = "postgresql://postgres:"+password+"@127.0.0.1:5432/mydb";
    // or var connStr = "mysql://root:"+password+"@127.0.0.1:3306/mydb";
    var mydb = new db(connStr);



/* using a env variables */
    var mydb = new db(); // init function looks for envs by default, then connection string.

SCHEMA TIPS FOR EASIER BETTER DML COMPATIBILITY:

create table <tableName> (
    <tableName>_id SERIAL PRIMARY KEY,
    ...
);
/* replace <tableName> with your table */
Insert, Update, Upsert, Delete Return the primary key of the table

Insert

INSERT INTO user (fname,lname) VALUES ('joe','johns');
mydb.user.insert().values({ fname: 'joe', lname: 'johns' }).run(console.log);

Upsert

/* UPDATE, IF NOT EXISTS, INSERT, IF DUPE then SELECT and RETURN PRIMARY KEY ID. */
mydb.user.util().upsertUsingColumnValues({ fname: 'joe', lname: 'johns' },{ fname: 'joe', lname: 'johns' },console.log);

Select

psql Select Example #1->

mydb# SELECT * FROM user;

Javascript Select Example #1->

mydb.user.selectAll().run(function(err,data){
    if(err) throw err;
    console.log(data);  
});

psql Select Example #2->

SELECT phone_id FROM user
JOIN phone ON phone.phone_id = user.phone_id
WHERE user.user_id = 1
GROUP BY user.user_id, user.time_created
ORDER BY user.time_created DESC
OFFSET 2
LIMIT 5;

Join , Order By , Group By, Limit , Offset

Javascript Select Example #2->

mydb.user.select(['phone_id'])
    .join({ phone: { on: [{"phone.phone_id":"user.phone_id"}] } })
    .where({ user_id: 1 })
    .groupBy("user.user_id, user.time_created")
    .orderBy("user.time_created DESC") // "col1 dir1, col2 dir2,...,etc"
    .offset(2)
    .limit('all') // 1 , 2, 100, 'all'
    .run()
    .then(function(data){
        console.log(data)
    })
    .fail(function(err){
        console.error(err)
    })
    .done()

Update

psql ->
    UPDATE user SET fname = 'joe', lname = 'johns' WHERE fname = 'joes';
js ->
    mydb.user.update().set({ fname: 'joe', lname: 'johns' }).where({ fname: 'joes' }).run(console.log)

Delete

psql ->
    DELETE FROM user WHERE true;
js ->
    mydb.user.deleteFrom().where("true").run(console.log); // oh no! nukes user table.

Raw PSQL

mydb.user.rawsql("Select * from user order by random() limit 1").run(console.log);