Skip to content

Commit 8803906

Browse files
author
David Gómez Matarrodona
committed
sql server optional
1 parent c42bf91 commit 8803906

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

doc/inputs/sqlserver.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## SQL Server Input
2+
3+
Permite la lectura de registros de tablas de Microsoft SQL Server
4+
5+
## Examples
6+
7+
Lectura de una table con seguridad integrada.
8+
```json
9+
"inputs": {
10+
"sqlserver": {
11+
"type": "sqlserver",
12+
"config": {
13+
"query": "select TOP (1000) [Index],[Height_Inches],[Weight_Pounds] from hw_25000 where [Index] > ${Index}",
14+
"watermark": {"Index": 0},
15+
"mode": "watermark",
16+
"options": {
17+
"database": "LogsDB",
18+
"server": "localhost",
19+
"pool": {
20+
"max": 10,
21+
"min": 0,
22+
"idleTimeoutMillis": 30000
23+
},
24+
"options": {
25+
"trustedConnection": false,
26+
}
27+
}
28+
}
29+
}
30+
}
31+
```
32+
33+
## Configuration parameters
34+
* **query** : Consulta SQL sobra la que se obtienen los registros (filas). Es parametrizable, usando como valores los atributos del último registro leido. Inicialmente, al no haber "ultimo registro", se usa el objeto *watermark*
35+
* **watermark** : Fila inicial para la parametrización de la consulta. Las posteriores ejecuciones usarán el último registro leido.
36+
* **mode** : Puede ser **start** or **watermark**. Cuando es **start**, el input siempre inicia usando como último registro el objeto *watermark* especificado. En caso de usar el modo **watermark**, las posteriores ejecuciones usarán el último registro leído.
37+
* **options** : Opciones de conexión y configuración del acceso a SQL Server. Se puede ver la especificación completa en [http://tediousjs.github.io/tedious/api-connection.html#function_newConnection](http://tediousjs.github.io/tedious/api-connection.html#function_newConnection)
38+
39+
## Output
40+
Cada lectura generará un objeto con el siguiente esquema:
41+
```javascript
42+
{
43+
id : '<input ID>',
44+
type : 'sqlserver',
45+
database: '<dbname>',
46+
originalMessage : '<String value or JSON object>'
47+
}
48+
```
49+
50+
### Ejemplo
51+
```json
52+
{
53+
"id":"sqlserver",
54+
"type":"sqlserver",
55+
"database":"LogsDB",
56+
"input":"sqlserver",
57+
"originalMessage":{
58+
"Index":26100,
59+
"Height_Inches":4739,
60+
"Weight_Pounds":7900
61+
}
62+
}
63+
```

lib/input/sqlserver.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ if(module.parent) {
190190
else {
191191
logger.setLevel('debug');
192192

193+
/*
193194
async function test() {
194195
const config = {
195196
query : 'select TOP (1000) [Index],[Height_Inches],[Weight_Pounds] from hw_25000 where [Index] > ${Index}',
@@ -225,7 +226,51 @@ else {
225226
}
226227
227228
test();
229+
*/
230+
231+
async function test() {
232+
const sqlConfig = {
233+
//domain : "GRUPOICA",
234+
user: "david.gomez",
235+
password: "",
236+
database: "LogsDB",
237+
server: 'GICA-MAD-671',
238+
pool: {
239+
max: 10,
240+
min: 0,
241+
idleTimeoutMillis: 30000
242+
},
243+
options: {
244+
trustedConnection: true,
245+
encrypt: false, // for azure
246+
trustServerCertificate: true // change to true for local dev / self-signed certs
247+
}
248+
}
249+
250+
try {
251+
let pool = await sql.connect(sqlConfig);
252+
253+
let mreq = await pool.request();
254+
let max = await mreq.query('SELECT MAX([Index]) as WM from hw_25000');
255+
let idx = max.recordset[0].WM;
256+
257+
for(let i=0;i<1000;i++) {
258+
idx++;
259+
let v1 = Math.floor(Math.random()*10000);
260+
let v2 = Math.floor(Math.random()*10000);
261+
let req = await pool.request();
262+
await req.query(`insert into hw_25000 ([Index],Height_Inches,Weight_Pounds) values (${idx},${v1},${v2})`);
263+
process.stdout.write('.');
264+
await timer(300);
265+
}
266+
console.log('DONE');
267+
process.exit(0);
268+
}catch(err) {
269+
console.error(err);
270+
}
271+
}
228272

273+
test();
229274

230275
/*
231276
async function test() {

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
"minimatch": "^3.0.4",
6262
"mkdirp": "^0.5.1",
6363
"mongodb": "^3.3.3",
64-
"msnodesqlv8": "^2.2.0",
65-
"mssql": "^7.2.1",
6664
"node-forge": "^0.10.0",
6765
"nools": "^0.4.4",
6866
"nsyslog-parser": "^0.9.6",
@@ -81,6 +79,10 @@
8179
"xml2js": "^0.4.19",
8280
"yallist": "^4.0.0"
8381
},
82+
"optionalDependencies": {
83+
"msnodesqlv8": "^2.2.0",
84+
"mssql": "^7.2.1"
85+
},
8486
"devDependencies": {
8587
"@pixi/jsdoc-template": "^2.4.3",
8688
"@types/mongodb": "^3.3.11",

0 commit comments

Comments
 (0)