Skip to content

Commit 2d1dd1a

Browse files
committed
Adjust Neo4j's currentUser to be more ergonomic
Typically, we don't need to bind it a query node name. And this is even forbidden in "path exists" expressions. So now ```ts currentUser == "(:User { id: $currentUser })" ``` And if you need to bind it, you can: ```ts currentUser.as('user') == "(user:User { id: $currentUser })" ```
1 parent dc9f91b commit 2d1dd1a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/core/database/query/create-relationships.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export function createRelationships<TResourceStatic extends ResourceShape<any>>(
107107
variable: !Array.isArray(varOrTuple)
108108
? varOrTuple instanceof Variable
109109
? varOrTuple.value
110+
: currentUser.is(varOrTuple)
111+
? relLabel
110112
: undefined
111113
: Array.isArray(varOrTuple[1])
112114
? `${relLabel}${i}`
@@ -156,8 +158,8 @@ export function createRelationships<TResourceStatic extends ResourceShape<any>>(
156158
flattened.map(({ variable, nodeLabel, id }) =>
157159
id instanceof Variable
158160
? []
159-
: id === currentUser
160-
? [currentUser]
161+
: currentUser.is(id)
162+
? [id.as(variable!)]
161163
: [node(variable, nodeLabel, { id })],
162164
),
163165
)

src/core/database/query/matching.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ import {
1919
import { variable } from '../query-augmentation/condition-variables';
2020
import { apoc, collect, listConcat, merge } from './cypher-functions';
2121

22-
export const currentUser = node('currentUser', 'User', {
23-
id: variable('$currentUser'),
24-
}) as Tagged<NodePattern, 'CurrentUser'>;
22+
const currentUserFlag = Symbol();
23+
const makeCurrentUser = (name: string) =>
24+
Object.assign(
25+
Object.defineProperty(
26+
node(name, 'User', { id: variable('$currentUser') }),
27+
currentUserFlag,
28+
{},
29+
) as Tagged<NodePattern, 'CurrentUser'>,
30+
{
31+
as: makeCurrentUser,
32+
is: (obj: object): obj is typeof currentUser =>
33+
Object.hasOwn(obj, currentUserFlag),
34+
},
35+
);
36+
export const currentUser = makeCurrentUser('');
2537

2638
export const requestingUser = (session?: Session | ID) => {
2739
const n = node('requestingUser', 'User', {

0 commit comments

Comments
 (0)