Skip to content

Commit de2c98d

Browse files
committed
Insert now has a dynamic metadata
1 parent 69302a2 commit de2c98d

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

component.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"placeholder": "INSERT INTO films (code,title,kind) VALUES (@code:number,@title:string,@kind:string)",
2424
"note": "You can use parameters of message body as <i>@value:number</i> and type is <i>:type</i>"
2525
}
26-
}
26+
},
27+
"dynamicMetadata": true
2728
},
2829
"selectAction": {
2930
"title": "SELECT",
@@ -51,11 +52,6 @@
5152
"viewClass": "TextAreaWithNoteView",
5253
"placeholder": "SELECT * FROM films WHERE title LIKE ${'%' + query + '%'}",
5354
"note": "You can use properties of message body as <i>${values}</i> in your insert or update or delete"
54-
},
55-
"batch": {
56-
"label": "Bundle results in batches",
57-
"required": false,
58-
"viewClass": "CheckBoxView"
5955
}
6056
}
6157
}

lib/actions/insert.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ let pstmt;
77

88
const VARS_REGEXP = /@([\w_$][\d\w_$]*:(string|boolean|date|number|bigint))/g;
99

10+
/**
11+
* This function will be called during component intialization
12+
*
13+
* @param cfg
14+
* @returns {Promise}
15+
*/
1016
function init(cfg) {
1117
const conString = cfg.uri;
1218
return co(function* gen() {
@@ -51,6 +57,44 @@ function init(cfg) {
5157
}.bind(this));
5258
}
5359

60+
/**
61+
* This function will be called to fetch metamodel for SQL query
62+
*
63+
* @param cfg
64+
*/
65+
function getMetaModel(cfg) {
66+
return co(function* gen() {
67+
const sql = cfg.query;
68+
const result = {
69+
in : {
70+
type: 'object',
71+
properties : {}
72+
},
73+
out : {}
74+
};
75+
if (sql && sql.length > 0) {
76+
const vars = sql.match(VARS_REGEXP);
77+
const fields = result.in.properties;
78+
for(const tuple of vars) {
79+
const [key,type] = tuple.split(':');
80+
let jsType = 'string';
81+
switch(type) {
82+
case 'date':
83+
jsType = 'string';
84+
break;
85+
case 'bigint':
86+
jsType = 'number';
87+
break;
88+
}
89+
fields[key.substr(1)] = {
90+
type : jsType
91+
};
92+
}
93+
}
94+
return result;
95+
});
96+
}
97+
5498
/**
5599
* This method will be called from elastic.io platform providing following data
56100
*
@@ -68,3 +112,4 @@ function processAction(msg) {
68112

69113
module.exports.process = processAction;
70114
module.exports.init = init;
115+
module.exports.getMetaModel = getMetaModel;

lib/actions/select.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function init(cfg) {
2020
yield connection.connect();
2121
}.bind(this));
2222
}
23+
2324
/**
2425
* This method will be called from elastic.io platform providing following data
2526
*
@@ -50,7 +51,7 @@ function processAction(msg, cfg) {
5051
this.emit('end');
5152
});
5253

53-
// Run it
54+
// Run it
5455
yield request.query(sql);
5556
}.bind(this));
5657
}

0 commit comments

Comments
 (0)