@natlibfi/melinda-rest-api-commons 2.3.0-alpha.3 → 2.3.0-alpha.4

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.
package/dist/mongoLog.js CHANGED
@@ -92,17 +92,23 @@ async function _default(MONGO_URI) {
92
92
  skip = 0,
93
93
  ...rest
94
94
  } = params;
95
- const result = await db.collection(collection).find(rest).limit(parseInt(limit, 10)).skip(parseInt(skip, 10)).toArray();
95
+ const result = await db.collection(collection) // eslint-disable-line functional/immutable-data
96
+ .find(rest).sort({
97
+ blobSequence: 1
98
+ }).skip(parseInt(skip, 10)).limit(parseInt(limit, 10)).toArray();
96
99
  logger.debug(result);
97
100
  logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items! (skip: ${skip} limit: ${limit})` : 'Not found!'}`);
98
101
  return result;
99
102
  }
100
103
 
101
- async function queryById(correlationIdString) {
104
+ async function queryById(correlationIdString, skip = 0, limit = 1) {
102
105
  logger.debug(`QueryById: ${correlationIdString}`);
103
- const result = await db.collection(collection).find({
106
+ const result = await db.collection(collection) // eslint-disable-line functional/immutable-data
107
+ .find({
104
108
  correlationId: correlationIdString
105
- }).toArray();
109
+ }).sort({
110
+ blobSequence: 1
111
+ }).skip(parseInt(skip, 10)).limit(parseInt(limit, 10)).toArray();
106
112
  logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items!` : 'Not found!'}`);
107
113
  return result;
108
114
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mongoLog.js","names":["MONGO_URI","logger","createLogger","client","MongoClient","connect","useNewUrlParser","useUnifiedTopology","db","collection","addLogItem","query","queryById","remove","logItem","time","moment","toDate","newLogItem","creationTime","result","insertOne","acknowledged","info","logItemType","correlationId","blobSequenceStart","blobSequenceEnd","ApiError","httpStatus","INTERNAL_SERVER_ERROR","error","logError","params","debug","JSON","stringify","limit","skip","rest","find","parseInt","toArray","length","correlationIdString","silly","clean","sanitize","deleteMany","err","message"],"sources":["../src/mongoLog.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MongoClient} from 'mongodb';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {logError} from './utils.js';\nimport moment from 'moment';\nimport httpStatus from 'http-status';\nimport sanitize from 'mongo-sanitize';\n\nexport default async function (MONGO_URI) {\n const logger = createLogger();\n\n // Connect to mongo (MONGO)\n const client = await MongoClient.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});\n const db = client.db('rest-api');\n const collection = 'logs';\n return {addLogItem, query, queryById, remove};\n\n async function addLogItem(logItem) {\n const time = moment().toDate();\n const newLogItem = {\n ...logItem,\n creationTime: time\n };\n try {\n const result = await db.collection(collection).insertOne(newLogItem);\n if (result.acknowledged) {\n logger.info(`*** New ${logItem.logItemType} added for ${logItem.correlationId} items ${logItem.blobSequenceStart}-${logItem.blobSequenceEnd}. ***`);\n return;\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n }\n\n async function query(params) {\n logger.debug(`Query params: ${JSON.stringify(params)}`);\n const {limit = 5, skip = 0, ...rest} = params;\n const result = await db.collection(collection)\n .find(rest)\n .limit(parseInt(limit, 10))\n .skip(parseInt(skip, 10))\n .toArray();\n logger.debug(result);\n logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items! (skip: ${skip} limit: ${limit})` : 'Not found!'}`);\n return result;\n }\n\n async function queryById(correlationIdString) {\n logger.debug(`QueryById: ${correlationIdString}`);\n const result = await db.collection(collection).find({correlationId: correlationIdString}).toArray();\n logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items!` : 'Not found!'}`);\n return result;\n }\n\n async function remove(params) {\n logger.silly(`${JSON.stringify(params)}`);\n logger.info(`Removing from Mongo (${collection}) correlationId: ${params.correlationId}`);\n const clean = sanitize(params.correlationId);\n logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);\n\n try {\n await db.collection(collection).deleteMany({correlationId: clean});\n return true;\n } catch (err) {\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n }\n}\n"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAlCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAUe,wBAAgBA,SAAhB,EAA2B;EACxC,MAAMC,MAAM,GAAG,IAAAC,mCAAA,GAAf,CADwC,CAGxC;;EACA,MAAMC,MAAM,GAAG,MAAMC,oBAAA,CAAYC,OAAZ,CAAoBL,SAApB,EAA+B;IAACM,eAAe,EAAE,IAAlB;IAAwBC,kBAAkB,EAAE;EAA5C,CAA/B,CAArB;EACA,MAAMC,EAAE,GAAGL,MAAM,CAACK,EAAP,CAAU,UAAV,CAAX;EACA,MAAMC,UAAU,GAAG,MAAnB;EACA,OAAO;IAACC,UAAD;IAAaC,KAAb;IAAoBC,SAApB;IAA+BC;EAA/B,CAAP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file":"mongoLog.js","names":["MONGO_URI","logger","createLogger","client","MongoClient","connect","useNewUrlParser","useUnifiedTopology","db","collection","addLogItem","query","queryById","remove","logItem","time","moment","toDate","newLogItem","creationTime","result","insertOne","acknowledged","info","logItemType","correlationId","blobSequenceStart","blobSequenceEnd","ApiError","httpStatus","INTERNAL_SERVER_ERROR","error","logError","params","debug","JSON","stringify","limit","skip","rest","find","sort","blobSequence","parseInt","toArray","length","correlationIdString","silly","clean","sanitize","deleteMany","err","message"],"sources":["../src/mongoLog.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MongoClient} from 'mongodb';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ApiError} from '@natlibfi/melinda-commons';\nimport {logError} from './utils.js';\nimport moment from 'moment';\nimport httpStatus from 'http-status';\nimport sanitize from 'mongo-sanitize';\n\nexport default async function (MONGO_URI) {\n const logger = createLogger();\n\n // Connect to mongo (MONGO)\n const client = await MongoClient.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});\n const db = client.db('rest-api');\n const collection = 'logs';\n return {addLogItem, query, queryById, remove};\n\n async function addLogItem(logItem) {\n const time = moment().toDate();\n const newLogItem = {\n ...logItem,\n creationTime: time\n };\n try {\n const result = await db.collection(collection).insertOne(newLogItem);\n if (result.acknowledged) {\n logger.info(`*** New ${logItem.logItemType} added for ${logItem.correlationId} items ${logItem.blobSequenceStart}-${logItem.blobSequenceEnd}. ***`);\n return;\n }\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n } catch (error) {\n logError(error);\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);\n }\n }\n\n async function query(params) {\n logger.debug(`Query params: ${JSON.stringify(params)}`);\n const {limit = 5, skip = 0, ...rest} = params;\n const result = await db.collection(collection) // eslint-disable-line functional/immutable-data\n .find(rest)\n .sort({blobSequence: 1})\n .skip(parseInt(skip, 10))\n .limit(parseInt(limit, 10))\n .toArray();\n logger.debug(result);\n logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items! (skip: ${skip} limit: ${limit})` : 'Not found!'}`);\n return result;\n }\n\n async function queryById(correlationIdString, skip = 0, limit = 1) {\n logger.debug(`QueryById: ${correlationIdString}`);\n const result = await db.collection(collection) // eslint-disable-line functional/immutable-data\n .find({correlationId: correlationIdString})\n .sort({blobSequence: 1})\n .skip(parseInt(skip, 10))\n .limit(parseInt(limit, 10))\n .toArray();\n logger.debug(`Query result: ${result.length > 0 ? `Found ${result.length} log items!` : 'Not found!'}`);\n return result;\n }\n\n async function remove(params) {\n logger.silly(`${JSON.stringify(params)}`);\n logger.info(`Removing from Mongo (${collection}) correlationId: ${params.correlationId}`);\n const clean = sanitize(params.correlationId);\n logger.silly(`mongo/remove: clean: ${JSON.stringify(clean)}`);\n\n try {\n await db.collection(collection).deleteMany({correlationId: clean});\n return true;\n } catch (err) {\n throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, err.message);\n }\n }\n}\n"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAlCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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,MAAM,CAACyB,MAAO,aAA3C,GAA0D,YAAa,EAArG;IACA,OAAOzB,MAAP;EACD;;EAED,eAAeP,MAAf,CAAsBoB,MAAtB,EAA8B;IAC5BhC,MAAM,CAAC8C,KAAP,CAAc,GAAEZ,IAAI,CAACC,SAAL,CAAeH,MAAf,CAAuB,EAAvC;IACAhC,MAAM,CAACsB,IAAP,CAAa,wBAAuBd,UAAW,oBAAmBwB,MAAM,CAACR,aAAc,EAAvF;IACA,MAAMuB,KAAK,GAAG,IAAAC,sBAAA,EAAShB,MAAM,CAACR,aAAhB,CAAd;IACAxB,MAAM,CAAC8C,KAAP,CAAc,wBAAuBZ,IAAI,CAACC,SAAL,CAAeY,KAAf,CAAsB,EAA3D;;IAEA,IAAI;MACF,MAAMxC,EAAE,CAACC,UAAH,CAAcA,UAAd,EAA0ByC,UAA1B,CAAqC;QAACzB,aAAa,EAAEuB;MAAhB,CAArC,CAAN;MACA,OAAO,IAAP;IACD,CAHD,CAGE,OAAOG,GAAP,EAAY;MACZ,MAAM,IAAIvB,qBAAJ,CAAaC,mBAAA,CAAWC,qBAAxB,EAA+CqB,GAAG,CAACC,OAAnD,CAAN;IACD;EACF;AACF"}
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "url": "git@github.com:natlibfi/melinda-rest-api-commons.git"
15
15
  },
16
16
  "license": "AGPL-3.0+",
17
- "version": "2.3.0-alpha.3",
17
+ "version": "2.3.0-alpha.4",
18
18
  "main": "./dist/index.js",
19
19
  "engines": {
20
20
  "node": ">=14"