jpat827
jpat827

Reputation: 114

pg-promise helpers.update not working

I am unable to get the helpers.update method working. I am almost 100% certain that it is something simple I am overlooking. Below are the columns in the table involved in the code:

Below is my definition for the ColumnSet. (Note I added some spacing that is NOT in my actual code. I just hate having to scroll those windows.)

const usersSocialProfiles = new pgp.helpers.ColumnSet(
  [ 
    'user_id', 
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table: 'users_social_profiles'}
);
const usersSocialProfilesUpdate = new pgp.helpers.ColumnSet(
  [ 
    '?user_social_profile_id',
    '?user_id',
    'user_social_profile_platform',
    'user_social_profile_link'
  ],
  {table:'users_social_profiles'}
);

Then a function to toggle between which ColumnSet I want to use

var pgpConstantSet = function(setName){
  if(setName === "usersSocialProfiles")
  {
    return usersSocialProfiles; 
  }
  if(setName === "updateUsersSocialProfiles")
  {
    return usersSocialProfilesUpdate;
  }
}

The attempt to use the ColumnSet

  var profiles = await pgp.helpers.update(userData.socialProfiles, 
  pgConstantSet("usersSocialProfiles"))+ 
  " WHERE t.user_social_profile_id=v.user_social_profile_id";
  console.log(profiles);

  return res.status(201).end();

And what the console.log(profiles) prints

UPDATE "users_social_profiles" AS "t" SET "user_id"="v"."user_id","user_social_profile_platform"="v"."user_social_profile_platform","user_social_profile_link"="v"."user_social_profile_link" FROM (VALUES(51,'facebook','http:/w.sefvfaboklink.com')) AS "v"("user_id","user_social_profile_platform","user_social_profile_link") WHERE t.user_social_profile_id=v.user_social_profile_id PATCH /api/users 201 55.858 ms - -

I should note that I used the other ColumnSet I defined for inserts worked like a charm. I really appreciate anyone who got here. I know this is a bit of a mess.

Upvotes: 1

Views: 1798

Answers (1)

vitaly-t
vitaly-t

Reputation: 25840

All types and methods in the helpers namespace are for generating queries, which you confuse for executing them.

In your code you only generate the query, but never execute it. You should remove the await before helpers.update call, as query generation is synchronous, and use one of Database methods to execute the query. The latter is asynchronous ;)


On another note, a better way for your example to declare usersSocialProfilesUpdate:

const usersSocialProfilesUpdate = usersSocialProfiles.merge([
    '?user_social_profile_id',
    '?user_id'
  ]);

See methods merge and extend that help avoiding re-declaration of the same columns.

Upvotes: 1

Related Questions