Skip to content

Commit

Permalink
feat: support plugins for special clients (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangedenCZD authored Aug 13, 2020
1 parent 8ef2ebc commit 67f8f1f
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ after_script:
- npminstall codecov && codecov
services:
- mongodb
cache:
npm: false
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ exports.mongoose = {
db1: {
url: 'mongodb://127.0.0.1/example1',
options: {},
// client scope plugin array
plugins: []
},
db2: {
url: 'mongodb://127.0.0.1/example2',
options: {},
},
},
// public scope plugin array
plugins: []
};
```

Expand Down
15 changes: 14 additions & 1 deletion lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const filterURLPassword = require('./filterURLPassword');

let count = 0;

const globalPlugins = [];

module.exports = app => {
const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins } = app.config.mongoose;

Expand All @@ -26,6 +28,7 @@ module.exports = app => {
mongoose.plugin.apply(mongoose, Array.isArray(plugin) ? plugin : [ plugin ]);
});
}
globalPlugins.push(...mongoose.plugins || []);

// TODO addSingleton support config[this.configName]?
app.addSingleton('mongoose', createOneClient);
Expand Down Expand Up @@ -53,7 +56,7 @@ module.exports = app => {
};

function createOneClient(config, app) {
const { url, options } = config;
const { url, options, plugins } = config;
const filteredURL = filterURLPassword(url);

assert(url, '[egg-mongoose] url is required on config');
Expand All @@ -66,6 +69,16 @@ function createOneClient(config, app) {
}
app.coreLogger.info('[egg-mongoose] connecting %s', filteredURL);

// remove all plugins
const length = Array.isArray(mongoose.plugins) ? mongoose.plugins.length : 0;
for (let index = length; index > 0; index--) {
mongoose.plugins.pop();
}
// combine clients plugins and public plugins
[].concat(plugins || [], globalPlugins).forEach(plugin => {
mongoose.plugin.apply(mongoose, Array.isArray(plugin) ? plugin : [ plugin ]);
});

const db = mongoose.createConnection(url, options);

/* istanbul ignore next */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = app => {
class BookController extends app.Controller {
* create() {
const book = new this.ctx.model.Book({
name: this.ctx.request.body.name,
user: this.ctx.request.body.user,
});
yield book.save();
this.ctx.body = book;
}

* show() {
const id = this.ctx.params.id;
const book = yield this.ctx.model.Book.findById(id)
.populate({
path: 'user',
model: this.ctx.model.User,
})
.exec();
this.ctx.body = book;
}
}

return BookController;

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

module.exports = app => {
class UserController extends app.Controller {
* create() {
const user = new this.ctx.model.User({
name: this.ctx.request.body.name,
});
yield user.save();
this.ctx.body = user;
}
}

return UserController;

};
12 changes: 12 additions & 0 deletions test/fixtures/apps/mongoose-multi-client-plugins/app/model/Book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = app => {
const db2 = app.mongooseDB.get('db2');
const { Schema } = app.mongoose;
const BoookSchema = new Schema({
name: { type: String },
user: { type: Schema.Types.ObjectId, ref: 'User' },
});

return db2.model('Book', BoookSchema);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.export = {

};
11 changes: 11 additions & 0 deletions test/fixtures/apps/mongoose-multi-client-plugins/app/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
const db1 = app.mongooseDB.get('db2');
const { Schema } = app.mongoose;
const UserSchema = new Schema({
name: { type: String },
});

return db1.model('User', UserSchema);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(app) {
app.resources('users', '/users', 'user');
app.resources('books', '/books', 'book');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const { lastModifiedPlugin } = require('../lib/mongoose');

exports.mongoose = {
clients: {
db1: {
url: process.env.MONGODB_URL_1,
options: {},
},
db2: {
url: process.env.MONGODB_URL,
options: {},
plugins: [[ lastModifiedPlugin, { field: 'updatedAt' }]],
},
},
plugins: [ lastModifiedPlugin ],
};

exports.keys = 'aaa';
15 changes: 15 additions & 0 deletions test/fixtures/apps/mongoose-multi-client-plugins/lib/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
lastModifiedPlugin,
};

function lastModifiedPlugin(schema, options = {}) {
const { field = 'lastMod' } = options;
schema.add({ [field]: Date });

schema.pre('save', function(next) {
this.$set(field, new Date());
next();
});
}
4 changes: 4 additions & 0 deletions test/fixtures/apps/mongoose-multi-client-plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mongoose-test",
"version": "0.0.1"
}
31 changes: 31 additions & 0 deletions test/mongoose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,35 @@ describe('test/mongoose.test.js', () => {
assert(book.updatedAt instanceof Date);
});
});


describe('multi client plugins', () => {
let app;
before(function* () {
app = mm.app({
baseDir: 'apps/mongoose-multi-client-plugins',
});
yield app.ready();
});

after(function* () {
yield app.close();
});
afterEach(mm.restore);
afterEach(function* () {
yield app.model.Book.remove({});
yield app.model.User.remove({});
});

it('should has model extra property', function* () {
const user = yield app.model.User.create({});
const book = yield app.model.Book.create({});
assert(user);
assert(user.lastMod instanceof Date);
assert(user.updatedAt instanceof Date);
assert(book);
assert(book.lastMod instanceof Date);
assert(book.updatedAt instanceof Date);
});
});
});

0 comments on commit 67f8f1f

Please sign in to comment.