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

nf-sql-db

v1.1.0

Published

**NilFactor SQL DB**

Downloads

5

Readme

NilFactor SQL DB

NF-SQL-DB is a framework for dealing with postgresql databases in NodeJS. Allows for easy creation of modules for dealing with interfacing with postgres. Built using pg node package.

This is just a simply lightweight tool with limited frills. If feature requests are made I may add them. I wrote this for simple uses and didn't need joins and all that. I just wanted a simply way to create objects that would save with out a bunch of work.

Updated: 2018-06-16

  • Fixed issue with updating entries

Configuration - Set Environment Variables

$ PGUSER=dbuser \
  PGHOST=database.server.com \
  PGPASSWORD=secretpassword \
  PGDATABASE=mydb \
  PGPORT=3211 \
  node script.js

Example - Module

// Example Module
const db = require('nf-sql-db');

module.exports = () => {
    var table = {
        tableName: 'users',
        primaryKey: 'user_id',
        columns: [
            'user_id',
            'username',
            'password',
            'email',
            'first_name',
            'last_name',
            'active',
            'last_login',
            'date_created',
            'reset_hash',
            'reset_hash_experation'
        ],
        type: {
            'user_id': 'integer',
            'username': 'text',
            'password': 'hash',
            'email': 'text',
            'first_name': 'text',
            'last_name': 'text',
            'active': 'boolean',
            'last_login': 'timestamp',
            'date_created': 'timestamp',
            'reset_hash': 'hash',
            'reset_hash_experation': 'timestamp'
        },
        delete: () => {
            return db.delete(table);
        },
        save: (object) => {
            if (typeof object == 'undefined') {
                return db.save(table);
            } else {
                return db.save(object);
            }
        },
        findById: (id) => {
            return db.findById(table, id);
        },
        find: (params, options) => {
            return db.find(table, params, options);
        },
        original: {
            user_id: null,
            username: null,
            password: null,
            email: null,
            first_name: null,
            last_name: null,
            active: null,
            last_login: null,
            date_created: null,
            reset_hash: null,
            reset_hash_experation: null,
        },
        user_id: null,
        username: null,
        password: null,
        email: null,
        first_name: null,
        last_name: null,
        active: null,
        last_login: null,
        date_created: null,
        reset_hash: null,
        reset_hash_experation: null,
    };

    return table;
};

Example - Using Module

var user = require('./example_table');

user.connect();
user.username = 'richard';
user.password = 'test';
user.email = '[email protected]';
user.first_name = 'Richard';
user.last_name = 'Williamson';
user.active = true;
user.last_login = null;
user.date_created = new Date();

// save your user
user.save()
    .then((user) => {
        console.log(user);
    })
    .catch((error) => {
        console.log("ERROR: ", error);
        user.close();
    });

Functions to Note

  1. connect() - connects to the postgres db
  2. close() - closes connection to postgres db
  3. save(object) - inserts/updates object given, where object is a module like example above
  4. findByID(object, id) - finds the given object type in the database by primary key id, where object is a module like example above
  5. find(object, params, options (optional)) - find object(s) based on given params and options where object is a module like example above params are like so {active: true, "email IS NOT NULL": null} options are optional and can be like the example below
  6. delete(object) - deletes given object from database

options to pass in for find

  1. array columns - array of strings with column names to select
  2. array sort - array of strings with column names to sort on
var options = {
    columns: ['user_id', 'user_name', 'email'],
    sort: ['user_name', 'email']
}