Skip to content
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

Consolidate document id field #253

Merged
merged 5 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class DefaultController extends EventEmitter {
super();
this._model = mongoose.model(modelName);
this.modelName = modelName;
this.docId = '_id';
this.logger = logger;
}

Expand All @@ -28,18 +27,17 @@ class DefaultController extends EventEmitter {
}

_getModelParamQuery(req) {
return {[this.docId]: req.params[this.modelName]};
return {_id: req.params[this.modelName]};
}
modelParam(req, res, next) {
// Find from db
const docId = this.docId;
logger.silly(`Register model middleware for ${this.modelName}`);
const find = this._getModelParamQuery(req);
logger.debug(`find document by ${JSON.stringify(find)} (model: ${this.modelName})`);
return this.Model.findOne(find)
.then((data) => {
if (!data) {
const error = new Error(`Document with id ${docId} not found`);
const error = new Error(`Document not found (${find})`);
error.statusCode = 404;
throw error;
}
Expand Down Expand Up @@ -111,7 +109,7 @@ class DefaultController extends EventEmitter {
}

update(req, res) {
const update = _.omit(req.body, [this.docId, '__v']);
const update = _.omit(req.body, ['_id', '__v']);
// increment version number every time when updating document
update.$inc = {__v: 1};
logger.debug(`updated: ${JSON.stringify(update)}`);
Expand Down Expand Up @@ -158,7 +156,7 @@ class DefaultController extends EventEmitter {
if (req[this.modelName]) {
const info = {
collection: this.modelName,
_id: _.get(req[this.modelName], this.docId)
_id: _.get(req[this.modelName], '_id')
};
req[this.modelName].remove((error) => {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ResourcesController extends DefaultController {
const find = {$or: []};
const _id = req.params[this.modelName];
if (DefaultController.isObjectId(_id)) {
find.$or.push({[this.docId]: _id});
find.$or.push({_id});
}
find.$or.push({'hw.sn': _id});
return find;
Expand Down
14 changes: 2 additions & 12 deletions app/models/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,13 @@ const ResourceSchema = new Schema({
// Parent Resource
parent: {type: ObjectId, ref: 'Resource'}
});

ResourceSchema.set('toJSON', {
virtuals: true,
getters: true,
minimize: true,
transform: (doc, ret) => {
const jsonResource = ret;

if (!jsonResource.id) {
jsonResource.id = ret._id;
}
delete jsonResource._id;

const ip = jsonResource.ip;
if (ip && ip.remote_connection && ip.remote_connection.authentication) {
delete ip.remote_connection.authentication;
}

_.unset(ret, 'ip.remote_connection.authentication');
return ret;
}
});
Expand Down
14 changes: 1 addition & 13 deletions app/models/testcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,10 @@ const TestCaseSchema = new Schema({
}
});


TestCaseSchema.set('toJSON', {
virtuals: true,
getters: true,
minimize: true,
transform(doc, ret) {
const jsonResource = ret;

if (!jsonResource.id) {
jsonResource.id = ret._id;
}

delete jsonResource._id;
delete jsonResource.__v;
return jsonResource;
}
minimize: true
});

TestCaseSchema.index({tcid: 1, 'ver.cur': -1}, {unique: true});
Expand Down
2 changes: 1 addition & 1 deletion test/tests_api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Events', function () {
.set('authorization', authString)
.end()
.then((response) => {
resourceId = response.body.id;
resourceId = response.body._id;
resourceHwSn = response.body.hw.sn;
});
});
Expand Down
10 changes: 6 additions & 4 deletions test/tests_api/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ describe('Resource', function () {
expect(res).to.have.property('status', 200);
expect(res.body).to.have.property('name');
expect(res.body).to.have.property('type');
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('_id');
expect(res.body._id).to.be.an('string');
expect(res.body.name).to.equal('dev1');
expect(res.body.type).to.equal('dut');
resourceId = res.body.id;
resourceId = res.body._id;
});
});

Expand All @@ -71,7 +72,8 @@ describe('Resource', function () {
expect(res).to.have.property('status', 200);
expect(res.body).to.have.property('name');
expect(res.body).to.have.property('type');
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('_id');
expect(res.body._id).to.be.an('string');
expect(res.body.name).to.equal('dev1');
expect(res.body.type).to.equal('dut');
});
Expand Down Expand Up @@ -151,7 +153,7 @@ describe('Resource', function () {
return superagent.post(`${api}/resources`)
.send(body)
.end(function (error, res) {
resourceId = res.body.id;
resourceId = res.body._id;
});
});
after(function () {
Expand Down