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

sequelize-raw-query

v1.1.0

Published

Sequelize-raw-query (sequery) executes raw sql statements, supports where, order and limit objects which sequelize.query() does not. Sequery provides templateData which is more friendly than replacements.

Downloads

103

Readme

Sequelize raw query

Sequelize-raw-query (sequery) executes raw sql statements, supports where, order and limit objects which sequelize.query() does not. Sequery provides templateData which is more friendly than replacements, the difference is:

* Replacements
The parameter form ":xxx" is used to specify the field value, for example "id = :id".

* TemplateData
The parameter form "{xxx}" is used to construct sql statements such as "where id > {id} and {dateCondition}".

TemplateData contains the functionality of replacements.
This is to avoid using both the replacements and templateData.

Sequery supports mssql and mysql. Sequery depends on sequelize 5.x.

Installation

npm i sequelize-raw-query --save

Test

The below usages are come from test cases, run test first to learn more.

git clone https://github.com/hiowenluke/sequelize-raw-query
cd sequelize-raw-query
npm install
npm test

Usage

Initialize sequery first:

const sequery = require('sequelize-raw-query');

// See "Config" section at the last to learn more
const config = {
    dialect: 'mysql', // mysql or mssql
    database: 'sys',
    username: 'root', 
    password: 'playboy',
    host: '127.0.0.1', // your db host
    port: 3306,
};

sequery.init(config);

