#Need help with nestjs repositories

17 messages · Page 1 of 1 (latest)

bleak thicket
#

I am new to nestjs and i need help.

I was using @slate stormRepository() before calling databases.

But i am facing an issue with @PrimaryGeneratedColumn() which is incrementing randomly (by 100) in most cases. So i have decided to create a service that will do it. But how do i initialize the required entity there? I was think of using datasource.getRepository(Entity) but i am having to create another separate connection for it. Can someone help me.

#1025199348096700476 #1025201603843719200

uneven hatch
#

I am not sure what exactly is the question here.
A repository is not a separate connection, but rather an abstraction over the database table. TypeORM manages a connection pool itself and each query to the database is serviced by one of the available connections at the moment.

bleak thicket
#

But i am unabke to access that connection using .getRepository(Entiy) it says Entity metadata not found. But it works if i do datasource.initialize()

#

@uneven hatch help me please

uneven hatch
#

okay, where _and when are you trying to access the repository?

#

Btw I would suggest that you don't worry about the generated id. It is guaranteed to be increasing, not strictly sequential.

bleak thicket
#

@uneven hatch It needs to sequencial else primary key value is going out of bounds for huge amounts of data.

#

@uneven hatch i am trying to make a repository inside a service. But unable to use . getRepository()... function

#

*access the repository inside service

uneven hatch
# bleak thicket <@646107453469229056> It needs to sequencial else primary key value is going out...

you can't enforce strictly sequential ids (i assume you use postgres, but most sql engines do the same). Postgres incerements the sequence for each attempted insert. That is, even for inserts that fail for transactions that are rolled back. You shouldn't really worry about that though. A simple 4 byte integer can (the default) can go up to 4 294 967 295 (that is, over 4 billion). If you're worried that it might go over that, you can use 8 byte integer or an uuid.

uneven hatch
bleak thicket
#

Function

uneven hatch
#

in onModuleInit?

bleak thicket
bleak thicket
uneven hatch
#

Can you share some code then? Ideally a minimal executable reproduction?

bleak thicket
#

Ya sure. Just gimme 15 mins