ffc-pay-etl-framework 1.4.5 → 1.4.7

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.
@@ -1,44 +1,88 @@
1
1
  const { Transform } = require('node:stream')
2
2
 
3
3
  /**
4
+ * Creates a Node.js Transform stream that replaces placeholder data with fake data.
4
5
  *
5
6
  * @param {Object} options
6
- * @param {Array} options.columns {
7
- * columns: [
8
- * {
9
- * name: "column-name",
10
- * faker: "company.name"
11
- * }
12
- * ]
13
- * }
14
- * @param {String} options.locale
15
- * @returns StreamReader
7
+ * @param {Array} options.columns List of column names and their mapped Faker methods.
8
+ * @param {String} options.locale Optional locale string identifier (e.g., 'en', 'de', 'en_IN').
9
+ * @returns {Transform}
16
10
  */
17
11
  function fakerTransformer (options) {
18
12
  const columns = options.columns
19
13
  let faker
20
- if (options.locale) {
21
- faker = require(`@faker-js/faker/locale/${options.locale}`).faker
22
- } else {
23
- faker = require('@faker-js/faker').faker
24
- }
14
+ let getFaker
15
+ let fakerLoadError
16
+ let fakerLoaded = false
17
+
18
+ async function loadFaker () {
19
+ const { Faker, allLocales, faker: defaultFaker } = await import('@faker-js/faker')
20
+
21
+ // Resolve the locale instance safely using the Faker v10 allLocales registry
22
+ if (options.locale) {
23
+ const localeKey = options.locale === 'en_IND' ? 'en_IN' : options.locale
24
+ const selectedLocale = allLocales[localeKey]
25
25
 
26
- function getFaker (fakerType) {
27
- return fakerType.split('.').reduce((a, b) => {
28
- return Object.keys(a).length === 0 ? faker[b] : a[b]
29
- }, {})
26
+ if (!selectedLocale) {
27
+ throw new Error(`Locale "${options.locale}" is not supported by Faker v10.`)
28
+ }
29
+
30
+ faker = new Faker({ locale: selectedLocale })
31
+ } else {
32
+ faker = defaultFaker
33
+ }
34
+
35
+ // Modern path resolver using optional chaining to dig down into Faker's Proxy structure
36
+ getFaker = function (fakerType) {
37
+ return fakerType.split('.').reduce((current, key) => current?.[key], faker)
38
+ }
39
+
40
+ fakerLoaded = true
30
41
  }
31
42
 
32
43
  return new Transform({
33
44
  readableObjectMode: true,
34
45
  writableObjectMode: true,
35
46
  transform (chunk, _, callback) {
36
- const { _columns } = chunk
37
- columns.forEach(column => {
38
- const colIndex = _columns.indexOf(column.name)
39
- chunk[colIndex] = getFaker(column.faker)()
40
- })
41
- callback(null, chunk)
47
+ if (fakerLoadError) {
48
+ callback(fakerLoadError)
49
+ return
50
+ }
51
+
52
+ const runTransform = async () => {
53
+ if (!fakerLoaded) {
54
+ await loadFaker()
55
+ }
56
+
57
+ const { _columns } = chunk
58
+
59
+ for (const column of columns) {
60
+ const colIndex = _columns.indexOf(column.name)
61
+
62
+ // Guard: Skip processing if the configured column name doesn't exist in the chunk headers
63
+ if (colIndex === -1) {
64
+ continue
65
+ }
66
+
67
+ const fakerMethod = getFaker(column.faker)
68
+
69
+ // Guard: Verify that the method exists and can be executed safely
70
+ if (typeof fakerMethod !== 'function') {
71
+ throw new TypeError(`Faker method "${column.faker}" is invalid or does not exist.`)
72
+ }
73
+
74
+ chunk[colIndex] = fakerMethod()
75
+ }
76
+
77
+ return chunk
78
+ }
79
+
80
+ runTransform()
81
+ .then((transformed) => callback(null, transformed))
82
+ .catch((err) => {
83
+ fakerLoadError = err
84
+ callback(err)
85
+ })
42
86
  }
43
87
  })
44
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ffc-pay-etl-framework",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
4
4
  "description": "A framework for creating ETL pipelines in node",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/DEFRA/ffc-pay-etl-framework",
@@ -12,8 +12,8 @@
12
12
  ],
13
13
  "license": "OGL-UK-3.0",
14
14
  "dependencies": {
15
- "@faker-js/faker": "8.4.1",
16
- "csv-parse": "6.1.0",
15
+ "@faker-js/faker": "10.5.0",
16
+ "csv-parse": "7.0.2",
17
17
  "sequelize": "6.37.8",
18
18
  "validator": "13.15.23"
19
19
  },
@@ -28,11 +28,12 @@
28
28
  },
29
29
  "overrides": {
30
30
  "sequelize": {
31
- "uuid": "11.1.1"
31
+ "uuid": "11.1.1",
32
+ "moment": "2.31.0"
32
33
  }
33
34
  },
34
35
  "scripts": {
35
- "test": "jest",
36
+ "test": "PORT=0 node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPatterns=test/unit",
36
37
  "test:unit": "PORT=0 node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPatterns=test/unit",
37
38
  "test:watch": "jest --watchAll",
38
39
  "test:lint": "eslint",