ffc-pay-etl-framework 1.4.5 → 2.0.0

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,7 +1,8 @@
1
- const DEFAULT_PORT = 5432
2
- const { Sequelize } = require('sequelize')
1
+ const { Database } = require('ffc-database')
3
2
  const debug = require('debug')('connection')
4
3
 
4
+ const DEFAULT_PORT = 5432
5
+
5
6
  /**
6
7
  *
7
8
  * @param {Object} options
@@ -11,32 +12,44 @@ const debug = require('debug')('connection')
11
12
  * @param {Object} options.database
12
13
  * @param {Object} options.host
13
14
  * @param {Object} options.port
15
+ * @param {Object} [options.schema]
16
+ * @param {Object} [options.ssl]
17
+ * @param {Object} [options.tables]
18
+ * @param {Object} [options.pool]
19
+ * @param {Boolean} [options.useAzureManagedIdentity]
20
+ * @param {String} [options.azureClientId]
14
21
  * @returns Connection
15
22
  */
16
23
  async function postgresDatabaseConnection (options) {
17
24
  const connectionname = options.connectionname
18
- const username = options.username
19
- const password = options.password
20
- const database = options.database
21
- const host = options.host
22
- const port = options.port || DEFAULT_PORT
23
25
 
24
- const sequelize = new Sequelize(database, username, password, {
25
- host,
26
- port,
27
- dialect: 'postgres',
28
- logging: false
26
+ const database = new Database({
27
+ database: options.database,
28
+ username: options.username,
29
+ password: options.password,
30
+ host: options.host,
31
+ port: options.port || DEFAULT_PORT,
32
+ schema: options.schema,
33
+ ssl: options.ssl,
34
+ tables: options.tables,
35
+ pool: options.pool,
36
+ useAzureManagedIdentity: options.useAzureManagedIdentity,
37
+ azureClientId: options.azureClientId
29
38
  })
30
39
 
31
40
  try {
32
- await sequelize.authenticate()
33
- debug('sequelize.authenticate succeeded')
41
+ const connection = database.connect()
42
+ await connection.client.raw('select 1')
43
+ debug('database connection succeeded')
34
44
  return {
35
45
  name: connectionname,
36
- db: sequelize
46
+ db: {
47
+ ...connection,
48
+ query: (sql) => connection.client.raw(sql)
49
+ }
37
50
  }
38
51
  } catch (e) {
39
- debug('sequelize.authenticate failed')
52
+ debug('database connection failed')
40
53
  debug(e)
41
54
  throw e
42
55
  }
@@ -2,15 +2,15 @@
2
2
  *
3
3
  * @param {Object} options
4
4
  * @param {Object} options.connectionname
5
- * @param {Object} options.sequelize
5
+ * @param {Object} options.connection - the object returned by ffc-database's Database.connect()
6
6
  * @returns Connection
7
7
  */
8
8
  async function providedConnection (options) {
9
9
  const connectionname = options.connectionname
10
- const sequelize = options.sequelize
10
+ const connection = options.connection
11
11
  return {
12
12
  name: connectionname,
13
- db: sequelize
13
+ db: connection ? { ...connection, query: (sql) => connection.client.raw(sql) } : connection
14
14
  }
15
15
  }
16
16
 
@@ -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": "2.0.0",
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,9 +12,9 @@
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",
17
- "sequelize": "6.37.8",
15
+ "@faker-js/faker": "10.5.0",
16
+ "csv-parse": "7.0.2",
17
+ "ffc-database": "2.0.0",
18
18
  "validator": "13.15.23"
19
19
  },
20
20
  "devDependencies": {
@@ -26,13 +26,8 @@
26
26
  "nodemon": "3.1.4",
27
27
  "prettier": "3.3.3"
28
28
  },
29
- "overrides": {
30
- "sequelize": {
31
- "uuid": "11.1.1"
32
- }
33
- },
34
29
  "scripts": {
35
- "test": "jest",
30
+ "test": "PORT=0 node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPatterns=test/unit",
36
31
  "test:unit": "PORT=0 node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPatterns=test/unit",
37
32
  "test:watch": "jest --watchAll",
38
33
  "test:lint": "eslint",
@@ -48,4 +43,4 @@
48
43
  "**/test-output/**"
49
44
  ]
50
45
  }
51
- }
46
+ }