@jsreport/jsreport-core 3.0.1 → 3.1.2-test.2

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 (79) hide show
  1. package/LICENSE +166 -166
  2. package/README.md +298 -284
  3. package/index.js +29 -27
  4. package/lib/main/blobStorage/blobStorage.js +52 -47
  5. package/lib/main/blobStorage/inMemoryProvider.js +27 -27
  6. package/lib/main/blobStorage/mainActions.js +24 -24
  7. package/lib/main/createDefaultLoggerFormat.js +17 -17
  8. package/lib/main/defaults.js +14 -14
  9. package/lib/main/extensions/discover.js +20 -20
  10. package/lib/main/extensions/extensionsManager.js +264 -265
  11. package/lib/main/extensions/fileUtils.js +56 -55
  12. package/lib/main/extensions/findVersion.js +49 -53
  13. package/lib/main/extensions/locationCache.js +103 -97
  14. package/lib/main/extensions/sorter.js +10 -10
  15. package/lib/main/extensions/validateMinimalVersion.js +50 -50
  16. package/lib/main/folders/cascadeFolderRemove.js +25 -25
  17. package/lib/main/folders/getEntitiesInFolder.js +53 -53
  18. package/lib/main/folders/index.js +42 -42
  19. package/lib/main/folders/moveBetweenFolders.js +354 -354
  20. package/lib/main/folders/validateDuplicatedName.js +107 -107
  21. package/lib/main/folders/validateReservedName.js +53 -53
  22. package/lib/main/logger.js +244 -244
  23. package/lib/main/migration/resourcesToAssets.js +230 -210
  24. package/lib/main/migration/xlsxTemplatesToAssets.js +128 -118
  25. package/lib/main/monitoring.js +91 -91
  26. package/lib/main/optionsLoad.js +237 -237
  27. package/lib/main/optionsSchema.js +237 -237
  28. package/lib/main/reporter.js +579 -578
  29. package/lib/main/schemaValidator.js +252 -252
  30. package/lib/main/settings.js +154 -154
  31. package/lib/main/store/checkDuplicatedId.js +27 -27
  32. package/lib/main/store/collection.js +329 -329
  33. package/lib/main/store/documentStore.js +469 -469
  34. package/lib/main/store/mainActions.js +28 -28
  35. package/lib/main/store/memoryStoreProvider.js +99 -99
  36. package/lib/main/store/queue.js +48 -48
  37. package/lib/main/store/referenceUtils.js +251 -251
  38. package/lib/main/store/setupValidateId.js +43 -43
  39. package/lib/main/store/setupValidateShortid.js +71 -71
  40. package/lib/main/store/transaction.js +69 -69
  41. package/lib/main/store/typeUtils.js +180 -180
  42. package/lib/main/templates.js +34 -34
  43. package/lib/main/validateEntityName.js +62 -62
  44. package/lib/shared/createError.js +36 -36
  45. package/lib/shared/encryption.js +114 -114
  46. package/lib/shared/folders/index.js +11 -11
  47. package/lib/shared/folders/normalizeEntityPath.js +15 -15
  48. package/lib/shared/folders/resolveEntityFromPath.js +88 -88
  49. package/lib/shared/folders/resolveEntityPath.js +46 -46
  50. package/lib/shared/folders/resolveFolderFromPath.js +38 -38
  51. package/lib/shared/generateRequestId.js +4 -4
  52. package/lib/shared/listenerCollection.js +169 -0
  53. package/lib/shared/normalizeMetaFromLogs.js +30 -30
  54. package/lib/shared/reporter.js +123 -123
  55. package/lib/shared/request.js +64 -64
  56. package/lib/shared/tempFilesHandler.js +81 -81
  57. package/lib/shared/templates.js +82 -82
  58. package/lib/static/helpers.js +33 -33
  59. package/lib/worker/blobStorage.js +34 -34
  60. package/lib/worker/defaultProxyExtend.js +46 -46
  61. package/lib/worker/documentStore.js +49 -49
  62. package/lib/worker/extensionsManager.js +17 -17
  63. package/lib/worker/logger.js +48 -48
  64. package/lib/worker/render/diff.js +138 -138
  65. package/lib/worker/render/executeEngine.js +207 -200
  66. package/lib/worker/render/htmlRecipe.js +10 -10
  67. package/lib/worker/render/moduleHelper.js +43 -43
  68. package/lib/worker/render/noneEngine.js +12 -12
  69. package/lib/worker/render/profiler.js +158 -158
  70. package/lib/worker/render/render.js +205 -209
  71. package/lib/worker/render/resolveReferences.js +60 -60
  72. package/lib/worker/reporter.js +191 -187
  73. package/lib/worker/sandbox/runInSandbox.js +13 -4
  74. package/lib/worker/sandbox/safeSandbox.js +828 -822
  75. package/lib/worker/templates.js +78 -78
  76. package/lib/worker/workerHandler.js +54 -54
  77. package/package.json +92 -92
  78. package/test/blobStorage/common.js +21 -21
  79. package/test/store/common.js +1449 -1449
