-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for using the
in
operator with context values
This change adds support for the `in` operator when using a context value in a prisma expression. This allows you to do useful stuff like allow a match against multiple contextually provided values (e.g. org membership). Signed-off-by: Lucian Buzzo <lucian.buzzo@gmail.com>
- Loading branch information
1 parent
f7d21d0
commit 5707f8a
Showing
4 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { escapeLiteral } from "./escape"; | ||
|
||
/** | ||
* Generates an AST fragment that will check if a column value exists in a JSONB array stored in a `current_setting` | ||
* The AST fragment represents SQL that looks like this: | ||
* = ANY (SELECT jsonb_array_elements_text(current_setting('ctx.my_context_value')::jsonb)) | ||
*/ | ||
export const jsonb_array_elements_text = (setting: string) => { | ||
return { | ||
type: "function", | ||
name: "ANY", | ||
args: { | ||
type: "expr_list", | ||
value: [ | ||
{ | ||
ast: { | ||
with: null, | ||
type: "select", | ||
options: null, | ||
distinct: { | ||
type: null, | ||
}, | ||
columns: [ | ||
{ | ||
type: "expr", | ||
expr: { | ||
type: "function", | ||
name: "jsonb_array_elements_text", | ||
args: { | ||
type: "expr_list", | ||
value: [ | ||
{ | ||
type: "cast", | ||
keyword: "cast", | ||
expr: { | ||
type: "function", | ||
name: "current_setting", | ||
args: { | ||
type: "expr_list", | ||
value: [ | ||
{ | ||
type: "parameter", | ||
value: escapeLiteral(setting.replace(/^___yates_context_/, "")), | ||
}, | ||
], | ||
}, | ||
}, | ||
as: null, | ||
symbol: "::", | ||
target: { | ||
dataType: "jsonb", | ||
}, | ||
arrows: [], | ||
properties: [], | ||
}, | ||
], | ||
}, | ||
}, | ||
as: null, | ||
}, | ||
], | ||
into: { | ||
position: null, | ||
}, | ||
from: null, | ||
where: null, | ||
groupby: null, | ||
having: null, | ||
orderby: null, | ||
limit: { | ||
seperator: "", | ||
value: [], | ||
}, | ||
window: null, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters