Sql-Moduleon is a very simple template engine to use .sql files in js.
- Add template tags in sql and let us focus on the queries.
- Ensure sql and js logic stay seperated.
- Leverage the power of parameterized query to stay safe.
- Compile into function to be as fast as possible.
- We want to leverage the performance and convinence of using raw sql in our application BUT using sql in our application code can feel messy especially when our queries start to get big.
- We want to avoid mixxing sql with js which makes it harder to read, harder to maintain and keep secure.
// about as good looking as we can get but insecure by default
sql = `UPDATE user SET
name = ${fullname}
, age = ${age}
, fullname = ${fullname}`
// have to keep track of the arguments order, every change in a big query is a pain
sql = `UPDATE user SET
name = ?
, age = ?
, fullname = ?`
values = [fullname, age, fullname]
// using prisma sql is pretty good and safe
// but you still have to write your sql in your js files
// not ideal for big queries
Prisma.sql`UPDATE user SET
name = ${fullname}
, age = ${age}
, fullname = ${fullname}`
Sql file where we focus on the sql
UPDATE user SET
name = {{= fullname }}
, age = {{= age }}
, fullname = {{= fullname }}
Js file where we focus on the js
const data = {fullname: 'luke', age: 19}
const {sql, values} = moduleon( sql )( data );
- no sql file loading
- no database query execution
- Write your .sql files with templates tags
- Load your .sql files
- Transform your sql into a function that generate parametrized queries <-- sql-Moduleon's job
- Call the sql function with your data
- Send the parameterized query to your database
npm install sql-moduleon --save
or using yarn
yarn add sql-moduleon
var moduleon = require('sql-moduleon');
moduleon.setDefaultConfig({ engine:'pg', array:'string' });
var fs = require('fs');
var sql = fs.readFileSync('./your/sqlfile.sql', 'utf-8');
// -> sql-moduleon <-
const data = { title : 'value' }
const sqlTemplateFunction = moduleon( sql );
const { sql, values } = sqlTemplateFunction( data );
//use your DB engine to execute the parameterized query
db.query( sql, values );
-
Condition
{{? value }} SELECT * FROM table {{?}}
will add the sql inbetween the tags only if the condition is fullfilled
-
Value
SELECT * FROM table WHERE id = {{= value }}
behind the scene it will replace this tag by a sql unnamed parameter (?) and generated the values array
-
Javascript Code
SELECT * FROM {{ out += '"Table"'; }}
simply throw in javascript code in the template function to add advanced logic
npm install --dev
npm test
This module was tested with postgres and sqlite, it may not work with other DBMS
whitelist in package.json "files"
npx publish-preview