{pg-promise} postgres - how to convert type int[] to be compatible with POINT() - pg-promise

So I'm using pg-promise to insert into a type POINT column. But it's giving me the following error:
function point(integer[]) does not exist
I'm passing the values as an array. What should I change to make it work?
Some code (not sure if useful):
simplified_query = `$${counter++}:name = POINT($${counter++})`
fields =
[
"geolocation",
[10, 10]
]

As per Custom Type Formatting, if your field is ['geolocation', [10, 10]], with the first value being the column name, you can use the following function:
function asPoint(field) {
return {
rawType: true,
toPostgres: () => pgp.as.format('$1:name = POINT($2:csv)', field)
};
}
Then you can use asPoint(field) as a query-formatting parameter:
const field = ['geolocation', [10, 10]];
db.any('SELECT * FROM table WHERE $1', [asPoint(field)])
//=> SELECT * FROM table WHERE "geolocation" = POINT(10, 10)
Alternatively, your field can be a custom-type class that implements Custom Type Formatting either explicitly or via the prototype, in which case it can be used as a formatting parameter directly.

Related

How to access customized object value inside postgresql function?

I'm new to postgresql function, wondering how to we access the value from a customized type inside postgresql?
For example the following example, BreakoutMatchingsDecrementProps is the customized type and I'm not able to use record.communityId to get the value...
CREATE TYPE BreakoutMatchingsDecrementProps AS (
weekId int,
communityId varchar,
timeSlot int,
groupSize breakoutsize,
day weekday
);
create or replace function Test (obj BreakoutMatchingsDecrementProps[])
returns void as
$$
DECLARE
record BreakoutMatchingsDecrementProps;
BEGIN
FOREACH record IN ARRAY obj LOOP
update breakout_matching
set number_of_registration = number_of_registration - 1
where week_id = 1
AND community_id = record.communityId
AND time_slot = 1
AND group_size = 'Two'
AND day = 'Monday'
AND number_of_registration > 0;
END
END
$$
language plpgsql;
I'm using Supabase and client side looks like:
const { data, error } = await adminClient.rpc(
"test",
{
obj:
{
timeSlot: 1,
groupSize: "Two",
day: "Monday",
communityId: "dshjdfdfak",
weekId: 1,
},
}
);
When I changed the record.communityId to "dshjdfdfak" then it works...
Wondering how do we access customized type object in plpgsql? Thanks!

Add jsonb property of type boolean

I'm trying to add a property to an existing jsonb column (column "data").
I want have my jsonb document to log like this
{
// ... existing properties
"Filed": false // new property
}
I tried
UPDATE "doc" SET "data" = jsonb_set("data"::jsonb, 'Filed', false, true)
I get this error:
[42883] ERROR: function jsonb_set(jsonb, unknown, boolean, boolean) does not exist
Hint: No function matches the given name and argument types.
You might need to add explicit type casts. Position: 46
It should be
jsonb_set("data"::jsonb, '{Filed}', 'false', TRUE)
The second parameter is an array denoting the path to the appropriate key, and 'false' is the string representation of a JSON boolean.
Better use the || operator.
UPDATE "doc" SET "data" = "data" || '{"Filed": false}';
This one is equivalent but more suitable for parameterization:
UPDATE "doc" SET "data" = "data" || jsonb_build_object('Filed', false);

How to bind an array to a parameter used alongside IN operator in sequelize query

I had to write a custom postgres sql query, so I used sequelize.query method. But I am a bit lost in how I can bind an array to a parameter used alongside an IN operator. The current code looks something like this, with obviously doesn't work.
sequelize.query('SELECT * FROM students WHERE grade IN $grades', {
bind: { grades: ['A+', 'A'] },
type: sequelize.QueryTypes.SELECT,
});
Use = any instead of in. Change the query text to
SELECT * FROM students WHERE grade = any(string_to_array($grades, ','))
or
SELECT * FROM students WHERE grade = any(('{'||$grades||'}')::text[])
and bind grades as a string, 'A+,A'.
The second option works for other data types too.

Call jsonb_contains function (postgres) using JPA criteria and JsonBinaryType

