Thomas 3 years ago
parent
commit
a881fc07b0
6 changed files with 36 additions and 15 deletions
  1. 3 2
      index.js
  2. 4 2
      index.ts
  3. 1 0
      package.json
  4. 11 4
      workers/esearch.js
  5. 12 7
      workers/esearch.ts
  6. 5 0
      yarn.lock

+ 3 - 2
index.js

@@ -60,9 +60,10 @@ const piscina_1 = __importDefault(require("piscina"));
             id: '19393038',
             retmode: 'xml',
             endpoint: 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi',
-            query: `**.AbstractText`
+            //query: `$..AbstractText[?(@.Label==='RESULTS')]`
+            query: `$..AbstractText`
         },
     ];
     const results = yield Promise.all(orders.map(e => getEsearch.run(e)));
-    console.log(results);
+    console.log(...results);
 }))();

+ 4 - 2
index.ts

@@ -29,6 +29,7 @@ import Piscina from "piscina"
     // const results = await Promise.all(orders.map(e => getEsearch.run(e)))
     // console.log(results);
 
+
     const orders =
     [
         // {
@@ -53,9 +54,10 @@ import Piscina from "piscina"
             id: '19393038',
             retmode: 'xml',
             endpoint: 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi',
-            query: `**.AbstractText`
+            //query: `$..AbstractText[?(@.Label==='RESULTS')]`
+            query: `$..AbstractText`
         },
     ]
     const results = await Promise.all(orders.map(e => getEsearch.run(e)))
-    console.log(results);
+    console.log(...results);
 })()

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "fast-xml-parser": "^4.0.3",
     "genbank-parser": "^1.2.4",
     "jsonata": "^1.8.6",
+    "jsonpath-plus": "^6.0.1",
     "piscina": "^3.2.0",
     "xml2json": "^0.12.0",
     "xmldom": "^0.6.0",

+ 11 - 4
workers/esearch.js

@@ -15,18 +15,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
 const axios_1 = __importDefault(require("axios"));
 const genbank_parser_1 = __importDefault(require("genbank-parser"));
 const fast_xml_parser_1 = require("fast-xml-parser");
-const jsonata_1 = __importDefault(require("jsonata"));
+const jsonpath_plus_1 = require("jsonpath-plus");
 const getEsearch = (params) => __awaiter(void 0, void 0, void 0, function* () {
     const endpoint = (params === null || params === void 0 ? void 0 : params.endpoint) ? params.endpoint : 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi';
     const query = (params === null || params === void 0 ? void 0 : params.query) || '';
     const allowedParams = ['id', 'db', 'retmode', 'rettype'];
-    // console.log('query :', `${endpoint}?${Object.keys(params).flatMap(k => allowedParams.includes(k) ? k + '=' + params[k] : []).join('&')}`);
     const response = yield axios_1.default.get(`${endpoint}?${Object.keys(params).flatMap(k => allowedParams.includes(k) ? k + '=' + params[k] : []).join('&')}`);
     let results = Array.isArray(response.data) ? response.data.join('') : response.data;
     if (params === null || params === void 0 ? void 0 : params.retmode) {
         switch (params.retmode) {
             case 'xml':
-                const parser = new fast_xml_parser_1.XMLParser({ ignoreAttributes: false, alwaysCreateTextNode: false, attributeNamePrefix: "attr-" });
+                const parser = new fast_xml_parser_1.XMLParser({
+                    ignoreAttributes: false,
+                    alwaysCreateTextNode: false,
+                    attributeNamePrefix: "",
+                    textNodeName: "value",
+                    allowBooleanAttributes: true
+                });
                 results = parser.parse(results);
                 break;
             case 'gb':
@@ -36,8 +41,10 @@ const getEsearch = (params) => __awaiter(void 0, void 0, void 0, function* () {
                 break;
         }
     }
+    // https://docs.jsonata.org/simple
+    // if (query !== '') results = jsonata(query).evaluate(results)
     if (query !== '')
-        results = (0, jsonata_1.default)(query).evaluate(results);
+        results = (0, jsonpath_plus_1.JSONPath)({ path: query, json: results });
     return results;
 });
 module.exports = (params) => __awaiter(void 0, void 0, void 0, function* () { return yield getEsearch(params); });

+ 12 - 7
workers/esearch.ts

@@ -2,15 +2,14 @@ import axios from 'axios'
 import genbankParser from 'genbank-parser'
 import { XMLParser } from 'fast-xml-parser'
 import jsonata from "jsonata"
+import {JSONPath} from 'jsonpath-plus';
 
-const getEsearch = async (
-    params: any
-) => {
+
+const getEsearch = async (params: any) => {
     const endpoint = params?.endpoint ? params.endpoint : 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi'
     const query = params?.query || ''
     const allowedParams = ['id', 'db', 'retmode', 'rettype']
     
-    // console.log('query :', `${endpoint}?${Object.keys(params).flatMap(k => allowedParams.includes(k) ? k + '=' + params[k] : []).join('&')}`);
     const response = await axios.get(`${endpoint}?${Object.keys(params).flatMap(k => allowedParams.includes(k) ? k + '=' + params[k] : []).join('&')}`)
     
     let results = Array.isArray(response.data) ? response.data.join('') : response.data
@@ -18,8 +17,12 @@ const getEsearch = async (
         switch (params.retmode) {
             case 'xml':
                 const parser = new XMLParser({
-                    ignoreAttributes : false, alwaysCreateTextNode: false, attributeNamePrefix : "attr-", allowBooleanAttributes: true
-            })
+                    ignoreAttributes : false, 
+                    alwaysCreateTextNode: false, 
+                    attributeNamePrefix : "",
+                    textNodeName: "value",
+                    allowBooleanAttributes: true
+                })
                 results = parser.parse(results)
                 break;
             case 'gb':
@@ -30,7 +33,9 @@ const getEsearch = async (
         }
     }
 
-    if (query !== '') results = jsonata(query).evaluate(results)
+    // https://docs.jsonata.org/simple
+    // if (query !== '') results = jsonata(query).evaluate(results)
+    if (query !== '') results = JSONPath({path: query, json: results})
 
     return results
 }

+ 5 - 0
yarn.lock

@@ -127,6 +127,11 @@ jsonata@^1.8.6:
   resolved "https://registry.yarnpkg.com/jsonata/-/jsonata-1.8.6.tgz#e5f0e6ace870a34bac881a182ca2b31227122791"
   integrity sha512-ZH2TPYdNP2JecOl/HvrH47Xc+9imibEMQ4YqKy/F/FrM+2a6vfbGxeCX23dB9Fr6uvGwv+ghf1KxWB3iZk09wA==
 
+jsonpath-plus@^6.0.1:
+  version "6.0.1"
+  resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-6.0.1.tgz#9a3e16cedadfab07a3d8dc4e8cd5df4ed8f49c4d"
+  integrity sha512-EvGovdvau6FyLexFH2OeXfIITlgIbgZoAZe3usiySeaIDm5QS+A10DKNpaPBBqqRSZr2HN6HVNXxtwUAr2apEw==
+
 nan@^2.13.2:
   version "2.15.0"
   resolved "https://registry.npmjs.org/nan/-/nan-2.15.0.tgz"