Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eggjs/egg-orm
Object relational mapping for Egg framework
https://github.com/eggjs/egg-orm
Last synced: about 1 month ago
JSON representation
Object relational mapping for Egg framework
- Host: GitHub
- URL: https://github.com/eggjs/egg-orm
- Owner: eggjs
- License: mit
- Created: 2020-01-14T09:19:23.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-19T09:40:48.000Z (12 months ago)
- Last Synced: 2024-04-14T00:58:56.919Z (8 months ago)
- Language: JavaScript
- Homepage: https://leoric.js.org/setup/egg
- Size: 70.3 KB
- Stars: 49
- Watchers: 5
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-orm - egg-orm 是一个适用于 Egg 框架的数据模型层插件,egg-orm 的对象关系映射能力来自 Leoric。 ![](https://img.shields.io/github/stars/eggjs/egg-orm.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-orm.svg?style=flat-square) (仓库 / 插件)
README
# 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 mysql # 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` in JavaScript:
```js
// app/model/user.js
module.exports = function(app) {
const { Bone, DataTypes: { STRING } } = app.model;return class User extends Bone {
static table = 'users'static attributes = {
name: STRING,
password: STRING,
avatar: STRING(2048),
}
});
}
```or in TypeScript:
```ts
// app/model/post.ts
import { Column, Bone, BelongsTo, DataTypes } from 'egg-orm';
import User from './user';export default class Post extends Bone {
@Column({ primaryKey: true })
id: bigint;@Column(DataTypes.TEXT)
content: string;@Column()
description: string;@Column()
userId: bigint;@BelongsTo()
user: User;
}// app/model/user.ts
import { Column, Bone, HasMany } from 'egg-orm';
import Post from './post';export default class User extends Bone {
@Column({ allowNull: false })
nickname: string;@Column()
email: string;@Column()
createdAt: Date;@HasMany()
posts: Post[];
}```
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: '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
[MIT](LICENSE)