Giter VIP home page Giter VIP logo

typeorm-configuration's Introduction

TypeORM-Configuration

Tabela de conteúdos

Configurando TypeORM

Instale as dependencias

yarn add typeorm pg reflect-metadata

Configuração do ormconfig.json para postgress

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "docker",
  "database": "gostack_gobarber",
  "entities": [
    "./src/models/*.ts"
  ],
  "migrations": [
    "./src/database/migrations/*.ts"
  ],
  "cli": {
    "migrationsDir": "./src/database/migrations"
  }
}

Crie uma pasta ./src/database, e no seu index coloque o codigo abaixo:

import { createConnection } from 'typeorm'

createConnection();

Crie uma pasta ./src/databe/migrations

No package.json, crie um script

...
"scripts" : {
  ...
  "typeorm": "ts-node-dev ./node_modules/typeorm/cli.js",
  ...
}
...

Criando migrations após configuração

yarn typeorm migration:create -n CreateAppointments

Example de migration:

import { MigrationInterface, QueryRunner, Table } from "typeorm";

export class CreateAppointments1608663619421 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
      await queryRunner.createTable(
        new Table({
          name: 'appointments',
          columns: [
            {
              name: 'id',
              type: 'varchar',
              isPrimary: true,
              generationStrategy: 'uuid',
            },
            {
              name: 'provider',
              type: 'varchar',
              isNullable: false,
            },
            {
              name: 'date',
              type: 'timestamp with time zone',
              isNullable: false,
            }
          ]
        })
      );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
      await queryRunner.dropTable('appointments');
    }
}

Commandos de typeorm para migrations:

1. Executar migrations:

yarn typeorm migration:run

2. Reverter ultima migrations:

yarn typeorm migration:revert

3. Mostrar log de migrations:

yarn typeorm migration:show

Configurando Models

Em tsconfig.json, habilite as configs descritas abaixo:

{
  ...
  "strictPropertyInitialization": false,  /* Enable strict checking of property initialization in classes. */
  ...
  "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
  "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
  ...
}

typeorm-configuration's People

Contributors

roymas100 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.