meadow-integration 1.0.19 → 1.0.21

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 (35) hide show
  1. package/example-applications/mapping-demo/.quackage.json +10 -0
  2. package/example-applications/mapping-demo/README.md +99 -0
  3. package/example-applications/mapping-demo/data/books-sample.csv +21 -0
  4. package/example-applications/mapping-demo/generate-build-config.js +44 -0
  5. package/example-applications/mapping-demo/mappings/books-to-book.json +14 -0
  6. package/example-applications/mapping-demo/package.json +14 -0
  7. package/example-applications/mapping-demo/server.js +814 -0
  8. package/example-applications/mapping-demo/source/MappingDemoApp.js +52 -0
  9. package/example-applications/mapping-demo/source/views/MappingDemoEditorView.js +186 -0
  10. package/example-applications/mapping-demo/web/index.html +892 -0
  11. package/example-applications/mapping-demo/web/mapping-demo-editor.js +3195 -0
  12. package/example-applications/mapping-demo/web/mapping-demo-editor.js.map +1 -0
  13. package/example-applications/mapping-demo/web/mapping-demo-editor.min.js +2 -0
  14. package/example-applications/mapping-demo/web/mapping-demo-editor.min.js.map +1 -0
  15. package/example-applications/mapping-demo/web/pict.min.js +12 -0
  16. package/package.json +11 -5
  17. package/source/Meadow-Integration-Browser.js +31 -0
  18. package/source/Meadow-Integration.js +30 -1
  19. package/source/services/certainty/Service-CertaintyAccumulator.js +402 -0
  20. package/source/services/clone/Meadow-Service-Sync-Entity-Initial.js +16 -3
  21. package/source/services/clone/Meadow-Service-Sync-Entity-Ongoing.js +15 -2
  22. package/source/services/clone/Meadow-Service-Sync.js +21 -0
  23. package/source/services/parser/Service-FileParser-CSV.js +263 -0
  24. package/source/services/parser/Service-FileParser-FixedWidth.js +158 -0
  25. package/source/services/parser/Service-FileParser-JSON.js +255 -0
  26. package/source/services/parser/Service-FileParser-XLSX.js +194 -0
  27. package/source/services/parser/Service-FileParser-XML.js +190 -0
  28. package/source/services/parser/Service-FileParser.js +142 -0
  29. package/source/views/MappingEditor-SchemaUtils.js +71 -0
  30. package/source/views/PictView-MeadowMappingEditor.js +1299 -0
  31. package/source/views/flow-cards/FlowCard-MappingSource.js +50 -0
  32. package/source/views/flow-cards/FlowCard-MappingTarget.js +49 -0
  33. package/source/views/flow-cards/FlowCard-SolverExpression.js +78 -0
  34. package/source/views/flow-cards/FlowCard-TemplateExpression.js +77 -0
  35. package/test/Meadow-Integration-CloneDeleteSync_test.js +809 -0
