1.0.1 • Published 4 years ago

mjsql v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

MyJSQL

=============

A library to simplify the usage of mysql module by chainning differents methods to construct a query and then execute it.

We provide the CRUD utils functions: SELECT, INSERT, UPDATE and DELETE.

Installation

To use with node:

$ npm install --save myjsql

Usage

...
const myjsql = require('myjsql');
...

Connecting to MySQL Database

...
myjsql.connect({    // Return object same as `createConnection` 
                    // of the module `mysql`
                    // You can work with as you are working `mysql`
                    // Same as `createConnection`
        "host"      :   "localhost", 
        "user"      :   "root", 
        "password"  :   "root", 
        "database"  :   "randomDB"
    });
...

SELECT Query

...
let selectFromQuery = myjsql.Query
    .select('colA, colB')       // String of cols with `,` between each col
 // .select(['colA', 'colB'])   // Or an Array that contains different cols
    .from('tableA')             // Table as string (Only string is supported)
    .where()
        .attr('age')
            .gte(25)
    .and()
        .attr('name')
            .like('%somename')
    .and()
        .attr("something")
            .equal()            // If nothing given, it replace it by '?'
    .exec(["18"]);              // Data given in order to replace the '?' value
                                // in the same order of the 'attr'
...

INSERT Query

...
let insertIntoQuery = myjsql.Query
    .insert('tableA')                   // Table as string (Only string is supported)
        .cols('colA, colB, colC')       // String of cols with `,` between each col
//      .cols(['colA', 'colB', 'colC']) // Or an Array that contains different cols
        .values([                       // Array of string
            '\'Value1\', \'Something\', 25', 
            '\'Value2\', \'Something\', 25', 
            '\'Value3\', \'Something\', 25', 
        ])
        .exec();
...

UPDATE Query

...
let updateQuery = myjsql.Query
    .update('tableA')                           // Table as string (Only string is supported)
        .set('onlyOneColName', 'theNewValue')   // Set the col and its value
                                                // If a string given in 1st arg, 
                                                // then the second arg its value is '?'
//      .set([                                  // If a lot values must be changed,
//          { column: 'colA', value: 'NewValue' }
//          { column: 'colB', value: 25 }
//          { column: 'colC', value: true }
//      ])                                      // you can give like this
        .where()                                // You can also chain it with where
            .attr('colId')
                .equal(25)
        .exec();
...

DELETE Query

...
let deleteFromQuery = myjsql.Query
    .delete('tableName')                        // Table as string (Only string is supported)
    .exec();
// Or
deleteFromQuery = myjsql.Query
    .delete()
        .from('tableName')
    .exec();
...

From Query To String

...
// If you want to get the query string
let query = myjsql.Query
    .select("*")
        .from("tableName");
query.toString();   // 'SELECT * FROM tableName'
...

Create an Query Object from my own query String

...
// If you have your own query string
let ownQuery = myjsql.Query.of("...");

Security issues

Security issues should be reported through GitHub by opening a GitHub issue simply asking or by emailing the module's author/contributors.

An ideal report would include a clear indication of what the security issue is and how it would be exploited.

Contributing

This project welcomes contributions from the community. Contributions are accepted using GitHub pull requests. If you're not familiar with making GitHub pull requests, please refer to the GitHub documentation "Creating a pull request".

1.0.1

4 years ago

1.0.0

4 years ago