Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

monologue

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

monologue

Streamlined MySQL query building

  • 0.2.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by83.33%
Maintainers
1
Weekly downloads
 
Created
Source

Monologue - Streamlined query building

Install

npm install monologue

Usage

This was ported from a PHP library, and uses named parameters for binding (PDO library). Some mysql packages in node support this, see their documentation for examples. This package will continue to evolve and support other methods in the future.

var monologue = require('monologue');

// Less than basic SELECT statement


// call the SQL wrappers in any order, see below: where, group, where, order
var mono = monologue().select( "*", "users")
    .where( { "id": [1,2,3,4,5,6] } ) // alternative to where("id").in([...])
    .where( 'date_time' ).between( '2012-09-12', '2013-01-20')
    .group( 'type' )
    .where( "name", "OR" ).like("ro%en") // out of order, also passing "OR" as separator
    .order( "id" )
    .limit( '300', 1000 )
    .query();

console.log( mono.sql )
// output: SELECT * FROM users WHERE id IN (:i_1,:i_2,:i_3,:i_4,:i_5,:i_6) AND username = :mono_someguy AND email = :mono_someguyexampleorg OR email = :mono_someguygmailcom AND date BETWEEN :b_20120912 AND :b_20130121 AND name LIKE :l_roen OR name LIKE :l_bb GROUP BY type ASC ORDER BY id ASC LIMIT 1000, 300

console.log( mono.params );
/* output:
    {
        b_20120912: "2012-09-12",
        b_20130121: "2013-01-20",
        i_1: 1,
        i_2: 2,
        i_3: 3,
        i_4: 4,
        i_5: 5,
        i_6: 6,
        l_roen: "ro%en"
    }
*/


// JOIN (default is left):
// SELECT * FROM users u LEFT JOIN posts p ON p.user_id = u.id WHERE category = :mono_67

monologue().select( "*", "users u" )
    .join( "posts p", "p.user_id = u.id" )
    .where( { "category": "67" } )
    .query().sql;


// JOIN (INNER, as argument):
// SELECT * FROM users u INNER JOIN posts p ON p.user_id = u.id WHERE category = :mono_67

monologue().select( "*", "users u" )
    .join( "INNER", "posts p", { "p.user_id": "u.id" } )
    .where( { "category": "67" } )
    .query().sql;


// SELECT into outfile: the third param (OPTIONALLY ENCLOSED BY) is, as stated, optional. Just pass in the line ending and leave the 4th param out, the rest will be taken care of
// output: SELECT * FROM users WHERE company = :mono_generalmotors INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

monologue().select( "*", "users" )
    .where( { "company": "general motors" } )
    .file( "/tmp/datafile", ",", '"', "\\n" )
    .query().sql;


// SELECT into outfile: without third param
// output: SELECT * FROM users WHERE company = :mono_generalmotors INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'

monologue().select( "*", "users")
    .where( { "company": "general motors" } )
    .file( "/tmp/datafile", ",", "\\n" )
    .query().sql;


// INSERT, passing an array of objects
// output: INSERT INTO users (username,password,first_name) VALUES (:mono_test,:mono_1234,:mono_me),(:mono_example,:mono_abcd,:mono_rasta)

monologue().insert( 'users', [
    { username: 'test', password: '1234', first_name: 'me' },
    { username: 'example', password: 'abcd', first_name: "rasta" }
] ).query().sql


// INSERT, passing a single object
// output: INSERT INTO users (username,password,first_name) VALUES (:mono_me,:mono_abcd,:mono_cubert)

monologue().insert( 'users', { username: 'me', password: 'abcd', first_name: "cubert" } ).query().sql


// UPDATE
// output: UPDATE users SET username = :mono_yoyo, email = :mono_kay, password = :mono_abcdefg WHERE id = :mono_23

monologue().update( "users", {username: "yoyo", email: 'some@email.com', password: "abcdefg"} ).where( {id: 23} ).query().sql


// DELETE
// output: DELETE FROM users WHERE username = :mono_test AND password = :mono_1234 AND first_name = :mono_me

monologue().delete( 'users', { username: 'test', password: '1234', first_name: "me" } ).query().sql;

Keywords

FAQs

Package last updated on 24 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc