Skip to content

Commit f258206

Browse files
update inputs to accept a valuesToEscape input
1 parent 04d95e0 commit f258206

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

machines/send-native-query.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ module.exports = {
2323
},
2424

2525
nativeQuery: {
26-
description: 'A SQL statement as a string (or to use built-in escaping, this should be provided as a dictionary).',
27-
extendedDescription: 'If provided as a dictionary, this should contain `sql` (the SQL statement string; e.g. \'SELECT * FROM dogs WHERE name = ?\') as well as an array of `bindings` (e.g. [\'David\']).',
28-
moreInfoUrl: 'https://github.com/felixge/node-mysql#performing-queries',
26+
description: 'A native query for the database.',
27+
extendedDescription: 'If `valuesToEscape` is provided, this supports template syntax like `$1`, `$2`, etc.',
2928
whereToGet: {
30-
description: 'This is oftentimes compiled from Waterline query syntax using "Compile statement", however it could also originate from userland code.',
29+
description: 'Write a native query for this database, or if this driver supports it, use `compileStatement()` to build a native query from Waterline syntax.',
30+
extendedDescription: 'This might be compiled from a Waterline statement (stage 4 query) using "Compile statement", however it could also originate directly from userland code.'
3131
},
32-
example: '===',
33-
// example: '*',
32+
example: 'SELECT * FROM pets WHERE species=$1 AND nickname=$2',
3433
required: true
3534
},
3635

36+
valuesToEscape: {
37+
description: 'An optional list of strings, numbers, or special literals (true, false, or null) to escape and include in the native query, in order.',
38+
extendedDescription: 'Note that numbers, `true`, `false`, and `null` are all interpreted exactly the same way as if they were wrapped in quotes. This array must never contain any arrays or dictionaries. The first value in the list will be used to replace `$1`, the second value to replace `$2`, and so on.',
39+
example: '===',
40+
defaultsTo: []
41+
},
42+
3743
meta: {
3844
friendlyName: 'Meta (custom)',
3945
description: 'Additional stuff to pass to the driver.',
@@ -95,21 +101,9 @@ module.exports = {
95101
}
96102

97103

98-
// Validate query
99-
// (supports raw SQL string or dictionary consisting of `sql` and `bindings` properties)
100-
var sql;
101-
var bindings = [];
102-
103-
if (_.isString(inputs.nativeQuery)) {
104-
sql = inputs.nativeQuery;
105-
} else if (_.isObject(inputs.nativeQuery) && _.isString(inputs.nativeQuery.sql)) {
106-
sql = inputs.nativeQuery.sql;
107-
if (_.isArray(inputs.nativeQuery.bindings)) {
108-
bindings = inputs.nativeQuery.bindings;
109-
}
110-
} else {
111-
return exits.error(new Error('Provided `nativeQuery` is invalid. Please specify either a string of raw SQL or a dictionary like `{sql: \'SELECT * FROM dogs WHERE name = $1\', bindings: [\'Rover\']}`.'));
112-
}
104+
// Validate provided native query.
105+
var sql = inputs.nativeQuery;
106+
var bindings = inputs.valuesToEscape || [];
113107

114108

115109
debug('Running SQL Query:');

0 commit comments

Comments
 (0)