@natlibfi/melinda-rest-api-commons 2.1.3 → 2.2.0-alpha.11
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/README.md +20 -17
- package/dist/constants.js +20 -7
- package/dist/constants.js.map +1 -1
- package/dist/helpers/conversion.js +18 -8
- package/dist/helpers/conversion.js.map +1 -1
- package/dist/helpers/conversion.spec.js +18 -7
- package/dist/helpers/conversion.spec.js.map +1 -1
- package/dist/index.js +29 -1
- package/dist/index.js.map +1 -1
- package/dist/mongo.js +332 -58
- package/dist/mongo.js.map +1 -1
- package/dist/utils.js +132 -0
- package/dist/utils.js.map +1 -1
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -10,27 +10,30 @@ Shared modules for microservices of Melinda rest api import system
|
|
|
10
10
|
|
|
11
11
|
#### QueueItemState flow for bulk jobs (multiple records, no validations):
|
|
12
12
|
|
|
13
|
-
|microservice | states
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
|
19
|
-
|any | any -> ERROR if errored
|
|
13
|
+
| microservice | states |
|
|
14
|
+
|--------------|--------------------------------------------------------|
|
|
15
|
+
| `http` | -> VALIDATOR.UPLOADING -> VALIDATOR.PENDING_QUEUING -> |
|
|
16
|
+
| `validator` | VALIDATOR.QUEUING_IN_PROGRESS -> IMPORTER.IN_QUEUE -> |
|
|
17
|
+
| `importer` | IMPORTER.IMPORTING -> IMPORT_JOB_STATEs* -> DONE |
|
|
18
|
+
| | |
|
|
19
|
+
| any | any -> ERROR if errored |
|
|
20
20
|
|
|
21
|
+
*IMPORT_JOB_STATEs: EMPTY or IN_QUEUE, PROCESSING -> final: DONE, ERROR, ABORT
|
|
21
22
|
|
|
22
23
|
#### QueueItemState flow for prio jobs (single record):
|
|
23
24
|
|
|
24
|
-
|microservice | states
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
|
33
|
-
| any
|
|
25
|
+
| microservice | states |
|
|
26
|
+
|--------------|------------------------------------------------------------|
|
|
27
|
+
| `http` | -> VALIDATOR.PENDING_VALIDATION -> |
|
|
28
|
+
| `validator` | VALIDATOR.VALIDATING -> IMPORTER.IN_QUEUE -> |
|
|
29
|
+
| `importer` | IMPORTER.IMPORTING -> IMPORT_JOB_STATEs* -> DONE |
|
|
30
|
+
| | |
|
|
31
|
+
| `http` | any -> ABORT if job has stayed in an active state too long |
|
|
32
|
+
| `validator` | VALIDATOR.VALIDATING -> DONE for noop (no-operation) jobs |
|
|
33
|
+
| | |
|
|
34
|
+
| any | any -> ERROR if errored |
|
|
35
|
+
|
|
36
|
+
*IMPORT_JOB_STATEs: EMPTY or IN_QUEUE, PROCESSING -> final: DONE, ERROR, ABORT
|
|
34
37
|
|
|
35
38
|
### CHUNK_SIZE
|
|
36
39
|
Chunck size to execution time ratio (From file to queue: ~450 records in 15sec)
|
package/dist/constants.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.QUEUE_ITEM_STATE = exports.OPERATIONS = exports.IMPORT_JOB_STATE = exports.CONVERSION_FORMATS = exports.CHUNK_SIZE = void 0;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
@@ -38,8 +38,9 @@ const QUEUE_ITEM_STATE = {
|
|
|
38
38
|
PENDING_QUEUING: 'PENDING_QUEUING',
|
|
39
39
|
PENDING_VALIDATION: 'PENDING_VALIDATION',
|
|
40
40
|
QUEUING_IN_PROGRESS: 'QUEUING_IN_PROGRESS',
|
|
41
|
+
UPLOADING: 'UPLOADING',
|
|
41
42
|
VALIDATING: 'VALIDATING',
|
|
42
|
-
|
|
43
|
+
WAITING_FOR_RECORDS: 'WAITING_FOR_RECORDS'
|
|
43
44
|
},
|
|
44
45
|
IMPORTER: {
|
|
45
46
|
IMPORTING: 'IMPORTING',
|
|
@@ -51,6 +52,17 @@ const QUEUE_ITEM_STATE = {
|
|
|
51
52
|
ABORT: 'ABORT'
|
|
52
53
|
};
|
|
53
54
|
exports.QUEUE_ITEM_STATE = QUEUE_ITEM_STATE;
|
|
55
|
+
const IMPORT_JOB_STATE = {
|
|
56
|
+
EMPTY: 'EMPTY',
|
|
57
|
+
QUEUING: 'QUEUING',
|
|
58
|
+
IN_QUEUE: 'IN_QUEUE',
|
|
59
|
+
IMPORTING: 'IMPORTING',
|
|
60
|
+
PROCESSING: 'PROCESSING',
|
|
61
|
+
DONE: 'DONE',
|
|
62
|
+
ERROR: 'ERROR',
|
|
63
|
+
ABORT: 'ABORT'
|
|
64
|
+
};
|
|
65
|
+
exports.IMPORT_JOB_STATE = IMPORT_JOB_STATE;
|
|
54
66
|
const OPERATIONS = {
|
|
55
67
|
CREATE: 'CREATE',
|
|
56
68
|
UPDATE: 'UPDATE'
|
|
@@ -58,10 +70,11 @@ const OPERATIONS = {
|
|
|
58
70
|
exports.OPERATIONS = OPERATIONS;
|
|
59
71
|
const CHUNK_SIZE = 100;
|
|
60
72
|
exports.CHUNK_SIZE = CHUNK_SIZE;
|
|
61
|
-
const
|
|
62
|
-
MARCXML:
|
|
63
|
-
ISO2709:
|
|
64
|
-
JSON:
|
|
73
|
+
const CONVERSION_FORMATS = {
|
|
74
|
+
MARCXML: 'MARCXML',
|
|
75
|
+
ISO2709: 'ISO2709',
|
|
76
|
+
JSON: 'JSON',
|
|
77
|
+
ALEPHSEQ: 'ALEPHSEQ'
|
|
65
78
|
};
|
|
66
|
-
exports.
|
|
79
|
+
exports.CONVERSION_FORMATS = CONVERSION_FORMATS;
|
|
67
80
|
//# sourceMappingURL=constants.js.map
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.js"],"names":["QUEUE_ITEM_STATE","VALIDATOR","PENDING_QUEUING","PENDING_VALIDATION","QUEUING_IN_PROGRESS","VALIDATING","
|
|
1
|
+
{"version":3,"sources":["../src/constants.js"],"names":["QUEUE_ITEM_STATE","VALIDATOR","PENDING_QUEUING","PENDING_VALIDATION","QUEUING_IN_PROGRESS","UPLOADING","VALIDATING","WAITING_FOR_RECORDS","IMPORTER","IMPORTING","IN_PROCESS","IN_QUEUE","DONE","ERROR","ABORT","IMPORT_JOB_STATE","EMPTY","QUEUING","PROCESSING","OPERATIONS","CREATE","UPDATE","CHUNK_SIZE","CONVERSION_FORMATS","MARCXML","ISO2709","JSON","ALEPHSEQ"],"mappings":";;;;;;;AAAA;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;AAEA;AAEO,MAAMA,gBAAgB,GAAG;AAC9BC,EAAAA,SAAS,EAAE;AACTC,IAAAA,eAAe,EAAE,iBADR;AAETC,IAAAA,kBAAkB,EAAE,oBAFX;AAGTC,IAAAA,mBAAmB,EAAE,qBAHZ;AAITC,IAAAA,SAAS,EAAE,WAJF;AAKTC,IAAAA,UAAU,EAAE,YALH;AAMTC,IAAAA,mBAAmB,EAAE;AANZ,GADmB;AAS9BC,EAAAA,QAAQ,EAAE;AACRC,IAAAA,SAAS,EAAE,WADH;AAERC,IAAAA,UAAU,EAAE,YAFJ;AAGRC,IAAAA,QAAQ,EAAE;AAHF,GAToB;AAc9BC,EAAAA,IAAI,EAAE,MAdwB;AAe9BC,EAAAA,KAAK,EAAE,OAfuB;AAgB9BC,EAAAA,KAAK,EAAE;AAhBuB,CAAzB;;AAmBA,MAAMC,gBAAgB,GAAG;AAC9BC,EAAAA,KAAK,EAAE,OADuB;AAE9BC,EAAAA,OAAO,EAAE,SAFqB;AAG9BN,EAAAA,QAAQ,EAAE,UAHoB;AAI9BF,EAAAA,SAAS,EAAE,WAJmB;AAK9BS,EAAAA,UAAU,EAAE,YALkB;AAM9BN,EAAAA,IAAI,EAAE,MANwB;AAO9BC,EAAAA,KAAK,EAAE,OAPuB;AAQ9BC,EAAAA,KAAK,EAAE;AARuB,CAAzB;;AAWA,MAAMK,UAAU,GAAG;AACxBC,EAAAA,MAAM,EAAE,QADgB;AAExBC,EAAAA,MAAM,EAAE;AAFgB,CAAnB;;AAKA,MAAMC,UAAU,GAAG,GAAnB;;AAEA,MAAMC,kBAAkB,GAAG;AAChCC,EAAAA,OAAO,EAAE,SADuB;AAEhCC,EAAAA,OAAO,EAAE,SAFuB;AAGhCC,EAAAA,IAAI,EAAE,MAH0B;AAIhCC,EAAAA,QAAQ,EAAE;AAJsB,CAA3B","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-2021 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\n// QueueItemState flows: see README.md\n\nexport const QUEUE_ITEM_STATE = {\n VALIDATOR: {\n PENDING_QUEUING: 'PENDING_QUEUING',\n PENDING_VALIDATION: 'PENDING_VALIDATION',\n QUEUING_IN_PROGRESS: 'QUEUING_IN_PROGRESS',\n UPLOADING: 'UPLOADING',\n VALIDATING: 'VALIDATING',\n WAITING_FOR_RECORDS: 'WAITING_FOR_RECORDS'\n },\n IMPORTER: {\n IMPORTING: 'IMPORTING',\n IN_PROCESS: 'IN_PROCESS',\n IN_QUEUE: 'IN_QUEUE'\n },\n DONE: 'DONE',\n ERROR: 'ERROR',\n ABORT: 'ABORT'\n};\n\nexport const IMPORT_JOB_STATE = {\n EMPTY: 'EMPTY',\n QUEUING: 'QUEUING',\n IN_QUEUE: 'IN_QUEUE',\n IMPORTING: 'IMPORTING',\n PROCESSING: 'PROCESSING',\n DONE: 'DONE',\n ERROR: 'ERROR',\n ABORT: 'ABORT'\n};\n\nexport const OPERATIONS = {\n CREATE: 'CREATE',\n UPDATE: 'UPDATE'\n};\n\nexport const CHUNK_SIZE = 100;\n\nexport const CONVERSION_FORMATS = {\n MARCXML: 'MARCXML',\n ISO2709: 'ISO2709',\n JSON: 'JSON',\n ALEPHSEQ: 'ALEPHSEQ'\n};\n"],"file":"constants.js"}
|
|
@@ -51,24 +51,28 @@ function _default() {
|
|
|
51
51
|
return {
|
|
52
52
|
serialize,
|
|
53
53
|
unserialize
|
|
54
|
-
};
|
|
54
|
+
}; // This uses format as a number form contants CONVERSION_FORMATS
|
|
55
55
|
|
|
56
56
|
function serialize(record, format) {
|
|
57
57
|
logger.verbose(`Serializing record to ${format}`);
|
|
58
58
|
|
|
59
59
|
try {
|
|
60
|
-
if (format === _constants.
|
|
60
|
+
if (format === _constants.CONVERSION_FORMATS.MARCXML) {
|
|
61
61
|
return _marcRecordSerializers.MARCXML.to(record);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
if (format === _constants.
|
|
64
|
+
if (format === _constants.CONVERSION_FORMATS.ISO2709) {
|
|
65
65
|
return _marcRecordSerializers.ISO2709.to(record);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
if (format === _constants.
|
|
68
|
+
if (format === _constants.CONVERSION_FORMATS.JSON) {
|
|
69
69
|
return _marcRecordSerializers.Json.to(record);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
if (format === _constants.CONVERSION_FORMATS.ALEPHSEQ) {
|
|
73
|
+
return _marcRecordSerializers.AlephSequential.to(record);
|
|
74
|
+
}
|
|
75
|
+
|
|
72
76
|
throw new _melindaCommons.Error(_httpStatus.default.UNSUPPORTED_MEDIA_TYPE);
|
|
73
77
|
} catch (err) {
|
|
74
78
|
(0, _utils.logError)(err);
|
|
@@ -80,7 +84,8 @@ function _default() {
|
|
|
80
84
|
|
|
81
85
|
throw new _melindaCommons.Error(_httpStatus.default.BAD_REQUEST, 'Error while serializing record');
|
|
82
86
|
}
|
|
83
|
-
}
|
|
87
|
+
} // eslint-disable-next-line max-statements
|
|
88
|
+
|
|
84
89
|
|
|
85
90
|
function unserialize(data, format, validationOptions = {
|
|
86
91
|
subfieldValues: false
|
|
@@ -91,21 +96,26 @@ function _default() {
|
|
|
91
96
|
logger.silly(`Data: ${JSON.stringify(data)}`);
|
|
92
97
|
|
|
93
98
|
try {
|
|
94
|
-
if (format === _constants.
|
|
99
|
+
if (format === _constants.CONVERSION_FORMATS.MARCXML) {
|
|
95
100
|
logger.silly('Unserialize format marcxml');
|
|
96
101
|
return _marcRecordSerializers.MARCXML.from(data, validationOptions);
|
|
97
102
|
}
|
|
98
103
|
|
|
99
|
-
if (format === _constants.
|
|
104
|
+
if (format === _constants.CONVERSION_FORMATS.ISO2709) {
|
|
100
105
|
logger.silly('Unserialize format iso2709');
|
|
101
106
|
return _marcRecordSerializers.ISO2709.from(data, validationOptions);
|
|
102
107
|
}
|
|
103
108
|
|
|
104
|
-
if (format === _constants.
|
|
109
|
+
if (format === _constants.CONVERSION_FORMATS.JSON) {
|
|
105
110
|
logger.silly('Unserialize format json');
|
|
106
111
|
return _marcRecordSerializers.Json.from(data, validationOptions);
|
|
107
112
|
}
|
|
108
113
|
|
|
114
|
+
if (format === _constants.CONVERSION_FORMATS.ALEPHSEQ) {
|
|
115
|
+
logger.silly('Unserialize format aleph sequential');
|
|
116
|
+
return _marcRecordSerializers.AlephSequential.from(data, validationOptions);
|
|
117
|
+
}
|
|
118
|
+
|
|
109
119
|
throw new _melindaCommons.Error(_httpStatus.default.UNSUPPORTED_MEDIA_TYPE);
|
|
110
120
|
} catch (err) {
|
|
111
121
|
(0, _utils.logError)(err);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/helpers/conversion.js"],"names":["logger","serialize","unserialize","record","format","verbose","
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/conversion.js"],"names":["logger","serialize","unserialize","record","format","verbose","CONVERSION_FORMATS","MARCXML","to","ISO2709","JSON","Json","ALEPHSEQ","AlephSequential","ConversionError","httpStatus","UNSUPPORTED_MEDIA_TYPE","err","BAD_REQUEST","data","validationOptions","subfieldValues","silly","debug","stringify","from"],"mappings":";;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAjCA;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;AASe,oBAAY;AACzB,QAAMA,MAAM,GAAG,0CAAf;AAEA,SAAO;AAACC,IAAAA,SAAD;AAAYC,IAAAA;AAAZ,GAAP,CAHyB,CAKzB;;AACA,WAASD,SAAT,CAAmBE,MAAnB,EAA2BC,MAA3B,EAAmC;AACjCJ,IAAAA,MAAM,CAACK,OAAP,CAAgB,yBAAwBD,MAAO,EAA/C;;AACA,QAAI;AACF,UAAIA,MAAM,KAAKE,8BAAmBC,OAAlC,EAA2C;AACzC,eAAOA,+BAAQC,EAAR,CAAWL,MAAX,CAAP;AACD;;AAED,UAAIC,MAAM,KAAKE,8BAAmBG,OAAlC,EAA2C;AACzC,eAAOA,+BAAQD,EAAR,CAAWL,MAAX,CAAP;AACD;;AAED,UAAIC,MAAM,KAAKE,8BAAmBI,IAAlC,EAAwC;AACtC,eAAOC,4BAAKH,EAAL,CAAQL,MAAR,CAAP;AACD;;AAED,UAAIC,MAAM,KAAKE,8BAAmBM,QAAlC,EAA4C;AAC1C,eAAOC,uCAAgBL,EAAhB,CA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AV,EAAgBC,iBAAhB,CAAP;AACD;;AAED,UAAIhB,MAAM,KAAKE,8BAAmBM,QAAlC,EAA4C;AAC1CZ,QAAAA,MAAM,CAACsB,KAAP,CAAa,qCAAb;AACA,eAAOT,uCAAgBY,IAAhB,CAAqBN,IAArB,EAA2BC,iBAA3B,CAAP;AACD;;AAED,YAAM,IAAIN,qBAAJ,CAAoBC,oBAAWC,sBAA/B,CAAN;AACD,KAtBD,CAsBE,OAAOC,GAAP,EAAY;AACZ,2BAASA,GAAT;;AACA,UAAIA,GAAG,YAAYH,qBAAnB,EAAoC;AAAE;AACpC,cAAMG,GAAN;AACD;;AACD,YAAM,IAAIH,qBAAJ,CAAoBC,oBAAWG,WAA/B,EAA4C,kCAA5C,CAAN;AACD;AACF;AACF","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-2021 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 httpStatus from 'http-status';\nimport {MARCXML, ISO2709, Json, AlephSequential} from '@natlibfi/marc-record-serializers';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport {Error as ConversionError} from '@natlibfi/melinda-commons';\nimport {CONVERSION_FORMATS} from '../constants';\nimport {logError} from '../utils';\n\nexport default function () {\n const logger = createLogger();\n\n return {serialize, unserialize};\n\n // This uses format as a number form contants CONVERSION_FORMATS\n function serialize(record, format) {\n logger.verbose(`Serializing record to ${format}`);\n try {\n if (format === CONVERSION_FORMATS.MARCXML) {\n return MARCXML.to(record);\n }\n\n if (format === CONVERSION_FORMATS.ISO2709) {\n return ISO2709.to(record);\n }\n\n if (format === CONVERSION_FORMATS.JSON) {\n return Json.to(record);\n }\n\n if (format === CONVERSION_FORMATS.ALEPHSEQ) {\n return AlephSequential.to(record);\n }\n\n throw new ConversionError(httpStatus.UNSUPPORTED_MEDIA_TYPE);\n } catch (err) {\n logError(err);\n if (err instanceof ConversionError) { // eslint-disable-line functional/no-conditional-statement\n throw err;\n }\n throw new ConversionError(httpStatus.BAD_REQUEST, 'Error while serializing record');\n }\n }\n\n // eslint-disable-next-line max-statements\n function unserialize(data, format, validationOptions = {subfieldValues: false}) {\n logger.verbose(`Unserializing record from ${format}`);\n logger.silly(`Format: ${format}`);\n logger.debug(`Validation options: ${JSON.stringify(validationOptions)}`);\n logger.silly(`Data: ${JSON.stringify(data)}`);\n try {\n if (format === CONVERSION_FORMATS.MARCXML) {\n logger.silly('Unserialize format marcxml');\n return MARCXML.from(data, validationOptions);\n }\n\n if (format === CONVERSION_FORMATS.ISO2709) {\n logger.silly('Unserialize format iso2709');\n return ISO2709.from(data, validationOptions);\n }\n\n if (format === CONVERSION_FORMATS.JSON) {\n logger.silly('Unserialize format json');\n return Json.from(data, validationOptions);\n }\n\n if (format === CONVERSION_FORMATS.ALEPHSEQ) {\n logger.silly('Unserialize format aleph sequential');\n return AlephSequential.from(data, validationOptions);\n }\n\n throw new ConversionError(httpStatus.UNSUPPORTED_MEDIA_TYPE);\n } catch (err) {\n logError(err);\n if (err instanceof ConversionError) { // eslint-disable-line functional/no-conditional-statement\n throw err;\n }\n throw new ConversionError(httpStatus.BAD_REQUEST, 'Error while unserializing record');\n }\n }\n}\n"],"file":"conversion.js"}
|
|
@@ -62,6 +62,9 @@ describe('services/conversion', () => {
|
|
|
62
62
|
const json = getFixture({
|
|
63
63
|
components: ['json1']
|
|
64
64
|
});
|
|
65
|
+
const alephSeq = getFixture({
|
|
66
|
+
components: ['aleph_sequential']
|
|
67
|
+
});
|
|
65
68
|
describe('factory', () => {
|
|
66
69
|
it('Should create the expected object', () => {
|
|
67
70
|
const service = (0, _conversion.default)();
|
|
@@ -73,37 +76,45 @@ describe('services/conversion', () => {
|
|
|
73
76
|
(0, _chai.expect)(conversionService.serialize).to.throw();
|
|
74
77
|
});
|
|
75
78
|
it('Should serialize to MARCXML', () => {
|
|
76
|
-
const data = conversionService.serialize(marcRecord, _constants.
|
|
79
|
+
const data = conversionService.serialize(marcRecord, _constants.CONVERSION_FORMATS.MARCXML);
|
|
77
80
|
(0, _chai.expect)(data).to.equal(marcXml);
|
|
78
81
|
});
|
|
79
82
|
it('Should serialize to ISO2709', () => {
|
|
80
|
-
const data = conversionService.serialize(marcRecord, _constants.
|
|
83
|
+
const data = conversionService.serialize(marcRecord, _constants.CONVERSION_FORMATS.ISO2709);
|
|
81
84
|
(0, _chai.expect)(data).to.equal(iso2709);
|
|
82
85
|
});
|
|
83
86
|
it('Should serialize to JSON', () => {
|
|
84
|
-
const data = conversionService.serialize(marcRecord, _constants.
|
|
87
|
+
const data = conversionService.serialize(marcRecord, _constants.CONVERSION_FORMATS.JSON);
|
|
85
88
|
(0, _chai.expect)(data).to.equal(json);
|
|
86
89
|
});
|
|
90
|
+
it('Should serialize to aleph sequential', () => {
|
|
91
|
+
const data = conversionService.serialize(marcRecord, _constants.CONVERSION_FORMATS.ALEPHSEQ);
|
|
92
|
+
(0, _chai.expect)(data).to.equal(alephSeq);
|
|
93
|
+
});
|
|
87
94
|
});
|
|
88
95
|
describe('#unserialize', () => {
|
|
89
96
|
it('Should throw because of unsupported format', () => {
|
|
90
97
|
(0, _chai.expect)(conversionService.unserialize).to.throw();
|
|
91
98
|
});
|
|
92
99
|
it('Should unserialize from MARCXML', async () => {
|
|
93
|
-
const record = await conversionService.unserialize(marcXml, _constants.
|
|
100
|
+
const record = await conversionService.unserialize(marcXml, _constants.CONVERSION_FORMATS.MARCXML);
|
|
94
101
|
(0, _chai.expect)(record.equalsTo(marcRecord)).to.equal(true);
|
|
95
102
|
});
|
|
96
103
|
it('Should unserialize from ISO2709', () => {
|
|
97
|
-
const record = conversionService.unserialize(iso2709, _constants.
|
|
104
|
+
const record = conversionService.unserialize(iso2709, _constants.CONVERSION_FORMATS.ISO2709);
|
|
98
105
|
(0, _chai.expect)(record.equalsTo(marcRecord)).to.equal(true);
|
|
99
106
|
});
|
|
100
107
|
it('Should unserialize from JSON', () => {
|
|
101
|
-
const record = conversionService.unserialize(json, _constants.
|
|
108
|
+
const record = conversionService.unserialize(json, _constants.CONVERSION_FORMATS.JSON);
|
|
109
|
+
(0, _chai.expect)(record.equalsTo(marcRecord)).to.equal(true);
|
|
110
|
+
});
|
|
111
|
+
it('Should unserialize from Aleph sequential', () => {
|
|
112
|
+
const record = conversionService.unserialize(alephSeq, _constants.CONVERSION_FORMATS.ALEPHSEQ);
|
|
102
113
|
(0, _chai.expect)(record.equalsTo(marcRecord)).to.equal(true);
|
|
103
114
|
});
|
|
104
115
|
it('Should throw because the record could not be unserialized', () => {
|
|
105
116
|
(0, _chai.expect)(() => {
|
|
106
|
-
conversionService.unserialize('', _constants.
|
|
117
|
+
conversionService.unserialize('', _constants.CONVERSION_FORMATS.JSON);
|
|
107
118
|
}).to.throw(_melindaCommons.Error);
|
|
108
119
|
});
|
|
109
120
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/helpers/conversion.spec.js"],"names":["describe","getFixture","root","__dirname","conversionService","marcRecord","MarcRecord","JSON","parse","components","subfieldValues","marcXml","iso2709","json","it","service","to","be","an","and","respondTo","serialize","throw","data","
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/conversion.spec.js"],"names":["describe","getFixture","root","__dirname","conversionService","marcRecord","MarcRecord","JSON","parse","components","subfieldValues","marcXml","iso2709","json","alephSeq","it","service","to","be","an","and","respondTo","serialize","throw","data","CONVERSION_FORMATS","MARCXML","equal","ISO2709","ALEPHSEQ","unserialize","record","equalsTo","ConversionError"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AAjCA;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;AASAA,QAAQ,CAAC,qBAAD,EAAwB,MAAM;AACpC,QAAM;AAACC,IAAAA;AAAD,MAAe,qBAAe;AAACC,IAAAA,IAAI,EAAE,CACzCC,SADyC,EAEzC,IAFyC,EAGzC,IAHyC,EAIzC,eAJyC,EAKzC,YALyC;AAAP,GAAf,CAArB;AAOA,QAAMC,iBAAiB,GAAG,0BAA1B;AAEA,QAAMC,UAAU,GAAG,IAAIC,sBAAJ,CAAeC,IAAI,CAACC,KAAL,CAAWP,UAAU,CAAC;AAACQ,IAAAA,UAAU,EAAE,CAAC,OAAD;AAAb,GAAD,CAArB,CAAf,EAAgE;AAACC,IAAAA,cAAc,EAAE;AAAjB,GAAhE,CAAnB;AACA,QAAMC,OAAO,GAAGV,UAAU,CAAC;AAACQ,IAAAA,UAAU,EAAE,CAAC,UAAD;AAAb,GAAD,CAA1B;AACA,QAAMG,OAAO,GAAGX,UAAU,CAAC;AAACQ,IAAAA,UAAU,EAAE,CAAC,WAAD;AAAb,GAAD,CAA1B;AACA,QAAMI,IAAI,GAAGZ,UAAU,CAAC;AAACQ,IAAAA,UAAU,EAAE,CAAC,OAAD;AAAb,GAAD,CAAvB;AACA,QAAMK,QAAQ,GAAGb,UAAU,CAAC;AAACQ,IAAAA,UAAU,EAAE,CAAC,kBAAD;AAAb,GAAD,CAA3B;AAGAT,EAAAA,QAAQ,CAAC,SAAD,EAAY,MAAM;AACxBe,IAAAA,EAAE,CAAC,mCAAD,EAAsC,MAAM;AAC5C,YAAMC,OAAO,GAAG,0BAAhB;AACA,w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,CAAP,EAAoCY,EAApC,CAAuCU,KAAvC,CAA6C,IAA7C;AACD,KAHC,CAAF;AAMAZ,IAAAA,EAAE,CAAC,2DAAD,EAA8D,MAAM;AACpE,wBAAO,MAAM;AACXX,QAAAA,iBAAiB,CAAC0B,WAAlB,CAA8B,EAA9B,EAAkCL,8BAAmBlB,IAArD;AACD,OAFD,EAEGU,EAFH,CAEMM,KAFN,CAEYU,qBAFZ;AAGD,KAJC,CAAF;AAKD,GA/BO,CAAR;AAgCD,CArFO,CAAR","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-2021 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 {expect} from 'chai';\nimport fixtureFactory from '@natlibfi/fixura';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {Error as ConversionError} from '@natlibfi/melinda-commons';\nimport createConversionService from './conversion';\nimport {CONVERSION_FORMATS} from '../constants';\n\ndescribe('services/conversion', () => {\n const {getFixture} = fixtureFactory({root: [\n __dirname,\n '..',\n '..',\n 'test-fixtures',\n 'conversion'\n ]});\n const conversionService = createConversionService();\n\n const marcRecord = new MarcRecord(JSON.parse(getFixture({components: ['json1']})), {subfieldValues: false});\n const marcXml = getFixture({components: ['marcxml1']});\n const iso2709 = getFixture({components: ['iso2709_1']});\n const json = getFixture({components: ['json1']});\n const alephSeq = getFixture({components: ['aleph_sequential']});\n\n\n describe('factory', () => {\n it('Should create the expected object', () => {\n const service = createConversionService();\n expect(service).to.be.an('object').and\n .respondTo('serialize')\n .respondTo('unserialize');\n });\n });\n\n describe('#serialize', () => {\n it('Should throw because of unsupported format', () => {\n expect(conversionService.serialize).to.throw();\n });\n\n it('Should serialize to MARCXML', () => {\n const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.MARCXML);\n expect(data).to.equal(marcXml);\n });\n\n it('Should serialize to ISO2709', () => {\n const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.ISO2709);\n expect(data).to.equal(iso2709);\n });\n\n it('Should serialize to JSON', () => {\n const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.JSON);\n expect(data).to.equal(json);\n });\n\n it('Should serialize to aleph sequential', () => {\n const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.ALEPHSEQ);\n expect(data).to.equal(alephSeq);\n });\n\n });\n\n describe('#unserialize', () => {\n it('Should throw because of unsupported format', () => {\n expect(conversionService.unserialize).to.throw();\n });\n\n it('Should unserialize from MARCXML', async () => {\n const record = await conversionService.unserialize(marcXml, CONVERSION_FORMATS.MARCXML);\n expect(record.equalsTo(marcRecord)).to.equal(true);\n });\n\n it('Should unserialize from ISO2709', () => {\n const record = conversionService.unserialize(iso2709, CONVERSION_FORMATS.ISO2709);\n expect(record.equalsTo(marcRecord)).to.equal(true);\n });\n\n it('Should unserialize from JSON', () => {\n const record = conversionService.unserialize(json, CONVERSION_FORMATS.JSON);\n expect(record.equalsTo(marcRecord)).to.equal(true);\n });\n\n it('Should unserialize from Aleph sequential', () => {\n const record = conversionService.unserialize(alephSeq, CONVERSION_FORMATS.ALEPHSEQ);\n expect(record.equalsTo(marcRecord)).to.equal(true);\n });\n\n\n it('Should throw because the record could not be unserialized', () => {\n expect(() => {\n conversionService.unserialize('', CONVERSION_FORMATS.JSON);\n }).to.throw(ConversionError);\n });\n });\n});\n"],"file":"conversion.spec.js"}
|
package/dist/index.js
CHANGED
|
@@ -9,8 +9,18 @@ var _exportNames = {
|
|
|
9
9
|
conversions: true,
|
|
10
10
|
validations: true,
|
|
11
11
|
format: true,
|
|
12
|
-
logError: true
|
|
12
|
+
logError: true,
|
|
13
|
+
createImportJobState: true,
|
|
14
|
+
createImportJobStateForQuery: true,
|
|
15
|
+
createRecordResponseItem: true,
|
|
16
|
+
addRecordResponseItem: true
|
|
13
17
|
};
|
|
18
|
+
Object.defineProperty(exports, "addRecordResponseItem", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _utils.addRecordResponseItem;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
14
24
|
Object.defineProperty(exports, "amqpFactory", {
|
|
15
25
|
enumerable: true,
|
|
16
26
|
get: function () {
|
|
@@ -23,6 +33,24 @@ Object.defineProperty(exports, "conversions", {
|
|
|
23
33
|
return _conversion.default;
|
|
24
34
|
}
|
|
25
35
|
});
|
|
36
|
+
Object.defineProperty(exports, "createImportJobState", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return _utils.createImportJobState;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "createImportJobStateForQuery", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () {
|
|
45
|
+
return _utils.createImportJobStateForQuery;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "createRecordResponseItem", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return _utils.createRecordResponseItem;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
26
54
|
exports.format = void 0;
|
|
27
55
|
Object.defineProperty(exports, "logError", {
|
|
28
56
|
enumerable: true,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;AACA;;AACA;;AACA;;AACA;;;;AACA;;AAGA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA","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-2021 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 mongoFactory from './mongo';\nimport amqpFactory from './amqp';\nimport conversions from './helpers/conversion';\nimport validations from './helpers/validation';\nimport * as format from './helpers/format';\nimport {logError, createImportJobState, createImportJobStateForQuery, createRecordResponseItem, addRecordResponseItem} from './utils';\n\nexport {mongoFactory, amqpFactory, conversions, validations, format, logError, createImportJobState, createImportJobStateForQuery, createRecordResponseItem, addRecordResponseItem};\nexport * from './constants';\n"],"file":"index.js"}
|