Then simulate a table for demo (you don't need to do this in your project):

const table = `(
    select 1 as id, '2019-02-01' as date 
    union 
    select 2, '2019-03-10' 
    union 
    select 3, '2019-10-16'
)`;

Then try the below usages.

.exec() or .do(), the alias of .exec demo

async function () {
    const sql = `select * from ${table} m`;
    const result = await sequery.exec(sql);
    console.log(result[0].id ) // 1
}
async function () {
    const sql = `select * from ${table} m where id = :id`;
    const replacements = {id: 2};
    const result = await sequery.exec(sql, {replacements});
    console.log(result[0].id) // 2
}
async function () {
    const sql = `select * from ${table} m where id > :id`;
    const result = await sequery.exec(sql, {id: 1});
    console.log(result.length) // 2`
}
async function () {
    const sql = `select * from ${table} m where id = {id}`;
    const templateData = {id: 2};
    const result = await sequery.exec(sql, {templateData});
    console.log(result[0].id) // 2
}
async function () {
    const sql = `select * from ${table} m where id > {id}`;
    const result = await sequery.exec(sql, {id: 1});
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where {condition}`;
    const result = await sequery.exec(sql, {condition: 'id > 1 and 1 > 0'});
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where {idCondition} and {dateCondition}`;
    const result = await sequery.exec(sql, {idCondition: 'id > 1 and 1 > 0', dateCondition: "date >= '2019-03-10'"});
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where {condition} and id > :id`;
    const replacements = {id: 2};
    const templateData = {condition: 'id > 1'};
    const result = await sequery.exec(sql, {replacements, templateData});
    console.log(result[0].id) // 3
}
async function () {
    const sql = `select * from {table} m where id > {id}`;
    const result = await sequery.exec(sql, {id: 1, table});
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where {condition} and id > :id`;
    const beforeExec = ({sql}) => {
        sql = 'select 1 as id';
        return {sql};
    };

    const result = await sequery.exec(sql, {beforeExec});
    console.log(result[0].id) // 1
}
async function () {
    const sql = `select * from ${table} m where {condition} and id > :id`;
    const beforeExec = ({sql}) => {
        sql = 'select 1 as id';
        return {sql};
    };

    const result = await sequery.exec(sql, beforeExec);
    console.log(result[0].id) // 1
}
async function () {
    const sql = `select * from ${table} m`;
    const beforeExec = () => {
        return undefined;
    };

    const result = await sequery.exec(sql, beforeExec);
    console.log(result.length) // 3
}
async function () {
    const sql = `select * from ${table} m where id > :id`;
    const afterExec = (result) => {
        result.push(5);
    };

    const result = await sequery.exec(sql, {id: 2}, {afterExec});
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where id > :id`;
    const afterExec = (result) => {
        result.push(5);
    };

    const result = await sequery.exec(sql, {id: 2}, afterExec);
    console.log(result.length) // 2
}
async function () {
    const sql = `select * from ${table} m where id > :id`;
    const afterExec = (result) => {
        result = [];
        return result;
    };

    const result = await sequery.exec(sql, {id: 2}, afterExec);
    console.log(result.length) // 0
}
async function () {
    const sql = `select * from ${table} m where id > :id`;
    const afterExec = (result) => {
        result = [];
        return result;
    };

    const result = await sequery.exec(sql, {id: 2}, afterExec);
    console.log(result.length) // 0
}
async function () {
    const sql = `
        delimiter $$
        drop function if exists fn_sequelize_raw_query $$
        create function fn_sequelize_raw_query(i int) returns int deterministic
        begin
            declare i_return int;
            set i_return = i + 1;
            return i_return;
        end;
        $$
        delimiter ;
        select fn_sequelize_raw_query(1) as result;
    `;

    const result = await sequery.exec(sql);
    console.log(result[0].result) // 2
}

.getWhereConditions(where) demo

async function () {
    const where = {"id": 2};
    const whereStr = sequery.getWhereConditions(where);
    console.log(whereStr) // '`id` = 2'
}
async function () {
    const where = {"id": 2};
    const tableAs = 'm';
    const whereStr = sequery.getWhereConditions(where, tableAs);
    console.log(whereStr) // '`m`.`id` = 2'
}
async function () {
    const where = '{"id": 2}';
    const whereStr = sequery.getWhereConditions(where);
    console.log(whereStr) // '`id` = 2'
}
async function () {
    const where = {"id": {$gt: 2}};
    const whereStr = sequery.getWhereConditions(where);
    console.log(whereStr) // '`id` > 2'
}
async function () {
    const Op = sequery.Sequelize.Op;
    const where = {
        id: {
            [Op.or]: {
                [Op.lt]: 1000,
                [Op.eq]: null
            }
        }
    };

    const whereStr = sequery.getWhereConditions(where);
    console.log(whereStr) // '(`id` < 1000 OR `id` IS NULL)'
}

.getOrderClause(order) demo

async function () {
    const order = 'id';
    const orderStr = sequery.getOrderClause(order);
    console.log(orderStr) // ' order by `id`'
}
async function () {
    const order = 'type, name desc';
    const orderStr = sequery.getOrderClause(order);
    console.log(orderStr) // ' order by `type`, `name` desc'
}
async function () {
    const order = ['type', 'name desc'];
    const orderStr = sequery.getOrderClause(order);
    console.log(orderStr) // ' order by `type`, `name` desc'
}
async function () {
    const order = [['type'], ['name', 'desc']];
    const orderStr = sequery.getOrderClause(order);
    console.log(orderStr) // ' order by `type`, `name` desc'
}
async function () {
    const order = ['type', 'name desc'];
    const tableAs = 'm';
    const orderStr = sequery.getOrderClause(order, tableAs);
    console.log(orderStr) // ' order by `m`.`type`, `m`.`name` desc'
}

.getGroupClause(group) demo

async function () {
    const group = 'id';
    const groupStr = sequery.getGroupClause(group);
    console.log(groupStr) // ' group by `id`'
}

.getLimitClause(options) demo

async function () {
    const options = {order: 'id', limit: 10, offset: 5};
    const limitStr = sequery.getLimitClause(options);
    console.log(limitStr) // ' order by `id` limit 5, 10'
}
async function () {
    const options = {order: 'id', tableAs: 'm', limit: 10, offset: 5};
    const tableAs = 'm';
    const limitStr = sequery.getLimitClause(options, tableAs);
    console.log(limitStr) // ' order by `m`.`id` limit 5, 10'
}

Config

Base configuration

// For mssql
const config = {
    dialect: 'mssql',
    database: 'master',
    username: 'sa',
    password: 'playboy',
    host: '192.168.197.80',
    port: 1433,
};

// For mysql
const config = {
    dialect: 'mysql',
    database: 'sys',
    username: 'root',
    password: 'playboy',
    host: '127.0.0.1',
    port: 3306,
};

config.isSimplifyResult demo

If it is true, simplify the result: If the result array has only one object element: If the object element has only one property, return the value of the property. Otherwise, return the whole object.

For one row

config.isSimplifyResult = true;
sequery.init(config);

...

async function () {
    const sql = `select * from ${table} m limit 1`;
    const result = await sequery.do(sql);
    console.log(result.id) // 1
}

For one field

config.isSimplifyResult = true;
sequery.init(config);

...

async function () {
    const sql = `select id from ${table} m limit 1`;
    const result = await sequery.do(sql);
    console.log(result) // 1
}

config.beforeExec demo

config.beforeExec = ({sql, replacements}) => {
    sql = sql + ' where date >= "2019-03-10"';
    return {sql};
};
sequery.init(config);

...

async function () {
    const sql = `select * from ${table} m`;
    const result = await sequery.do(sql);
    console.log(result.length) // 1
}

config.afterExec demo

config.afterExec = (result) => {
    if (!config.times) {
        config.times = 1;
        result.pop();
    }
    return result;
};
sequery.init(config);

...

async function () {
    const sql = `select * from ${table} m`;
    const result = await sequery.do(sql);
    console.log(result.length) // 2
}

config.enableGlobal demo

If it is true, save the data to global.__sequelize_raw_query.

If your project includes multiple subprojects, it is needed to enable global mode. Otherwise, since the sequelize in each subproject is a different instance and cannot share the same data, it will cause an error.

config.enableGlobal = true;
sequery.init(config);

License

MIT

Copyright (c) 2019, Owen Luke