Skip to content

Commit

Permalink
fix mongo enviriment parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanoff committed Apr 6, 2017
1 parent ccdac96 commit e7df25c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
9 changes: 5 additions & 4 deletions controllers/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ exports = module.exports = function (name, model) {

delete: function (req, res, next) {
var rel = this.getRelationsData(req);
if(!rel) {
if(!rel || !rel.data) {
var search = req.params._id? { _id: req.params._id } : {};
model.delete(search, function (err, doc) {
if (err) return req._error.show(err);
Expand All @@ -196,6 +196,7 @@ exports = module.exports = function (name, model) {
res.json({ ok: 1, deleted: _.map(doc.ops, '_id'), updated: [] });
});
} else {
console.log(rel);
var m = req.models[rel.table2];
var search = {};
search[rel.field2] = { $in: [rel.data] };
Expand All @@ -207,12 +208,12 @@ exports = module.exports = function (name, model) {
if (index > -1) {
d[rel.field2].splice(index, 1);
}
if(!d[rel.field2]) {
m.delete({_id: d._id}, function(){})
if(!d[rel.field2][0]) {
deleted.push(d._id);
m.delete({_id: d._id}, function(err,ddd){console.log(err,ddd)})
} else {
m.update(d._id, d, function(){})
updated.push(d._id);
m.update(d._id, {$set:{writers:d[rel.field2]}}, function(err,ddd){console.log(err,ddd)})
}
});
res.json({ ok: 1, deleted: deleted, updated: updated });
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ function start() {
: optDb.login ? optDb.login + ':' + optDb.password + '@'
: '';
var dbUrl = process.env.DB_URL || optDb.url || 'localhost';
dbUrl += ':' + (optDb.port || 27017);
dbUrl += '/' + (optDb.name || 'default');
if(optDb.port) dbUrl += ':' + optDb.port;
if(optDb.name) dbUrl += '/' + optDb.name;
dbUrl = 'mongodb://' + dbAuth + dbUrl;
console.log(dbUrl);

app._start(HOST, PORT, dbUrl);
}
9 changes: 8 additions & 1 deletion models/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ module.exports = function (name, model, db) {
db.collection(name).find(search, fields).sort(sort).skip(skip).limit(limit).toArray(next);
});
} else {
db.collection(name).find(search, fields).sort(sort).skip(skip).limit(limit).toArray(next);
var res = db.collection(name).find(search, fields).sort(sort);
if(skip) res = res.skip(skip);
if(limit) res = res.limit(limit);
res.toArray(next);
}
},

Expand All @@ -34,6 +37,10 @@ module.exports = function (name, model, db) {
},

update: function (id, data, res) {
console.log('!!!!!!!!!!!!!!!!!!');
console.log(name);
console.log(id);
console.log(data);
db.collection(name).update({ _id: id }, data, res);
},

Expand Down
11 changes: 10 additions & 1 deletion test/07.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,16 @@ describe('App', function () {
done();
});
});

/*
it('delete data', function (done) {
chai.request(app)
.delete('/categories/' + id)
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
*/
it('delete data', function (done) {
chai.request(app)
.delete('/categories/' + id)
Expand Down

0 comments on commit e7df25c

Please sign in to comment.