@kysera/rls 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { R as RLSSchema, O as Operation, P as PolicyCondition, a as PolicyHints, b as PolicyDefinition, F as FilterCondition, T as TableRLSConfig, C as CompiledPolicy, c as CompiledFilterPolicy, d as RLSContext, e as RLSAuthContext, f as RLSRequestContext, g as PolicyEvaluationContext } from './types-6eCXh_Jd.js';
2
2
  export { h as PolicyType } from './types-6eCXh_Jd.js';
3
3
  import { KyseraLogger, ErrorCode } from '@kysera/core';
4
- import { Plugin } from '@kysera/repository';
4
+ import { Plugin } from '@kysera/executor';
5
5
  import 'kysely';
6
6
 
7
7
  /**
@@ -598,7 +598,7 @@ interface RLSPluginOptions<DB = unknown> {
598
598
  * },
599
599
  * });
600
600
  *
601
- * // Create ORM with RLS plugin
601
+ * // Create repository with RLS plugin
602
602
  * const orm = await createORM(db, [
603
603
  * rlsPlugin({ schema }),
604
604
  * ]);
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { silentLogger } from '@kysera/core';
2
+ import { getRawDb } from '@kysera/executor';
2
3
  import { sql } from 'kysely';
3
4
  import { AsyncLocalStorage } from 'async_hooks';
4
5
 
@@ -1203,7 +1204,7 @@ function rlsPlugin(options) {
1203
1204
  let mutationGuard;
1204
1205
  return {
1205
1206
  name: "@kysera/rls",
1206
- version: "0.5.1",
1207
+ version: "0.7.0",
1207
1208
  // Run after soft-delete (priority 0), before audit
1208
1209
  priority: 50,
1209
1210
  // No dependencies by default
@@ -1302,6 +1303,8 @@ function rlsPlugin(options) {
1302
1303
  const originalUpdate = baseRepo.update?.bind(baseRepo);
1303
1304
  const originalDelete = baseRepo.delete?.bind(baseRepo);
1304
1305
  const originalFindById = baseRepo.findById?.bind(baseRepo);
1306
+ const rawDb = getRawDb(baseRepo.executor);
1307
+ const hasRawDb = baseRepo.executor.__rawDb !== void 0;
1305
1308
  const extendedRepo = {
1306
1309
  ...baseRepo,
1307
1310
  /**
@@ -1338,12 +1341,19 @@ function rlsPlugin(options) {
1338
1341
  * Wrapped update with RLS check
1339
1342
  */
1340
1343
  async update(id, data) {
1341
- if (!originalUpdate || !originalFindById) {
1344
+ if (!originalUpdate) {
1342
1345
  throw new RLSError("Repository does not support update operation", RLSErrorCodes.RLS_POLICY_INVALID);
1343
1346
  }
1344
1347
  const ctx = rlsContext.getContextOrNull();
1345
1348
  if (ctx && !ctx.auth.isSystem && !bypassRoles.some((role) => ctx.auth.roles.includes(role))) {
1346
- const existingRow = await originalFindById(id);
1349
+ let existingRow;
1350
+ if (hasRawDb) {
1351
+ existingRow = await rawDb.selectFrom(table).selectAll().where("id", "=", id).executeTakeFirst();
1352
+ } else if (originalFindById) {
1353
+ existingRow = await originalFindById(id);
1354
+ } else {
1355
+ throw new RLSError("Repository does not support update operation", RLSErrorCodes.RLS_POLICY_INVALID);
1356
+ }
1347
1357
  if (!existingRow) {
1348
1358
  return originalUpdate(id, data);
1349
1359
  }
@@ -1377,12 +1387,19 @@ function rlsPlugin(options) {
1377
1387
  * Wrapped delete with RLS check
1378
1388
  */
1379
1389
  async delete(id) {
1380
- if (!originalDelete || !originalFindById) {
1390
+ if (!originalDelete) {
1381
1391
  throw new RLSError("Repository does not support delete operation", RLSErrorCodes.RLS_POLICY_INVALID);
1382
1392
  }
1383
1393
  const ctx = rlsContext.getContextOrNull();
1384
1394
  if (ctx && !ctx.auth.isSystem && !bypassRoles.some((role) => ctx.auth.roles.includes(role))) {
1385
- const existingRow = await originalFindById(id);
1395
+ let existingRow;
1396
+ if (hasRawDb) {
1397
+ existingRow = await rawDb.selectFrom(table).selectAll().where("id", "=", id).executeTakeFirst();
1398
+ } else if (originalFindById) {
1399
+ existingRow = await originalFindById(id);
1400
+ } else {
1401
+ throw new RLSError("Repository does not support delete operation", RLSErrorCodes.RLS_POLICY_INVALID);
1402
+ }
1386
1403
  if (!existingRow) {
1387
1404
  return originalDelete(id);
1388
1405
  }