Skip to content

Commit 5b4cf88

Browse files
committed
Now both SELECT trigger and action both changed to use incoming data as SQL query and not the field
1 parent aa9ba36 commit 5b4cf88

File tree

3 files changed

+110
-83
lines changed

3 files changed

+110
-83
lines changed

component.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@
5151
"description": "Executes a SELECT statement that fetches potentially multiple database rows from the database",
5252
"main": "./lib/actions/select.js",
5353
"type": "polling",
54-
"fields": {
55-
"query": {
56-
"label": "SQL Query",
57-
"required": true,
58-
"viewClass": "TextAreaWithNoteView",
59-
"placeholder": "SELECT * FROM films WHERE title LIKE ${'%' + query + '%'}",
60-
"note": "You can use properties of message body as <i>${values}</i> in your insert or update or delete"
54+
"metadata": {
55+
"in": {
56+
"type": "object",
57+
"properties": {
58+
"query": {
59+
"type": "string",
60+
"title": "SQL Query",
61+
"required": true
62+
}
63+
}
6164
}
6265
}
6366
}

lib/actions/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function init(cfg) {
3131
* @param cfg configuration that is account information and configuration field values
3232
*/
3333
function processAction(msg, cfg, snapshot = {}) {
34-
const originalSql = cfg.query;
34+
const originalSql = cfg.query || msg.body.query;
3535
const now = new Date().toISOString();
3636
// Last poll should come from Snapshot, if not it's beginning of time
3737
const lastPoll = snapshot.lastPoll || new Date(0).toISOString();

spec-integration/integration.spec.js

Lines changed: 99 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,127 @@
22
const expect = require('chai').expect;
33
const insert = require('../lib/actions/insert');
44
const select = require('../lib/actions/select');
5+
const {messages} = require('elasticio-node');
56
const EventEmitter = require('events');
67

78
class TestEmitter extends EventEmitter {
89

9-
constructor(done) {
10-
super();
11-
this.data = [];
12-
this.end = 0;
13-
this.error = [];
10+
constructor(done) {
11+
super();
12+
this.data = [];
13+
this.end = 0;
14+
this.error = [];
1415

15-
this.on('data', (value) => this.data.push(value));
16-
this.on('error', (value) => {
17-
this.error.push(value)
18-
console.error(value.stack || value);
19-
});
20-
this.on('end', () => {
21-
this.end++;
22-
done();
23-
});
24-
}
16+
this.on('data', (value) => this.data.push(value));
17+
this.on('error', (value) => {
18+
this.error.push(value)
19+
console.error(value.stack || value);
20+
});
21+
this.on('end', () => {
22+
this.end++;
23+
done();
24+
});
25+
}
2526

2627
}
2728

2829
describe('Integration test', () => {
2930

3031

31-
before(() => {
32-
if (!process.env.MSSQL_URL) throw new Error("Please set MSSQL_URL env variable to proceed");
33-
});
32+
before(() => {
33+
if (!process.env.MSSQL_URL) throw new Error("Please set MSSQL_URL env variable to proceed");
34+
});
3435

35-
describe('for INSERT', () => {
36+
describe('for INSERT', () => {
3637

37-
const cfg = {
38-
uri : process.env.MSSQL_URL,
39-
query: 'INSERT INTO Test2.dbo.Tweets (Lang, Retweeted, Favorited, "Text", id, CreatedAt, Username, ScreenName) '
40-
+ 'VALUES (@lang, @retweeted:boolean, @favorited:boolean, @text:string, @id:bigint, @created_at:date, @username, @screenname:string)'
41-
};
38+
const cfg = {
39+
uri: process.env.MSSQL_URL,
40+
query: 'INSERT INTO Test2.dbo.Tweets (Lang, Retweeted, Favorited, "Text", id, CreatedAt, Username, ScreenName) '
41+
+ 'VALUES (@lang, @retweeted:boolean, @favorited:boolean, @text:string, @id:bigint, @created_at:date, @username, @screenname:string)'
42+
};
4243

43-
before(() => {
44-
return insert.init(cfg);
45-
});
44+
before(() => {
45+
return insert.init(cfg);
46+
});
4647

47-
it('should insert data', () => {
48-
const emitter = new TestEmitter();
49-
const msg = {
50-
body: {
51-
lang: 'en',
52-
retweeted: false,
53-
favorited: false,
54-
text: 'Hello integration testing',
55-
id: 12345678910,
56-
created_at: new Date().toISOString(),
57-
username: 'Renat Zubairov',
58-
screenname: 'zubairov'
59-
}
60-
};
61-
return insert.process.call(emitter, msg).then((result) => {
62-
expect(result).deep.equal(msg);
63-
expect(emitter.data.length).to.equal(0);
64-
// promises, no need to emit end
65-
expect(emitter.end).to.equal(0);
66-
// No error
67-
expect(emitter.error.length).to.equal(0);
68-
});
48+
it('should insert data', () => {
49+
const emitter = new TestEmitter();
50+
const msg = {
51+
body: {
52+
lang: 'en',
53+
retweeted: false,
54+
favorited: false,
55+
text: 'Hello integration testing',
56+
id: 12345678910,
57+
created_at: new Date().toISOString(),
58+
username: 'Renat Zubairov',
59+
screenname: 'zubairov'
60+
}
61+
};
62+
return insert.process.call(emitter, msg).then((result) => {
63+
expect(result).deep.equal(msg);
64+
expect(emitter.data.length).to.equal(0);
65+
// promises, no need to emit end
66+
expect(emitter.end).to.equal(0);
67+
// No error
68+
expect(emitter.error.length).to.equal(0);
69+
});
70+
});
6971
});
70-
});
7172

72-
describe('for SELECT', () => {
73+
describe('for SELECT', () => {
74+
75+
const cfg = {
76+
uri: process.env.MSSQL_URL
77+
};
7378

74-
const cfg = {
75-
uri : process.env.MSSQL_URL,
76-
query: 'select * from Tweets ORDER BY id OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;'
77-
};
79+
before(() => {
80+
return select.init(cfg);
81+
});
7882

79-
before(() => {
80-
return select.init(cfg);
83+
it('should select data', (done) => {
84+
const emitter = new TestEmitter(() => {
85+
expect(emitter.error.length).to.equal(0);
86+
expect(emitter.data.length).to.equal(10);
87+
expect(emitter.end).to.equal(1);
88+
done();
89+
});
90+
const msg = messages.newMessageWithBody({
91+
query: 'select * from Tweets ORDER BY id OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;'
92+
});
93+
select.process.call(emitter, msg, cfg).catch(err => done(err));
94+
});
8195
});
8296

83-
it('should select data', (done) => {
84-
const emitter = new TestEmitter(() => {
85-
expect(emitter.error.length).to.equal(0);
86-
expect(emitter.data.length).to.equal(10);
87-
expect(emitter.end).to.equal(1);
88-
done();
89-
});
90-
const msg = {
91-
body: {
92-
lang: 'en'
93-
}
94-
};
95-
select.process.call(emitter, msg, cfg).catch(err => done(err));
97+
describe('for legacy SELECT configuration', () => {
98+
99+
const cfg = {
100+
uri: process.env.MSSQL_URL,
101+
query: 'select * from Tweets ORDER BY id OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;'
102+
};
103+
104+
before(() => {
105+
return select.init(cfg);
106+
});
107+
108+
it('should select data', (done) => {
109+
const emitter = new TestEmitter(() => {
110+
expect(emitter.error.length).to.equal(0);
111+
expect(emitter.data.length).to.equal(10);
112+
expect(emitter.end).to.equal(1);
113+
done();
114+
});
115+
const msg = messages.newMessageWithBody({
116+
});
117+
select.process.call(emitter, msg, cfg).catch(err => done(err));
118+
});
96119
});
97-
});
98120

99-
describe('for polling SELECT', () => {
121+
122+
describe('for polling SELECT', () => {
100123

101124
const cfg = {
102-
uri : process.env.MSSQL_URL,
103-
query: "select * from Leads where Created >= '%%EIO_LAST_POLL%%'"
125+
uri: process.env.MSSQL_URL
104126
};
105127

106128
before(() => {
@@ -114,7 +136,9 @@ describe('Integration test', () => {
114136
done();
115137
});
116138
const msg = {
117-
body: {}
139+
body: {
140+
query: "select * from Leads where Created >= '%%EIO_LAST_POLL%%'"
141+
}
118142
};
119143
select.process.call(emitter, msg, cfg, {}).catch(err => done(err));
120144
});

0 commit comments

Comments
 (0)