diff --git a/config/config.default.js b/config/config.default.js index 26ea6e2..eb04de1 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -12,6 +12,8 @@ exports.mongoose = { options: {}, plugins: [], loadModel: true, + modelDir: 'model', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` + delegate: 'model', // load all files in `app/${baseDir}` as models, default to `model` app: true, agent: false, }; diff --git a/index.d.ts b/index.d.ts index e069a6f..455f886 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,7 +13,10 @@ declare module 'egg' { type MongooseConfig = { url: string, - options?: mongoose.ConnectionOptions + options?: mongoose.ConnectionOptions, + loadModel?: boolean, + delegate?: string, + modelDir?: string }; // extend app @@ -36,7 +39,10 @@ declare module 'egg' { client?: MongooseConfig, clients?: { [key: string]: MongooseConfig - } + }, + loadModel?: boolean, + delegate?: string, + modelDir?: string }; } diff --git a/lib/mongoose.js b/lib/mongoose.js index d47d220..37fc7ef 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -11,7 +11,7 @@ let count = 0; const globalPlugins = []; module.exports = app => { - const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins } = app.config.mongoose; + const { client, clients, url, options, defaultDB, customPromise, loadModel, modelDir, delegate, plugins } = app.config.mongoose; // compatibility if (!client && !clients && url) { @@ -46,11 +46,11 @@ module.exports = app => { /* deprecated, next primary version remove */ app.__mongoose = mongoose; - app.mongoose.loadModel = () => loadModelToApp(app); + app.mongoose.loadModel = () => loadModelToApp(app, modelDir, delegate); if (loadModel) { app.beforeStart(() => { - loadModelToApp(app); + loadModelToApp(app, modelDir, delegate); }); } }; @@ -119,9 +119,10 @@ function createOneClient(config, app) { return db; } -function loadModelToApp(app) { - const dir = path.join(app.config.baseDir, 'app/model'); - app.loader.loadToApp(dir, 'model', { +function loadModelToApp(app, modelDir, delegate) { + const dir = path.join(app.config.baseDir, `app/${modelDir ? modelDir : 'model'}`); + app.coreLogger.info(`[egg-mongoose] model directory: ${dir}`); + app.loader.loadToApp(dir, delegate ? delegate : 'model', { inject: app, caseStyle: 'upper', filter(model) { diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app.js b/test/fixtures/apps/mongoose-loadModel-delegate/app.js new file mode 100644 index 0000000..8d0c1bb --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = app => { + app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose); + app.mongoose.loadModel(); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/book.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/book.js new file mode 100644 index 0000000..a2b5786 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/book.js @@ -0,0 +1,21 @@ +'use strict'; + +module.exports = app => { + class BookController extends app.Controller { + * index() { + const books = yield this.ctx.model.Book.find({}); + this.ctx.body = books; + } + + * create() { + const book = new this.ctx.model.Book({ + name: this.ctx.request.body.name, + }); + yield book.save(); + this.ctx.body = book; + } + } + + return BookController; + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/user.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/user.js new file mode 100644 index 0000000..331dd42 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/controller/user.js @@ -0,0 +1,21 @@ +'use strict'; + +module.exports = app => { + class UserController extends app.Controller { + * index() { + const users = yield this.ctx.model.User.find({}); + this.ctx.body = users; + } + + * create() { + const user = new this.ctx.model.User({ + name: this.ctx.request.body.name, + }); + yield user.save(); + this.ctx.body = user; + } + } + + return UserController; + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/model/Book.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/Book.js new file mode 100644 index 0000000..6beaf78 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/Book.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = app => { + const mymongoose = app.mymongoose; + const mongoose = app.mongoose; + const BoookSchema = new mongoose.Schema({ + name: { type: String }, + }); + + return mymongoose.model('Book', BoookSchema, null, { cache: false }); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/model/other.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/other.js new file mode 100644 index 0000000..057414a --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/other.js @@ -0,0 +1,5 @@ +'use strict'; + +module.export = { + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/model/user.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/user.js new file mode 100644 index 0000000..43ce97b --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/model/user.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = app => { + const mymongoose = app.mymongoose; + const mongoose = app.mongoose; + const UserSchema = new mongoose.Schema({ + name: { type: String }, + }); + + return mymongoose.model('User', UserSchema, null, { cache: false }); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/app/router.js b/test/fixtures/apps/mongoose-loadModel-delegate/app/router.js new file mode 100644 index 0000000..828ade7 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/app/router.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = function(app) { + app.resources('users', '/users', 'user'); + app.resources('books', '/books', 'book'); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/config/config.default.js b/test/fixtures/apps/mongoose-loadModel-delegate/config/config.default.js new file mode 100644 index 0000000..a4af615 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/config/config.default.js @@ -0,0 +1,13 @@ +'use strict'; + +exports.mongoose = { + loadModel: false, + delegate: 'mongo', +}; + +exports.mymongoose = { + url: process.env.MONGODB_URL, + options: {}, +}; + +exports.keys = 'aaa'; diff --git a/test/fixtures/apps/mongoose-loadModel-delegate/package.json b/test/fixtures/apps/mongoose-loadModel-delegate/package.json new file mode 100644 index 0000000..dec528e --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-delegate/package.json @@ -0,0 +1,4 @@ +{ + "name": "mongoose-test", + "version": "0.0.1" +} \ No newline at end of file diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app.js new file mode 100644 index 0000000..8d0c1bb --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = app => { + app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose); + app.mongoose.loadModel(); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/book.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/book.js new file mode 100644 index 0000000..a2b5786 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/book.js @@ -0,0 +1,21 @@ +'use strict'; + +module.exports = app => { + class BookController extends app.Controller { + * index() { + const books = yield this.ctx.model.Book.find({}); + this.ctx.body = books; + } + + * create() { + const book = new this.ctx.model.Book({ + name: this.ctx.request.body.name, + }); + yield book.save(); + this.ctx.body = book; + } + } + + return BookController; + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/user.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/user.js new file mode 100644 index 0000000..331dd42 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/controller/user.js @@ -0,0 +1,21 @@ +'use strict'; + +module.exports = app => { + class UserController extends app.Controller { + * index() { + const users = yield this.ctx.model.User.find({}); + this.ctx.body = users; + } + + * create() { + const user = new this.ctx.model.User({ + name: this.ctx.request.body.name, + }); + yield user.save(); + this.ctx.body = user; + } + } + + return UserController; + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/Book.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/Book.js new file mode 100644 index 0000000..6beaf78 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/Book.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = app => { + const mymongoose = app.mymongoose; + const mongoose = app.mongoose; + const BoookSchema = new mongoose.Schema({ + name: { type: String }, + }); + + return mymongoose.model('Book', BoookSchema, null, { cache: false }); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/other.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/other.js new file mode 100644 index 0000000..057414a --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/other.js @@ -0,0 +1,5 @@ +'use strict'; + +module.export = { + +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/user.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/user.js new file mode 100644 index 0000000..43ce97b --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/user.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = app => { + const mymongoose = app.mymongoose; + const mongoose = app.mongoose; + const UserSchema = new mongoose.Schema({ + name: { type: String }, + }); + + return mymongoose.model('User', UserSchema, null, { cache: false }); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/app/router.js b/test/fixtures/apps/mongoose-loadModel-modelDir/app/router.js new file mode 100644 index 0000000..828ade7 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/app/router.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = function(app) { + app.resources('users', '/users', 'user'); + app.resources('books', '/books', 'book'); +}; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/config/config.default.js b/test/fixtures/apps/mongoose-loadModel-modelDir/config/config.default.js new file mode 100644 index 0000000..42af7f1 --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/config/config.default.js @@ -0,0 +1,13 @@ +'use strict'; + +exports.mongoose = { + loadModel: false, + modelDir: 'mongo', +}; + +exports.mymongoose = { + url: process.env.MONGODB_URL, + options: {}, +}; + +exports.keys = 'aaa'; diff --git a/test/fixtures/apps/mongoose-loadModel-modelDir/package.json b/test/fixtures/apps/mongoose-loadModel-modelDir/package.json new file mode 100644 index 0000000..dec528e --- /dev/null +++ b/test/fixtures/apps/mongoose-loadModel-modelDir/package.json @@ -0,0 +1,4 @@ +{ + "name": "mongoose-test", + "version": "0.0.1" +} \ No newline at end of file diff --git a/test/mongoose.test.js b/test/mongoose.test.js index 40ac53d..50ed61e 100644 --- a/test/mongoose.test.js +++ b/test/mongoose.test.js @@ -212,6 +212,70 @@ describe('test/mongoose.test.js', () => { }); }); + describe('custom loadModel and set delegate', () => { + let app; + before(function* () { + app = mm.app({ + baseDir: 'apps/mongoose-loadModel-delegate', + }); + yield app.ready(); + }); + + after(function* () { + yield app.close(); + }); + afterEach(mm.restore); + afterEach(function* () { + yield app.mongo.Book.remove({}); + yield app.mongo.User.remove({}); + }); + + it('should has app custom mymongoose', function* () { + assert(app.mymongoose); + }); + + it('should has app mongo property', function* () { + assert(app.mongo); + assert(app.mongo.User.prototype instanceof app.mongoose.Model); + assert(app.mongo.user === undefined); + assert(app.mongo.Book.prototype instanceof app.mongoose.Model); + assert(app.mongo.book === undefined); + assert(app.mongo.Other === undefined); + }); + }); + + describe('custom loadModel and set model directory', () => { + let app; + before(function* () { + app = mm.app({ + baseDir: 'apps/mongoose-loadModel-modelDir', + }); + 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 app custom mymongoose', function* () { + assert(app.mymongoose); + }); + + it('should has app model property', function* () { + assert(app.model); + assert(app.model.User.prototype instanceof app.mongoose.Model); + assert(app.model.user === undefined); + assert(app.model.Book.prototype instanceof app.mongoose.Model); + assert(app.model.book === undefined); + assert(app.model.Other === undefined); + }); + }); + describe('throws on first connection', () => { let app; const createConnection = mongoose.createConnection;