I have a JPA/Hibernate entity which has a JSONB column (using https://github.com/vladmihalcea/hibernate-types ) for storing a list of strings. This works fine so far.
#TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
#Type(type = "jsonb")
#Column(name = "TAGS", columnDefinition = "jsonb")
private List<String> tags;
Now I want to check if another string is contained in the list of strings.
I can do this by writing a native query and use the #> operator from Postgres. Because of other reasons (the query is more complex) I do not want to go in that direction. My current approach is calling the jsonb_contains method in a Spring Data specification (since the operator is just alias to this function), e.g. jsonb_contains('["tag1", "tag2", "tag3"]','["tag1"]'). What I am struggling with is, getting the second parameter right.
My initial approach is to also use a List of Strings.
public static Specification<MyEntity> hasTag(String tag) {
return (root, query, cb) -> {
if (StringUtils.isEmpty(tag)) {
return criteriaBuilder.conjunction();
}
Expression<Boolean> expression = criteriaBuilder.function("jsonb_contains",
Boolean.class,
root.get("tags"),
criteriaBuilder.literal(List.of(tag)));
return criteriaBuilder.isTrue(expression);
};
}
This results in the following error.
Caused by: org.postgresql.util.PSQLException: ERROR: function jsonb_contains(jsonb, character varying) does not exist
Hinweis: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 375
It does know that root.get("tags") is mapped to JSONB but for the second parameter it does not. How can I get this right? Is this actually possible?
jsonb_contains(jsob, jsonb) parameters must be jsonb type.
You can not pass a Java String as a parameter to the function.
You can not do casting in Postgresql via JPA Criteria.
Using JSONObject or whatever does not help because Postgresql sees it as
bytea type.
There are 2 possible solutions:
Solution 1
Create jsonb with jsonb_build_object(text[]) function and send it to jsonb_contains(jsonb, jsonb) function:
public static Specification<MyEntity> hasTag(String tag) {
// get List of key-value: [key1, value1, key2, value2...]
List<Object> tags = List.of(tag);
// create jsonb from array list
Expression<?> jsonb = criteriaBuilder.function(
"jsonb_build_object",
Object.class,
cb.literal(tags)
);
Expression<Boolean> expression = criteriaBuilder.function(
"jsonb_contains",
Boolean.class,
root.get("tags"),
jsonb
);
return criteriaBuilder.isTrue(expression);
}
Solution 2
Create custom function in your Postgresql and use it in Java:
SQL:
CREATE FUNCTION jsonb_contains_as_text(a jsonb, b text)
RETURNS BOOLEAN AS $$
SELECT CASE
WHEN a #> b::jsonb THEN TRUE
ELSE FALSE
END;$$
LANGUAGE SQL IMMUTABLE STRICT;
Java Code:
public static Specification<MyEntity> hasTag(String tag) {
Expression<Boolean> expression = criteriaBuilder.function(
"jsonb_contains_as_text",
Boolean.class,
root.get("tags"),
criteriaBuilder.literal(tag)
);
return criteriaBuilder.isTrue(expression);
}
I think that the reason is that you pass the varchar as the second param. jsonb_contains() requires two jsonb params.
To check a jsonb array contains all/any values from a string array you need to use another operators: ?& or ?|.
The methods bindings for them in PSQL 9.4 are: jsonb_exists_all and jsonb_exists_any correspondingly.
In your PSQL version, you could check it by the following command:
select * from pg_operator where oprname = '?&'

Node pg-promise, bind multiple values with type casting

I'm currently using the pg-promise library to insert multiple values into a database in the format:
const cs = new pgp.helpers.ColumnSet(['booking_id', {name:'timeslot', cast:'timestamp'}], {table: 'booking'});
// data input values:
const values = [];
bookings.forEach(slot => {
values.push({booking_id: booking_id, timeslot: slot});
});
Where I need timeslot to be a timestamp. However it comes into the API as value like
1515586500.0
Using the above cast property my query gets resolved like so
insert into "booking"("booking_id","timeslot") values(1,'1515586500.0'::timestamp)
however this throws an error of cannot cast type numeric to timestamp without time zone
If I use the to_timestamp function however this works how I need it to e.g
insert into "booking"("booking_id","timeslot") values(1,to_timestamp('1515586500.0'));
Is there any way I can get pg-promise to use to_timestamp rather than the ::timestamp notation?
Change the column definition to this one:
{
name: 'timeslot',
mod: ':raw',
init: c => pgp.as.format('to_timestamp($1)', c.value)
}
or
{
name: 'timeslot',
mod: ':raw',
init: c => pgp.as.format('to_timestamp(${value})', c)
}
...as per the Column type documentation.
Or you can use Custom Type Formatting on the type, to self-format automatically.
Also, you do not need to remap values to suit the ColumnSet object, you use ColumnSet object to fit the data instead. So if the value for column timeslot is in property slot, you just use prop: 'slot' within your column definition to change where the value is coming from.