GithubHelp home page GithubHelp logo

Comments (18)

leosuncin avatar leosuncin commented on May 10, 2024 2

@leosuncin I wonder how I can migrate the database once I add more entities. and also when I changed the database name?

How to add more entities

  1. Create a new entity somewhere in your project
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Pet {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}
  1. Register it the data-source.ts file
import { Pet } from './wherever/pet.entity';

export const dataSourceOptions: DataSourceOptions = {
  // ...
  entities: [ /* the other entities*/, Pet],
  // ...
};
  1. Generate the new migration using TypeORM CLI
npm run typeorm migration:generate src/migrations/NameOfTheMigration
  1. Review the generated migration and modified it as needed

  2. Then add it to data-source.ts file

import { Pet } from './wherever/pet.entity';
import { NameOfTheMigration } from './migrations/name-of-the-migration';

export const dataSourceOptions: DataSourceOptions = {
  // ...
  entities: [ /* the other entities*/, Pet],
  migrations: [
    /* the other migrations, keep in mind that the order is important! */
    NameOfTheMigration,
  ],
  // ...
};
  1. Run the migrations
npm run typeorm migration:run

Check the documentation for further information https://orkhan.gitbook.io/typeorm/docs/migrations

Change the name of the database

The name of the database doesn't need to be "nestjs", it can be whatever you pick for, and set it in .env.

If you want to change the name of an existing database google it and again change it in the .env file

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

image

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

Replace the values in your connection connection string with the ones you're using

By example:

DATABASE_URL=postgres://user:password@postgres-host-name:5432/nestjs

Sorry, I'm writing from my phone, I can check it later if you need more help.

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

I run your docker container, so I guess, it's built-in setting?

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

image
image
image

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

The values in the .env.example are examples values and they should be replaced with a more accurate ones.

The default ones should work when you run the following steps:

  • Copy the default values: cp .env.example .env
  • Run the services with docker compose: docker compose up -d
  • Run the project with a local node.js installation: npm run start:dev

This is because Docker Compose will run an instance of PostgreSQL listening on localhost:5432 with the user user and password password

When the project run as a Docker container the PostgreSQL instance is not accessible through localhost:5432, you'll need to link both containers (the project and PostgreSQL) or run in the same network. Google it for more information

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

Yeah, that's exactly what I did (just followed steps in README)

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

But when I run yarn start:dev, it shows the following error:
image

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

Hi @crazydevlegend, I updated the dependencies of the project, pull the changes and try again, please

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

@leosuncin I'm using windows 11, and am not sure if this template work on Windows either.
when i run npm run typeorm migration:run, it said, NODE_OPTIONS undefined.
So I installed wi-node-env, and it shows another error.
image

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

oh, i just fixed this issue by installing ts-node, .thanks 🙏

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

@leosuncin I'm using windows 11, and am not sure if this template work on Windows either. when i run npm run typeorm migration:run, it said, NODE_OPTIONS undefined. So I installed wi-node-env, and it shows another error. image

Sorry, I'm not giving support for Windows since I don't use it anymore, but it should work with Bash for Windows or Windows Subsystem for Linux (WSL), due to this template heavily rely on environment variables as Bash define it. I haven't used wi-node-env but I know that cross-env works.

from nest-auth-example.

salahawk avatar salahawk commented on May 10, 2024

@leosuncin I wonder how I can migrate the database once I add more entities.
and also when I changed the database name?

from nest-auth-example.

crazydevlegend avatar crazydevlegend commented on May 10, 2024

You should take a look at your docker configuration, @salahawk

from nest-auth-example.

salahawk avatar salahawk commented on May 10, 2024

@leosuncin What if I want to remove an entity? (aka. TODO entity)
I'm building my own app, and want to remove this TODO entity, but when I run npm run typeorm migration:generate src/migrations/RemovedTODO after removing TODO in the entities list in data-source.ts, it says, no changes were found!!! (but actually, there's been changes - TODO entity has been removed)

image

Do you have any ideas on this?

Thanks in advance

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

@leosuncin What if I want to remove an entity? (aka. TODO entity)
I'm building my own app, and want to remove this TODO entity, but when I run npm run typeorm migration:generate src/migrations/RemovedTODO after removing TODO in the entities list in data-source.ts, it says, no changes were found!!! (but actually, there's been changes - TODO entity has been removed)

image

Do you have any ideas on this?

Thanks in advance

This is not related to this project, so I won't go deep but removing a entity from the data source doesn't necessary means that the table must be removed just ignored by the ORM, if you still need to remove the table create an empty migration and add the drop table statement

from nest-auth-example.

salahawk avatar salahawk commented on May 10, 2024

@leosuncin What if I want to remove an entity? (aka. TODO entity)
I'm building my own app, and want to remove this TODO entity, but when I run npm run typeorm migration:generate src/migrations/RemovedTODO after removing TODO in the entities list in data-source.ts, it says, no changes were found!!! (but actually, there's been changes - TODO entity has been removed)
image
Do you have any ideas on this?
Thanks in advance

This is not related to this project, so I won't go deep but removing a entity from the data source doesn't necessary means that the table must be removed just ignored by the ORM, if you still need to remove the table create an empty migration and add the drop table statement

So you mean, typeorm does not automatically generate that DROP statement, right?

from nest-auth-example.

leosuncin avatar leosuncin commented on May 10, 2024

This is not proper place to ask questions about any other library if you have any technical questions ask them on stackoverflow, I will only respond to questions related to the functionality of this repository

from nest-auth-example.

Related Issues (4)

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.