forest-cli 3.0.10 → 3.0.12-beta.1
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.
- package/bin/run +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +17 -8
- package/src/abstract-authenticated-command.js +0 -46
- package/src/abstract-command.js +0 -27
- package/src/commands/branch.js +0 -136
- package/src/commands/deploy.js +0 -90
- package/src/commands/environments/copy-layout.js +0 -116
- package/src/commands/environments/create.js +0 -62
- package/src/commands/environments/delete.js +0 -81
- package/src/commands/environments/get.js +0 -48
- package/src/commands/environments/reset.js +0 -76
- package/src/commands/environments/update.js +0 -46
- package/src/commands/environments.js +0 -41
- package/src/commands/init.js +0 -169
- package/src/commands/login.js +0 -34
- package/src/commands/logout.js +0 -18
- package/src/commands/projects/create.js +0 -262
- package/src/commands/projects/get.js +0 -53
- package/src/commands/projects.js +0 -36
- package/src/commands/push.js +0 -75
- package/src/commands/schema/apply.js +0 -140
- package/src/commands/schema/update.js +0 -70
- package/src/commands/set-origin.js +0 -59
- package/src/commands/switch.js +0 -117
- package/src/commands/user.js +0 -44
- package/src/context/command-project-create-plan.js +0 -11
- package/src/context/command-projects-common-plan.js +0 -65
- package/src/context/command-schema-update-command-plan.js +0 -4
- package/src/context/dependencies-plan.js +0 -21
- package/src/context/env-plan.js +0 -27
- package/src/context/plan.js +0 -12
- package/src/context/process-plan.js +0 -3
- package/src/context/renderers-plan.js +0 -7
- package/src/context/serializers-plan.js +0 -8
- package/src/context/services-plan.js +0 -8
- package/src/context/static.js +0 -19
- package/src/context/utils-plan.js +0 -7
- package/src/deserializers/api-error.js +0 -21
- package/src/deserializers/application-token.js +0 -15
- package/src/deserializers/branch.js +0 -8
- package/src/deserializers/environment.js +0 -5
- package/src/deserializers/job.js +0 -5
- package/src/deserializers/project.js +0 -11
- package/src/errors/application-error.js +0 -9
- package/src/errors/application-token/unable-to-create-application-token-error.js +0 -16
- package/src/errors/database/empty-database-error.js +0 -5
- package/src/errors/dumper/incompatible-liana-for-update-error.js +0 -16
- package/src/errors/dumper/invalid-forest-cli-project-structure-error.js +0 -16
- package/src/errors/forest-cli-error.js +0 -32
- package/src/index.js +0 -1
- package/src/renderers/branches.js +0 -45
- package/src/renderers/defaults.js +0 -19
- package/src/renderers/environment.js +0 -51
- package/src/renderers/environments.js +0 -51
- package/src/renderers/project.js +0 -45
- package/src/renderers/projects.js +0 -45
- package/src/serializers/application-token.js +0 -11
- package/src/serializers/deployment-request.js +0 -5
- package/src/serializers/environment.js +0 -20
- package/src/serializers/job.js +0 -5
- package/src/serializers/project.js +0 -17
- package/src/serializers/schema.js +0 -116
- package/src/services/api.js +0 -110
- package/src/services/apimap-sorter.js +0 -104
- package/src/services/application-token.js +0 -56
- package/src/services/ask-for-environment.js +0 -24
- package/src/services/authenticator.js +0 -187
- package/src/services/branch-manager.js +0 -169
- package/src/services/dumper/dumper.js +0 -503
- package/src/services/dumper/handlebars/helpers/equal.js +0 -3
- package/src/services/dumper/handlebars/helpers/indent.js +0 -3
- package/src/services/dumper/handlebars/helpers/is-array.js +0 -3
- package/src/services/dumper/handlebars/helpers/is-object.js +0 -3
- package/src/services/dumper/handlebars/helpers/sum.js +0 -3
- package/src/services/dumper/handlebars/helpers/wrap-quotes.js +0 -7
- package/src/services/dumper/handlebars/loader.js +0 -10
- package/src/services/dumper/handlebars/partials/render-nested.js +0 -27
- package/src/services/dumper/templates/app/Dockerfile.hbs +0 -6
- package/src/services/dumper/templates/app/app.hbs +0 -79
- package/src/services/dumper/templates/app/config/databases.hbs +0 -49
- package/src/services/dumper/templates/app/docker-compose.hbs +0 -33
- package/src/services/dumper/templates/app/dockerignore.hbs +0 -3
- package/src/services/dumper/templates/app/env.hbs +0 -20
- package/src/services/dumper/templates/app/forest/collection.hbs +0 -13
- package/src/services/dumper/templates/app/gitignore.hbs +0 -3
- package/src/services/dumper/templates/app/middlewares/forestadmin.hbs +0 -21
- package/src/services/dumper/templates/app/middlewares/welcome.hbs +0 -7
- package/src/services/dumper/templates/app/models/index.hbs +0 -56
- package/src/services/dumper/templates/app/models/mongo-model.hbs +0 -16
- package/src/services/dumper/templates/app/models/sequelize-model.hbs +0 -48
- package/src/services/dumper/templates/app/public/favicon.png +0 -0
- package/src/services/dumper/templates/app/routes/route.hbs +0 -61
- package/src/services/dumper/templates/app/server.hbs +0 -56
- package/src/services/dumper/templates/app/views/index.hbs +0 -73
- package/src/services/environment-manager.js +0 -139
- package/src/services/init-manager.js +0 -183
- package/src/services/job-state-checker.js +0 -57
- package/src/services/logger.js +0 -125
- package/src/services/oidc/authenticator.js +0 -109
- package/src/services/oidc/error.js +0 -32
- package/src/services/project-manager.js +0 -89
- package/src/services/projects/create/command-generate-config-getter.js +0 -61
- package/src/services/projects/create/project-creator.js +0 -89
- package/src/services/prompter/abstract-prompter.js +0 -13
- package/src/services/prompter/application-prompts.js +0 -63
- package/src/services/prompter/database-prompts.js +0 -208
- package/src/services/prompter/general-prompter.js +0 -61
- package/src/services/prompter/project-prompts.js +0 -47
- package/src/services/prompter/prompter-error.js +0 -10
- package/src/services/schema/schema-service.js +0 -154
- package/src/services/schema/update/analyzer/database-analyzer.js +0 -64
- package/src/services/schema/update/analyzer/mongo-collections-analyzer.js +0 -344
- package/src/services/schema/update/analyzer/mongo-embedded-analyzer.js +0 -226
- package/src/services/schema/update/analyzer/mongo-hasmany-analyzer.js +0 -55
- package/src/services/schema/update/analyzer/mongo-references-analyzer.js +0 -51
- package/src/services/schema/update/analyzer/mysql-table-constraints-getter.js +0 -59
- package/src/services/schema/update/analyzer/sequelize-column-type-getter.js +0 -147
- package/src/services/schema/update/analyzer/sequelize-default-value.js +0 -122
- package/src/services/schema/update/analyzer/sequelize-table-constraints-getter.js +0 -298
- package/src/services/schema/update/analyzer/sequelize-tables-analyzer.js +0 -529
- package/src/services/schema/update/database.js +0 -160
- package/src/services/schema-sender.js +0 -55
- package/src/services/spinner.js +0 -130
- package/src/services/with-current-project.js +0 -41
- package/src/utils/database-url.js +0 -33
- package/src/utils/directory-existence-checker.js +0 -17
- package/src/utils/error-handler.js +0 -63
- package/src/utils/error.js +0 -21
- package/src/utils/event-sender.js +0 -80
- package/src/utils/fields.js +0 -14
- package/src/utils/key-generator.js +0 -18
- package/src/utils/messages.js +0 -7
- package/src/utils/mongo-collections.js +0 -35
- package/src/utils/mongo-primitive-type.js +0 -44
- package/src/utils/strings.js +0 -49
- package/src/utils/terminator.js +0 -38
- package/src/utils/to-valid-package-name.js +0 -24
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const EmptyDatabaseError = require('../../../../errors/database/empty-database-error');
|
|
2
|
-
|
|
3
|
-
module.exports = class DatabaseAnalyzer {
|
|
4
|
-
constructor({
|
|
5
|
-
assertPresent,
|
|
6
|
-
mongoAnalyzer,
|
|
7
|
-
sequelizeAnalyzer,
|
|
8
|
-
terminator,
|
|
9
|
-
}) {
|
|
10
|
-
assertPresent({
|
|
11
|
-
mongoAnalyzer,
|
|
12
|
-
sequelizeAnalyzer,
|
|
13
|
-
terminator,
|
|
14
|
-
});
|
|
15
|
-
this.mongoAnalyzer = mongoAnalyzer;
|
|
16
|
-
this.sequelizeAnalyzer = sequelizeAnalyzer;
|
|
17
|
-
this.terminator = terminator;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async reportEmptyDatabase(orm, dialect) {
|
|
21
|
-
const logs = [`Your database looks empty! Please create some ${orm === 'mongoose' ? 'collections' : 'tables'} before running the command.`];
|
|
22
|
-
if (orm === 'sequelize') {
|
|
23
|
-
logs.push('If not, check whether you are using a custom database schema (use in that case the --schema option).');
|
|
24
|
-
}
|
|
25
|
-
return this.terminator.terminate(1, {
|
|
26
|
-
logs,
|
|
27
|
-
errorCode: 'database_empty',
|
|
28
|
-
errorMessage: 'Your database is empty.',
|
|
29
|
-
context: {
|
|
30
|
-
orm,
|
|
31
|
-
dialect,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async _analyze(analyze, databaseConnection, config, allowWarning) {
|
|
37
|
-
return analyze(databaseConnection, config, allowWarning)
|
|
38
|
-
.catch((error) => {
|
|
39
|
-
if (error instanceof EmptyDatabaseError) {
|
|
40
|
-
return this.reportEmptyDatabase(error.details.orm, error.details.dialect);
|
|
41
|
-
}
|
|
42
|
-
throw error;
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async analyzeMongoDb(databaseConnection, config, allowWarning) {
|
|
47
|
-
const analyze = this.mongoAnalyzer.analyzeMongoCollections.bind(this.mongoAnalyzer);
|
|
48
|
-
return this._analyze(analyze, databaseConnection.db(), config, allowWarning);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async analyze(databaseConnection, config, allowWarning) {
|
|
52
|
-
let analyze;
|
|
53
|
-
if (config.dbDialect === 'mongodb') {
|
|
54
|
-
analyze = this.mongoAnalyzer.analyzeMongoCollectionsWithoutProgressBar.bind(
|
|
55
|
-
this.mongoAnalyzer,
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
databaseConnection = databaseConnection.db();
|
|
59
|
-
} else {
|
|
60
|
-
analyze = this.sequelizeAnalyzer;
|
|
61
|
-
}
|
|
62
|
-
return this._analyze(analyze, databaseConnection, config, allowWarning);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
const P = require('bluebird');
|
|
2
|
-
const { ObjectId } = require('mongodb');
|
|
3
|
-
|
|
4
|
-
const EmptyDatabaseError = require('../../../../errors/database/empty-database-error');
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
getMongooseSchema,
|
|
8
|
-
hasEmbeddedTypes,
|
|
9
|
-
} = require('../../../../services/schema/update/analyzer/mongo-embedded-analyzer');
|
|
10
|
-
|
|
11
|
-
const MAP_REDUCE_ERROR_STRING = 'MapReduceError';
|
|
12
|
-
|
|
13
|
-
// NOTICE: This code runs on the MongoDB side (mapReduce feature) or locally
|
|
14
|
-
// when the the JS is disabled.
|
|
15
|
-
// The supported JS version is not the same than elsewhere.
|
|
16
|
-
// The code used here must work with MongoDB lower version supported.
|
|
17
|
-
/* eslint-disable vars-on-top, no-var, no-undef, no-restricted-syntax,
|
|
18
|
-
sonarjs/cognitive-complexity, no-use-before-define */
|
|
19
|
-
/* istanbul ignore next */
|
|
20
|
-
function mapCollection(keys = this, emitFunction, store) {
|
|
21
|
-
// this block is to inject the emit function when this code is running locally
|
|
22
|
-
var emitAction;
|
|
23
|
-
if (emitFunction && store) {
|
|
24
|
-
emitAction = function emit(key, value) {
|
|
25
|
-
emitFunction(key, value, store);
|
|
26
|
-
};
|
|
27
|
-
} else {
|
|
28
|
-
// when the emit is defined by mongodb
|
|
29
|
-
emitAction = emit;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function allItemsAreObjectIDs(array) {
|
|
33
|
-
if (!array.length) return false;
|
|
34
|
-
var itemToCheckCount = array.length > 3 ? 3 : array.length;
|
|
35
|
-
var arrayOfObjectIDs = true;
|
|
36
|
-
for (var i = 0; i < itemToCheckCount; i += 1) {
|
|
37
|
-
if (!(array[i] instanceof ObjectId)) {
|
|
38
|
-
arrayOfObjectIDs = false;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return arrayOfObjectIDs;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
for (var key in keys) {
|
|
46
|
-
if (keys[key] instanceof ObjectId && key !== '_id') {
|
|
47
|
-
emitAction(key, 'Mongoose.Schema.Types.ObjectId');
|
|
48
|
-
} else if (keys[key] instanceof Date) {
|
|
49
|
-
emitAction(key, 'Date');
|
|
50
|
-
} else if (typeof keys[key] === 'boolean') {
|
|
51
|
-
emitAction(key, 'Boolean');
|
|
52
|
-
} else if (typeof keys[key] === 'string') {
|
|
53
|
-
emitAction(key, 'String');
|
|
54
|
-
} else if (typeof keys[key] === 'number' && key !== '__v') {
|
|
55
|
-
emitAction(key, 'Number');
|
|
56
|
-
} else if (typeof keys[key] === 'object') {
|
|
57
|
-
if (Array.isArray(keys[key]) && allItemsAreObjectIDs(keys[key])) {
|
|
58
|
-
emitAction(key, '[Mongoose.Schema.Types.ObjectId]');
|
|
59
|
-
} else if (key !== '_id') {
|
|
60
|
-
var analysis = getMongooseSchema(keys[key]);
|
|
61
|
-
if (analysis) {
|
|
62
|
-
// Notice: Wrap the analysis of embedded in a recognizable object for further treatment
|
|
63
|
-
emitAction(key, { type: 'embedded', schema: analysis });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/* eslint-enable */
|
|
70
|
-
|
|
71
|
-
/* istanbul ignore next */
|
|
72
|
-
function reduceCollection(key, analyses) {
|
|
73
|
-
if (hasEmbeddedTypes(analyses)) {
|
|
74
|
-
const formattedAnalysis = { type: 'embedded', schemas: [] };
|
|
75
|
-
analyses.forEach((analysis) => {
|
|
76
|
-
if (analysis.type === 'embedded') {
|
|
77
|
-
formattedAnalysis.schemas.push(analysis.schema);
|
|
78
|
-
} else {
|
|
79
|
-
formattedAnalysis.schemas.push(analysis);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
return formattedAnalysis;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return analyses.length ? analyses[0] : null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/* eslint-disable no-shadow */
|
|
89
|
-
class MongoCollectionsAnalyzer {
|
|
90
|
-
constructor({
|
|
91
|
-
assertPresent, logger, detectReferences,
|
|
92
|
-
applyReferences, detectHasMany, applyHasMany,
|
|
93
|
-
isUnderscored, getMongooseTypeFromValue,
|
|
94
|
-
isOfMongooseType, getMongooseArraySchema,
|
|
95
|
-
getMongooseEmbeddedSchema, getMongooseSchema,
|
|
96
|
-
haveSameEmbeddedType, hasEmbeddedTypes,
|
|
97
|
-
mergeAnalyzedSchemas, isSystemCollection,
|
|
98
|
-
getCollectionName, mapCollection, reduceCollection,
|
|
99
|
-
makeProgressBar,
|
|
100
|
-
}) {
|
|
101
|
-
assertPresent({
|
|
102
|
-
logger,
|
|
103
|
-
detectReferences,
|
|
104
|
-
applyReferences,
|
|
105
|
-
detectHasMany,
|
|
106
|
-
applyHasMany,
|
|
107
|
-
isUnderscored,
|
|
108
|
-
getMongooseTypeFromValue,
|
|
109
|
-
isOfMongooseType,
|
|
110
|
-
getMongooseArraySchema,
|
|
111
|
-
getMongooseEmbeddedSchema,
|
|
112
|
-
getMongooseSchema,
|
|
113
|
-
haveSameEmbeddedType,
|
|
114
|
-
hasEmbeddedTypes,
|
|
115
|
-
mergeAnalyzedSchemas,
|
|
116
|
-
isSystemCollection,
|
|
117
|
-
getCollectionName,
|
|
118
|
-
mapCollection,
|
|
119
|
-
reduceCollection,
|
|
120
|
-
makeProgressBar,
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
this.logger = logger;
|
|
124
|
-
this.detectReferences = detectReferences;
|
|
125
|
-
this.applyReferences = applyReferences;
|
|
126
|
-
this.detectHasMany = detectHasMany;
|
|
127
|
-
this.applyHasMany = applyHasMany;
|
|
128
|
-
this.isUnderscored = isUnderscored;
|
|
129
|
-
this.getCollectionName = getCollectionName;
|
|
130
|
-
this.isSystemCollection = isSystemCollection;
|
|
131
|
-
this.mergeAnalyzedSchemas = mergeAnalyzedSchemas;
|
|
132
|
-
this.mapCollection = mapCollection;
|
|
133
|
-
this.reduceCollection = reduceCollection;
|
|
134
|
-
this.makeProgressBar = makeProgressBar;
|
|
135
|
-
|
|
136
|
-
this.restoreDefaultState();
|
|
137
|
-
|
|
138
|
-
this.mapReduceOptions = {
|
|
139
|
-
out: { inline: 1 },
|
|
140
|
-
limit: 100,
|
|
141
|
-
scope: {
|
|
142
|
-
getMongooseArraySchema,
|
|
143
|
-
getMongooseEmbeddedSchema,
|
|
144
|
-
getMongooseSchema,
|
|
145
|
-
getMongooseTypeFromValue,
|
|
146
|
-
haveSameEmbeddedType,
|
|
147
|
-
hasEmbeddedTypes,
|
|
148
|
-
isOfMongooseType,
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
restoreDefaultState() {
|
|
154
|
-
this.isProgressBarDisplay = true;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
mergeField(field) {
|
|
158
|
-
if (field.value && field.value.type === 'embedded') {
|
|
159
|
-
const schemas = field.value.schemas ? field.value.schemas : [field.value.schema];
|
|
160
|
-
const mergedSchema = this.mergeAnalyzedSchemas(schemas);
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
name: field._id,
|
|
164
|
-
type: mergedSchema,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
name: field._id,
|
|
169
|
-
type: field.value,
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
mapReduceErrors(resolve, reject) {
|
|
174
|
-
return (err, results) => {
|
|
175
|
-
if (err) {
|
|
176
|
-
if (err.message && (err.message.startsWith('CMD_NOT_ALLOWED') || err.message.startsWith('MapReduce'))) {
|
|
177
|
-
return resolve(MAP_REDUCE_ERROR_STRING);
|
|
178
|
-
}
|
|
179
|
-
if (err.codeName && err.codeName === 'CommandNotSupportedOnView') {
|
|
180
|
-
// NOTICE: Silently ignore views errors (e.g do not import views).
|
|
181
|
-
// See: https://github.com/ForestAdmin/lumber/issues/265
|
|
182
|
-
return resolve([]);
|
|
183
|
-
}
|
|
184
|
-
return reject(err);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return resolve(results.map((result) => this.mergeField(result)));
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
static emit(attributeName, attributesType, fieldsTypes) {
|
|
192
|
-
if (fieldsTypes[attributeName]) {
|
|
193
|
-
fieldsTypes[attributeName].push(attributesType);
|
|
194
|
-
} else {
|
|
195
|
-
fieldsTypes[attributeName] = [attributesType];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
getFields(fieldWithTypes) {
|
|
200
|
-
const keys = Object.keys(fieldWithTypes);
|
|
201
|
-
return keys.reduce((fields, key) => {
|
|
202
|
-
const field = this.mergeField({
|
|
203
|
-
_id: key,
|
|
204
|
-
value: this.reduceCollection(key, fieldWithTypes[key]),
|
|
205
|
-
});
|
|
206
|
-
fields.push(field);
|
|
207
|
-
return fields;
|
|
208
|
-
}, []);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// M0 free clusters and M2/M5 shared clusters do not support server-side JavaScript.
|
|
212
|
-
// Also, JS can be disabled on the mongodb instance.
|
|
213
|
-
// https://docs.atlas.mongodb.com/reference/free-shared-limitations/
|
|
214
|
-
// https://docs.mongodb.com/manual/core/server-side-javascript/
|
|
215
|
-
async analyzeMongoCollectionLocally(databaseConnection, collectionName) {
|
|
216
|
-
const collection = databaseConnection.collection(collectionName);
|
|
217
|
-
const analyze = await this.analyzeCollectionAndDisplayProgressBarIfIsAllow(
|
|
218
|
-
collection, collectionName,
|
|
219
|
-
);
|
|
220
|
-
return this.getFields(analyze);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
analyzeMongoCollectionRemotely(databaseConnection, collectionName) {
|
|
224
|
-
return new Promise((resolve, reject) => {
|
|
225
|
-
databaseConnection.collection(collectionName).mapReduce(
|
|
226
|
-
this.mapCollection,
|
|
227
|
-
this.reduceCollection,
|
|
228
|
-
this.mapReduceOptions,
|
|
229
|
-
this.mapReduceErrors(resolve, reject),
|
|
230
|
-
);
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
buildSchema(fields) {
|
|
235
|
-
return {
|
|
236
|
-
fields,
|
|
237
|
-
references: [],
|
|
238
|
-
primaryKeys: ['_id'],
|
|
239
|
-
options: {
|
|
240
|
-
timestamps: this.isUnderscored(fields),
|
|
241
|
-
},
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async applyRelationships(databaseConnection, fields, collectionName) {
|
|
246
|
-
const references = await this.detectReferences(databaseConnection, fields, collectionName);
|
|
247
|
-
this.applyReferences(fields, references);
|
|
248
|
-
const hasMany = await this.detectHasMany(databaseConnection, fields, collectionName);
|
|
249
|
-
this.applyHasMany(fields, hasMany);
|
|
250
|
-
return fields;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
fetchByChunkFunction(collection, numberOfDocumentAllowed) {
|
|
254
|
-
return async (fieldsTypes, index) => {
|
|
255
|
-
const minIndex = index * numberOfDocumentAllowed;
|
|
256
|
-
const options = { minIndex, limit: numberOfDocumentAllowed };
|
|
257
|
-
const documents = await collection.find({}, options).toArray();
|
|
258
|
-
documents.map((document) =>
|
|
259
|
-
this.mapCollection(document, MongoCollectionsAnalyzer.emit, fieldsTypes));
|
|
260
|
-
return fieldsTypes;
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
async analyzeCollectionAndDisplayProgressBarIfIsAllow(collection, collectionName) {
|
|
265
|
-
const countDocuments = await collection.countDocuments();
|
|
266
|
-
if (countDocuments === 0) {
|
|
267
|
-
return {};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const numberOfDocumentAllowed = 50;
|
|
271
|
-
const countIterations = Math.ceil(countDocuments / numberOfDocumentAllowed);
|
|
272
|
-
|
|
273
|
-
let fetchFunction = this.fetchByChunkFunction(collection, numberOfDocumentAllowed);
|
|
274
|
-
if (this.isProgressBarDisplay) {
|
|
275
|
-
const bar = this.makeProgressBar(
|
|
276
|
-
`Analysing the **${collectionName}** collection`,
|
|
277
|
-
countIterations,
|
|
278
|
-
);
|
|
279
|
-
bar.update(0);
|
|
280
|
-
|
|
281
|
-
fetchFunction = async (fieldTypes, index) => {
|
|
282
|
-
const wrapper = this.fetchByChunkFunction(collection, numberOfDocumentAllowed);
|
|
283
|
-
await wrapper(fieldTypes, index);
|
|
284
|
-
bar.tick();
|
|
285
|
-
return fieldTypes;
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const iterations = [...Array(countIterations).keys()];
|
|
290
|
-
return P.reduce(iterations, fetchFunction, {});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
async analyzeMongoCollectionsWithoutProgressBar(databaseConnection) {
|
|
294
|
-
this.isProgressBarDisplay = false;
|
|
295
|
-
return this.analyzeMongoCollections(databaseConnection);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
async analyzeMongoCollections(databaseConnection) {
|
|
299
|
-
const collections = await databaseConnection.collections();
|
|
300
|
-
if (collections.length === 0) {
|
|
301
|
-
this.restoreDefaultState();
|
|
302
|
-
throw new EmptyDatabaseError('no collections found', {
|
|
303
|
-
orm: 'mongoose',
|
|
304
|
-
dialect: 'mongodb',
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
const collectionsInfos = await databaseConnection.listCollections().toArray();
|
|
308
|
-
const isView = (name) => collectionsInfos.find(
|
|
309
|
-
(info) => !!info.options.viewOn && name === info.name,
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
let isMongodbInstanceSupportJs = true;
|
|
313
|
-
const schema = await P.reduce(collections, async (schema, collection) => {
|
|
314
|
-
const collectionName = this.getCollectionName(collection);
|
|
315
|
-
|
|
316
|
-
// Ignore system collections and collection without a valid name.
|
|
317
|
-
if (!collectionName || this.isSystemCollection(collection)) {
|
|
318
|
-
return schema;
|
|
319
|
-
}
|
|
320
|
-
let analysis = [];
|
|
321
|
-
if (isMongodbInstanceSupportJs && !isView(collectionName)) {
|
|
322
|
-
analysis = await this.analyzeMongoCollectionRemotely(databaseConnection, collectionName);
|
|
323
|
-
if (analysis === MAP_REDUCE_ERROR_STRING) {
|
|
324
|
-
isMongodbInstanceSupportJs = false;
|
|
325
|
-
this.logger.warn('The analysis is running locally instead of in the db instance because your instance does not support javascript.'
|
|
326
|
-
+ ' This action can takes a bit of time because it fetches all the collections.');
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (!isMongodbInstanceSupportJs && !isView(collectionName)) {
|
|
331
|
-
analysis = await this.analyzeMongoCollectionLocally(databaseConnection, collectionName);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
analysis = await this.applyRelationships(databaseConnection, analysis, collectionName);
|
|
335
|
-
schema[collectionName] = this.buildSchema(analysis);
|
|
336
|
-
return schema;
|
|
337
|
-
}, {});
|
|
338
|
-
|
|
339
|
-
this.restoreDefaultState();
|
|
340
|
-
return schema;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
module.exports = { MongoCollectionsAnalyzer, mapCollection, reduceCollection };
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
const {
|
|
2
|
-
getMongooseTypeFromValue,
|
|
3
|
-
isOfMongooseType,
|
|
4
|
-
} = require('../../../../utils/mongo-primitive-type');
|
|
5
|
-
|
|
6
|
-
/* eslint-disable vars-on-top, no-var, no-use-before-define, no-param-reassign */
|
|
7
|
-
/* istanbul ignore next */
|
|
8
|
-
function getMongooseEmbeddedSchema(embeddedObject, handleId = false) {
|
|
9
|
-
if (!embeddedObject) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const schema = {};
|
|
14
|
-
var keysToAnalyse = Object.keys(embeddedObject);
|
|
15
|
-
|
|
16
|
-
if (!handleId) {
|
|
17
|
-
keysToAnalyse = keysToAnalyse.filter((value) => value !== '_id');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
keysToAnalyse.forEach((key) => {
|
|
21
|
-
const analysis = getMongooseSchema(embeddedObject[key]);
|
|
22
|
-
|
|
23
|
-
if (analysis) {
|
|
24
|
-
schema[key] = analysis;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
if (Object.keys(schema).length === 0) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return schema;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/* istanbul ignore next */
|
|
36
|
-
function getMongooseArraySchema(arrayValue) {
|
|
37
|
-
if (!arrayValue || arrayValue.length === 0 || !Array.isArray(arrayValue)) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const analyses = [];
|
|
42
|
-
|
|
43
|
-
arrayValue.forEach((value) => {
|
|
44
|
-
const analysis = getMongooseSchema(value, true);
|
|
45
|
-
|
|
46
|
-
if (analysis) {
|
|
47
|
-
analyses.push(analysis);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
return analyses.length ? analyses : null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/* istanbul ignore next */
|
|
55
|
-
function getMongooseSchema(value, handleId = false) {
|
|
56
|
-
if (isOfMongooseType(value)) {
|
|
57
|
-
return getMongooseTypeFromValue(value);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (Array.isArray(value)) {
|
|
61
|
-
return getMongooseArraySchema(value);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (typeof value === 'object') {
|
|
65
|
-
return getMongooseEmbeddedSchema(value, handleId);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/* istanbul ignore next */
|
|
72
|
-
function hasEmbeddedTypes(analyses) {
|
|
73
|
-
if (!analyses || !analyses.length) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
return analyses.filter((analysis) => analysis.type === 'embedded').length > 0;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function haveSameEmbeddedType(type1, type2) {
|
|
80
|
-
return typeof type1 === typeof type2
|
|
81
|
-
&& Array.isArray(type1) === Array.isArray(type2);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function areSchemaTypesMixed(type1, type2) {
|
|
85
|
-
if (type1 === 'Object' || type2 === 'Object') {
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (type1 == null || type2 == null) {
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (typeof type1 === 'object' || typeof type2 === 'object') {
|
|
94
|
-
return !haveSameEmbeddedType(type1, type2);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return type1 !== type2;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function areAnalysesSameEmbeddedType(arrayOfAnalysis) {
|
|
101
|
-
if (!Array.isArray(arrayOfAnalysis) || !arrayOfAnalysis.length) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const firstAnalysis = arrayOfAnalysis[0];
|
|
106
|
-
|
|
107
|
-
for (var i = 1; i < arrayOfAnalysis.length; i += 1) {
|
|
108
|
-
if (!haveSameEmbeddedType(arrayOfAnalysis[i], firstAnalysis)) {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function addMongooseType(type, schema, currentKey) {
|
|
117
|
-
if (!schema[currentKey]) {
|
|
118
|
-
schema[currentKey] = type;
|
|
119
|
-
} else if (areSchemaTypesMixed(type, schema[currentKey])) {
|
|
120
|
-
schema[currentKey] = 'Object';
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function detectSubDocumentsIdUsage(schema1, schema2) {
|
|
125
|
-
if (schema1._id === 'ambiguous' || schema2._id === 'ambiguous') {
|
|
126
|
-
return 'ambiguous';
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (schema1._id && schema2._id) {
|
|
130
|
-
return true;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (!schema1._id && !schema2._id) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return 'ambiguous';
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function iterateOnTypeKeysToAddNestedSchemas(type, schema, isArray) {
|
|
141
|
-
Object.keys(type).forEach((key) => {
|
|
142
|
-
addNestedSchemaToParentSchema(type[key], schema, isArray ? 0 : key);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function setIdToSchema(type, schema) {
|
|
147
|
-
const idUsage = detectSubDocumentsIdUsage(schema, type);
|
|
148
|
-
|
|
149
|
-
if (['ambiguous', false].includes(idUsage)) {
|
|
150
|
-
schema._id = idUsage;
|
|
151
|
-
delete type._id;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function addObjectSchema(type, parentSchema, currentKey) {
|
|
156
|
-
const isTypeAnArray = Array.isArray(type);
|
|
157
|
-
|
|
158
|
-
if (parentSchema[currentKey] !== undefined) {
|
|
159
|
-
if (areSchemaTypesMixed(parentSchema[currentKey], type)) {
|
|
160
|
-
parentSchema[currentKey] = 'Object';
|
|
161
|
-
} else {
|
|
162
|
-
// NOTICE: Checking subDocuments id usage for array of subDocuments.
|
|
163
|
-
if (Array.isArray(parentSchema)) {
|
|
164
|
-
setIdToSchema(type, parentSchema[currentKey]);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
iterateOnTypeKeysToAddNestedSchemas(type, parentSchema[currentKey], isTypeAnArray);
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
parentSchema[currentKey] = isTypeAnArray ? [] : {};
|
|
171
|
-
|
|
172
|
-
// NOTICE: Init id usage for the first subDocument.
|
|
173
|
-
if (!isTypeAnArray && Array.isArray(parentSchema)) {
|
|
174
|
-
type._id = type._id || false;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
iterateOnTypeKeysToAddNestedSchemas(type, parentSchema[currentKey], isTypeAnArray);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function addNestedSchemaToParentSchema(type, schema, currentKey) {
|
|
182
|
-
if (typeof type === 'object') {
|
|
183
|
-
addObjectSchema(type, schema, currentKey);
|
|
184
|
-
} else {
|
|
185
|
-
addMongooseType(type, schema, currentKey);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function mergeAnalyzedSchemas(keyAnalyses) {
|
|
190
|
-
if (!areAnalysesSameEmbeddedType(keyAnalyses)) {
|
|
191
|
-
return 'Object';
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const firstAnalysis = keyAnalyses[0];
|
|
195
|
-
var schema;
|
|
196
|
-
var isNestedArray;
|
|
197
|
-
|
|
198
|
-
if (Array.isArray(firstAnalysis)) {
|
|
199
|
-
schema = [];
|
|
200
|
-
isNestedArray = true;
|
|
201
|
-
} else {
|
|
202
|
-
schema = {};
|
|
203
|
-
isNestedArray = false;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
keyAnalyses.forEach((keyAnalysis) => {
|
|
207
|
-
iterateOnTypeKeysToAddNestedSchemas(keyAnalysis, schema, isNestedArray);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return schema;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
module.exports = {
|
|
214
|
-
addMongooseType,
|
|
215
|
-
addNestedSchemaToParentSchema,
|
|
216
|
-
addObjectSchema,
|
|
217
|
-
areAnalysesSameEmbeddedType,
|
|
218
|
-
areSchemaTypesMixed,
|
|
219
|
-
detectSubDocumentsIdUsage,
|
|
220
|
-
getMongooseArraySchema,
|
|
221
|
-
getMongooseEmbeddedSchema,
|
|
222
|
-
getMongooseSchema,
|
|
223
|
-
haveSameEmbeddedType,
|
|
224
|
-
hasEmbeddedTypes,
|
|
225
|
-
mergeAnalyzedSchemas,
|
|
226
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
const P = require('bluebird');
|
|
3
|
-
const {
|
|
4
|
-
findCollectionMatchingSamples,
|
|
5
|
-
filterReferenceCollection,
|
|
6
|
-
} = require('../../../../utils/mongo-collections');
|
|
7
|
-
|
|
8
|
-
const OBJECT_ID_ARRAY = '[Mongoose.Schema.Types.ObjectId]';
|
|
9
|
-
const SAMPLE_COUNT_TO_FETCH = 10;
|
|
10
|
-
const SAMPLE_COUNT_TO_FETCH_ARRAY = 5;
|
|
11
|
-
|
|
12
|
-
const pickSampleValues = (databaseConnection, collectionName, field) =>
|
|
13
|
-
databaseConnection.collection(collectionName)
|
|
14
|
-
.aggregate([
|
|
15
|
-
{ $project: { [field.name]: { $slice: [`$${field.name}`, SAMPLE_COUNT_TO_FETCH_ARRAY] } } },
|
|
16
|
-
{ $match: { [field.name]: { $ne: null } } },
|
|
17
|
-
{ $sample: { size: SAMPLE_COUNT_TO_FETCH } },
|
|
18
|
-
{ $unwind: `$${field.name}` },
|
|
19
|
-
{ $project: { _id: false, value: `$${field.name}` } },
|
|
20
|
-
])
|
|
21
|
-
.toArray()
|
|
22
|
-
.then((samples) => _.map(samples, 'value'));
|
|
23
|
-
|
|
24
|
-
const buildReference = (collectionName, referencedCollection, field) => {
|
|
25
|
-
if (referencedCollection) {
|
|
26
|
-
return {
|
|
27
|
-
from: { collectionName, fieldName: field.name },
|
|
28
|
-
to: { collectionName: referencedCollection },
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const detectReference = (databaseConnection, field, collectionName) =>
|
|
35
|
-
pickSampleValues(databaseConnection, collectionName, field)
|
|
36
|
-
.then((samples) => findCollectionMatchingSamples(databaseConnection, samples))
|
|
37
|
-
.then((matches) => filterReferenceCollection(matches))
|
|
38
|
-
.then((referencedCollection) => buildReference(collectionName, referencedCollection, field));
|
|
39
|
-
|
|
40
|
-
const detectHasMany = (databaseConnection, fields, collectionName) => {
|
|
41
|
-
const objectIdFields = fields.filter((field) => field.type === OBJECT_ID_ARRAY);
|
|
42
|
-
return P.mapSeries(
|
|
43
|
-
objectIdFields,
|
|
44
|
-
(objectIdField) => detectReference(databaseConnection, objectIdField, collectionName),
|
|
45
|
-
).then((references) => references.filter((reference) => reference));
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const applyHasMany = (fields, references) =>
|
|
49
|
-
references.forEach((reference) => {
|
|
50
|
-
const field = _.find(fields, { name: reference.from.fieldName });
|
|
51
|
-
field.ref = reference.to.collectionName;
|
|
52
|
-
field.hasMany = true;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
module.exports = { detectHasMany, applyHasMany };
|