|
1 |
| -/* |
2 |
| - * Copyright 2014-2015 MarkLogic Corporation |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -// module that exports get, post, put and delete |
18 |
| -function get(context, params) { |
19 |
| - context.outputTypes = ["application/json"]; |
20 |
| - var arg1 = params.arg1; |
21 |
| - var arg2 = params.arg2; |
22 |
| - var x = arg1.toString(); |
23 |
| - |
24 |
| - |
25 |
| -return { |
26 |
| - "argument1": x, |
27 |
| - "argument2": arg2, |
28 |
| - "database-name":xdmp.databaseName(xdmp.database()), |
29 |
| - "document-count":fn.count(fn.doc()), |
30 |
| - "content": "This is a JSON document", |
31 |
| - "document-content": fn.doc(), |
32 |
| - "response": xdmp.getResponseCode(), |
33 |
| - "outputTypes": context.outputTypes, |
34 |
| - |
35 |
| - } |
36 |
| -}; |
37 |
| -function post(context, params, input) { |
38 |
| - |
39 |
| - var argUrl = params.uri; |
40 |
| - |
41 |
| - xdmp.eval(" \ |
42 |
| - declareUpdate(); \ |
43 |
| - var argUrl; \ |
44 |
| - var sibling=cts.doc(argUrl).root.content; \ |
45 |
| - var inputObject; \ |
46 |
| - var newNode = new NodeBuilder(); \ |
47 |
| - newNode.addNode(inputObject); \ |
48 |
| - var named = newNode.toNode().xpath('.//array-node()'); xdmp.nodeInsertAfter(sibling,named);\ |
49 |
| - ",{"inputObject":input,"argUrl":argUrl},{"isolation":"different-transaction"}); |
50 |
| - return ({"response": xdmp.getResponseCode()}) |
51 |
| - |
52 |
| - }; |
53 |
| - |
54 |
| -// Function responding to PUT method - must use local name 'put'. |
55 |
| -function put(context, params, input) { |
56 |
| - var argUrl = params.uri; |
57 |
| - var inputObject = input; |
58 |
| - //xdmp.documentInsert(argUrl,input); |
59 |
| - xdmp.eval("declareUpdate(); var argUrl; var input;xdmp.documentInsert(argUrl,input)",{"argUrl":argUrl,"input":inputObject},{"isolation":"different-transaction"}); |
60 |
| - var count = xdmp.eval("fn.count(fn.doc())"); |
61 |
| - xdmp.log(count); |
62 |
| - return ({"response": xdmp.getResponseCode()}) |
63 |
| -}; |
64 |
| - |
65 |
| -// Function responding to DELETE method - must use local name 'delete'. |
66 |
| -function deleteFunction(context, params) { |
67 |
| - var docuri = params.uri; |
68 |
| - xdmp.documentDelete(docuri); |
69 |
| - return({"response": xdmp.getResponseCode()}) |
70 |
| -}; |
71 |
| -exports.GET = get; |
72 |
| -exports.POST = post; |
73 |
| -exports.PUT = put; |
74 |
| -exports.DELETE = deleteFunction; |
| 1 | +/* |
| 2 | + * Copyright 2014-2017 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// module that exports get, post, put and delete |
| 18 | +function get(context, params) { |
| 19 | + context.outputTypes = ["application/json"]; |
| 20 | + var arg1 = params.arg1; |
| 21 | + var arg2 = params.arg2; |
| 22 | + var x = arg1.toString(); |
| 23 | + |
| 24 | + |
| 25 | +return { |
| 26 | + "argument1": x, |
| 27 | + "argument2": arg2, |
| 28 | + "database-name":xdmp.databaseName(xdmp.database()), |
| 29 | + "document-count":fn.count(fn.doc()), |
| 30 | + "content": "This is a JSON document", |
| 31 | + "document-content": fn.doc(), |
| 32 | + "response": xdmp.getResponseCode(), |
| 33 | + "outputTypes": context.outputTypes, |
| 34 | + |
| 35 | + } |
| 36 | +}; |
| 37 | +function post(context, params, input) { |
| 38 | + |
| 39 | + var argUrl = params.uri; |
| 40 | + |
| 41 | + xdmp.eval(" \ |
| 42 | + declareUpdate(); \ |
| 43 | + var argUrl; \ |
| 44 | + var sibling=cts.doc(argUrl).root.content; \ |
| 45 | + var inputObject; \ |
| 46 | + var newNode = new NodeBuilder(); \ |
| 47 | + newNode.addNode(inputObject); \ |
| 48 | + var named = newNode.toNode().xpath('.//array-node()'); xdmp.nodeInsertAfter(sibling,named);\ |
| 49 | + ",{"inputObject":input,"argUrl":argUrl},{"isolation":"different-transaction"}); |
| 50 | + return ({"response": xdmp.getResponseCode()}) |
| 51 | + |
| 52 | + }; |
| 53 | + |
| 54 | +// Function responding to PUT method - must use local name 'put'. |
| 55 | +function put(context, params, input) { |
| 56 | + var argUrl = params.uri; |
| 57 | + var inputObject = input; |
| 58 | + //xdmp.documentInsert(argUrl,input); |
| 59 | + xdmp.eval("declareUpdate(); var argUrl; var input;xdmp.documentInsert(argUrl,input)",{"argUrl":argUrl,"input":inputObject},{"isolation":"different-transaction"}); |
| 60 | + var count = xdmp.eval("fn.count(fn.doc())"); |
| 61 | + xdmp.log(count); |
| 62 | + return ({"response": xdmp.getResponseCode()}) |
| 63 | +}; |
| 64 | + |
| 65 | +// Function responding to DELETE method - must use local name 'delete'. |
| 66 | +function deleteFunction(context, params) { |
| 67 | + var docuri = params.uri; |
| 68 | + xdmp.documentDelete(docuri); |
| 69 | + return({"response": xdmp.getResponseCode()}) |
| 70 | +}; |
| 71 | +exports.GET = get; |
| 72 | +exports.POST = post; |
| 73 | +exports.PUT = put; |
| 74 | +exports.DELETE = deleteFunction; |
0 commit comments