Skip to content

Commit 77ac23f

Browse files
committed
Implement parse-native-query-error and parse-native-query-result. Get rid of notUnique exit.
1 parent a2a29ec commit 77ac23f

File tree

5 files changed

+180
-18
lines changed

5 files changed

+180
-18
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ Learn more at <a href="http://node-machine.org/implementing/FAQ" title="Machine
2727
## License
2828

2929
MIT &copy; 2015 contributors
30-

machines/parse-native-query-error.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
module.exports = {
2+
3+
4+
friendlyName: 'Parse native query error',
5+
6+
7+
description: 'Attempt to identify and parse a raw error from sending a native query and normalize it to a standard error footprint.',
8+
9+
10+
cacheable: true,
11+
12+
13+
sync: true,
14+
15+
16+
inputs: {
17+
18+
queryType: {
19+
description: 'The type of query operation this raw error came from.',
20+
extendedDescription: 'Either "select", "insert", "delete", or "update". This determines how the provided raw error will be parsed/coerced.',
21+
moreInfoUrl: 'https://github.com/particlebanana/waterline-query-builder/blob/master/docs/syntax.md',
22+
required: true,
23+
example: 'select',// (select|insert|delete|update)
24+
},
25+
26+
nativeQueryError: {
27+
description: 'The error sent back from the database as a result of a native query.',
28+
extendedDescription: 'This is referring to e.g. the output (`err`) returned through the `error` exit of `sendNativeQuery()` in this driver.',
29+
required: true,
30+
example: '==='
31+
},
32+
33+
meta:
34+
require('../constants/meta.input')
35+
36+
},
37+
38+
39+
exits: {
40+
41+
success: {
42+
description: 'The normalization is complete. If the error cannot be normalized into any other more specific footprint, then the catchall footprint will be returned.',
43+
outputVariableName: 'report',
44+
outputDescription: 'The `footprint` property is the normalized "footprint" representing the provided raw error. Conforms to one of a handful of standardized footprint types expected by the Waterline driver interface. The `meta` property is reserved for custom adapter-specific extensions.',
45+
example: {
46+
footprint: {},
47+
meta: '==='
48+
}
49+
},
50+
51+
},
52+
53+
54+
fn: function (inputs, exits) {
55+
56+
var footprint = { identity: 'catchall' };
57+
switch (inputs.queryType){
58+
case 'select':
59+
break;
60+
61+
case 'insert':
62+
case 'update':
63+
// TODO: negotiate `notUnique` error.
64+
//
65+
// For implementation help w/ building `columns`, see:
66+
// • https://github.com/balderdashy/sails-postgresql/blob/a51b3643777dcf1af5517acbf76e09612d36b301/lib/adapter.js#L1308
67+
// if (inputs.nativeQueryError.??????) {
68+
// footprint.identity = 'notUnique';
69+
// footprint.columns = [ 'email_address' ];
70+
// }
71+
break;
72+
73+
case 'delete':
74+
break;
75+
76+
default:
77+
78+
}
79+
80+
return exits.success({
81+
footprint: footprint
82+
});
83+
}
84+
85+
86+
};

machines/parse-native-query-result.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
module.exports = {
2+
3+
4+
friendlyName: 'Parse native query result',
5+
6+
7+
description: 'Parse a raw result from a native query and normalize it for the specified query type.',
8+
9+
10+
cacheable: true,
11+
12+
13+
sync: true,
14+
15+
16+
inputs: {
17+
18+
queryType: {
19+
description: 'The type of query operation this raw result came from.',
20+
extendedDescription: 'Either "select", "insert", "delete", or "update". This determines how the provided raw result will be parsed/coerced.',
21+
moreInfoUrl: 'https://github.com/particlebanana/waterline-query-builder/blob/master/docs/syntax.md',
22+
required: true,
23+
example: 'select',// (select|insert|delete|update)
24+
},
25+
26+
nativeQueryResult: {
27+
description: 'The result data sent back from the the database as a result of a native query.',
28+
extendedDescription: 'The provided data will be coerced to a JSON-serializable value if it isn\'t one already (see [rttc.dehydrate()](https://github.com/node-machine/rttc#dehydratevalue-allownullfalse-dontstringifyfunctionsfalse)). That means any Date instances therein will be converted to timezone-agnostic ISO timestamp strings (i.e. JSON timestamps).',
29+
required: true,
30+
example: '*'
31+
},
32+
33+
meta:
34+
require('../constants/meta.input')
35+
36+
},
37+
38+
39+
exits: {
40+
41+
success: {
42+
description: 'The result was successfully normalized.',
43+
outputVariableName: 'report',
44+
outputDescription: 'The `result` property is the normalized version of the raw result originally provided. The `meta` property is reserved for custom adapter-specific extensions.',
45+
example: {
46+
result: '*',
47+
meta: '==='
48+
}
49+
},
50+
51+
},
52+
53+
54+
fn: function (inputs, exits) {
55+
56+
var normalizedResult;
57+
switch (inputs.queryType){
58+
case 'select':
59+
normalizedResult = inputs.nativeQueryResult.rows;
60+
break;
61+
62+
case 'insert':
63+
normalizedResult = {
64+
inserted: inputs.nativeQueryResult.oid
65+
// TODO ^validate that this actually works
66+
};
67+
break;
68+
69+
case 'update':
70+
normalizedResult = {
71+
numRecordsUpdated: inputs.nativeQueryResult.rowCount
72+
};
73+
break;
74+
75+
case 'delete':
76+
normalizedResult = {
77+
numRecordsDeleted: inputs.nativeQueryResult.rowCount
78+
};
79+
break;
80+
81+
default:
82+
83+
}
84+
85+
return exits.success({
86+
result: normalizedResult
87+
});
88+
}
89+
90+
91+
};

machines/send-native-query.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,7 @@ module.exports = {
4040
},
4141

4242
badConnection:
43-
require('../constants/badConnection.exit'),
44-
45-
notUnique: {
46-
friendlyName: 'Not unique',
47-
description: 'The provided query failed because it would violate one or more uniqueness constraints.',
48-
outputVariableName: 'report',
49-
outputDescription: 'The `columns` property is an array containing the names of columns with uniquness constraint violations. The `error` property is a JavaScript Error instance containing the raw error from the database. The `meta` property is reserved for custom adapter-specific extensions.',
50-
example: {
51-
columns: [ 'email_address' ],
52-
error: '===',
53-
meta: '==='
54-
}
55-
}
43+
require('../constants/badConnection.exit')
5644

5745
},
5846

@@ -84,10 +72,6 @@ module.exports = {
8472
// Send native query
8573
inputs.connection.client.query(sql, bindings, function query(err, result) {
8674
if (err) {
87-
// TODO: negotiate `notUnique` error.
88-
//
89-
// For implementation help w/ building `columns`, see:
90-
// • https://github.com/balderdashy/sails-postgresql/blob/a51b3643777dcf1af5517acbf76e09612d36b301/lib/adapter.js#L1308
9175
return exits.error(err);
9276
}
9377

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"get-connection",
3333
"release-connection",
3434
"send-native-query",
35+
"parse-native-query-result",
36+
"parse-native-query-error",
3537
"begin-transaction",
3638
"commit-transaction",
3739
"rollback-transaction",

0 commit comments

Comments
 (0)