-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update MongoDB to use lean() #999
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,30 @@ let context = { | |
op: 'IoTAgentNGSI.MongoDBGroupRegister' | ||
}; | ||
|
||
const attributeList = [ | ||
'url', | ||
'resource', | ||
'apikey', | ||
'type', | ||
'service', | ||
'subservice', | ||
'description', | ||
'trust', | ||
'cbHost', | ||
'timezone', | ||
'timestamp', | ||
'commands', | ||
'lazy', | ||
'attributes', | ||
'staticAttributes', | ||
'internalAttributes', | ||
'autoprovision', | ||
'explicitAttrs', | ||
'expressionLanguage', | ||
'defaultEntityNameConjunction', | ||
'ngsiVersion' | ||
]; | ||
|
||
/** | ||
* Generates a handler for the save device group operations. The handler will take the customary error and the saved | ||
* device group as the parameters (and pass the serialized DAO as the callback value). | ||
|
@@ -58,33 +82,10 @@ function saveGroupHandler(groupDAO, callback) { | |
function createGroup(group, callback) { | ||
/* eslint-disable-next-line new-cap */ | ||
const groupObj = new Group.model(); | ||
const attributeList = [ | ||
'url', | ||
'resource', | ||
'apikey', | ||
'type', | ||
'service', | ||
'subservice', | ||
'description', | ||
'trust', | ||
'cbHost', | ||
'timezone', | ||
'timestamp', | ||
'commands', | ||
'lazy', | ||
'attributes', | ||
'staticAttributes', | ||
'internalAttributes', | ||
'autoprovision', | ||
'explicitAttrs', | ||
'expressionLanguage', | ||
'defaultEntityNameConjunction', | ||
'ngsiVersion' | ||
]; | ||
|
||
for (let i = 0; i < attributeList.length; i++) { | ||
groupObj[attributeList[i]] = group[attributeList[i]]; | ||
} | ||
|
||
attributeList.forEach((key) => { | ||
groupObj[key] = group[key]; | ||
}); | ||
|
||
logger.debug( | ||
context, | ||
|
@@ -163,7 +164,7 @@ function getById(id, callback) { | |
const query = Group.model.findOne({ _id: id }); | ||
query.select({ __v: 0 }); | ||
|
||
query.exec(function handleGet(error, data) { | ||
query.lean().exec(function handleGet(error, data) { | ||
if (error) { | ||
logger.debug(context, 'Internal MongoDB Error getting group: %s', error); | ||
|
||
|
@@ -210,6 +211,24 @@ function find(service, subservice, callback) { | |
}); | ||
} | ||
|
||
function findOneInMongoDB(queryObj, fields, callback) { | ||
const query = Group.model.findOne(queryObj); | ||
query.select({ __v: 0 }); | ||
query.lean().exec(function handleGet(error, data) { | ||
if (error) { | ||
logger.debug(context, 'Internal MongoDB Error getting group: %s', error); | ||
callback(new errors.InternalDbError(error)); | ||
} else if (data) { | ||
context = fillService(context, data); | ||
logger.debug(context, 'Device group data found: %j', data); | ||
callback(null, data); | ||
} else { | ||
logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj); | ||
callback(new errors.DeviceGroupNotFound(fields, queryObj)); | ||
} | ||
}); | ||
} | ||
|
||
function findBy(fields) { | ||
return function () { | ||
const queryObj = {}; | ||
|
@@ -228,24 +247,7 @@ function findBy(fields) { | |
|
||
context = fillService(context, { service: 'n/a', subservice: 'n/a' }); | ||
logger.debug(context, 'Looking for group params %j with queryObj %j', fields, queryObj); | ||
const query = Group.model.findOne(queryObj); | ||
|
||
query.select({ __v: 0 }); | ||
|
||
query.exec(function handleGet(error, data) { | ||
if (error) { | ||
logger.debug(context, 'Internal MongoDB Error getting group: %s', error); | ||
callback(new errors.InternalDbError(error)); | ||
} else if (data) { | ||
context = fillService(context, data); | ||
logger.debug(context, 'Device group data found: %j', data.toObject()); | ||
callback(null, data.toObject()); | ||
} else { | ||
logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj); | ||
|
||
callback(new errors.DeviceGroupNotFound(fields, queryObj)); | ||
} | ||
}); | ||
findOneInMongoDB(queryObj, fields, callback); | ||
}; | ||
} | ||
|
||
|
@@ -255,36 +257,15 @@ function update(id, body, callback) { | |
if (error) { | ||
callback(error); | ||
} else { | ||
const attributes = [ | ||
'url', | ||
'apikey', | ||
'type', | ||
'service', | ||
'subservice', | ||
'description', | ||
'trust', | ||
'cbHost', | ||
'timezone', | ||
'timestamp', | ||
'commands', | ||
'lazy', | ||
'attributes', | ||
'staticAttributes', | ||
'internalAttributes', | ||
'explicitAttrs', | ||
'expressionLanguage', | ||
'defaultEntityNameConjunction', | ||
'ngsiVersion' | ||
]; | ||
|
||
for (let i = 0; i < attributes.length; i++) { | ||
if (body[attributes[i]] !== undefined) { | ||
group[attributes[i]] = body[attributes[i]]; | ||
attributeList.forEach((key) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Until this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you really want separately maintained lists for create and update, at least bring these out as constants at the head of the file. The current position where they are buried in the middle of the file means that they are more likely to get out of sync. Difficult to decide if this is a deliberate decision or someone missed an entry in a list. It also means a new array is created and destroyed for every update. I'm happy to re-alter the functionality here, but I'd like a clear and definite plan. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (body[key] !== undefined) { | ||
group[key] = body[key]; | ||
} | ||
} | ||
|
||
group.isNew = false; | ||
group.save(saveGroupHandler(group, callback)); | ||
}); | ||
/* eslint-disable-next-line new-cap */ | ||
const groupObj = new Group.model(group); | ||
groupObj.isNew = false; | ||
groupObj.save(saveGroupHandler(groupObj, callback)); | ||
} | ||
}); | ||
} | ||
|
@@ -303,7 +284,6 @@ function remove(id, callback) { | |
callback(new errors.InternalDbError(error)); | ||
} else { | ||
logger.debug(context, 'Device [%s] successfully removed.', id); | ||
|
||
callback(null, deviceGroup); | ||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR should include an entry in CHANGES_NEXT_RELEASE file describing the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 39c7874