@@ -1,53 +1,49 @@
1
- const path = require('path')
2
- const fsAsync = require('fs/promises')
3
-
4
- module.exports = async (extension) => {
5
- const result = {
6
- source: 'anonymous'
7
- }
8
-
9
- if (extension.version != null || extension.source != null) {
10
- result.source = extension.source
11
- result.version = extension.version
12
- result.pkgVersion = extension.pkgVersion
13
-
14
- return result
15
- }
16
-
17
- if (extension.name == null) {
18
- return result
19
- }
20
-
21
- if (extension.directory == null) {
22
- result.source = 'local'
23
- result.version = 'local:inline'
24
- return result
25
- }
26
-
27
- try {
28
- const packageJsonContent = await fsAsync.readFile(path.join(extension.directory, 'package.json'))
29
- const packageJson = JSON.parse(packageJsonContent.toString())
30
-
31
- if (packageJson._requested && packageJson._requested.type === 'git') {
32
- result.source = 'git'
33
- result.version = packageJson._resolved
34
- result.pkgVersion = packageJson.version
35
- } else if (packageJson._requested && packageJson.version != null) {
36
- result.source = 'npm'
37
- result.version = packageJson.version
38
- result.pkgVersion = packageJson.version
39
- } else if (packageJson.version != null) {
40
- result.source = 'local'
41
- result.version = `local:${extension.directory}:${packageJson.version}`
42
- result.pkgVersion = packageJson.version
43
- } else {
44
- result.source = 'local'
45
- result.version = `local:${extension.directory}`
46
- }
47
- } catch (e) {
48
- result.source = 'local'
49
- result.version = `local:${extension.directory}`
50
- }
51
-
52
- return result
53
- }
1
+ const path = require('path')
2
+ const fsAsync = require('fs/promises')
3
+
4
+ module.exports = async (extension) => {
5
+ const result = {
6
+ source: 'anonymous'
7
+ }
8
+
9
+ if (extension.version != null || extension.source != null) {
10
+ result.source = extension.source
11
+ result.version = extension.version
12
+ result.pkgVersion = extension.pkgVersion
13
+
14
+ return result
15
+ }
16
+
17
+ if (extension.name == null) {
18
+ return result
19
+ }
20
+
21
+ if (extension.directory == null) {
22
+ result.source = 'local'
23
+ result.version = 'local:inline'
24
+ return result
25
+ }
26
+
27
+ try {
28
+ const packageJsonContent = await fsAsync.readFile(path.join(extension.directory, 'package.json'))
29
+ const packageJson = JSON.parse(packageJsonContent.toString())
30
+
31
+ if (extension.directory.includes('node_modules') && packageJson.version != null) {
32
+ result.source = 'npm'
33
+ result.version = packageJson.version
34
+ result.pkgVersion = packageJson.version
35
+ } else if (packageJson.version != null) {
36
+ result.source = 'local'
37
+ result.version = `local:${extension.directory}:${packageJson.version}`
38
+ result.pkgVersion = packageJson.version
39
+ } else {
40
+ result.source = 'local'
41
+ result.version = `local:${extension.directory}`
42
+ }
43
+ } catch (e) {
44
+ result.source = 'local'
45
+ result.version = `local:${extension.directory}`
46
+ }
47
+
48
+ return result
49
+ }
@@ -1,97 +1,103 @@
1
- const path = require('path')
2
- const fs = require('fs/promises')
3
- const fileUtils = require('./fileUtils')
4
-
5
- module.exports = (config) => {
6
- const cacheEntryRootPath = path.join(__dirname, '../../../../')
7
- const pathToLocationCache = path.join(config.tempCoreDirectory, 'locations.json')
8
-
9
- return {
10
- async get () {
11
- if (process.env.NODE_ENV === 'jsreport-development' || config.useExtensionsLocationCache === false) {
12
- config.logger.info('Skipping extensions location cache when NODE_ENV=jsreport-development or when option useExtensionsLocationCache === false, crawling now')
13
-
14
- return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
15
- }
16
-
17
- try {
18
- await fs.stat(pathToLocationCache)
19
- } catch (e) {
20
- config.logger.info('Extensions location cache not found, crawling directories')
21
- return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
22
- }
23
-
24
- const content = await fs.readFile(pathToLocationCache, 'utf8')
25
-
26
- let cache
27
-
28
- try {
29
- cache = JSON.parse(content)[cacheEntryRootPath]
30
- } catch (e) {
31
- // file is corrupted, nevermind and crawl the extensions
32
- }
33
-
34
- if (!cache) {
35
- config.logger.info('Extensions location cache doesn\'t contain entry yet, crawling')
36
- return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
37
- }
38
-
39
- if (cache.rootDirectory !== config.rootDirectory) {
40
- config.logger.info(`Extensions location cache ${pathToLocationCache} contains information with different rootDirectory, crawling`)
41
- return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
42
- }
43
-
44
- const stat = await fs.stat(cacheEntryRootPath)
45
-
46
- if (stat.mtime.getTime() > cache.lastSync) {
47
- config.logger.info(`Extensions location cache ${pathToLocationCache} contains older information, crawling`)
48
- return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
49
- }
50
-
51
- await Promise.all(cache.locations.map((l) => fs.stat(l)))
52
-
53
- config.logger.info(`Extensions location cache contains up to date information, skipping crawling in ${config.rootDirectory}`)
54
-
55
- const directories = fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js', cacheEntryRootPath)
56
- const result = directories.concat(cache.locations)
57
-
58
- return result
59
- },
60
-
61
- async save (extensions) {
62
- const directories = (
63
- extensions
64
- .map((e) => path.join(e.directory, 'jsreport.config.js'))
65
- .filter((d) => d.indexOf(cacheEntryRootPath) !== -1)
66
- )
67
-
68
- await fs.mkdir(config.tempCoreDirectory, { recursive: true })
69
-
70
- try {
71
- await fs.stat(pathToLocationCache)
72
- } catch (e) {
73
- await fs.writeFile(pathToLocationCache, JSON.stringify({}), 'utf8')
74
- }
75
-
76
- const content = await fs.readFile(pathToLocationCache, 'utf8')
77
-
78
- let nodes = {}
79
-
80
- try {
81
- nodes = JSON.parse(content)
82
- } catch (e) {
83
- // file is corrupted, nevermind and override all
84
- }
85
-
86
- nodes[cacheEntryRootPath] = {
87
- rootDirectory: config.rootDirectory,
88
- locations: directories,
89
- lastSync: new Date().getTime()
90
- }
91
-
92
- config.logger.debug(`Writing extension locations cache to ${pathToLocationCache}`)
93
-
94
- return fs.writeFile(pathToLocationCache, JSON.stringify(nodes), 'utf8')
95
- }
96
- }
97
- }
1
+ const path = require('path')
2
+ const fs = require('fs/promises')
3
+ const fileUtils = require('./fileUtils')
4
+
5
+ module.exports = (config) => {
6
+ // we want to have as cache entry the node_modules folder
7
+ const cacheEntryRootPath = path.join(__dirname, '../../../../../')
8
+ const pathToLocationCache = path.join(config.tempCoreDirectory, 'locations.json')
9
+
10
+ return {
11
+ async get () {
12
+ if (process.env.NODE_ENV === 'jsreport-development' || config.useExtensionsLocationCache === false) {
13
+ config.logger.info('Skipping extensions location cache when NODE_ENV=jsreport-development or when option useExtensionsLocationCache === false, crawling now')
14
+
15
+ return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
16
+ }
17
+
18
+ try {
19
+ await fs.stat(pathToLocationCache)
20
+ } catch (e) {
21
+ config.logger.info('Extensions location cache not found, crawling directories')
22
+ return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
23
+ }
24
+
25
+ const content = await fs.readFile(pathToLocationCache, 'utf8')
26
+
27
+ let cache
28
+
29
+ try {
30
+ cache = JSON.parse(content)[cacheEntryRootPath]
31
+ } catch (e) {
32
+ // file is corrupted, never mind and crawl the extensions
33
+ }
34
+
35
+ if (!cache) {
36
+ config.logger.info('Extensions location cache doesn\'t contain entry yet, crawling')
37
+ return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
38
+ }
39
+
40
+ if (cache.rootDirectory !== config.rootDirectory) {
41
+ config.logger.info(`Extensions location cache ${pathToLocationCache} contains information with different rootDirectory, crawling`)
42
+ return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
43
+ }
44
+
45
+ const stat = await fs.stat(cacheEntryRootPath)
46
+
47
+ if (stat.mtime.getTime() > cache.lastSync) {
48
+ config.logger.info(`Extensions location cache ${pathToLocationCache} contains older information, crawling`)
49
+ return fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js')
50
+ }
51
+
52
+ await Promise.all(cache.locations.map((l) => fs.stat(l)))
53
+
54
+ config.logger.info(`Extensions location cache contains up to date information, skipping crawling in ${config.rootDirectory}`)
55
+
56
+ // since the rootDirectory is dynamic (can be changed from options) we need to still
57
+ // crawl to see if we find new extensions if the rootDirectory was changed
58
+ const directories = fileUtils.walkSync(config.rootDirectory, 'jsreport.config.js', cacheEntryRootPath)
59
+ const result = directories.concat(cache.locations)
60
+
61
+ return result
62
+ },
63
+
64
+ async save (extensions) {
65
+ // we just want to store in cache the extensions found from the cache entry root path
66
+ // or in other words the ones from node_modules, we skip storing the extensions found
67
+ // in the rootDirectory
68
+ const directories = (
69
+ extensions
70
+ .map((e) => path.join(e.directory, 'jsreport.config.js'))
71
+ .filter((d) => d.startsWith(cacheEntryRootPath))
72
+ )
73
+
74
+ await fs.mkdir(config.tempCoreDirectory, { recursive: true })
75
+
76
+ try {
77
+ await fs.stat(pathToLocationCache)
78
+ } catch (e) {
79
+ await fs.writeFile(pathToLocationCache, JSON.stringify({}), 'utf8')
80
+ }
81
+
82
+ const content = await fs.readFile(pathToLocationCache, 'utf8')
83
+
84
+ let nodes = {}
85
+
86
+ try {
87
+ nodes = JSON.parse(content)
88
+ } catch (e) {
89
+ // file is corrupted, never mind and override all
90
+ }
91
+
92
+ nodes[cacheEntryRootPath] = {
93
+ rootDirectory: config.rootDirectory,
94
+ locations: directories,
95
+ lastSync: new Date().getTime()
96
+ }
97
+
98
+ config.logger.debug(`Writing extension locations cache to ${pathToLocationCache}`)
99
+
100
+ return fs.writeFile(pathToLocationCache, JSON.stringify(nodes), 'utf8')
101
+ }
102
+ }
103
+ }
@@ -1,10 +1,10 @@
1
- module.exports = (pa, pb) => {
2
- // todo, sort better by dependencies
3
- pa.dependencies = pa.dependencies || []
4
- pb.dependencies = pb.dependencies || []
5
-
6
- if (pa.dependencies.length > pb.dependencies.length) return 1
7
- if (pa.dependencies.length < pb.dependencies.length) return -1
8
-
9
- return 0
10
- }
1
+ module.exports = (pa, pb) => {
2
+ // todo, sort better by dependencies
3
+ pa.dependencies = pa.dependencies || []
4
+ pb.dependencies = pb.dependencies || []
5
+
6
+ if (pa.dependencies.length > pb.dependencies.length) return 1
7
+ if (pa.dependencies.length < pb.dependencies.length) return -1
8
+
9
+ return 0
10
+ }
@@ -1,50 +1,50 @@
1
- const semver = require('semver')
2
-
3
- function validateMinimalVersion (baseExtension, extension) {
4
- const generalErrorMsg = `${extension.name == null ? 'Anonymous extension' : `Extension "${extension.name}"`} is not compatible with "${baseExtension.name}" extension.`
5
- const getExtensionLabel = () => extension.name == null ? 'anonymous extension' : `"${extension.name}"`
6
-
7
- // this allows to validate pre-release versions too
8
- const normalizeVersionResults = /^(\d+)\.(\d+)\.(\d+)(-.+)?$/.exec(baseExtension.version)
9
-
10
- if (normalizeVersionResults == null) {
11
- throw new Error(`Invalid format for version of "${baseExtension.name}" extension. Version found: "${baseExtension.version}"`)
12
- }
13
-
14
- let shouldValidateThatRequiresIsNotEmpty = true
15
-
16
- if (
17
- (extension.source === 'anonymous') ||
18
- (extension.source === 'local' && extension.pkgVersion == null)
19
- ) {
20
- shouldValidateThatRequiresIsNotEmpty = false
21
- }
22
-
23
- if (shouldValidateThatRequiresIsNotEmpty && extension.requires == null) {
24
- throw new Error(`${generalErrorMsg} Missing ".requires" information in ${getExtensionLabel(extension)} definition`)
25
- }
26
-
27
- const extensionRequires = extension.requires || {}
28
- const baseExtensionRequireInExtension = extensionRequires[baseExtension.name]
29
-
30
- if (baseExtensionRequireInExtension == null) {
31
- return
32
- }
33
-
34
- const minimalVersionFormatRegExp = /^(x{1}|\d+)\.(x{1}|\d+)\.(x{1}|\d+)$/
35
-
36
- if (!minimalVersionFormatRegExp.test(baseExtensionRequireInExtension)) {
37
- throw new Error(`${generalErrorMsg} Invalid format for minimal version of "${baseExtension.name}" extension. Minimal version found in ${getExtensionLabel(extension)} definition: "${baseExtensionRequireInExtension}"`)
38
- }
39
-
40
- const baseVersion = `${normalizeVersionResults[1]}.${normalizeVersionResults[2]}.${normalizeVersionResults[3]}`
41
- const targetSemVer = `>=${baseExtensionRequireInExtension}`
42
-
43
- const isValid = semver.satisfies(baseVersion, targetSemVer)
44
-
45
- if (!isValid) {
46
- throw new Error(`${generalErrorMsg} Minimal version spec "${baseExtensionRequireInExtension}" found in ${getExtensionLabel(extension)} does not match with version "${baseExtension.version}" of "${baseExtension.name}"`)
47
- }
48
- }
49
-
50
- module.exports = validateMinimalVersion
1
+ const semver = require('semver')
2
+
3
+ function validateMinimalVersion (baseExtension, extension) {
4
+ const generalErrorMsg = `${extension.name == null ? 'Anonymous extension' : `Extension "${extension.name}"`} is not compatible with "${baseExtension.name}" extension.`
5
+ const getExtensionLabel = () => extension.name == null ? 'anonymous extension' : `"${extension.name}"`
6
+
7
+ // this allows to validate pre-release versions too
8
+ const normalizeVersionResults = /^(\d+)\.(\d+)\.(\d+)(-.+)?$/.exec(baseExtension.version)
9
+
10
+ if (normalizeVersionResults == null) {
11
+ throw new Error(`Invalid format for version of "${baseExtension.name}" extension. Version found: "${baseExtension.version}"`)
12
+ }
13
+
14
+ let shouldValidateThatRequiresIsNotEmpty = true
15
+
16
+ if (
17
+ (extension.source === 'anonymous') ||
18
+ (extension.source === 'local' && extension.pkgVersion == null)
19
+ ) {
20
+ shouldValidateThatRequiresIsNotEmpty = false
21
+ }
22
+
23
+ if (shouldValidateThatRequiresIsNotEmpty && extension.requires == null) {
24
+ throw new Error(`${generalErrorMsg} Missing ".requires" information in ${getExtensionLabel(extension)} definition`)
25
+ }
26
+
27
+ const extensionRequires = extension.requires || {}
28
+ const baseExtensionRequireInExtension = extensionRequires[baseExtension.name]
29
+
30
+ if (baseExtensionRequireInExtension == null) {
31
+ return
32
+ }
33
+
34
+ const minimalVersionFormatRegExp = /^(x{1}|\d+)\.(x{1}|\d+)\.(x{1}|\d+)$/
35
+
36
+ if (!minimalVersionFormatRegExp.test(baseExtensionRequireInExtension)) {
37
+ throw new Error(`${generalErrorMsg} Invalid format for minimal version of "${baseExtension.name}" extension. Minimal version found in ${getExtensionLabel(extension)} definition: "${baseExtensionRequireInExtension}"`)
38
+ }
39
+
40
+ const baseVersion = `${normalizeVersionResults[1]}.${normalizeVersionResults[2]}.${normalizeVersionResults[3]}`
41
+ const targetSemVer = `>=${baseExtensionRequireInExtension}`
42
+
43
+ const isValid = semver.satisfies(baseVersion, targetSemVer)
44
+
45
+ if (!isValid) {
46
+ throw new Error(`${generalErrorMsg} Minimal version spec "${baseExtensionRequireInExtension}" found in ${getExtensionLabel(extension)} does not match with version "${baseExtension.version}" of "${baseExtension.name}"`)
47
+ }
48
+ }
49
+
50
+ module.exports = validateMinimalVersion
@@ -1,25 +1,25 @@
1
- module.exports = (reporter) => {
2
- reporter.documentStore.collection('folders').beforeRemoveListeners.add('folders', async (q, req) => {
3
- const foldersToRemove = await reporter.documentStore.collection('folders').find(q, req)
4
-
5
- for (const folder of foldersToRemove) {
6
- for (const c of Object.keys(reporter.documentStore.collections)) {
7
- const entities = await reporter.documentStore.collection(c).find({
8
- folder: {
9
- shortid: folder.shortid
10
- }
11
- }, req)
12
-
13
- if (entities.length === 0) {
14
- continue
15
- }
16
-
17
- for (const e of entities) {
18
- await reporter.documentStore.collection(c).remove({
19
- _id: e._id
20
- }, req)
21
- }
22
- }
23
- }
24
- })
25
- }
1
+ module.exports = (reporter) => {
2
+ reporter.documentStore.collection('folders').beforeRemoveListeners.add('folders', async (q, req) => {
3
+ const foldersToRemove = await reporter.documentStore.collection('folders').find(q, req)
4
+
5
+ for (const folder of foldersToRemove) {
6
+ for (const c of Object.keys(reporter.documentStore.collections)) {
7
+ const entities = await reporter.documentStore.collection(c).find({
8
+ folder: {
9
+ shortid: folder.shortid
10
+ }
11
+ }, req)
12
+
13
+ if (entities.length === 0) {
14
+ continue
15
+ }
16
+
17
+ for (const e of entities) {
18
+ await reporter.documentStore.collection(c).remove({
19
+ _id: e._id
20
+ }, req)
21
+ }
22
+ }
23
+ }
24
+ })
25
+ }
@@ -1,53 +1,53 @@
1
-
2
- module.exports = (reporter) => async function getEntitiesInFolder (folderShortId, recursive, req) {
3
- if (folderShortId == null) {
4
- throw new Error('Missing folder shortid param')
5
- }
6
-
7
- const entities = []
8
- const lookup = []
9
-
10
- for (const [entitySetName] of Object.entries(reporter.documentStore.model.entitySets)) {
11
- lookup.push(reporter.documentStore.collection(entitySetName).find({
12
- folder: {
13
- shortid: folderShortId
14
- }
15
- }, req).then((results) => {
16
- if (results.length === 0) {
17
- return
18
- }
19
-
20
- if (entitySetName === 'folders') {
21
- return Promise.all(results.map((folder) => {
22
- entities.push({
23
- entitySet: entitySetName,
24
- entity: folder
25
- })
26
-
27
- if (recursive === true) {
28
- return getEntitiesInFolder(folder.shortid, true, req)
29
- }
30
-
31
- return undefined
32
- })).then((folderLookupResults) => {
33
- folderLookupResults.forEach((childEntities) => {
34
- if (childEntities) {
35
- entities.push(...childEntities)
36
- }
37
- })
38
- })
39
- } else {
40
- results.forEach((entity) => {
41
- entities.push({
42
- entitySet: entitySetName,
43
- entity
44
- })
45
- })
46
- }
47
- }))
48
- }
49
-
50
- await Promise.all(lookup)
51
-
52
- return entities
53
- }
1
+
2
+ module.exports = (reporter) => async function getEntitiesInFolder (folderShortId, recursive, req) {
3
+ if (folderShortId == null) {
4
+ throw new Error('Missing folder shortid param')
5
+ }
6
+
7
+ const entities = []
8
+ const lookup = []
9
+
10
+ for (const [entitySetName] of Object.entries(reporter.documentStore.model.entitySets)) {
11
+ lookup.push(reporter.documentStore.collection(entitySetName).find({
12
+ folder: {
13
+ shortid: folderShortId
14
+ }
15
+ }, req).then((results) => {
16
+ if (results.length === 0) {
17
+ return
18
+ }
19
+
20
+ if (entitySetName === 'folders') {
21
+ return Promise.all(results.map((folder) => {
22
+ entities.push({
23
+ entitySet: entitySetName,
24
+ entity: folder
25
+ })
26
+
27
+ if (recursive === true) {
28
+ return getEntitiesInFolder(folder.shortid, true, req)
29
+ }
30
+
31
+ return undefined
32
+ })).then((folderLookupResults) => {
33
+ folderLookupResults.forEach((childEntities) => {
34
+ if (childEntities) {
35
+ entities.push(...childEntities)
36
+ }
37
+ })
38
+ })
39
+ } else {
40
+ results.forEach((entity) => {
41
+ entities.push({
42
+ entitySet: entitySetName,
43
+ entity
44
+ })
45
+ })
46
+ }
47
+ }))
48
+ }
49
+
50
+ await Promise.all(lookup)
51
+
52
+ return entities
53
+ }