Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adriano-di-giovanni/node-sql-template
A simple SQL template engine and query runner for Node.js
https://github.com/adriano-di-giovanni/node-sql-template
Last synced: 1 day ago
JSON representation
A simple SQL template engine and query runner for Node.js
- Host: GitHub
- URL: https://github.com/adriano-di-giovanni/node-sql-template
- Owner: adriano-di-giovanni
- Created: 2015-04-22T11:12:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-15T11:03:51.000Z (about 9 years ago)
- Last Synced: 2024-11-05T22:14:39.321Z (8 days ago)
- Language: JavaScript
- Size: 180 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SQLTemplate
SQLTemplate is a simple SQL template engine and query runner for Node.js.
## Installation
```
npm install node-sql-template mysql --save
```## Usage
### Template rendering
`/sql/example-1.sql`
```sql
SELECT ?
````/index.js`
```javascript
var
path = require('path');var
SQLTemplate = require('node-sql-template');var
options = {
template: {
dir: path.resolve(__dirname, './sql'),
ext: 'sql'
}
},
template = SQLTemplate.forge(options),// escape values are optional
// see https://github.com/felixge/node-mysql#escaping-query-values
// see https://github.com/felixge/node-mysql#escaping-query-identifiers
escape = [ 1 ];console.log(template.render('example-1', escape)); // SELECT 1
```### Query execution w/ callback
`/sql/example-2.sql`
```sql
SELECT * FROM `player` WHERE `name` LIKE ?
````/index.js`
```javascript
var
path = require('path');var
SQLTemplate = require('node-sql-template');var
options = {
template: {
dir: path.resolve(__dirname, './sql'),
ext: 'sql'
},
// https://github.com/felixge/node-mysql#connection-options
connection: {
host: 'localhost',
user: 'root',
password: 'root',
multipleStatements: true
},isDebug: true // sql will be output to the console upon #run()
},
template = SQLTemplate.forge(options);template.run('example-2', [ 'A%' ], function (error, rows) {
console.log(error, rows);
});
```### Query execution w/o callback
`/sql/example-2.sql`
```sql
SELECT * FROM `player` WHERE `name` LIKE ?
````/index.js`
```javascript
var
path = require('path');var
SQLTemplate = require('node-sql-template');var
options = {
template: {
dir: path.resolve(__dirname, './sql'),
ext: 'sql'
},// https://github.com/felixge/node-mysql#connection-options
connection: {
host: 'localhost',
user: 'root',
password: 'root',
multipleStatements: true
},isDebug: true // sql will be output to the console upon #run()
},
template = SQLTemplate.forge(options),stream = template.run('example-2', [ 'A%' ]);
stream
.on('error', function (error) {})
.on('result', function (row) {})
.on('fields', function (fields) {})
.on('end', function (end) {});
```## Change Log
### 0.2.3
Fix package definition
### 0.2.2
Add `#end()` method to release connection pool.
### 0.2.1
Add `isDebug` option to output sql upon `#run()`.
### 0.2.0
you now need to also `npm install` mysql package