pub fn jsonb_insert<E: JsonbOrNullableJsonb + SingleValue, Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E, Jsonb>, base, path, new_value>(
base: base,
path: path,
new_value: new_value,
) -> jsonb_insert<E, Arr, base, path, new_value>
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 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::<Jsonb, Array<Text>, _, _, _>(
json!({"a":[0,1,2]}),
vec!["a","1"],
json!("new_value"),
)).get_result::<Value>(connection)?;
let expected: Value = json!({"a":[0,"new_value",1,2]});
assert_eq!(result, expected);
let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(
None::<serde_json::Value>,
vec!["a","1"],
Some(json!("new_value")),
)).get_result::<Option<Value>>(connection)?;
assert_eq!(result, None);