@appolo/typeorm
v8.1.0
Published
TypeORM module for [`appolo`](https://github.com/shmoop207/appolo) built with [`TypeORM`](https://github.com/typeorm/typeorm)
Downloads
39
Readme
Appolo TypeOrm Module
TypeORM module for appolo
built with TypeORM
Installation
npm i @appolo/typeorm
Options
| key | Description | Type | Default
| --- | --- | --- | --- |
| id
| ModelRepository
injection id | string
| modelRepository
|
| config
| typeorm connection options | object
| {} |
in config/modules/all.ts
import {TypeOrmModule} from '@appolo/typeorm';
export = async function (app: App) {
await app.module(new TypeOrmModule({
config: {
type: "postgres",
url: process.env.POSTGRES_URL,
ssl: true,
}}));
}
Usage
import {define, singleton} from 'appolo'
import {Entity,Column,model,Index,PrimaryGeneratedColumn,Repository} from "@appolo/typeorm";
@model()
@Entity({name: "user"})
export class User{
@PrimaryGeneratedColumn()
id: number;
@Column({
type: "varchar",
length: 100
})
name: string;
@Column({
type: "varchar",
length: 100
})
@Index()
email: string;
}
@define()
@singleton()
export class SomeManager {
@injectModel(User) userModel: Repository<User>;
async getUser(id:string): Promise<User> {
let user = await this.userModel.findOne(id)
return user;
}
}
ModelRepository
with modelRepository you can access to the typeorm repositories and db connection.
connection
getter return typeorm connection
getModel
getModel(model: typeof Schema): Model
return mongoose model by schema type
@define()
@singleton()
export class SomeManager {
@inject() modelRepository:ModelRepository;
async getUser(id:string): Promise<User> {
let user = await this.modelRepository.getModel(User).findOne(id)
return user;
}
}