https://github.com/foolishchow/fmp-typeorm
midway playground for typeorm
https://github.com/foolishchow/fmp-typeorm
Last synced: about 1 month ago
JSON representation
midway playground for typeorm
- Host: GitHub
- URL: https://github.com/foolishchow/fmp-typeorm
- Owner: foolishchow
- Created: 2020-03-18T14:09:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-10T16:24:53.000Z (over 3 years ago)
- Last Synced: 2025-02-24T02:14:17.286Z (over 1 year ago)
- Language: TypeScript
- Size: 938 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# fmp-typeorm
> midway playground for typeorm
# enable in config
- `src/config/plugin.ts`
```typescript
import { EggPlugin } from 'midway';
export default {
static: true, // default is true
typeorm: {
enable: true,
package: "fmp-typeorm"
}
} as EggPlugin;
```
- `src/config/config.*.ts`
```typescript
import { EggAppConfig, EggAppInfo, PowerPartial } from 'midway';
export type DefaultConfig = PowerPartial
export default (appInfo: EggAppInfo) => {
const config = {};
config.typeorm = {
client: {
type: "mysql",
host: "localhost",
port: 3306,
username: 'root',
password: 'root',
database: 'database',
entities: ['models'],
synchronize: true
}
}
return config;
};
```
- `ts declare`
```typescript
import { Connection, ConnectionOptions, ConnectionManager, Repository, getConnectionManager } from 'typeorm';
interface TypeormConfig {
/**
* @description typeorm conn option
*/
client?: ConnectionOptions;
/**
* @description typeorm conns option
*/
clients?: {
[key: string]: ConnectionOptions
};
}
```
# use
- `models/user.ts`
```typescript
import { makeRepository } from "fmp-typeorm";
import { Entity, PrimaryGeneratedColumn, Column, Repository } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
userId: number;
@Column()
userName: string;
@Column()
userEmail: string;
}
export type UserRepository = Repository;
export const factory = makeRepository(User);
```
- `service/home.ts`
```typescript
import { Application } from "egg";
import { config, Context, inject, provide } from "midway";
import { UserRepository } from "../models/user";
@provide()
export class MysqlSlaveStatusService {
@inject() userRepository: UserRepository;
@inject() ctx: Context;
async test() {
let users = await this.userRepository.find({...})
}
}
```