Skip to content

Commit

Permalink
feat: merge the new feature from the original one (#9)
Browse files Browse the repository at this point in the history
* docs: make intro clear

* docs: update readme

* format: the ci process

* build: grammar error

* docs: make the intro more clear

* build: publish when merge cur-publish

* feat: add new version no

* build: make ci better
  • Loading branch information
oneWalker authored Nov 6, 2024
1 parent 9e3baf2 commit a74e0c0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/autoUnitTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Run tests

on:
push:
branches: [ master ]
branches: [ cur-publish, master ]
pull_request:
branches: [ master ]
branches: [ master, cur-publish ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Release
on:
push:
branches: [ master ]
branches: [ cur-publish ]

jobs:
release:
Expand Down
77 changes: 40 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# egg-mongoose

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Run tests](https://github.com/eggjs/egg-mongoose/actions/workflows/autoUnitTest.yml/badge.svg)](https://github.com/oneWalker/egg-mongoose/actions/workflows/autoUnitTest.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/@oneWalker/egg-mongoose.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@oneWalker/egg-mongoose
[travis-image]: https://img.shields.io/travis/eggjs/egg-mongoose.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-mongoose
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-mongoose.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-mongoose?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-mongoose.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-mongoose
[npm-image]: https://img.shields.io/npm/v/@onewalker/egg-mongoose.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/@onewalker/egg-mongoose
[codecov-image]: https://img.shields.io/codecov/c/github/oneWalker/egg-mongoose.svg?style=flat-square
[codecov-url]: https://app.codecov.io/github/oneWalker/egg-mongoose?branch=cur-publish-dev
[snyk-image]: https://snyk.io/test/npm/egg-mongoose/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-mongoose
[download-image]: https://img.shields.io/npm/dm/@oneWalker/egg-mongoose.svg?style=flat-square
[download-url]: https://npmjs.org/package/@oneWalker/egg-mongoose
[download-image]: https://img.shields.io/npm/dm/@onewalker/egg-mongoose.svg?style=flat-square
[download-url]: https://www.npmjs.com/package/@onewalker/egg-mongoose


Egg's mongoose plugin.

## Notice
The plugin is a ahead version of egg-mongoose with some features not supported in the official one.
It's keep up to date with the latest version of official [egg-mongoose](https://github.com/eggjs/egg-mongoose).
The new features are not supported in the current official one. Pull Requests link: https://xgithub.com/eggjs/egg-mongoose/pull/60
- place the model files in a custom location
- rename the delegate property to `Context`.

The version of Egg's mongoose plugin add two new features, place the model files in the location you want and rename the delegate property to `Context`. It published for the original one seems like not to be maintained by the maintainers. When the original one merge the reuqest, you can also use the original one.

## Install

Expand All @@ -33,16 +34,16 @@ $ npm i @onewalker/egg-mongoose --save

## Configuration

Change `{app_root}/config/plugin.js` to enable `egg-mongoose` plugin:
Enable the `egg-mongoose` plugin by modifying `{app_root}/config/plugin.js`:

```js
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
package: '@onewalker/egg-mongoose',
};
```

## Simple connection
## Simple Connection

### Config

Expand All @@ -54,10 +55,11 @@ exports.mongoose = {
// mongoose global plugins, expected a function or an array of function and options
plugins: [createdPlugin, [updatedPlugin, pluginOptions]],
};
// recommended

// Recommended
exports.mongoose = {
//baseDir:'model', //models in `app/${model}`
//delegate:'model' //lood to `app[delegate]`
// baseDir: 'model', // models in `app/${model}`, // define the dir of model
// delegate: 'model' // load to `app[delegate]` // define the delegate
client: {
url: 'mongodb://127.0.0.1/example',
options: {},
Expand All @@ -76,20 +78,20 @@ module.exports = app => {
const Schema = mongoose.Schema;

const UserSchema = new Schema({
userName: { type: String },
password: { type: String },
userName: { type: String },
password: { type: String },
});

return mongoose.model('User', UserSchema);
}
};

// {app_root}/app/controller/user.js
exports.index = function* (ctx) {
ctx.body = yield ctx.model.User.find({});
}
};
```

## Multiple connections
## Multiple Connections

### Config

Expand All @@ -102,36 +104,36 @@ exports.mongoose = {
url: 'mongodb://127.0.0.1/example1',
options: {},
// client scope plugin array
plugins: []
plugins: [],
},
db2: {
url: 'mongodb://127.0.0.1/example2',
options: {},
},
},
// public scope plugin array
plugins: []
plugins: [],
};
```

### Example

```js
// {app_root}/app/model/user.js
// {app_root}/app/{baseDir}/user.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const conn = app.mongooseDB.get('db1');
const conn = app.mongooseDB.get('db1');

const UserSchema = new Schema({
userName: { type: String },
password: { type: String },
});

return conn.model('User', UserSchema);
}
};

// {app_root}/app/model/book.js
// {app_root}/app/{baseDir}/book.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
Expand All @@ -142,24 +144,24 @@ module.exports = app => {
});

return conn.model('Book', BookSchema);
}
};

// app/controller/user.js
exports.index = function* (ctx) {
ctx.body = yield ctx.model.User.find({}); // get data from db1
}
};

// app/controller/book.js
exports.index = function* (ctx) {
ctx.body = yield ctx.model.Book.find({}); // get data from db2
}
};
```

### Default config
### Default Config

see [config/config.default.js](config/config.default.js) for more detail.
See [config/config.default.js](config/config.default.js) for more details.

## Multi-mongos support
## Multi-Mongos Support

```js
// {app_root}/config/config.default.js
Expand All @@ -183,4 +185,5 @@ If you are a contributor, follow [CONTRIBUTING](https://eggjs.org/zh-cn/contribu

## License

[MIT](LICENSE)
[MIT](LICENSE)

4 changes: 2 additions & 2 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ exports.mongoose = {
loadModel: true,
app: true,
agent: false,
baseDir: 'model', // models in `app/${model}`,the deafault is `app/model`
delegate: 'model', // lood to `app[delegate]`,the deafault is app.model
baseDir: 'model', // models in `app/${model}`,the default is `app/model`
delegate: 'model', // load to `app[delegate]`,the default is app.model
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onewalker/egg-mongoose",
"version": "4.1.0",
"version": "4.1.3",
"description": "egg mongoose plugin advanced by @oneWalker",
"eggPlugin": {
"name": "mongoose"
Expand Down

0 comments on commit a74e0c0

Please sign in to comment.