|
1 | 1 | # TypeORMer |
| 2 | + |
| 3 | +> TypeORM helper |
| 4 | +
|
| 5 | +[](https://www.npmjs.com/package/typeormer) |
| 6 | +[](https://github.com/frouriojs/typeormer/actions?query=workflow%3A%22Node.js+CI%22) |
| 7 | +[](https://codecov.io/gh/frouriojs/typeormer) |
| 8 | +[](https://lgtm.com/projects/g/frouriojs/typeormer/context:javascript) |
| 9 | +[](https://github.com/frouriojs/typeormer/blob/master/LICENSE) |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +- Using [npm](https://www.npmjs.com/): |
| 14 | + |
| 15 | + ```sh |
| 16 | + $ npm install reflect-metadata typeorm typeormer |
| 17 | + ``` |
| 18 | + |
| 19 | +- Using [Yarn](https://yarnpkg.com/): |
| 20 | + |
| 21 | + ```sh |
| 22 | + $ yarn add reflect-metadata typeorm typeormer |
| 23 | + ``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +`entity/Task.ts` |
| 28 | +```ts |
| 29 | +import { Entity, PrimaryColumn, Column } from 'typeorm' |
| 30 | + |
| 31 | +@Entity() |
| 32 | +export class Task { |
| 33 | + @PrimaryColumn() |
| 34 | + id: number |
| 35 | + |
| 36 | + @Column({ length: 100 }) |
| 37 | + label: string |
| 38 | + |
| 39 | + @Column({ default: false }) |
| 40 | + done: boolean |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +`subscriber/TaskSubscriber.ts` |
| 45 | +```ts |
| 46 | +import { EntitySubscriberInterface, EventSubscriber, InsertEvent } from 'typeorm' |
| 47 | +import { Task } from '../entity/Task' |
| 48 | + |
| 49 | +@EventSubscriber() |
| 50 | +export class TaskSubscriber implements EntitySubscriberInterface<Task> { |
| 51 | + listenTo() { |
| 52 | + return Task |
| 53 | + } |
| 54 | + |
| 55 | + afterInsert(event: InsertEvent<Task>) { |
| 56 | + console.log(event) |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +`ormconfig.ts` |
| 62 | +```ts |
| 63 | +import { ConnectionOptions } from 'typeorm' |
| 64 | +import dotenv from 'dotenv' |
| 65 | + |
| 66 | +dotenv.config() |
| 67 | + |
| 68 | +const options: ConnectionOptions = { |
| 69 | + type: 'mysql', |
| 70 | + host: process.env.TYPEORM_HOST, |
| 71 | + username: process.env.TYPEORM_USERNAME, |
| 72 | + password: process.env.TYPEORM_PASSWORD, |
| 73 | + database: process.env.TYPEORM_DATABASE, |
| 74 | + port: Number(process.env.TYPEORM_PORT), |
| 75 | + synchronize: false, |
| 76 | + logging: false, |
| 77 | + entities: ['entity/**/*.ts'], |
| 78 | + migrations: ['migration/**/*.ts'], |
| 79 | + cli: { |
| 80 | + migrationsDir: 'migration' |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// @ts-ignore |
| 85 | +export = options |
| 86 | +``` |
| 87 | + |
| 88 | +`package.json` |
| 89 | +```json |
| 90 | +{ |
| 91 | + "scripts": { |
| 92 | + "migration:generate": "typeorm migration:generate -n Task", |
| 93 | + "typeormer": "typeormer" |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +`tarminal` |
| 99 | +```bash |
| 100 | +$ npm run migration:generate # created migration/xxx-Task.ts |
| 101 | +$ npm run typeormer # created $orm.ts |
| 102 | +``` |
| 103 | + |
| 104 | +`index.ts` |
| 105 | +```ts |
| 106 | +import 'reflect-metadata' |
| 107 | +import { createConnection, Connection, ConnectionOptions } from 'typeorm' |
| 108 | +import ormconfig from './ormconfig' |
| 109 | +import options from './$orm' |
| 110 | + |
| 111 | +const connection = await createConnection({ |
| 112 | + ...ormconfig, |
| 113 | + ...options |
| 114 | +}) |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +TypeORMer is licensed under a [MIT License](https://github.com/frouriojs/typeormer/blob/master/LICENSE). |
0 commit comments