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

feat: support modelDir option #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +39,10 @@ declare module 'egg' {
client?: MongooseConfig,
clients?: {
[key: string]: MongooseConfig
}
},
loadModel?: boolean,
delegate?: string,
modelDir?: string
};
}

Expand Down
13 changes: 7 additions & 6 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
};
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-delegate/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose);
app.mongoose.loadModel();
};
Original file line number Diff line number Diff line change
@@ -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;

};
Original file line number Diff line number Diff line change
@@ -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;

};
11 changes: 11 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-delegate/app/model/Book.js
Original file line number Diff line number Diff line change
@@ -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 });
};
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-loadModel-delegate/app/model/user.js
Original file line number Diff line number Diff line change
@@ -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 });
};
6 changes: 6 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-delegate/app/router.js
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,13 @@
'use strict';

exports.mongoose = {
loadModel: false,
delegate: 'mongo',
};

exports.mymongoose = {
url: process.env.MONGODB_URL,
options: {},
};

exports.keys = 'aaa';
4 changes: 4 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-delegate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mongoose-test",
"version": "0.0.1"
}
6 changes: 6 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-modelDir/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose);
app.mongoose.loadModel();
};
Original file line number Diff line number Diff line change
@@ -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;

};
Original file line number Diff line number Diff line change
@@ -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;

};
11 changes: 11 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-modelDir/app/mongo/Book.js
Original file line number Diff line number Diff line change
@@ -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 });
};
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-loadModel-modelDir/app/mongo/user.js
Original file line number Diff line number Diff line change
@@ -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 });
};
6 changes: 6 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-modelDir/app/router.js
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,13 @@
'use strict';

exports.mongoose = {
loadModel: false,
modelDir: 'mongo',
};

exports.mymongoose = {
url: process.env.MONGODB_URL,
options: {},
};

exports.keys = 'aaa';
4 changes: 4 additions & 0 deletions test/fixtures/apps/mongoose-loadModel-modelDir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mongoose-test",
"version": "0.0.1"
}
64 changes: 64 additions & 0 deletions test/mongoose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down