@gencow/core 0.1.11 → 0.1.12
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/rls-db.d.ts +1 -16
- package/dist/rls-db.js +14 -9
- package/package.json +1 -1
- package/src/rls-db.ts +16 -11
package/dist/rls-db.d.ts
CHANGED
|
@@ -7,19 +7,4 @@ import type { PgDatabase } from "drizzle-orm/pg-core";
|
|
|
7
7
|
* → is_local=true: 현재 트랜잭션에서만 유효
|
|
8
8
|
* → PgBouncer transaction 모드에서 안전
|
|
9
9
|
*/
|
|
10
|
-
export declare function createRlsDb(db: PgDatabase<any, any, any>, userId: string):
|
|
11
|
-
transaction: typeof db.transaction;
|
|
12
|
-
_: {
|
|
13
|
-
readonly schema: any;
|
|
14
|
-
readonly fullSchema: any;
|
|
15
|
-
readonly tableNamesMap: Record<string, string>;
|
|
16
|
-
readonly session: import("drizzle-orm/pg-core").PgSession<any, any, any>;
|
|
17
|
-
};
|
|
18
|
-
query: import("drizzle-orm").DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?"> | {
|
|
19
|
-
[x: string]: import("drizzle-orm/pg-core/query-builders/query").RelationalQueryBuilder<any, any>;
|
|
20
|
-
};
|
|
21
|
-
$with: import("drizzle-orm/pg-core").WithBuilder;
|
|
22
|
-
$cache: {
|
|
23
|
-
invalidate: import("drizzle-orm/cache/core").Cache["onMutate"];
|
|
24
|
-
};
|
|
25
|
-
};
|
|
10
|
+
export declare function createRlsDb(db: PgDatabase<any, any, any>, userId: string): PgDatabase<any, any, any>;
|
package/dist/rls-db.js
CHANGED
|
@@ -8,13 +8,18 @@ import { sql } from "drizzle-orm";
|
|
|
8
8
|
* → PgBouncer transaction 모드에서 안전
|
|
9
9
|
*/
|
|
10
10
|
export function createRlsDb(db, userId) {
|
|
11
|
-
return {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
return new Proxy(db, {
|
|
12
|
+
get(target, prop, receiver) {
|
|
13
|
+
if (prop === "transaction") {
|
|
14
|
+
return async (callback, ...rest) => {
|
|
15
|
+
return await target.transaction(async (tx) => {
|
|
16
|
+
await tx.execute(sql `SELECT set_config('app.current_user_id', ${userId}, true)`);
|
|
17
|
+
return await callback(tx);
|
|
18
|
+
}, ...rest);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const value = Reflect.get(target, prop, receiver);
|
|
22
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
20
25
|
}
|
package/package.json
CHANGED
package/src/rls-db.ts
CHANGED
|
@@ -10,15 +10,20 @@ import type { PgDatabase } from "drizzle-orm/pg-core";
|
|
|
10
10
|
* → PgBouncer transaction 모드에서 안전
|
|
11
11
|
*/
|
|
12
12
|
export function createRlsDb(db: PgDatabase<any, any, any>, userId: string) {
|
|
13
|
-
return {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
return new Proxy(db, {
|
|
14
|
+
get(target, prop, receiver) {
|
|
15
|
+
if (prop === "transaction") {
|
|
16
|
+
return async (callback: any, ...rest: any[]) => {
|
|
17
|
+
return await target.transaction(async (tx) => {
|
|
18
|
+
await tx.execute(
|
|
19
|
+
sql`SELECT set_config('app.current_user_id', ${userId}, true)`
|
|
20
|
+
);
|
|
21
|
+
return await callback(tx);
|
|
22
|
+
}, ...rest as any);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const value = Reflect.get(target, prop, receiver);
|
|
26
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
24
29
|
}
|