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-querycd sequelize-raw-querynpm installnpm test
Usage
Initialize sequery first:
const sequery = ; // See "Config" section at the last to learn moreconst config = dialect: 'mysql' // mysql or mssql database: 'sys' username: 'root' password: 'playboy' host: '127.0.0.1' // your db host port: 3306; sequery;
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.
demo
.exec() or .do(), the alias of .exec { const sql = `select * from m`; const result = await sequery; console // 1}
{ const sql = `select * from m where id = :id`; const replacements = id: 2; const result = await sequery; console // 2}
{ const sql = `select * from m where id > :id`; const result = await sequery; console // 2`}
{ const sql = `select * from m where id = {id}`; const templateData = id: 2; const result = await sequery; console // 2}
{ const sql = `select * from m where id > {id}`; const result = await sequery; console // 2}
{ const sql = `select * from m where {condition}`; const result = await sequery; console // 2}
{ const sql = `select * from m where {idCondition} and {dateCondition}`; const result = await sequery; console // 2}
{ const sql = `select * from m where {condition} and id > :id`; const replacements = id: 2; const templateData = condition: 'id > 1'; const result = await sequery; console // 3}
{ const sql = `select * from {table} m where id > {id}`; const result = await sequery; console // 2}
{ const sql = `select * from m where {condition} and id > :id`; const beforeExec = { sql = 'select 1 as id'; return sql; }; const result = await sequery; console // 1}
{ const sql = `select * from m where {condition} and id > :id`; const beforeExec = { sql = 'select 1 as id'; return sql; }; const result = await sequery; console // 1}
{ const sql = `select * from m`; const beforeExec = { return undefined; }; const result = await sequery; console // 3}
{ const sql = `select * from m where id > :id`; const afterExec = { result; }; const result = await sequery; console // 2}
{ const sql = `select * from m where id > :id`; const afterExec = { result; }; const result = await sequery; console // 2}
{ const sql = `select * from m where id > :id`; const afterExec = { result = ; return result; }; const result = await sequery; console // 0}
{ const sql = `select * from m where id > :id`; const afterExec = { result = ; return result; }; const result = await sequery; console // 0}
{ 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; console // 2}
demo
.getWhereConditions(where) { const where = "id": 2; const whereStr = sequery; console // '`id` = 2'}
{ const where = "id": 2; const tableAs = 'm'; const whereStr = sequery; console // '`m`.`id` = 2'}
{ const where = '{"id": 2}'; const whereStr = sequery; console // '`id` = 2'}
{ const where = "id": $gt: 2; const whereStr = sequery; console // '`id` > 2'}
{ const Op = sequerySequelizeOp; const where = id: Opor: Oplt: 1000 Opeq: null ; const whereStr = sequery; console // '(`id` < 1000 OR `id` IS NULL)'}
demo
.getOrderClause(order) { const order = 'id'; const orderStr = sequery; console // ' order by `id`'}
{ const order = 'type, name desc'; const orderStr = sequery; console // ' order by `type`, `name` desc'}
{ const order = 'type' 'name desc'; const orderStr = sequery; console // ' order by `type`, `name` desc'}
{ const order = 'type' 'name' 'desc'; const orderStr = sequery; console // ' order by `type`, `name` desc'}
{ const order = 'type' 'name desc'; const tableAs = 'm'; const orderStr = sequery; console // ' order by `m`.`type`, `m`.`name` desc'}
demo
.getGroupClause(group) { const group = 'id'; const groupStr = sequery; console // ' group by `id`'}
demo
.getLimitClause(options) { const options = order: 'id' limit: 10 offset: 5; const limitStr = sequery; console // ' order by `id` limit 5, 10'}
{ const options = order: 'id' tableAs: 'm' limit: 10 offset: 5; const tableAs = 'm'; const limitStr = sequery; console // ' order by `m`.`id` limit 5, 10'}
Config
Base configuration
// For mssqlconst config = dialect: 'mssql' database: 'master' username: 'sa' password: 'playboy' host: '192.168.197.80' port: 1433; // For mysqlconst config = dialect: 'mysql' database: 'sys' username: 'root' password: 'playboy' host: '127.0.0.1' port: 3306;
demo
config.isSimplifyResultIf 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
configisSimplifyResult = true;sequery; ... { const sql = `select * from m limit 1`; const result = await sequery; console // 1}
For one field
configisSimplifyResult = true;sequery; ... { const sql = `select id from m limit 1`; const result = await sequery; console // 1}
demo
config.beforeExecconfig { sql = sql + ' where date >= "2019-03-10"'; return sql;};sequery; ... { const sql = `select * from m`; const result = await sequery; console // 1}
demo
config.afterExecconfig { if !configtimes configtimes = 1; result; return result;};sequery; ... { const sql = `select * from m`; const result = await sequery; console // 2}
demo
config.enableGlobalIf 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.
configenableGlobal = true;sequery;
License
Copyright (c) 2019, Owen Luke