meadow-endpoints 3.0.7 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/Dockerfile_LUXURYCode +1 -1
  2. package/README.md +48 -14
  3. package/debug/Animal.json +62 -0
  4. package/debug/Harness-Configuration.json +31 -0
  5. package/debug/Harness.js +7 -108
  6. package/debug/KillHarness.sh +10 -0
  7. package/dist/meadowendpoints.js +4402 -0
  8. package/dist/meadowendpoints.min.js +92 -0
  9. package/dist/meadowendpoints.min.js.map +1 -0
  10. package/gulpfile.js +83 -0
  11. package/package.json +27 -15
  12. package/source/Meadow-Endpoints-Browser-Shim.js +14 -0
  13. package/source/Meadow-Endpoints.js +176 -565
  14. package/source/controller/Meadow-Endpoints-Controller-Base.js +161 -0
  15. package/source/controller/components/Meadow-Endpoints-Controller-BehaviorInjection.js +125 -0
  16. package/source/controller/components/Meadow-Endpoints-Controller-Error-StatusCodes.txt +189 -0
  17. package/source/controller/components/Meadow-Endpoints-Controller-Error.js +118 -0
  18. package/source/controller/components/Meadow-Endpoints-Controller-Log.js +103 -0
  19. package/source/controller/utility/Meadow-Endpoints-Filter-Parser.js +225 -0
  20. package/source/controller/utility/Meadow-Endpoints-Session-Marshaler.js +48 -0
  21. package/source/controller/utility/Meadow-Endpoints-Stream-RecordArray.js +66 -0
  22. package/source/endpoints/count/Meadow-Endpoint-Count.js +49 -0
  23. package/source/endpoints/count/Meadow-Endpoint-CountBy.js +40 -0
  24. package/source/endpoints/create/Meadow-Endpoint-BulkCreate.js +53 -0
  25. package/source/endpoints/create/Meadow-Endpoint-Create.js +58 -0
  26. package/source/endpoints/create/Meadow-Operation-Create.js +83 -0
  27. package/source/endpoints/delete/Meadow-Endpoint-Delete.js +93 -0
  28. package/source/endpoints/delete/Meadow-Endpoint-Undelete.js +108 -0
  29. package/source/endpoints/read/Meadow-Endpoint-Read.js +72 -0
  30. package/source/endpoints/read/Meadow-Endpoint-ReadDistinctList.js +92 -0
  31. package/source/endpoints/read/Meadow-Endpoint-ReadLiteList.js +85 -0
  32. package/source/endpoints/read/Meadow-Endpoint-ReadMax.js +55 -0
  33. package/source/endpoints/read/Meadow-Endpoint-ReadSelectList.js +89 -0
  34. package/source/endpoints/read/Meadow-Endpoint-Reads.js +75 -0
  35. package/source/endpoints/read/Meadow-Endpoint-ReadsBy.js +100 -0
  36. package/source/{crud → endpoints/read}/Meadow-Marshal-DistinctList.js +4 -13
  37. package/source/{crud → endpoints/read}/Meadow-Marshal-LiteList.js +5 -14
  38. package/source/endpoints/schema/Meadow-Endpoint-New.js +36 -0
  39. package/source/endpoints/schema/Meadow-Endpoint-Schema.js +36 -0
  40. package/source/endpoints/schema/Meadow-Endpoint-Validate.js +41 -0
  41. package/source/endpoints/update/Meadow-Endpoint-BulkUpdate.js +50 -0
  42. package/source/endpoints/update/Meadow-Endpoint-Update.js +58 -0
  43. package/source/endpoints/update/Meadow-Operation-Update.js +115 -0
  44. package/source/endpoints/upsert/Meadow-Endpoint-BulkUpsert.js +52 -0
  45. package/source/endpoints/upsert/Meadow-Endpoint-Upsert.js +57 -0
  46. package/source/endpoints/upsert/Meadow-Operation-Upsert.js +137 -0
  47. package/test/MeadowEndpoints_basic_tests.js +50 -2408
  48. package/test_support/bookstore-api-endpoint-exercises.paw +0 -0
  49. package/test_support/bookstore-configuration.json +28 -0
  50. package/test_support/bookstore-import-books-run.js +1 -0
  51. package/test_support/bookstore-import-books.js +215 -0
  52. package/test_support/bookstore-serve-meadow-endpoint-apis-IPC.js +138 -0
  53. package/test_support/bookstore-serve-meadow-endpoint-apis-run.js +6 -0
  54. package/test_support/bookstore-serve-meadow-endpoint-apis.js +129 -0
  55. package/test_support/data/books.csv +10001 -0
  56. package/test_support/model/ddl/BookStore.ddl +66 -0
  57. package/test_support/model/generated_diagram/README.md +1 -0
  58. package/test_support/model/generated_diagram/Stricture_Output.dot +13 -0
  59. package/test_support/model/generated_diagram/Stricture_Output.png +0 -0
  60. package/test_support/model/generated_documentation/Dictionary.md +18 -0
  61. package/test_support/model/generated_documentation/Model-Author.md +20 -0
  62. package/test_support/model/generated_documentation/Model-Book.md +26 -0
  63. package/test_support/model/generated_documentation/Model-BookAuthorJoin.md +14 -0
  64. package/test_support/model/generated_documentation/Model-BookPrice.md +25 -0
  65. package/test_support/model/generated_documentation/Model-Review.md +22 -0
  66. package/test_support/model/generated_documentation/ModelChangeTracking.md +17 -0
  67. package/test_support/model/generated_documentation/README.md +1 -0
  68. package/test_support/model/manual_scripts/DropTables.sql +5 -0
  69. package/test_support/model/manual_scripts/README.md +2 -0
  70. package/test_support/model/sql_create/BookStore-CreateDatabase.mysql.sql +116 -0
  71. package/test_support/model/sql_create/README.md +1 -0
  72. package/test_support/test_old/Tests.js +3243 -0
  73. package/test_support/test_old/untitled.js +88 -0
  74. package/source/Meadow-Authenticator.js +0 -31
  75. package/source/Meadow-Authorizers.js +0 -214
  76. package/source/Meadow-BehaviorModifications.js +0 -170
  77. package/source/Meadow-CommonServices.js +0 -206
  78. package/source/Meadow-MarshallSessionData.js +0 -64
  79. package/source/Restify-RouteParser.js +0 -114
  80. package/source/authorizers/Meadow-Authorizer-Allow.js +0 -17
  81. package/source/authorizers/Meadow-Authorizer-Deny.js +0 -17
  82. package/source/authorizers/Meadow-Authorizer-Mine.js +0 -47
  83. package/source/authorizers/Meadow-Authorizer-MyCustomer.js +0 -48
  84. package/source/crud/Meadow-Endpoint-BulkCreate.js +0 -67
  85. package/source/crud/Meadow-Endpoint-BulkUpdate.js +0 -74
  86. package/source/crud/Meadow-Endpoint-BulkUpsert.js +0 -76
  87. package/source/crud/Meadow-Endpoint-Count.js +0 -93
  88. package/source/crud/Meadow-Endpoint-CountBy.js +0 -101
  89. package/source/crud/Meadow-Endpoint-Create.js +0 -77
  90. package/source/crud/Meadow-Endpoint-Delete.js +0 -139
  91. package/source/crud/Meadow-Endpoint-Read.js +0 -109
  92. package/source/crud/Meadow-Endpoint-ReadDistinctList.js +0 -146
  93. package/source/crud/Meadow-Endpoint-ReadLiteList.js +0 -139
  94. package/source/crud/Meadow-Endpoint-ReadMax.js +0 -86
  95. package/source/crud/Meadow-Endpoint-ReadSelectList.js +0 -145
  96. package/source/crud/Meadow-Endpoint-Reads.js +0 -129
  97. package/source/crud/Meadow-Endpoint-ReadsBy.js +0 -155
  98. package/source/crud/Meadow-Endpoint-Undelete.js +0 -161
  99. package/source/crud/Meadow-Endpoint-Update.js +0 -80
  100. package/source/crud/Meadow-Endpoint-Upsert.js +0 -78
  101. package/source/crud/Meadow-Operation-Create.js +0 -105
  102. package/source/crud/Meadow-Operation-Update.js +0 -145
  103. package/source/crud/Meadow-Operation-Upsert.js +0 -106
  104. package/source/crud/Meadow-StreamRecordArray.js +0 -45
  105. package/source/schema/Meadow-Endpoint-New.js +0 -37
  106. package/source/schema/Meadow-Endpoint-Schema.js +0 -37
  107. package/source/schema/Meadow-Endpoint-Validate.js +0 -43
  108. package/test/Animal.json +0 -140
  109. package/test/MeadowEndpoints_disabledAuth_tests.js +0 -1325
  110. package/test/MeadowEndpoints_trustedSession_tests.js +0 -1731
  111. package/test/load/artillery-low.yml +0 -10
  112. package/test/load/cloud9setup.sh +0 -25
  113. package/test/load/package.json +0 -19
  114. package/test/load/test-schema-initializedatabase.sql +0 -29
  115. package/test/load/test-schema.json +0 -119
  116. package/test/load/test-server.js +0 -157
  117. package/test/scripts/InitializeDatabase-C9.sql +0 -7
  118. /package/{test/schemas → test_support/model}/json_schema/BookStore-Extended.json +0 -0
  119. /package/{test/schemas → test_support/model}/json_schema/BookStore-PICT.json +0 -0
  120. /package/{test/schemas → test_support/model}/json_schema/BookStore.json +0 -0
  121. /package/{test/schemas → test_support/model}/json_schema/README.md +0 -0
  122. /package/{test/schemas → test_support/model}/meadow_schema/BookStore-MeadowSchema-Author.json +0 -0
  123. /package/{test/schemas → test_support/model}/meadow_schema/BookStore-MeadowSchema-Book.json +0 -0
  124. /package/{test/schemas → test_support/model}/meadow_schema/BookStore-MeadowSchema-BookAuthorJoin.json +0 -0
  125. /package/{test/schemas → test_support/model}/meadow_schema/BookStore-MeadowSchema-BookPrice.json +0 -0
  126. /package/{test/schemas → test_support/model}/meadow_schema/BookStore-MeadowSchema-Review.json +0 -0
  127. /package/{test/schemas → test_support/model}/meadow_schema/README.md +0 -0
