Skip to content

Commit fc694ff

Browse files
lint and normalize queryable interface machines
1 parent 8a795ce commit fc694ff

File tree

4 files changed

+115
-95
lines changed

4 files changed

+115
-95
lines changed

machines/compile-statement.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ module.exports = {
2323
required: true
2424
},
2525

26-
meta:
27-
require('../constants/meta.input')
26+
meta: {
27+
friendlyName: 'Meta (custom)',
28+
description: 'Additional stuff to pass to the driver.',
29+
extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
30+
example: '==='
31+
}
2832

2933
},
3034

@@ -65,8 +69,8 @@ module.exports = {
6569
},
6670

6771

68-
fn: function (inputs, exits) {
69-
var SQLBuilder = require('machinepack-sql-builder');
72+
fn: function compileStatement(inputs, exits) {
73+
var SQLBuilder = require('waterline-query-builder');
7074

7175
SQLBuilder.generateSql({
7276
dialect: 'mysql',
@@ -77,20 +81,23 @@ module.exports = {
7781
},
7882
malformed: function malformed(err) {
7983
return exits.malformed({
80-
error: err
84+
error: err,
85+
meta: inputs.meta
8186
});
8287
},
83-
notSupported: function notSupported(err){
88+
notSupported: function notSupported(err) {
8489
return exits.notSupported({
85-
error: err
90+
error: err,
91+
meta: inputs.meta
8692
});
8793
},
8894
success: function success(compiledNativeQuery) {
8995
return exits.success({
90-
nativeQuery: compiledNativeQuery
96+
nativeQuery: compiledNativeQuery,
97+
meta: inputs.meta
9198
});
9299
}
93-
});//</generateQuery>
100+
});
94101
}
95102

96103

machines/parse-native-query-error.js

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ module.exports = {
2525
example: '==='
2626
},
2727

28-
meta:
29-
require('../constants/meta.input')
28+
meta: {
29+
friendlyName: 'Meta (custom)',
30+
description: 'Additional stuff to pass to the driver.',
31+
extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
32+
example: '==='
33+
}
3034

3135
},
3236

@@ -47,53 +51,54 @@ module.exports = {
4751
},
4852

4953

