pub fn jsonb_insert_with_insert_after<E: JsonbOrNullableJsonb + SingleValue, Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E, Jsonb>, base, path, new_value, insert_after>(
base: base,
path: path,
new_value: new_value,
insert_after: insert_after,
) -> jsonb_insert_with_insert_after<E, Arr, base, path, new_value, insert_after>where
base: AsExpression<E>,
path: AsExpression<Arr>,
new_value: AsExpression<E>,
insert_after: AsExpression<Bool>,
Available on crate feature
postgres_backend
only.Expand description
Returns target with new_value
inserted into base
.
If the item designated by the path
is an array element, new_value
will be inserted before that
item if insert_after
is false (which is the default),
or after it if insert_after
is true.
If the item designated by the path
is an object field, new_value
will be inserted only
if the object does not already contain that key.
- All earlier steps in the
path
must exist, or the target is returned unchanged. - As with the path oriented operators, negative integers that appear in the
path
count from the end of JSON arrays. - If the last
path
step is an array index that is out of range, the new value is added at the beginning of the array if the index is negative, or at the end of the array if it is positive.
§Example
let result = diesel::select(jsonb_insert_with_insert_after::<Jsonb, Array<Text>, _, _, _, _>(
json!({"a":[0,1,2]}),
vec!["a","1"],
json!("new_value"),
false
)).get_result::<Value>(connection)?;
let expected: Value = json!({"a":[0,"new_value",1,2]});
assert_eq!(result, expected);
let result = diesel::select(jsonb_insert_with_insert_after::<Jsonb, Array<Text>, _, _, _, _>(
json!({"a":[0,1,2]}),
vec!["a","1"],
json!("new_value"),
true
)).get_result::<Value>(connection)?;
let expected: Value = json!({"a":[0,1,"new_value",2,]});
assert_eq!(result, expected);