-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Use field path when creating index names instead of field names
- Loading branch information
Showing
2 changed files
with
33 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1041,7 +1041,7 @@ Schema.setMethod(function getFieldNames() { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.2.0 | ||
* @version 1.3.17 | ||
* @version 1.4.0 | ||
* | ||
* @param {string|FieldType} _field | ||
* @param {Object} options | ||
|
@@ -1073,13 +1073,33 @@ Schema.setMethod(function addIndex(_field, _options) { | |
// Set the default options | ||
options = Object.assign({}, this.indexOptions, options); | ||
|
||
if (options.name == null) { | ||
options.name = field.name; | ||
const setIndexName = () => { | ||
|
||
if (options.unique) { | ||
options.name += '_uq'; | ||
if (options.name) { | ||
return; | ||
} | ||
|
||
if (!this.model_name) { | ||
return; | ||
} | ||
|
||
if (options.name == null) { | ||
options.name = field.path; | ||
|
||
if (options.unique) { | ||
options.name += '_uq'; | ||
} | ||
} | ||
} | ||
|
||
if (this.indexes[options.name] == null) { | ||
// Create the index group if it doesn't exist yet. | ||
// The first time it's called will define the group options. | ||
this.indexes[options.name] = { | ||
fields: {}, | ||
options: options | ||
}; | ||
} | ||
}; | ||
|
||
if (typeof options.order == 'number') { | ||
if (options.order == 'asc') { | ||
|
@@ -1089,22 +1109,16 @@ Schema.setMethod(function addIndex(_field, _options) { | |
} | ||
} | ||
|
||
if (this.indexes[options.name] == null) { | ||
// Create the index group if it doesn't exist yet. | ||
// The first time it's called will define the group options. | ||
this.indexes[options.name] = { | ||
fields: {}, | ||
options: options | ||
}; | ||
} | ||
|
||
// Even if an index is unique, | ||
// it needs the 'alternate' property in order to be used | ||
// as an alternate method of updating without _id | ||
if (options.alternate) { | ||
this.has_alternates++; | ||
} | ||
|
||
// Try to set the index name now, if possible | ||
setIndexName(); | ||
|
||
const that = this; | ||
|
||
that.getDatasource().done(function gotDs(err, datasource) { | ||
|
@@ -1123,6 +1137,9 @@ Schema.setMethod(function addIndex(_field, _options) { | |
return alchemy.printLog('error', ['Unable to ensure index on this datasource', options.name], {err: new Error()}); | ||
} | ||
|
||
// Try to set the index name again, if it hasn't been done yet | ||
setIndexName(); | ||
|
||
let path = field.path; | ||
|
||
if (options.db_property) { | ||
|