From 37f3491b173854b374ecccf3ca856465b7c03e85 Mon Sep 17 00:00:00 2001 From: welefen Date: Mon, 10 Aug 2015 17:33:04 +0800 Subject: [PATCH] update mongodb --- src/adapter/db/mongo.js | 9 +++++++++ src/adapter/socket/mongo.js | 2 +- src/model/mongo.js | 14 ++++++++++++-- test/adapter/socket/mongo.js | 2 +- test/controller/base.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/adapter/db/mongo.js b/src/adapter/db/mongo.js index 450138e6..68420b1c 100644 --- a/src/adapter/db/mongo.js +++ b/src/adapter/db/mongo.js @@ -71,6 +71,15 @@ export default class { this.lastInsertId = result.insertedIds; return result; } + /** + * select data + * @param {Object} options [] + * @return {Promise} [] + */ + async select(options){ + let collection = await this.collection(options.table); + return collection.find({name: "welefen"}, {name: false}).limit(2).toArray(); + } /** * close socket * @return {} [] diff --git a/src/adapter/socket/mongo.js b/src/adapter/socket/mongo.js index 9cbad92d..72ede465 100644 --- a/src/adapter/socket/mongo.js +++ b/src/adapter/socket/mongo.js @@ -60,7 +60,7 @@ export default class extends think.adapter.socket { this.connection = connection; return connection; }) - let err = new Error(`mongodb://${auth}${config.host}:${config.port}`); + let err = new Error(str); return think.error(promise, err); }); } diff --git a/src/model/mongo.js b/src/model/mongo.js index 23ba6760..53eb19c8 100644 --- a/src/model/mongo.js +++ b/src/model/mongo.js @@ -120,7 +120,17 @@ export default class extends Base { * select data * @return {Promise} [] */ - select(options){ - + async select(options){ + options = await this.parseOptions(options); + let data = await this.db().select(options); + return this._afterSelect(data, options); + } + /** + * select one row data + * @param {Object} options [] + * @return {Promise} [] + */ + find(options){ + } } \ No newline at end of file diff --git a/test/adapter/socket/mongo.js b/test/adapter/socket/mongo.js index d0827f27..21b1d1da 100644 --- a/test/adapter/socket/mongo.js +++ b/test/adapter/socket/mongo.js @@ -187,7 +187,7 @@ describe('adapter/socket/mongo', function(){ } }); instance.getConnection().catch(function(err){ - assert.equal(err.message, 'Address not available, mongodb://welefen:suredy@127.0.0.1:27017. http://www.thinkjs.org/doc/error.html#EADDRNOTAVAIL') + assert.equal(err.message, 'Address not available, mongodb://welefen:suredy@127.0.0.1:27017/test?slaveOk=true. http://www.thinkjs.org/doc/error.html#EADDRNOTAVAIL') think.npm = npm; think.reject = reject; done(); diff --git a/test/controller/base.js b/test/controller/base.js index b176ab6a..f1cb3634 100644 --- a/test/controller/base.js +++ b/test/controller/base.js @@ -196,84 +196,110 @@ describe('controller/base.js', function(){ }) }) it('send time', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.sendTime(); + muk.restore(); done(); }) }) it('error', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.error(); + muk.restore(); done(); }) }) it('fail', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.fail(); + muk.restore(); done(); }) }) it('success', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.success(); + muk.restore(); done(); }) }) it('type', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.type(); + muk.restore(); done(); }) }) it('end', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.end(); + muk.restore(); done(); }) }) it('echo', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.echo(); + muk.restore(); done(); }) }) it('deny', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.deny(); + muk.restore(); done(); }) }) it('deny 404', function(done){ + muk(think, 'log', function(){}) getInstance({ status: function(status){ assert.equal(status, 404); } }).then(function(instance){ var data = instance.deny(404); + muk.restore(); done(); }) }) it('status', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.status(); + muk.restore(); done(); }) }) it('json', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.json(); + muk.restore(); done(); }) }) it('jsonp', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.jsonp(); + muk.restore(); done(); }) }) it('redirect', function(done){ + muk(think, 'log', function(){}) getInstance().then(function(instance){ var data = instance.redirect(); + muk.restore(); done(); }) }) @@ -340,11 +366,13 @@ describe('controller/base.js', function(){ }) }) it('display', function(done){ + muk(think, 'log', function(){}) getInstance({ }).then(function(instance){ return instance.display(__filename) }).catch(function(err){ assert.equal(think.isPrevent(err), true); + muk.restore(); done(); }) })