Skip to content

Commit 085054c

Browse files
committed
Added polling
1 parent f5a7c79 commit 085054c

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ With this action you may fetch data out of the database, e.g. using ``SELECT`` s
2929
This trigger & action has no limitations on the number of rows so you may expect to get all of these
3030
via sequential fetching that is implemented within the node.js ``mssql`` driver.
3131

32+
#### Polling
33+
34+
Comopnent will remember last execution timestamp and let you build queries on it:
35+
36+
```sql
37+
select * from Leads where Created >= '%%EIO_LAST_POLL%%'
38+
```
39+
3240
### INSERT/DELETE/UPDATE Action
3341

3442
![image](https://cloud.githubusercontent.com/assets/56208/22904204/cef8cb06-f23b-11e6-998f-3fe65ab81540.png)

component.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"actions": {
1515
"insert": {
1616
"title": "INSERT/UPDATE/DELETE",
17+
"description": "Executes a single SQL statement that causes database data update and returns a number of affected records, like INSERT, UPDATE or DELETE",
1718
"main": "./lib/actions/insert.js",
1819
"fields": {
1920
"query": {
@@ -28,6 +29,7 @@
2829
},
2930
"selectAction": {
3031
"title": "SELECT",
32+
"description": "Executes a SELECT statement that fetches potentially multiple database rows from the database",
3133
"main": "./lib/actions/select.js",
3234
"fields": {
3335
"query": {
@@ -43,6 +45,7 @@
4345
"triggers": {
4446
"selectTrigger": {
4547
"title": "SELECT",
48+
"description": "Executes a SELECT statement that fetches potentially multiple database rows from the database",
4649
"main": "./lib/actions/select.js",
4750
"type": "polling",
4851
"fields": {

lib/actions/select.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const eioUtils = require('elasticio-node').messages;
44
const co = require('co');
55
const cosql = require('co-mssql');
66

7+
const LAST_POLL_PLACEHOLDER = "%%EIO_LAST_POLL%%";
8+
79
let connection;
810

911
/**
@@ -28,9 +30,15 @@ function init(cfg) {
2830
* @param msg incoming message object that contains ``body`` with payload
2931
* @param cfg configuration that is account information and configuration field values
3032
*/
31-
function processAction(msg, cfg) {
32-
const sql = cfg.query;
33-
console.log('Executing query=%s', sql);
33+
function processAction(msg, cfg, snapshot) {
34+
const originalSql = cfg.query;
35+
const now = new Date().toISOString();
36+
// Last poll should come from Snapshot, if not it's beginning of time
37+
const lastPoll = snapshot?snapshot.lastPoll:null || new Date(0).toISOString();
38+
console.log('Last polling timestamp=%s', lastPoll);
39+
const sql = originalSql.split(LAST_POLL_PLACEHOLDER).join(lastPoll);
40+
console.log('Original query=%s', originalSql);
41+
console.log('Transformed query=%s', sql);
3442
return co(function* gen() {
3543
const request = new cosql.Request(connection);
3644
request.stream = true;
@@ -49,6 +57,9 @@ function processAction(msg, cfg) {
4957

5058
request.on('done', (affected) => {
5159
console.log('Query execution completed, affected=%s', affected);
60+
this.emit('snapshot', {
61+
lastPoll: now
62+
});
5263
this.emit('end');
5364
});
5465

spec-integration/integration.spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class TestEmitter extends EventEmitter {
1313
this.error = [];
1414

1515
this.on('data', (value) => this.data.push(value));
16-
this.on('error', (value) => this.error.push(value));
16+
this.on('error', (value) => {
17+
this.error.push(value)
18+
console.error(value.stack || value);
19+
});
1720
this.on('end', () => {
1821
this.end++;
1922
done();
@@ -77,7 +80,7 @@ describe('Integration test', () => {
7780
return select.init(cfg);
7881
});
7982

80-
it('should insert data', (done) => {
83+
it('should select data', (done) => {
8184
const emitter = new TestEmitter(() => {
8285
expect(emitter.error.length).to.equal(0);
8386
expect(emitter.data.length).to.equal(10);
@@ -91,4 +94,28 @@ describe('Integration test', () => {
9194
});
9295
});
9396

97+
describe('for polling SELECT', () => {
98+
99+
const cfg = {
100+
uri : process.env.MSSQL_URL,
101+
query: "select * from Leads where Created >= '%%EIO_LAST_POLL%%'"
102+
};
103+
104+
before(() => {
105+
return select.init(cfg);
106+
});
107+
108+
it('should insert data', (done) => {
109+
const emitter = new TestEmitter(() => {
110+
expect(emitter.error.length).to.equal(0);
111+
expect(emitter.end).to.equal(1);
112+
done();
113+
});
114+
const msg = {
115+
body: {}
116+
};
117+
select.process.call(emitter, msg, cfg).catch(err => done(err));
118+
});
119+
});
120+
94121
});

0 commit comments

Comments
 (0)