Size: a a a

TypeORM - Русскоязычное сообщество

2020 February 07

n

neurosys in TypeORM - Русскоязычное сообщество
Sounds like this is accessible in Subscribers, but not Listeners. But let me double check.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
https://github.com/typeorm/typeorm/blob/master/src/subscriber/Broadcaster.ts#L38

Just to make sure, EntityListeners are not passed in any data.
источник
2020 February 08

КБ

Константин Брызгалин in TypeORM - Русскоязычное сообщество
neurosys
Sounds like this is accessible in Subscribers, but not Listeners. But let me double check.
yeah, I use Subscribers mostly. there’s a trick that allows to use them as part of NestJS DI/Module system, so you can inject dependencies and write async handlers. Listeners, as far as I remember, are synchronous and you cannot inject anything into them. mostly Listeners are useful for validations and pre-save data transformations…
источник
2020 February 09

n

neurosys in TypeORM - Русскоязычное сообщество
Константин Брызгалин
yeah, I use Subscribers mostly. there’s a trick that allows to use them as part of NestJS DI/Module system, so you can inject dependencies and write async handlers. Listeners, as far as I remember, are synchronous and you cannot inject anything into them. mostly Listeners are useful for validations and pre-save data transformations…
> Listeners, as far as I remember, are synchronous and you cannot inject anything into them.

Actually synchronicity is rather useful (if not performant) for deterministic behaviour. Even though validations should be independent of each other, more often than not, I expect them to run in a certain order.

Some validations I have require database look-ups, and they are practically impossible with Listeners if you want them to be in a transaction. So I'll move all of those into Subscribers soon.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
On another point, I like to keep related things together, i.e. entities as fat models. Having to navigate different parts of a codebase often results in missing functionality that are hidden away in other files and executed through implicit lifecycle hooks.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
As for Nest.js, cannot use them due to our controllers not being HTTP (they are through a custom protocol). We could still benefit from everything else other than the Nest.js controllers though, need to look into it.
источник
2020 February 10

J

Joel in TypeORM - Русскоязычное сообщество
neurosys
> Listeners, as far as I remember, are synchronous and you cannot inject anything into them.

Actually synchronicity is rather useful (if not performant) for deterministic behaviour. Even though validations should be independent of each other, more often than not, I expect them to run in a certain order.

Some validations I have require database look-ups, and they are practically impossible with Listeners if you want them to be in a transaction. So I'll move all of those into Subscribers soon.
Do you know how to get the user who ran the subscriber? is to save in the column of type updatedBy
источник

n

neurosys in TypeORM - Русскоязычное сообщество
Joel
Do you know how to get the user who ran the subscriber? is to save in the column of type updatedBy
I don't fully understand, can you rephrase the question?

If you mean the user of the service/application, it is part of the Entity, and set on the entity by the controller before saving/updating it.
источник

J

Joel in TypeORM - Русскоязычное сообщество
neurosys
I don't fully understand, can you rephrase the question?

If you mean the user of the service/application, it is part of the Entity, and set on the entity by the controller before saving/updating it.
For example, I have an audit entity, it has a subscriber, of any entity, that is, when there is any change in any other entity the subscriber will be executed, but I would like to determine which user performed such action
источник

J

Joel in TypeORM - Русскоязычное сообщество
Something like logging in, edit a record of an entity called photo, then the audit subscriber runs and I would like to know the id of the user who performed the action, do you understand me?
источник
2020 February 11

YV

Yaroslav Veselovskyi in TypeORM - Русскоязычное сообщество
всем привет!
как такой запрос написать в TypeORM?

SELECT
 DISTINCT clients.id,
 (
   SELECT
     COUNT(*)
   FROM
     user_accounts
   WHERE
     client_id = clients.id
 ) AS users_amount
FROM
 clients;
источник

КБ

Константин Брызгалин in TypeORM - Русскоязычное сообщество
вот дали людям подзапросы, совсем про джойны такими темпами забудут… 😕
источник

КБ

Константин Брызгалин in TypeORM - Русскоязычное сообщество
почему бы не написать
select c.id, count(ua.*) as users_amount from clients c left join user_account ua on ua.client_id = c.id group by c.id

?
источник
2020 February 12

n

neurosys in TypeORM - Русскоязычное сообщество
Joel
Something like logging in, edit a record of an entity called photo, then the audit subscriber runs and I would like to know the id of the user who performed the action, do you understand me?
Sorry for late reply. In a threaded environment, you could use a Thread Local, which is typically used for things like userId or transactionId. But I think even that is an ugly solution and far from ideal (it's just a thread specific global state).

The correct way would be to send the data (in this case userId) through a deterministic path, by explicitly setting it on your Entity (even as a virtual field not mapped to database column), which will eventually reach your Subscriber.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
I don't fully understand what you are confused about it, because an Entity Subscriber is triggered for an Entity, which you can set "userId" on before saving or updating it.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
Question:

We are using PostgresDB with table IDs as BSON ObjectIDs. They are not autogenerated, but set by the application on create requests.

I am planning to auto generate these IDs in beforeInsert hook like this:

if (this.id === undefined) 
   this.id = new ObjectId().toString()

Does anyone see any potential problems with this approach?
источник

n

neurosys in TypeORM - Русскоязычное сообщество
TIL: typeof null === 'object';
источник

n

neurosys in TypeORM - Русскоязычное сообщество
https://github.com/typeorm/typeorm/issues/3603#issuecomment-464039794

I don't understand why the behaviour here such that it sets the foreign key to null instead of deleting the row. Setting the foreign_key to null is almost always guaranteed to cause null contraint errors.
источник

n

neurosys in TypeORM - Русскоязычное сообщество
Same problem but better formulated: https://github.com/typeorm/typeorm/issues/3938
источник
2020 February 13

n

neurosys in TypeORM - Русскоязычное сообщество
Is it possible to find relations with condition (such as archived_at is null) when doing:

myRepository.find({ where: { id: 'some-id' }, relations: ['someRelation'] });
источник