#Insert Data into custom entity

8 messages · Page 1 of 1 (latest)

pearl monolith
#

I am quite new to TypeORM which handles this case I believe: What is the easiest and recommended way to insert / read data into a custom entity? I have created an entity in models/ and created a migration which successfully ran. Now I have a new empty table categories. I would like to write insert / read methods that execute SQL statements on the table. How could I combine typeORM and medusa here to achieve this? Sorry this is a newbie question.

Plus what is the recommended way to test these routes / new tables ?

placid lark
#

You can create a repository or get a repository from the entity (see typeorm doc) and then you can look at the repository

#

Repositories are also injected in the services and can be resolved in your end point handler. Then you can follow typeorm documentation

pearl monolith
#

So the flow would be e.g.:

  1. client calls endpoint
  2. endpoint resolves repository (typeorm)
  3. repository exposes methods to resolver (endpoint in this case)
  4. repository manages direct queries on db table

is this correct?

placid lark
#

Yes thats correct, you can create a service that expose the methods and the service would consume the repository. The service would be called by the end point. Just to separate the responsibilities

pearl monolith
#

that makes perfect sense. sorry my background is more in DE, not so much backend design 😅

thorny pulsar
#

it is correct but you should add a services layer to do a business logic.

placid lark