Some jsonb functions #74
Replies: 1 comment
-
Hi ! Thanks, thats nice to read :) FYI, there are very few functions already implemented in pg-mem (they all are here, and none are about jsonb).
The functions you mention obviously fall in the second category ! Regarding your implementation of set/insert, I've not tested it but it seems to be pragmatic. That said, I'd add several things:
as you can see, the fourth argument is optional, and the second argument is a pg-mem can also do that... but unfortunately, I did not expose any way to declare array arguments through the public API yet (that would lock implementation details). function jsonb_set(original: any, path: string[], newData: any, inject?: boolean) {
// path will already be parsed as a string[]
}
// 1rst overload
db.public.registerFunction({
name: 'jsonb_set',
args: [DataType.jsonb, Types.text().asArray(), DataType.jsonb, DataType.bool],
returns: DataType.jsonb,
implementation: jsonb_set,
});
// 2nd overload
db.public.registerFunction({
name: 'jsonb_set',
args: [DataType.jsonb, Types.text().asArray(), DataType.jsonb],
returns: DataType.jsonb,
implementation: jsonb_set,
});
That could look like premature optimization, but since those functions can be called in critical parts such as |
Beta Was this translation helpful? Give feedback.
-
Hello! First off, just wanted to say this is an awesome library!
I see that pg-mem supports some json/jsonb functionality but was missing jsonb_set/insert and to_jsonb methods (among others). Anyone that's trying to perform updates without first querying the data, may need these methods. For unit testing purposes we can take some shortcuts to simulate these functions.
Disclaimer: the following was written quick and dirty at 1am while working on a side project. As I keep iterating on them, I may think about something more official, but often some quick examples are useful for folks!
Beta Was this translation helpful? Give feedback.
All reactions