You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
awaituserRepo.find(); // will return only the results where the db rls policy applies
55
55
```
56
56
57
-
In the above example, you'll have to work with the supplied connection. Calling TypeORM function directly will work with the original connection which is not RLS aware.
57
+
In the above example, you'll have to work with the supplied connection. Calling TypeORM function directly will work with the original DataSource object which is not RLS aware.
58
58
59
59
## NestJS integration
60
60
61
61
If you are using NestJS, this library provides helpers for making your connections and queries tenant aware.
62
62
63
-
Create your TypeORM config and load the TypeORM module using `.forRoot`. Then you'll need to load the `RLSModule` with `.forRoot` where you'll define where to take the `tenantId` and `actorId` from. The second part is that you now need to replace the `TypeOrmModule.forFeature` with `RLSModule.forFeature`.
63
+
Create your TypeORM config and load the TypeORM module using `.forRoot`. Then you'll need to load the `RLSModule` with `.forRoot` where you'll define where to take the `tenantId` and `actorId` from. The second part is that you now need to replace the `TypeOrmModule.forFeature` with `RLSModule.forFeature`. This should be a 1-to-1 replacement.
64
64
You can inject non-entity dependent Modules and Providers. First array imports modules, second array injects providers.
65
65
66
66
When using `RLSModule.forRoot` it will set your `scope` to `REQUEST`! Be sure you understand the implications of this and especially read about the request-scoped authentication strategy on [Nestjs docs](https://docs.nestjs.com/security/authentication#request-scoped-strategies).
67
67
68
+
The `RLSModule.forRoot` accepts the factory funtion as async or non-async function.
// You can take the tenantId and actorId from headers/tokens etc
76
78
const tenantId =req.headers['tenant_id'];
77
79
const actorId =req.headers['actor_id'];
@@ -107,6 +109,9 @@ export class AppService {
107
109
}
108
110
```
109
111
110
-
Same as before, do not use the TypeORM functions directly (eg: `getConnection()`) as that will give you the default connection to the database, not the wrapped instance.
112
+
Same as before, do not use the TypeORM functions directly from the `dataSource` as that will give you the default connection to the database, not the wrapped instance.
111
113
112
114
For more specific examples, check the `test/nestjs/src`.
115
+
116
+
# Typeorm >v0.3.0
117
+
Since typeorm v0.3.0, the Connection class has been replaced by DataSource. This module still uses Connection as its language which is also helpful now to differenciate between the actual database connection (typeorm DataSource) and RLS wrapper (RLSConnection). However, if you want to be on par with typeorm terminalogy, there is an alias for `RLSConnection` called `RLSDataSource`.
0 commit comments