@@ -0,0 +1,263 @@
1
+ 'use strict';
2
+
3
+ const libFableServiceProviderBase = require('fable-serviceproviderbase');
4
+ const libFS = require('fs');
5
+ const libReadline = require('readline');
6
+
7
+ const defaultCSVParserOptions = (
8
+ {
9
+ delimiter: ',',
10
+ quoteChar: '"',
11
+ hasHeaders: true,
12
+ skipRows: 0,
13
+ commentPrefix: '',
14
+ trim: true,
15
+ chunkSize: 100
16
+ });
17
+
18
+ class MeadowIntegrationFileParserCSV extends libFableServiceProviderBase
19
+ {
20
+ constructor(pFable, pOptions, pServiceHash)
21
+ {
22
+ let tmpOptions = Object.assign({}, defaultCSVParserOptions, pOptions);
23
+ super(pFable, tmpOptions, pServiceHash);
24
+
25
+ this.serviceType = 'MeadowIntegrationFileParserCSV';
26
+
27
+ this._headers = null;
28
+ }
29
+
30
+ /**
31
+ * Parse a single CSV line into an array of values.
32
+ * Handles quoted fields (including embedded commas and escaped quotes).
33
+ *
34
+ * @param {string} pLine - Raw CSV line
35
+ * @param {string} pDelimiter - Field delimiter character
36
+ * @param {string} pQuoteChar - Quote character
37
+ * @param {boolean} pTrim - Whether to trim field values
38
+ * @returns {Array<string>} Parsed field values
39
+ */
40
+ _parseCSVLine(pLine, pDelimiter, pQuoteChar, pTrim)
41
+ {
42
+ let tmpDelimiter = pDelimiter || ',';
43
+ let tmpQuoteChar = pQuoteChar || '"';
44
+ let tmpValues = [];
45
+ let tmpCurrent = '';
46
+ let tmpInQuotes = false;
47
+
48
+ for (let i = 0; i < pLine.length; i++)
49
+ {
50
+ let tmpChar = pLine[i];
51
+
52
+ if (tmpChar === tmpQuoteChar)
53
+ {
54
+ if (tmpInQuotes && pLine[i + 1] === tmpQuoteChar)
55
+ {
56
+ // Escaped quote (doubled)
57
+ tmpCurrent += tmpQuoteChar;
58
+ i++;
59
+ }
60
+ else
61
+ {
62
+ tmpInQuotes = !tmpInQuotes;
63
+ }
64
+ }
65
+ else if (tmpChar === tmpDelimiter && !tmpInQuotes)
66
+ {
67
+ tmpValues.push(pTrim ? tmpCurrent.trim() : tmpCurrent);
68
+ tmpCurrent = '';
69
+ }
70
+ else
71
+ {
72
+ tmpCurrent += tmpChar;
73
+ }
74
+ }
75
+
76
+ tmpValues.push(pTrim ? tmpCurrent.trim() : tmpCurrent);
77
+ return tmpValues;
78
+ }
79
+
80
+ /**
81
+ * Parse a CSV file using streaming readline.
82
+ * Fires chunkCallback with arrays of records as they accumulate.
83
+ * Fires completionCallback when the file is fully consumed.
84
+ *
85
+ * @param {string} pFilePath - Absolute path to the CSV file
86
+ * @param {object} pOptions - Parser options (overrides instance options)
87
+ * @param {function} pChunkCallback - Called with (pError, pRecords) per chunk
88
+ * @param {function} pCompletionCallback - Called with (pError, pTotalCount) when done
89
+ */
90
+ parseFile(pFilePath, pOptions, pChunkCallback, pCompletionCallback)
91
+ {
92
+ let tmpOptions = Object.assign({}, this.options, pOptions);
93
+ let tmpChunkSize = tmpOptions.chunkSize || 100;
94
+ let tmpHasHeaders = tmpOptions.hasHeaders !== false;
95
+ let tmpSkipRows = parseInt(tmpOptions.skipRows, 10) || 0;
96
+ let tmpCommentPrefix = tmpOptions.commentPrefix || '';
97
+ let tmpTrim = tmpOptions.trim !== false;
98
+ let tmpDelimiter = tmpOptions.delimiter || ',';
99
+ let tmpQuoteChar = tmpOptions.quoteChar || '"';
100
+
101
+ this._headers = null;
102
+
103
+ let tmpLineIndex = 0;
104
+ let tmpRecordCount = 0;
105
+ let tmpChunkBuffer = [];
106
+
107
+ const tmpReadline = libReadline.createInterface(
108
+ {
109
+ input: libFS.createReadStream(pFilePath),
110
+ crlfDelay: Infinity
111
+ });
112
+
113
+ tmpReadline.on('line',
114
+ (pLine) =>
115
+ {
116
+ // Skip comment lines
117
+ if (tmpCommentPrefix && pLine.startsWith(tmpCommentPrefix))
118
+ {
119
+ return;
120
+ }
121
+
122
+ // Skip header/preamble rows
123
+ if (tmpLineIndex < tmpSkipRows)
124
+ {
125
+ tmpLineIndex++;
126
+ return;
127
+ }
128
+
129
+ let tmpValues = this._parseCSVLine(pLine, tmpDelimiter, tmpQuoteChar, tmpTrim);
130
+
131
+ // First non-skipped, non-comment line becomes headers
132
+ if (tmpHasHeaders && !this._headers)
133
+ {
134
+ this._headers = tmpValues;
135
+ tmpLineIndex++;
136
+ return;
137
+ }
138
+
139
+ let tmpRecord;
140
+ if (this._headers)
141
+ {
142
+ tmpRecord = {};
143
+ for (let i = 0; i < this._headers.length; i++)
144
+ {
145
+ tmpRecord[this._headers[i]] = (tmpValues && tmpValues[i] !== undefined) ? tmpValues[i] : '';
146
+ }
147
+ }
148
+ else
149
+ {
150
+ tmpRecord = tmpValues || [];
151
+ }
152
+
153
+ tmpChunkBuffer.push(tmpRecord);
154
+ tmpRecordCount++;
155
+ tmpLineIndex++;
156
+
157
+ if (tmpChunkBuffer.length >= tmpChunkSize)
158
+ {
159
+ pChunkCallback(null, tmpChunkBuffer.splice(0, tmpChunkBuffer.length));
160
+ }
161
+ });
162
+
163
+ tmpReadline.on('close',
164
+ () =>
165
+ {
166
+ if (tmpChunkBuffer.length > 0)
167
+ {
168
+ pChunkCallback(null, tmpChunkBuffer.splice(0, tmpChunkBuffer.length));
169
+ }
170
+ return pCompletionCallback(null, tmpRecordCount);
171
+ });
172
+
173
+ tmpReadline.on('error',
174
+ (pError) =>
175
+ {
176
+ return pCompletionCallback(pError);
177
+ });
178
+ }
179
+
180
+ /**
181
+ * Parse CSV content string into a full array of records.
182
+ *
183
+ * @param {string} pContent - Raw CSV text
184
+ * @param {object} pOptions - Parser options
185
+ * @param {function} fCallback - Called with (pError, pRecords)
186
+ */
187
+ parseContent(pContent, pOptions, fCallback)
188
+ {
189
+ let tmpOptions = Object.assign({}, this.options, pOptions);
190
+ let tmpHasHeaders = tmpOptions.hasHeaders !== false;
191
+ let tmpSkipRows = parseInt(tmpOptions.skipRows, 10) || 0;
192
+ let tmpCommentPrefix = tmpOptions.commentPrefix || '';
193
+ let tmpTrim = tmpOptions.trim !== false;
194
+ let tmpDelimiter = tmpOptions.delimiter || ',';
195
+ let tmpQuoteChar = tmpOptions.quoteChar || '"';
196
+
197
+ let tmpLines = pContent.split('\n');
198
+ let tmpHeaders = null;
199
+ let tmpRecords = [];
200
+ let tmpLineIndex = 0;
201
+
202
+ for (let i = 0; i < tmpLines.length; i++)
203
+ {
204
+ let tmpLine = tmpLines[i];
205
+
206
+ // Strip trailing \r for Windows line endings
207
+ if (tmpLine.length > 0 && tmpLine[tmpLine.length - 1] === '\r')
208
+ {
209
+ tmpLine = tmpLine.slice(0, -1);
210
+ }
211
+
212
+ // Skip comment lines
213
+ if (tmpCommentPrefix && tmpLine.startsWith(tmpCommentPrefix))
214
+ {
215
+ continue;
216
+ }
217
+
218
+ // Skip preamble rows
219
+ if (tmpLineIndex < tmpSkipRows)
220
+ {
221
+ tmpLineIndex++;
222
+ continue;
223
+ }
224
+
225
+ // Skip blank lines
226
+ if (!tmpLine || tmpLine.trim().length === 0)
227
+ {
228
+ tmpLineIndex++;
229
+ continue;
230
+ }
231
+
232
+ let tmpValues = this._parseCSVLine(tmpLine, tmpDelimiter, tmpQuoteChar, tmpTrim);
233
+
234
+ if (tmpHasHeaders && !tmpHeaders)
235
+ {
236
+ tmpHeaders = tmpValues;
237
+ tmpLineIndex++;
238
+ continue;
239
+ }
240
+
241
+ let tmpRecord;
242
+ if (tmpHeaders)
243
+ {
244
+ tmpRecord = {};
245
+ for (let j = 0; j < tmpHeaders.length; j++)
246
+ {
247
+ tmpRecord[tmpHeaders[j]] = (tmpValues && tmpValues[j] !== undefined) ? tmpValues[j] : '';
248
+ }
249
+ }
250
+ else
251
+ {
252
+ tmpRecord = tmpValues || [];
253
+ }
254
+
255
+ tmpRecords.push(tmpRecord);
256
+ tmpLineIndex++;
257
+ }
258
+
259
+ return fCallback(null, tmpRecords);
260
+ }
261
+ }
262
+
263
+ module.exports = MeadowIntegrationFileParserCSV;
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+
3
+ const libFableServiceProviderBase = require('fable-serviceproviderbase');
4
+ const libFS = require('fs');
5
+ const libReadline = require('readline');
6
+
7
+ const defaultFixedWidthParserOptions = (
8
+ {
9
+ skipLines: 0,
10
+ columns: []
11
+ });
12
+
13
+ class MeadowIntegrationFileParserFixedWidth extends libFableServiceProviderBase
14
+ {
15
+ constructor(pFable, pOptions, pServiceHash)
16
+ {
17
+ let tmpOptions = Object.assign({}, defaultFixedWidthParserOptions, pOptions);
18
+ super(pFable, tmpOptions, pServiceHash);
19
+
20
+ this.serviceType = 'MeadowIntegrationFileParserFixedWidth';
21
+ }
22
+
23
+ /**
24
+ * Extract fields from a fixed-width line using a columns definition.
25
+ * Column start positions are 1-based.
26
+ *
27
+ * @param {string} pLine - Raw text line
28
+ * @param {Array} pColumns - Array of {name, start, width}
29
+ * @returns {object} Extracted record
30
+ */
31
+ _parseLine(pLine, pColumns)
32
+ {
33
+ let tmpRecord = {};
34
+ for (let i = 0; i < pColumns.length; i++)
35
+ {
36
+ let tmpCol = pColumns[i];
37
+ // start is 1-based
38
+ let tmpStartIdx = (parseInt(tmpCol.start, 10) || 1) - 1;
39
+ let tmpWidth = parseInt(tmpCol.width, 10) || 0;
40
+ let tmpValue = pLine.substring(tmpStartIdx, tmpStartIdx + tmpWidth).trim();
41
+ tmpRecord[tmpCol.name] = tmpValue;
42
+ }
43
+ return tmpRecord;
44
+ }
45
+
46
+ /**
47
+ * Parse a fixed-width file using streaming readline.
48
+ *
49
+ * @param {string} pFilePath - Absolute path to the fixed-width file
50
+ * @param {object} pOptions - Parser options: skipLines, columns, chunkSize
51
+ * @param {function} pChunkCallback - Called with (pError, pRecords) per chunk
52
+ * @param {function} pCompletionCallback - Called with (pError, pTotalCount) when done
53
+ */
54
+ parseFile(pFilePath, pOptions, pChunkCallback, pCompletionCallback)
55
+ {
56
+ let tmpOptions = Object.assign({}, this.options, pOptions);
57
+ let tmpColumns = tmpOptions.columns || [];
58
+ let tmpSkipLines = parseInt(tmpOptions.skipLines, 10) || 0;
59
+ let tmpChunkSize = parseInt(tmpOptions.chunkSize, 10) || 100;
60
+
61
+ if (!tmpColumns || tmpColumns.length === 0)
62
+ {
63
+ return pCompletionCallback(new Error('FixedWidth parser requires options.columns array of {name, start, width}'));
64
+ }
65
+
66
+ let tmpLineIndex = 0;
67
+ let tmpRecordCount = 0;
68
+ let tmpChunkBuffer = [];
69
+
70
+ const tmpReadline = libReadline.createInterface(
71
+ {
72
+ input: libFS.createReadStream(pFilePath),
73
+ crlfDelay: Infinity
74
+ });
75
+
76
+ tmpReadline.on('line',
77
+ (pLine) =>
78
+ {
79
+ if (tmpLineIndex < tmpSkipLines)
80
+ {
81
+ tmpLineIndex++;
82
+ return;
83
+ }
84
+
85
+ // Skip blank lines
86
+ if (!pLine || pLine.trim().length === 0)
87
+ {
88
+ tmpLineIndex++;
89
+ return;
90
+ }
91
+
92
+ let tmpRecord = this._parseLine(pLine, tmpColumns);
93
+ tmpChunkBuffer.push(tmpRecord);
94
+ tmpRecordCount++;
95
+ tmpLineIndex++;
96
+
97
+ if (tmpChunkBuffer.length >= tmpChunkSize)
98
+ {
99
+ pChunkCallback(null, tmpChunkBuffer.splice(0, tmpChunkBuffer.length));
100
+ }
101
+ });
102
+
103
+ tmpReadline.on('close',
104
+ () =>
105
+ {
106
+ if (tmpChunkBuffer.length > 0)
107
+ {
108
+ pChunkCallback(null, tmpChunkBuffer.splice(0, tmpChunkBuffer.length));
109
+ }
110
+ return pCompletionCallback(null, tmpRecordCount);
111
+ });
112
+
113
+ tmpReadline.on('error',
114
+ (pError) =>
115
+ {
116
+ return pCompletionCallback(pError);
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Parse fixed-width content string into a full array of records.
122
+ *
123
+ * @param {string} pContent - Raw fixed-width text
124
+ * @param {object} pOptions - Parser options
125
+ * @param {function} fCallback - Called with (pError, pRecords)
126
+ */
127
+ parseContent(pContent, pOptions, fCallback)
128
+ {
129
+ let tmpOptions = Object.assign({}, this.options, pOptions);
130
+ let tmpColumns = tmpOptions.columns || [];
131
+ let tmpSkipLines = parseInt(tmpOptions.skipLines, 10) || 0;
132
+
133
+ if (!tmpColumns || tmpColumns.length === 0)
134
+ {
135
+ return fCallback(new Error('FixedWidth parser requires options.columns array of {name, start, width}'));
136
+ }
137
+
138
+ let tmpLines = pContent.split('\n');
139
+ let tmpRecords = [];
140
+
141
+ for (let i = tmpSkipLines; i < tmpLines.length; i++)
142
+ {
143
+ let tmpLine = tmpLines[i];
144
+
145
+ // Skip blank lines
146
+ if (!tmpLine || tmpLine.trim().length === 0)
147
+ {
148
+ continue;
149
+ }
150
+
151
+ tmpRecords.push(this._parseLine(tmpLine, tmpColumns));
152
+ }
153
+
154
+ return fCallback(null, tmpRecords);
155
+ }
156
+ }
157
+
158
+ module.exports = MeadowIntegrationFileParserFixedWidth;
@@ -0,0 +1,255 @@
1
+ 'use strict';
2
+
3
+ const libFableServiceProviderBase = require('fable-serviceproviderbase');
4
+ const libFS = require('fs');
5
+
6
+ const defaultJSONParserOptions = (
7
+ {
8
+ rootPath: '',
9
+ flattenNested: false,
10
+ flattenDelimiter: '_'
11
+ });
12
+
13
+ class MeadowIntegrationFileParserJSON extends libFableServiceProviderBase
14
+ {
15
+ constructor(pFable, pOptions, pServiceHash)
16
+ {
17
+ let tmpOptions = Object.assign({}, defaultJSONParserOptions, pOptions);
18
+ super(pFable, tmpOptions, pServiceHash);
19
+
20
+ this.serviceType = 'MeadowIntegrationFileParserJSON';
21
+ }
22
+
23
+ /**
24
+ * Navigate a nested object using a dot-separated path with optional
25
+ * array index notation (e.g. "Results.series[0].data").
26
+ *
27
+ * @param {object} pObject - The object to navigate
28
+ * @param {string} pPath - Dot-separated path, segments may include [n]
29
+ * @returns {*} The resolved value, or null if the path is invalid
30
+ */
31
+ _resolveDataPath(pObject, pPath)
32
+ {
33
+ if (!pPath || typeof pPath !== 'string')
34
+ {
35
+ return pObject;
36
+ }
37
+
38
+ let tmpSegments = pPath.split('.');
39
+ let tmpCurrent = pObject;
40
+
41
+ for (let i = 0; i < tmpSegments.length; i++)
42
+ {
43
+ if (tmpCurrent === null || tmpCurrent === undefined || typeof tmpCurrent !== 'object')
44
+ {
45
+ return null;
46
+ }
47
+
48
+ let tmpSegment = tmpSegments[i];
49
+ // Check for array index notation: name[index]
50
+ let tmpMatch = tmpSegment.match(/^([^\[]+)\[(\d+)\]$/);
51
+ if (tmpMatch)
52
+ {
53
+ let tmpKey = tmpMatch[1];
54
+ let tmpIndex = parseInt(tmpMatch[2], 10);
55
+ if (!(tmpKey in tmpCurrent) || !Array.isArray(tmpCurrent[tmpKey]))
56
+ {
57
+ return null;
58
+ }
59
+ tmpCurrent = tmpCurrent[tmpKey][tmpIndex];
60
+ }
61
+ else
62
+ {
63
+ if (!(tmpSegment in tmpCurrent))
64
+ {
65
+ return null;
66
+ }
67
+ tmpCurrent = tmpCurrent[tmpSegment];
68
+ }
69
+ }
70
+
71
+ return tmpCurrent;
72
+ }
73
+
74
+ /**
75
+ * Flatten a nested object into a single-level object using a delimiter.
76
+ *
77
+ * @param {object} pObject - Nested object
78
+ * @param {string} pDelimiter - Key delimiter (default '_')
79
+ * @param {string} pPrefix - Key prefix for recursion
80
+ * @returns {object} Flat object
81
+ */
82
+ _flattenObject(pObject, pDelimiter, pPrefix)
83
+ {
84
+ let tmpDelimiter = pDelimiter || '_';
85
+ let tmpPrefix = pPrefix || '';
86
+ let tmpResult = {};
87
+
88
+ let tmpKeys = Object.keys(pObject);
89
+ for (let i = 0; i < tmpKeys.length; i++)
90
+ {
91
+ let tmpKey = tmpKeys[i];
92
+ let tmpFullKey = tmpPrefix ? `${tmpPrefix}${tmpDelimiter}${tmpKey}` : tmpKey;
93
+ let tmpValue = pObject[tmpKey];
94
+
95
+ if (tmpValue !== null && typeof tmpValue === 'object' && !Array.isArray(tmpValue))
96
+ {
97
+ let tmpNested = this._flattenObject(tmpValue, tmpDelimiter, tmpFullKey);
98
+ let tmpNestedKeys = Object.keys(tmpNested);
99
+ for (let j = 0; j < tmpNestedKeys.length; j++)
100
+ {
101
+ tmpResult[tmpNestedKeys[j]] = tmpNested[tmpNestedKeys[j]];
102
+ }
103
+ }
104
+ else
105
+ {
106
+ tmpResult[tmpFullKey] = tmpValue;
107
+ }
108
+ }
109
+
110
+ return tmpResult;
111
+ }
112
+
113
+ /**
114
+ * Resolve parsed JSON to a records array, applying rootPath navigation.
115
+ *
116
+ * @param {*} pParsed - Parsed JSON value
117
+ * @param {object} pOptions - Parser options
118
+ * @returns {Array|null} Array of records or null on failure
119
+ */
120
+ _resolveRecords(pParsed, pOptions)
121
+ {
122
+ let tmpData = pParsed;
123
+
124
+ if (pOptions && pOptions.rootPath)
125
+ {
126
+ tmpData = this._resolveDataPath(pParsed, pOptions.rootPath);
127
+ if (tmpData === null || tmpData === undefined)
128
+ {
129
+ return null;
130
+ }
131
+ }
132
+
133
+ let tmpRecords;
134
+ if (Array.isArray(tmpData))
135
+ {
136
+ tmpRecords = tmpData;
137
+ }
138
+ else if (typeof tmpData === 'object' && tmpData !== null)
139
+ {
140
+ // Common envelope keys
141
+ if (Array.isArray(tmpData.data))
142
+ {
143
+ tmpRecords = tmpData.data;
144
+ }
145
+ else if (Array.isArray(tmpData.records))
146
+ {
147
+ tmpRecords = tmpData.records;
148
+ }
149
+ else if (Array.isArray(tmpData.rows))
150
+ {
151
+ tmpRecords = tmpData.rows;
152
+ }
153
+ else
154
+ {
155
+ tmpRecords = [tmpData];
156
+ }
157
+ }
158
+ else
159
+ {
160
+ return null;
161
+ }
162
+
163
+ return tmpRecords;
164
+ }
165
+
166
+ /**
167
+ * Parse a JSON file into an array of records.
168
+ * Reads the entire file into memory.
169
+ *
170
+ * @param {string} pFilePath - Absolute path to the JSON file
171
+ * @param {object} pOptions - Parser options
172
+ * @param {function} pChunkCallback - Called with (pError, pRecords) once with all records
173
+ * @param {function} pCompletionCallback - Called with (pError, pTotalCount) when done
174
+ */
175
+ parseFile(pFilePath, pOptions, pChunkCallback, pCompletionCallback)
176
+ {
177
+ let tmpOptions = Object.assign({}, this.options, pOptions);
178
+
179
+ let tmpContent;
180
+ try
181
+ {
182
+ tmpContent = libFS.readFileSync(pFilePath, 'utf8');
183
+ }
184
+ catch (pError)
185
+ {
186
+ return pCompletionCallback(new Error(`JSON file read error: ${pError.message}`));
187
+ }
188
+
189
+ this.parseContent(tmpContent, tmpOptions,
190
+ (pError, pRecords) =>
191
+ {
192
+ if (pError)
193
+ {
194
+ return pCompletionCallback(pError);
195
+ }
196
+ pChunkCallback(null, pRecords);
197
+ return pCompletionCallback(null, pRecords.length);
198
+ });
199
+ }
200
+
201
+ /**
202
+ * Parse JSON content string into a full array of records.
203
+ *
204
+ * @param {string} pContent - Raw JSON text
205
+ * @param {object} pOptions - Parser options
206
+ * @param {function} fCallback - Called with (pError, pRecords)
207
+ */
208
+ parseContent(pContent, pOptions, fCallback)
209
+ {
210
+ let tmpOptions = Object.assign({}, this.options, pOptions);
211
+ let tmpFlattenNested = tmpOptions.flattenNested || false;
212
+ let tmpFlattenDelimiter = tmpOptions.flattenDelimiter || '_';
213
+
214
+ let tmpParsed;
215
+ try
216
+ {
217
+ tmpParsed = JSON.parse(pContent);
218
+ }
219
+ catch (pError)
220
+ {
221
+ return fCallback(new Error(`JSON parse error: ${pError.message}`));
222
+ }
223
+
224
+ let tmpRecords = this._resolveRecords(tmpParsed, tmpOptions);
225
+ if (tmpRecords === null)
226
+ {
227
+ if (tmpOptions.rootPath)
228
+ {
229
+ return fCallback(new Error(`rootPath '${tmpOptions.rootPath}' not found in JSON content`));
230
+ }
231
+ return fCallback(new Error(`Could not resolve records from JSON content`));
232
+ }
233
+
234
+ if (tmpFlattenNested)
235
+ {
236
+ let tmpFlattened = [];
237
+ for (let i = 0; i < tmpRecords.length; i++)
238
+ {
239
+ if (tmpRecords[i] !== null && typeof tmpRecords[i] === 'object')
240
+ {
241
+ tmpFlattened.push(this._flattenObject(tmpRecords[i], tmpFlattenDelimiter));
242
+ }
243
+ else
244
+ {
245
+ tmpFlattened.push(tmpRecords[i]);
246
+ }
247
+ }
248
+ return fCallback(null, tmpFlattened);
249
+ }
250
+
251
+ return fCallback(null, tmpRecords);
252
+ }
253
+ }
254
+
255
+ module.exports = MeadowIntegrationFileParserJSON;