|
| 1 | +const { groupBy, omit, partition } = require('lodash'); |
1 | 2 | const {
|
2 | 3 | getTableInfo,
|
3 | 4 | getTableRow,
|
@@ -42,28 +43,21 @@ const {
|
42 | 43 | } = require('./helpers');
|
43 | 44 | const pipe = require('../helpers/pipe');
|
44 | 45 |
|
45 |
| -const mergeCollectionsWithViews = jsonSchemas => |
46 |
| - jsonSchemas.reduce((structuredJSONSchemas, jsonSchema) => { |
47 |
| - if (jsonSchema.relatedTables) { |
48 |
| - const currentIndex = structuredJSONSchemas.findIndex( |
49 |
| - structuredSchema => jsonSchema.collectionName === structuredSchema.collectionName && jsonSchema.dbName, |
50 |
| - ); |
51 |
| - const relatedTableSchemaIndex = structuredJSONSchemas.findIndex(({ collectionName, dbName }) => |
52 |
| - jsonSchema.relatedTables.find( |
53 |
| - ({ tableName, schemaName }) => tableName === collectionName && schemaName === dbName, |
54 |
| - ), |
55 |
| - ); |
56 |
| - |
57 |
| - if (relatedTableSchemaIndex !== -1 && doesViewHaveRelatedTables(jsonSchema, structuredJSONSchemas)) { |
58 |
| - structuredJSONSchemas[relatedTableSchemaIndex].views.push(jsonSchema); |
59 |
| - } |
60 |
| - |
61 |
| - delete jsonSchema.relatedTables; |
62 |
| - return structuredJSONSchemas.filter((schema, i) => i !== currentIndex); |
63 |
| - } |
| 46 | +const mergeCollectionsWithViews = ({ jsonSchemas }) => { |
| 47 | + const [viewSchemas, collectionSchemas] = partition(jsonSchemas, jsonSchema => jsonSchema.relatedTables); |
| 48 | + const groupedViewSchemas = groupBy(viewSchemas, 'dbName'); |
| 49 | + const combinedViewSchemas = Object.entries(groupedViewSchemas).map(([dbName, views]) => { |
| 50 | + return { |
| 51 | + dbName, |
| 52 | + entityLevel: {}, |
| 53 | + emptyBucket: false, |
| 54 | + bucketInfo: views[0].bucketInfo, |
| 55 | + views: views.map(view => omit(view, ['relatedTables'])), |
| 56 | + }; |
| 57 | + }); |
64 | 58 |
|
65 |
| - return structuredJSONSchemas; |
66 |
| - }, jsonSchemas); |
| 59 | + return [...collectionSchemas, ...combinedViewSchemas]; |
| 60 | +}; |
67 | 61 |
|
68 | 62 | const getCollectionsRelationships = logger => async dbConnectionClient => {
|
69 | 63 | const dbName = dbConnectionClient.config.database;
|
|
0 commit comments