diff --git a/Readme.md b/Readme.md index 55c2bef..d4de775 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,79 @@ -# egg ORM plugin +# egg-orm + +[中文介绍](Readme.zh-CN.md) + +Yet another object-relational mapping plugin for Egg, which is based on [Leoric](https://leoric.js.org). + +## Install + +```bash +$ npm i --save egg-orm +$ npm install --save mysql2 # MySQL or compatible dialects + +# Or use other database backend. +$ npm install --save pg # PostgreSQL +$ npm install --save sqlite3 # SQLite +``` + +## Usage + +With egg-orm you can define models in `app/model`: + +```js +// app/model/user.js +module.exports = function(app) { + const { STRING } = app.model.DataTypes; + + return app.model.define('User', { + name: STRING, + password: STRING, + avatar: STRING(2048), + }, { + tableName: 'users', + }); +} +``` + +and use them like below: + +```js +// app/controller/home.js +const { Controller } = require('egg'); +module.exports = class HomeController extends Controller { + async index() { + const users = await ctx.model.User.find({ + corpId: ctx.model.Corp.findOne({ name: 'tyrael' }), + }); + ctx.body = users; + } +}; +``` + +## Configuration + +Firstly, enable egg-orm plugin: + +```js +// config/plugin.js +exports.orm = { + enable: true, + package: 'egg-orm', +}; +``` + +Secondly, configure the plugin accordingly: + +```js +// config/config.default.js +exports.orm = { + client: 'mysql', + database: 'temp', + host: 'localhost', + baseDir: 'app/model', +}; +``` + +In this example above, we're accessing the `temp` database of MySQL via `localhost` with the models defined in directory `app/model`. For more information, please refer to [Setup Leoric in Egg](https://leoric.js.org/setup/egg). ## License diff --git a/Readme.zh-CN.md b/Readme.zh-CN.md new file mode 100644 index 0000000..c45a88e --- /dev/null +++ b/Readme.zh-CN.md @@ -0,0 +1,78 @@ +# egg-orm + +egg-orm 是一个适用于 Egg 框架的数据模型层插件,egg-orm 的对象关系映射能力来自 [Leoric](https://leoric.js.org)。 + +## 安装 + +```bash +$ npm i --save egg-orm +$ npm install --save mysql2 # MySQL 或者其他兼容 MySQL 的数据库 + +# 其他数据库类型 +$ npm install --save pg # PostgreSQL +$ npm install --save sqlite3 # SQLite +``` + +## 使用 + +开启 egg-orm 插件即可在 `app/model` 中定义数据模型: + +```js +// app/model/user.js +module.exports = function(app) { + const { STRING } = app.model.DataTypes; + + return app.model.define('User', { + name: STRING, + password: STRING, + avatar: STRING(2048), + }, { + tableName: 'users', + }); +} +``` + +在 Controller 调用: + +```js +// app/controller/home.js +const { Controller } = require('egg'); +module.exports = class HomeController extends Controller { + async index() { + const users = await ctx.model.User.find({ + corpId: ctx.model.Corp.findOne({ name: 'tyrael' }), + }); + ctx.body = users; + } +}; +``` + +## 配置 + +首先开启(并安装) egg-orm 插件 + +```js +// config/plugin.js +exports.orm = { + enable: true, + package: 'egg-orm', +}; +``` + +然后按需配置数据库: + +```js +// config/config.default.js +exports.orm = { + client: 'mysql', + database: 'temp', + host: 'localhost', + baseDir: 'app/model', +}; +``` + +在上面这个例子中,我们将数据模型定义文件放在 `app/model` 目录,通过 `localhost` 访问 MySQL 中的 `temp` 数据库。推荐阅读 [在 Egg 中使用 Leoric](https://leoric.js.org/setup/egg)一文了解更多有关在 Egg 中使用 egg-orm 和 Leoric 的信息。 + +## 授权许可 + +[MIT](LICENSE)