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

sqlm

v0.0.2

Published

Modelize SQL queries with key to parameter bindings

Downloads

4

Readme

sqlm

Modelize SQL queries with key to parameter bindings

install

$ npm install sqlm

usage

var sqlm = require('sqlm')
var User = sqlm(function (query, params, done) {
  // query database
});

example

var sqlm = require('sqlm')
  , fs = require('fs')
  , pg = require('pg')

pg.connect({database: 'db', function (err, db, close) {
  if (err) { throw err; }
  var User = sqlm(function (query, params, done) {
    if (query && params && done) {
      db.query(query, params, done);
    } else if (query && params) {
      db.query(query, params);
    }
  });

  // salt passwords each time they are passed to
  // a model method
  User.use('password', function (value) {
    var md5 = Hash('md5');
    md5.write(value);
    md5.end();
    return md5.read().toString('hex');
  });

  User.bind('create', {
    username: 1, email: 2, password: 3
  }, fs.readFileSync('/path/to/users/create.sql'));

  User.create({
    username: 'werle' email: '[email protected]',
    password: 'foo' // becomes 'acbd18db4cc2f85cedef654fccc4a4d8'
  }, function (err, res) {
    if (err) { throw err; }
    console.log(res.rows); // from pg query above
  });
});

api

Model

function Model (query) { ... }
  • Function query - A function called when attempting to query the database

#exec

Model.prototype.exec = function (sql, params, done) { ... }

Executes a query ignoring filters or usage of a `Binding'

  • String sql - SQL query
  • Array params - Query Paramaters
  • Function done - Callback called after querying database

#bind

Model.prototype.bind = function (name, map, sql) { ... }

Creates a binding for a model and attaches as a method to instance

  • String name - Binding name attached as a method to Model instance
  • object map - key to numbered query paramaters map
  • string sql - raw parameterized sql query

#use

Model.prototype.use = function (name, fn) { ... }

Installs plugin for property found in binding maps

  • String name - Property name to install plugin for
  • Function fn - Plugin routine

#filter

Model.prototype.filter = function (o) { ... }

Applies set filters to an object

  • Object} o - Object to apply filters to

Binding

function Binding (map, sql, query) { ... }
  • object map - key to numbered query paramaters map
  • string sql - raw parameterized sql query
  • Function query - A function called when attempting to query the database

#_query

_query() interface method that must be implemented.

#query

Binding.prototype.query = function (data, fn) { ... }

Query with underlying query function and data bound to params

  • Object data - optional Data that will be paramterized in the order in which they were defined in the map
  • Function fn - Callback function passed to the _query function

#params

Binding.prototype.params = function (data) { ... }

Builds SQL parameters

  • Object data - Data to paramertize in an array defiend by the map

license

MIT