@@ -1,64 +0,0 @@
1
- /**
2
- * Meadow Session Marshaller - Extract session data based on configuration.
3
- *
4
- * @license MIT
5
- *
6
- * @author Alex Decker <alex.decker@headlight.com>
7
- * @module Meadow
8
- */
9
-
10
- module.exports = (pFable) =>
11
- {
12
- const _Fable = pFable;
13
- const _SessionDataSource = _Fable.settings.MeadowEndpointsSessionDataSource || 'Request';
14
-
15
- return (pRequest, pResponse, fNext) =>
16
- {
17
- let session;
18
-
19
- switch (_SessionDataSource)
20
- {
21
- default:
22
- _Fable.log.warn(`Unknown session source configured: ${_SessionDataSource} - defaulting to Request for backward compatibility`);
23
- case 'Request':
24
- // noop - already set by orator-session
25
- session = pRequest.UserSession;
26
- break;
27
- case 'None':
28
- break;
29
- case 'Header':
30
- try
31
- {
32
- const sessionStr = pRequest.headers['x-trusted-session'];
33
- if (!sessionStr)
34
- {
35
- break;
36
- }
37
- session = JSON.parse(sessionStr);
38
- }
39
- catch (pError)
40
- {
41
- _Fable.log.error('Error marshalling session data from header.', { Error: pError.message, Stack: pError.stack });
42
- }
43
- break;
44
- }
45
-
46
- if (!session)
47
- {
48
- // blank session so things don't break, for now
49
- session =
50
- {
51
- SessionID: '',
52
- UserID: 0,
53
- UserRole: '',
54
- UserRoleIndex: 0,
55
- LoggedIn: false,
56
- DeviceID: '',
57
- CustomerID: 0,
58
- };
59
- }
60
-
61
- pRequest.UserSession = session;
62
- fNext();
63
- }
64
- };
@@ -1,114 +0,0 @@
1
- //var assert = require('assert');
2
- var url = require('url');
3
-
4
- /**
5
- * given a request, try to match it against the regular expression to
6
- * get the route params.
7
- * i.e., /foo/:param1/:param2
8
- * @private
9
- * @function matchURL
10
- * @param {String | RegExp} re a string or regular expression
11
- * @param {Object} req the request object
12
- * @returns {Object}
13
- */
14
- function matchURL(re, req) {
15
- var i = 0;
16
- var result = re.exec(req.path());
17
- var params = {};
18
-
19
- if (!result) {
20
- return (false);
21
- }
22
-
23
- // This means the user original specified a regexp match, not a url
24
- // string like /:foo/:bar
25
- if (!re.restifyParams) {
26
- for (i = 1; i < result.length; i++) {
27
- params[(i - 1)] = result[i];
28
- }
29
-
30
- return (params);
31
- }
32
-
33
- // This was a static string, like /foo
34
- if (re.restifyParams.length === 0) {
35
- return (params);
36
- }
37
-
38
- // This was the "normal" case, of /foo/:id
39
- re.restifyParams.forEach(function (p) {
40
- if (++i < result.length) {
41
- params[p] = result[i];
42
- }
43
- });
44
-
45
- return (params);
46
- }
47
-
48
- var _RegexCache = {};
49
-
50
- function compileURL(options) {
51
- if (options.url instanceof RegExp)
52
- return (options.url);
53
- //assert.string(options.url, 'url');
54
- if (_RegexCache[options.url])
55
- {
56
- return _RegexCache[options.url];
57
- }
58
-
59
- var params = [];
60
- var pattern = '^';
61
- var re;
62
- var _url = url.parse(options.url).pathname;
63
- _url.split('/').forEach(function (frag) {
64
- if (frag.length <= 0)
65
- return (false);
66
-
67
- pattern += '\\/+';
68
- if (frag.charAt(0) === ':') {
69
- var label = frag;
70
- var index = frag.indexOf('(');
71
- var subexp;
72
- if (index === -1) {
73
- if (options.urlParamPattern) {
74
- subexp = options.urlParamPattern;
75
- } else {
76
- subexp = '[^/]*';
77
- }
78
- } else {
79
- label = frag.substring(0, index);
80
- subexp = frag.substring(index+1, frag.length-1);
81
- }
82
- pattern += '(' + subexp + ')';
83
- params.push(label.slice(1));
84
- } else {
85
- pattern += frag;
86
- }
87
- return (true);
88
- });
89
-
90
- if (pattern === '^')
91
- pattern += '\\/';
92
- pattern += '$';
93
-
94
- re = new RegExp(pattern, options.flags);
95
- re.restifyParams = params;
96
-
97
- _RegexCache[options.url] = re;
98
-
99
- return (re);
100
- }
101
-
102
- module.exports = function parseParams(pRoute, pRequestPath)
103
- {
104
- var tmpPath = compileURL({url: pRoute});
105
-
106
- var tmpReq = {
107
- path: function()
108
- {
109
- return pRequestPath;
110
- }
111
- };
112
-
113
- return matchURL(tmpPath, tmpReq);
114
- }
@@ -1,17 +0,0 @@
1
- // ##### Part of the **[retold](https://stevenvelozo.github.io/retold/)** system
2
- /**
3
- * @license MIT
4
- * @author <steven@velozo.com>
5
- */
6
-
7
- /**
8
- * Meadow Authorizer - Always Allow Access
9
- */
10
- var doAuthorize = function(pRequest, fNext)
11
- {
12
- pRequest.MeadowAuthorization = true;
13
-
14
- return fNext();
15
- };
16
-
17
- module.exports = doAuthorize;
@@ -1,17 +0,0 @@
1
- // ##### Part of the **[retold](https://stevenvelozo.github.io/retold/)** system
2
- /**
3
- * @license MIT
4
- * @author <steven@velozo.com>
5
- */
6
-
7
- /**
8
- * Meadow Authorizer - Always Deny Access
9
- */
10
- var doAuthorize = function(pRequest, fNext)
11
- {
12
- pRequest.MeadowAuthorization = false;
13
-
14
- return fNext();
15
- };
16
-
17
- module.exports = doAuthorize;
@@ -1,47 +0,0 @@
1
- // ##### Part of the **[retold](https://stevenvelozo.github.io/retold/)** system
2
- /**
3
- * @license MIT
4
- * @author <steven@velozo.com>
5
- */
6
-
7
- /**
8
- * Meadow Authorizer - Records from My Customer
9
- */
10
- var doAuthorize = function(pRequest, fNext)
11
- {
12
- if (pRequest.EndpointHash === 'Count' ||
13
- pRequest.EndpointHash === 'CountBy')
14
- {
15
- pRequest.MeadowAuthorization = true;
16
- return fNext();
17
- }
18
- if (typeof(pRequest.Record) !== 'object')
19
- {
20
- // If there is no record, fail.
21
- pRequest.MeadowAuthorization = false;
22
- return fNext();
23
- }
24
-
25
- if (pRequest.Record.hasOwnProperty('CreatingIDUser'))
26
- {
27
- if (pRequest.Record.CreatingIDUser === pRequest.UserSession.UserID)
28
- {
29
- // If the UserID matches
30
- pRequest.MeadowAuthorization = (true && pRequest.MeadowAuthorization);
31
- }
32
- else
33
- {
34
- // DENY If the user IDs don't match
35
- pRequest.MeadowAuthorization = false;
36
- }
37
- }
38
- else
39
- {
40
- // This will pass records that don't have a CreatingIDUser. Do we want that?
41
- pRequest.MeadowAuthorization = true;
42
- }
43
-
44
- return fNext();
45
- };
46
-
47
- module.exports = doAuthorize;
@@ -1,48 +0,0 @@
1
- // ##### Part of the **[retold](https://stevenvelozo.github.io/retold/)** system
2
- /**
3
- * @license MIT
4
- * @author <steven@velozo.com>
5
- */
6
-
7
- /**
8
- * Meadow Authorizer - Records from My Customer
9
- */
10
- var doAuthorize = function(pRequest, fNext)
11
- {
12
- if (pRequest.EndpointHash === 'Count' ||
13
- pRequest.EndpointHash === 'CountBy')
14
- {
15
- pRequest.MeadowAuthorization = true;
16
- return fNext();
17
- }
18
- if (typeof(pRequest.Record) !== 'object')
19
- {
20
- // If there is no record, fail.
21
- pRequest.MeadowAuthorization = false;
22
- return fNext();
23
- }
24
-
25
- if (pRequest.Record.hasOwnProperty('IDCustomer'))
26
- {
27
- if (pRequest.Record.IDCustomer === 0 ||
28
- pRequest.Record.IDCustomer === pRequest.UserSession.CustomerID)
29
- {
30
- // If the customer matches
31
- pRequest.MeadowAuthorization = (true && pRequest.MeadowAuthorization);
32
- }
33
- else
34
- {
35
- // Deny if Customer ID's don't match
36
- pRequest.MeadowAuthorization = false;
37
- }
38
- }
39
- else
40
- {
41
- // This will pass records that don't have a CustomerID. Do we want that?
42
- pRequest.MeadowAuthorization = true;
43
- }
44
-
45
- return fNext();
46
- };
47
-
48
- module.exports = doAuthorize;
@@ -1,67 +0,0 @@
1
- /**
2
- * Meadow Endpoint - Create a set of Record in Bulk
3
- *
4
- * @license MIT
5
- *
6
- * @author Steven Velozo <steven@velozo.com>
7
- * @module Meadow
8
- */
9
- /**
10
- * Bulk Create records using the Meadow DAL object
11
- */
12
-
13
- var libAsync = require('async');
14
-
15
- var doCreate = require('./Meadow-Operation-Create.js');
16
- const streamRecordsToResponse = require('./Meadow-StreamRecordArray');
17
-
18
- var doAPIBulkCreateEndpoint = function(pRequest, pResponse, fNext)
19
- {
20
- // Configure the request for the generic create operation
21
- pRequest.CreatedRecords = [];
22
- pRequest.MeadowOperation = 'CreateBulk';
23
-
24
- libAsync.waterfall(
25
- [
26
- function(fStageComplete)
27
- {
28
- //1. Validate request body to ensure it is a valid record
29
- if (!Array.isArray(pRequest.body))
30
- {
31
- return pRequest.CommonServices.sendError('Bulk record create failure - a valid array of records is required.', pRequest, pResponse, fNext);
32
- }
33
-
34
- pRequest.BulkRecords = pRequest.body;
35
-
36
- return fStageComplete(null);
37
- },
38
- function(fStageComplete)
39
- {
40
- pRequest.Response = pResponse;
41
- pRequest.BehaviorModifications.runBehavior('Create-PreRequest', pRequest, fStageComplete);
42
- },
43
- function(fStageComplete)
44
- {
45
- libAsync.eachSeries(pRequest.BulkRecords,
46
- function (pRecord, fCallback)
47
- {
48
- doCreate(pRecord, pRequest, pResponse, fCallback);
49
- }, fStageComplete);
50
- },
51
- function(fStageComplete)
52
- {
53
- //5. Respond with the new records
54
- return streamRecordsToResponse(pResponse, pRequest.CreatedRecords, fStageComplete);
55
- }
56
- ], function(pError)
57
- {
58
- if (pError)
59
- {
60
- return pRequest.CommonServices.sendCodedError('Error bulk creating a record.', pError, pRequest, pResponse, fNext);
61
- }
62
-
63
- return fNext();
64
- });
65
- };
66
-
67
- module.exports = doAPIBulkCreateEndpoint;
@@ -1,74 +0,0 @@
1
- /**
2
- * Meadow Endpoint - Update a set of Records
3
- *
4
- * @license MIT
5
- *
6
- * @author Steven Velozo <steven@velozo.com>
7
- * @module Meadow
8
- */
9
- /**
10
- * Update a set of records using the Meadow DAL object
11
- */
12
-
13
- var libAsync = require('async');
14
-
15
- var doUpdate = require('./Meadow-Operation-Update.js');
16
- const streamRecordsToResponse = require('./Meadow-StreamRecordArray');
17
-
18
- var doAPIUpdateEndpoint = function(pRequest, pResponse, fNext)
19
- {
20
- // This state is the requirement for the UserRoleIndex value in the UserSession object... processed by default as >=
21
- // The default here is that any authenticated user can use this endpoint.
22
- pRequest.EndpointAuthorizationRequirement = pRequest.EndpointAuthorizationLevels.Update;
23
-
24
- // INJECT: Pre authorization (for instance to change the authorization level)
25
-
26
- if (pRequest.CommonServices.authorizeEndpoint(pRequest, pResponse, fNext) === false)
27
- {
28
- // If this endpoint fails, it's sent an error automatically.
29
- return;
30
- }
31
-
32
- // Configure the request for the generic update operation
33
- pRequest.UpdatedRecords = [];
34
- pRequest.MeadowOperation = 'UpdateBulk';
35
-
36
- libAsync.waterfall(
37
- [
38
- function(fStageComplete)
39
- {
40
- //1. Validate request body to ensure it is a valid record
41
- if (!Array.isArray(pRequest.body))
42
- {
43
- return pRequest.CommonServices.sendError('Record update failure - a valid record is required.', pRequest, pResponse, fNext);
44
- }
45
-
46
- pRequest.BulkRecords = pRequest.body;
47
-
48
- return fStageComplete(null);
49
- },
50
- function(fStageComplete)
51
- {
52
- libAsync.eachSeries(pRequest.BulkRecords,
53
- function (pRecord, fCallback)
54
- {
55
- doUpdate(pRecord, pRequest, pResponse, fCallback);
56
- }, fStageComplete);
57
- },
58
- function(fStageComplete)
59
- {
60
- //5. Respond with the new record
61
- return streamRecordsToResponse(pResponse, pRequest.UpdatedRecords, fStageComplete);
62
- }
63
- ], function(pError)
64
- {
65
- if (pError)
66
- {
67
- return pRequest.CommonServices.sendCodedError('Error bulk updating records.', pError, pRequest, pResponse, fNext);
68
- }
69
-
70
- return fNext();
71
- });
72
- };
73
-
74
- module.exports = doAPIUpdateEndpoint;
@@ -1,76 +0,0 @@
1
- /**
2
- * Meadow Endpoint - Upsert a set of Records
3
- *
4
- * @license MIT
5
- *
6
- * @author Steven Velozo <steven@velozo.com>
7
- * @module Meadow
8
- */
9
- /**
10
- * Upsert a set of records using the Meadow DAL object
11
- */
12
-
13
- var libAsync = require('async');
14
-
15
- var doUpsert = require('./Meadow-Operation-Upsert.js');
16
- var marshalLiteList = require('./Meadow-Marshal-LiteList.js');
17
- const streamRecordsToResponse = require('./Meadow-StreamRecordArray');
18
-
19
- var doAPIUpsertEndpoint = function(pRequest, pResponse, fNext)
20
- {
21
- // This state is the requirement for the UserRoleIndex value in the UserSession object... processed by default as >=
22
- // The default here is that any authenticated user can use this endpoint.
23
- pRequest.EndpointAuthorizationRequirement = pRequest.EndpointAuthorizationLevels.Update;
24
-
25
- // INJECT: Pre authorization (for instance to change the authorization level)
26
- if (pRequest.CommonServices.authorizeEndpoint(pRequest, pResponse, fNext) === false)
27
- {
28
- // If this endpoint fails, it's sent an error automatically.
29
- return;
30
- }
31
-
32
- // Configure the request for the generic upsert operation
33
- pRequest.CreatedRecords = [];
34
- pRequest.UpdatedRecords = [];
35
- pRequest.UpsertedRecords = [];
36
- pRequest.MeadowOperation = 'UpsertBulk';
37
-
38
- libAsync.waterfall(
39
- [
40
- function(fStageComplete)
41
- {
42
- //1. Validate request body to ensure it is a valid record
43
- if (!Array.isArray(pRequest.body))
44
- {
45
- return pRequest.CommonServices.sendError('Record upsert failure - a valid record is required.', pRequest, pResponse, fNext);
46
- }
47
-
48
- pRequest.BulkRecords = pRequest.body;
49
-
50
- return fStageComplete(null);
51
- },
52
- function(fStageComplete)
53
- {
54
- libAsync.eachSeries(pRequest.BulkRecords,
55
- function (pRecord, fCallback)
56
- {
57
- doUpsert(pRecord, pRequest, pResponse, fCallback);
58
- }, fStageComplete);
59
- },
60
- function(fStageComplete)
61
- {
62
- //5. Respond with the new records
63
- return streamRecordsToResponse(pResponse, marshalLiteList(pRequest.UpsertedRecords, pRequest), fStageComplete);
64
- }
65
- ], function(pError)
66
- {
67
- if (pError)
68
- {
69
- return pRequest.CommonServices.sendCodedError('Error bulk upserting records.', pError, pRequest, pResponse, fNext);
70
- }
71
-
72
- return fNext();
73
- });
74
- };
75
-
76
- module.exports = doAPIUpsertEndpoint;
@@ -1,93 +0,0 @@
1
- /**
2
- * Meadow Endpoint - Count a Record
3
- *
4
- * @license MIT
5
- *
6
- * @author Steven Velozo <steven@velozo.com>
7
- * @module Meadow
8
- */
9
-
10
- var libAsync = require('async');
11
- const meadowFilterParser = require('meadow-filter').parse;
12
- /**
13
- * Count a record using the Meadow DAL object
14
- */
15
- var doAPICountEndpoint = function(pRequest, pResponse, fNext)
16
- {
17
- // This state is the requirement for the UserRoleIndex value in the UserSession object... processed by default as >=
18
- // The default here is that any authenticated user can use this endpoint.
19
- pRequest.EndpointAuthorizationRequirement = pRequest.EndpointAuthorizationLevels.Count;
20
-
21
- if (pRequest.CommonServices.authorizeEndpoint(pRequest, pResponse, fNext) === false)
22
- {
23
- // If this endpoint fails, it's sent an error automatically.
24
- return;
25
- }
26
-
27
- libAsync.waterfall(
28
- [
29
- // 1: Create the query
30
- function (fStageComplete)
31
- {
32
- pRequest.Query = pRequest.DAL.query;
33
- if (typeof(pRequest.params.Filter) === 'string')
34
- {
35
- // If a filter has been passed in, parse it and add the values to the query.
36
- meadowFilterParser(pRequest.params.Filter, pRequest.Query);
37
- }
38
- else if (pRequest.params.Filter)
39
- {
40
- pRequest.Query.setFilter(pRequest.params.Filter);
41
- }
42
- fStageComplete(false);
43
- },
44
- // 2: Check if there is an authorizer set for this endpoint and user role combination, and authorize based on that
45
- function (fStageComplete)
46
- {
47
- pRequest.Authorizers.authorizeRequest('Count', pRequest, fStageComplete);
48
- },
49
- // 3. INJECT: Query configuration
50
- function (fStageComplete)
51
- {
52
- pRequest.BehaviorModifications.runBehavior('Reads-QueryConfiguration', pRequest, fStageComplete);
53
- },
54
- // 4: Check if authorization or post processing denied security access to the record
55
- function (fStageComplete)
56
- {
57
- if (pRequest.MeadowAuthorization)
58
- {
59
- // This will complete the waterfall operation
60
- return fStageComplete(false);
61
- }
62
-
63
- // It looks like this record was not authorized. Send an error.
64
- return fStageComplete({Code:405,Message:'UNAUTHORIZED ACCESS IS NOT ALLOWED'});
65
- },
66
- // 5: Do the count
67
- function (fStageComplete)
68
- {
69
- pRequest.DAL.doCount(pRequest.Query,
70
- function(pError, pQuery, pCount)
71
- {
72
- pRequest.Result = {Count:pCount};
73
-
74
- return fStageComplete(pError);
75
- });
76
- }
77
- ],
78
- // 6. Return the results to the user
79
- function(pError)
80
- {
81
- if (pError)
82
- {
83
- return pRequest.CommonServices.sendCodedError('Error retreiving a count.', pError, pRequest, pResponse, fNext);
84
- }
85
-
86
- pRequest.CommonServices.log.info('Delivered recordset count of ' + pRequest.Result.Count + '.', {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-Count'}, pRequest);
87
- pResponse.send(pRequest.Result);
88
- return fNext();
89
- }
90
- );
91
- };
92
-
93
- module.exports = doAPICountEndpoint;