@midwayjs/orm
TypeScript icon, indicating that this package has built-in type declarations

3.4.3 • Public • Published

midway orm component

How to use

in Configuration.ts file

import * as orm from '@midwayjs/orm';
import { join } from 'path';

@Configuration({
  imports: [
    orm,
  ],
  importConfigs: [
    join(__dirname, './config')
  ]
})
export class ContainerConfiguration {
}

Configuration

in config files

export default {
  orm: {
    type: 'mysql',
    host: '',
    port: 3306,
    username: '',
    password: '',
    database: undefined,
    synchronize: true,
    logging: false,
 }
};

or

export const orm = {
  type: 'sqlite',  // or use mysql see typeorm docs
  database: join(__dirname, './test.sqlite'),
  logging: true,
}

Define EntityModel

// model/user.ts
import { EntityModel } from '@midwayjs/orm';
import { PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';

@EntityModel('test_user')
export class Photo {
  @PrimaryGeneratedColumn({ name: "id" })
  id: number;

  @Column({ name: "name" })
  name: string;

  @OneToMany(type => Message, message => message.sender)
  messages: Message[];
}

Use Model

in code files

import { InjectEntityModel } from '@midwayjs/orm';
import { User } from './model/user';
import { Repository } from 'typeorm';

@Provide()
export class UserService {

  @InjectEntityModel(User)
  userModel: Repository<User>;

  async testUser() {
    const u = new User();
    u.name = 'oneuser1';
    const uu = await this.userModel.save(u);
    console.log('user one id = ', uu.id);

    const user = new User();
    user.id = 1;
    const users = await this.userModel.findAndCount(user);
    return 'hello world' + JSON.stringify(users);
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @midwayjs/orm

Weekly Downloads

153

Version

3.4.3

License

MIT

Unpacked Size

26.3 kB

Total Files

16

Last publish

Collaborators

  • mariodu
  • czy88840616
  • legendecas
  • echosoar
  • lellansin
  • lxxyx
  • stone-jin