50-
fn: function (inputs, exits) {
51-
var util = require('util');
52-
53-
54+
fn: function parseNativeQueryError(inputs, exits) {
55+
var _ = require('lodash');
5456

5557
// Quick reference of hand-tested errors:
5658

57-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
59+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
5860
// `code` : 'ER_PARSE_ERROR'
5961
// `errno` : 1064
6062
// `sqlState` : '42000'
6163
// `index` : 0
62-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
64+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
6365

64-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
66+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
6567
// `code` : 'ER_NO_SUCH_TABLE'
6668
// `errno` : 1146
6769
// `sqlState` : '42S02'
6870
// `index` : 0
69-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
71+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
7072

71-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
73+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
7274
// `code` : 'ER_DUP_ENTRY'
7375
// `errno` : 1062
7476
// `sqlState` : '23000'
7577
// `index` : 0
76-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
77-
78+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
7879

7980

8081
// Local variable (`err`) for convenience.
8182
var err = inputs.nativeQueryError;
8283

8384
// `footprint` is primarily what will be returned by this machine.
84-
var footprint;
85-
86-
// `meta` will also be returned if it is set below.
87-
var meta;
85+
// For miscellaneous errors which are not explicitly in the
86+
// spec, return the catchall footprint. Drivers should not
87+
// add their own additional footprints-- instead, if they want
88+
// to allow for easily identifying a particular error, the
89+
// `catchall` footprint should still be used; but additional
90+
// information sent back in `meta`.
91+
var footprint = { identity: 'catchall' };
8892

8993

9094
// If the incoming native query error is not an object, or it is
9195
// missing a `code` property, then we'll go ahead and bail out w/
9296
// the "catchall" footprint to avoid continually doing these basic
9397
// checks in the more detailed error negotiation below.
94-
if ( !util.isObject(err) || !err.code) {
98+
if (!_.isObject(err) || !err.code) {
9599
return exits.success({
96-
footprint: { identity: 'catchall' }
100+
footprint: footprint,
101+
meta: inputs.meta
97102
});
98103
}
99104

@@ -106,7 +111,7 @@ module.exports = {
106111
// > that is, only one of them should be run.
107112

108113

109-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
114+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
110115
// `code` : 'ER_DUP_ENTRY'
111116
// `errno` : 1062
112117
// `sqlState` : '23000'
@@ -116,7 +121,7 @@ module.exports = {
116121
// Waterline driver spec. If additional information
117122
// is needed in userland beyond what is guaranteed in
118123
// the spec, then you should take advantage of `meta`.
119-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
124+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
120125
if (err.code === 'ER_DUP_ENTRY') {
121126
// Negotiate `notUnique` error footprint.
122127
footprint = {
@@ -126,16 +131,16 @@ module.exports = {
126131
// Now build our footprint's `keys` property by manually parsing
127132
// the MySQL error message and extracting the relevant bits.
128133
// (See also: https://github.com/balderdashy/sails-mysql/blob/2c414f1191c3595df2cea8e40259811eb3ca05f9/lib/adapter.js#L1223)
129-
if ( util.isString(err.message) ) {
134+
if (_.isString(err.message)) {
130135
var matches = err.message.match(/Duplicate entry '.*' for key '(.*?)'$/);
131-
if ( matches && matches.length > 0 ) {
136+
if (matches && matches.length > 0) {
132137
footprint.keys.push(matches[1]);
133138
}
134139
}
135140
}
136141

137142

138-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
143+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
139144
// `code` : 'ER_NO_SUCH_TABLE'
140145
// `errno` : 1146
141146
// `sqlState` : '42S02'
@@ -145,15 +150,15 @@ module.exports = {
145150
// reference. If this driver wants to move ahead of
146151
// the core Waterline/machine spec, this can be handled
147152
// using the `catchall` footprint + `meta`.
148-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
153+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
149154
// else if ( ... ){
150155
// footprint = { identity: 'catchall' };
151156
// // e.g.
152157
// meta = { problem: 'noSuchTable', foo: ..., bar: ... };
153158
// }
154159

155160

156-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
161+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
157162
// `code` : 'ER_PARSE_ERROR'
158163
// `errno` : 1064
159164
// `sqlState` : '42000'
@@ -163,38 +168,21 @@ module.exports = {
163168
// reference. If this driver wants to move ahead of
164169
// the core Waterline/machine spec, this can be handled
165170
// using the `catchall` footprint + `meta`.
166-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
171+
// --o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
167172
// else if ( ... ){
168173
// footprint = { identity: 'catchall' };
169174
// // e.g.
170175
// meta = { problem: 'couldNotParse', foo: ..., bar: ... };
171176
// }
172177

173178

174-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
175-
// ??????
176-
//
177-
// For miscellaneous errors which are not explicitly in the
178-
// spec, return the catchall footprint. Drivers should not
179-
// add their own additional footprints-- instead, if they want
180-
// to allow for easily identifying a particular error, the
181-
// `catchall` footprint should still be used; but additional
182-
// information sent back in `meta`.
183-
//
184-
//--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o--o-<>
185-
else {
186-
footprint = { identity: 'catchall' };
187-
}
188-
189-
190-
191179
// Finally, return the normalized footprint.
192180
//
193181
// (as well as any additional metadata that was stuffed
194182
// into `meta` above)
195183
return exits.success({
196184
footprint: footprint,
197-
meta: meta
185+
meta: inputs.meta
198186
});
199187
}
200188

machines/parse-native-query-result.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ module.exports = {
1919
description: 'The type of query operation this raw result came from.',
2020
moreInfoUrl: 'https://github.com/node-machine/waterline-driver-interface#query-results',
2121
extendedDescription:
22-
'Either "select", "insert", "destroy", "update", "count", "sum", or "avg". '+
22+
'Either "select", "insert", "destroy", "update", "count", "sum", or "avg". ' +
2323
'This determines how the provided raw result will be parsed/coerced.',
2424
required: true,
25-
example: 'select',// (select|insert|destroy|update|count|sum|avg)
25+
example: 'select'
2626
},
2727

2828
nativeQueryResult: {
@@ -32,8 +32,12 @@ module.exports = {
3232
example: '==='
3333
},
3434

35-
meta:
36-
require('../constants/meta.input')
35+
meta: {
36+
friendlyName: 'Meta (custom)',
37+
description: 'Additional stuff to pass to the driver.',
38+
extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
39+
example: '==='
40+
}
3741

3842
},
3943

@@ -53,10 +57,10 @@ module.exports = {
5357
},
5458

5559

56-
fn: function (inputs, exits) {
57-
60+
fn: function parseNativeQueryResult(inputs, exits) {
5861
var normalizedResult;
59-
switch (inputs.queryType){
62+
63+
switch (inputs.queryType) {
6064
case 'select':
6165
normalizedResult = inputs.nativeQueryResult.rows;
6266
break;
@@ -84,7 +88,8 @@ module.exports = {
8488
}
8589

8690
return exits.success({
87-
result: normalizedResult
91+
result: normalizedResult,
92+
meta: inputs.meta
8893
});
8994
}
9095

0 commit comments

Comments
 (0)