-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support plugins for special clients (#41)
- Loading branch information
1 parent
8ef2ebc
commit 67f8f1f
Showing
13 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ after_script: | |
- npminstall codecov && codecov | ||
services: | ||
- mongodb | ||
cache: | ||
npm: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/fixtures/apps/mongoose-multi-client-plugins/app/controller/book.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
16 changes: 16 additions & 0 deletions
16
test/fixtures/apps/mongoose-multi-client-plugins/app/controller/user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
test/fixtures/apps/mongoose-multi-client-plugins/app/model/Book.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/fixtures/apps/mongoose-multi-client-plugins/app/model/other.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.export = { | ||
|
||
}; |
11 changes: 11 additions & 0 deletions
11
test/fixtures/apps/mongoose-multi-client-plugins/app/model/user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
6 changes: 6 additions & 0 deletions
6
test/fixtures/apps/mongoose-multi-client-plugins/app/router.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
20 changes: 20 additions & 0 deletions
20
test/fixtures/apps/mongoose-multi-client-plugins/config/config.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/fixtures/apps/mongoose-multi-client-plugins/lib/mongoose.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
test/fixtures/apps/mongoose-multi-client-plugins/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "mongoose-test", | ||
"version": "0.0.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters